Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

SequenceExample.cpp

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: SequenceExample.cpp,v 12.2 2005/06/16 20:22:14 bostic Exp $
00008  */
00009 
00010 #include <sys/types.h>
00011 
00012 #include <iostream>
00013 #include <errno.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016 
00017 #ifdef _WIN32
00018 extern "C" {
00019   extern int getopt(int, char * const *, const char *);
00020   extern int optind;
00021 }
00022 #else
00023 #include <unistd.h>
00024 #endif
00025 
00026 #include <db_cxx.h>
00027 
00028 #define DATABASE        "sequence.db"
00029 #define SEQUENCE        "my_sequence"
00030 
00031 using std::cout;
00032 using std::cerr;
00033 
00034 class SequenceExample
00035 {
00036 public:
00037         SequenceExample();
00038         void run(bool removeExistingDatabase, const char *fileName);
00039 
00040 private:
00041         // no need for copy and assignment
00042         SequenceExample(const SequenceExample &);
00043         void operator = (const SequenceExample &);
00044 };
00045 
00046 int
00047 usage()
00048 {
00049         (void)fprintf(stderr, "usage: SequenceExample [-r] [database]\n");
00050         return (EXIT_FAILURE);
00051 }
00052 
00053 int
00054 main(int argc, char *argv[])
00055 {
00056         int ch, rflag;
00057         const char *database;
00058 
00059         rflag = 0;
00060         while ((ch = getopt(argc, argv, "r")) != EOF)
00061                 switch (ch) {
00062                 case 'r':
00063                         rflag = 1;
00064                         break;
00065                 case '?':
00066                 default:
00067                         return (usage());
00068                 }
00069         argc -= optind;
00070         argv += optind;
00071 
00072         /* Accept optional database name. */
00073         database = *argv == NULL ? DATABASE : argv[0];
00074 
00075         // Use a try block just to report any errors.
00076         // An alternate approach to using exceptions is to
00077         // use error models (see DbEnv::set_error_model()) so
00078         // that error codes are returned for all Berkeley DB methods.
00079         //
00080         try {
00081                 SequenceExample app;
00082                 app.run((bool)(rflag == 1 ? true : false), database);
00083                 return (EXIT_SUCCESS);
00084         }
00085         catch (DbException &dbe) {
00086                 cerr << "SequenceExample: " << dbe.what() << "\n";
00087                 return (EXIT_FAILURE);
00088         }
00089 }
00090 
00091 SequenceExample::SequenceExample()
00092 {
00093 }
00094 
00095 void SequenceExample::run(bool removeExistingDatabase, const char *fileName)
00096 {
00097         // Remove the previous database.
00098         if (removeExistingDatabase)
00099                 (void)remove(fileName);
00100 
00101         // Create the database object.
00102         // There is no environment for this simple example.
00103         Db db(0, 0);
00104 
00105         db.set_error_stream(&cerr);
00106         db.set_errpfx("SequenceExample");
00107         db.open(NULL, fileName, NULL, DB_BTREE, DB_CREATE, 0664);
00108 
00109         // We put a try block around this section of code
00110         // to ensure that our database is properly closed
00111         // in the event of an error.
00112         //
00113         try {
00114                 Dbt key((void *)SEQUENCE, (u_int32_t)strlen(SEQUENCE));
00115                 DbSequence seq(&db, 0);
00116                 seq.open(0, &key, DB_CREATE);
00117 
00118                 for (int i = 0; i < 10; i++) {
00119                         db_seq_t seqnum;
00120                         seq.get(0, 1, &seqnum, 0);
00121 
00122                         // We don't have a portable way to print 64-bit numbers.
00123                         cout << "Got sequence number (" <<
00124                             (int)(seqnum >> 32) << ", " << (unsigned)seqnum <<
00125                             ")\n";
00126                 }
00127 
00128                 seq.close(0);
00129         } catch (DbException &dbe) {
00130                 cerr << "SequenceExample: " << dbe.what() << "\n";
00131         }
00132 
00133         db.close(0);
00134 }

Generated on Sun Dec 25 12:14:26 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2