diff options
Diffstat (limited to 'bdb/examples_cxx')
-rw-r--r-- | bdb/examples_cxx/AccessExample.cpp | 41 | ||||
-rw-r--r-- | bdb/examples_cxx/BtRecExample.cpp | 59 | ||||
-rw-r--r-- | bdb/examples_cxx/EnvExample.cpp | 39 | ||||
-rw-r--r-- | bdb/examples_cxx/LockExample.cpp | 48 | ||||
-rw-r--r-- | bdb/examples_cxx/MpoolExample.cpp | 114 | ||||
-rw-r--r-- | bdb/examples_cxx/TpcbExample.cpp | 213 |
6 files changed, 241 insertions, 273 deletions
diff --git a/bdb/examples_cxx/AccessExample.cpp b/bdb/examples_cxx/AccessExample.cpp index ae885aa8388..921463b3a54 100644 --- a/bdb/examples_cxx/AccessExample.cpp +++ b/bdb/examples_cxx/AccessExample.cpp @@ -1,29 +1,26 @@ /*- * See the file LICENSE for redistribution information. * - * Copyright (c) 1997, 1998, 1999, 2000 + * Copyright (c) 1997-2002 * Sleepycat Software. All rights reserved. * - * $Id: AccessExample.cpp,v 11.7 2000/12/06 18:58:23 bostic Exp $ + * $Id: AccessExample.cpp,v 11.18 2002/01/23 15:33:20 bostic Exp $ */ -#include "db_config.h" - -#ifndef NO_SYSTEM_INCLUDES #include <sys/types.h> -#include <iostream.h> +#include <iostream> +#include <iomanip> #include <errno.h> #include <stdlib.h> #include <string.h> -#ifndef _MSC_VER -#include <unistd.h> -#endif -#endif -#include <iomanip.h> #include <db_cxx.h> +using std::cin; +using std::cout; +using std::cerr; + class AccessExample { public: @@ -38,14 +35,8 @@ private: void operator = (const AccessExample &); }; -static void usage(); // forward - -int main(int argc, char *argv[]) +int main() { - if (argc > 1) { - usage(); - } - // Use a try block just to report any errors. // An alternate approach to using exceptions is to // use error models (see DbEnv::set_error_model()) so @@ -54,20 +45,14 @@ int main(int argc, char *argv[]) try { AccessExample app; app.run(); - return 0; + return (EXIT_SUCCESS); } catch (DbException &dbe) { cerr << "AccessExample: " << dbe.what() << "\n"; - return 1; + return (EXIT_FAILURE); } } -static void usage() -{ - cerr << "usage: AccessExample\n"; - exit(1); -} - const char AccessExample::FileName[] = "access.db"; AccessExample::AccessExample() @@ -77,7 +62,7 @@ AccessExample::AccessExample() void AccessExample::run() { // Remove the previous database. - (void)unlink(FileName); + (void)remove(FileName); // Create the database object. // There is no environment for this simple example. @@ -87,7 +72,7 @@ void AccessExample::run() db.set_errpfx("AccessExample"); db.set_pagesize(1024); /* Page size: 1K. */ db.set_cachesize(0, 32 * 1024, 0); - db.open(FileName, NULL, DB_BTREE, DB_CREATE, 0664); + db.open(NULL, FileName, NULL, DB_BTREE, DB_CREATE, 0664); // // Insert records into the database, where the key is the user diff --git a/bdb/examples_cxx/BtRecExample.cpp b/bdb/examples_cxx/BtRecExample.cpp index 98d9626b969..b56c3fe5837 100644 --- a/bdb/examples_cxx/BtRecExample.cpp +++ b/bdb/examples_cxx/BtRecExample.cpp @@ -1,35 +1,31 @@ /*- * See the file LICENSE for redistribution information. * - * Copyright (c) 1997, 1998, 1999, 2000 + * Copyright (c) 1997-2002 * Sleepycat Software. All rights reserved. * - * $Id: BtRecExample.cpp,v 11.6 2000/02/19 20:57:59 bostic Exp $ + * $Id: BtRecExample.cpp,v 11.21 2002/01/23 15:33:20 bostic Exp $ */ -#include "db_config.h" - -#ifndef NO_SYSTEM_INCLUDES #include <sys/types.h> + #include <errno.h> -#include <iostream.h> +#include <iostream> +#include <iomanip> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#endif -#include <iomanip.h> #include <db_cxx.h> +using std::cout; +using std::cerr; + #define DATABASE "access.db" #define WORDLIST "../test/wordlist" -void usage(); -extern "C" int getopt(int, char * const *, const char *); - -char *progname = "BtRecExample"; // Program name. +const char *progname = "BtRecExample"; // Program name. class BtRecExample { @@ -38,7 +34,7 @@ public: ~BtRecExample(); void run(); void stats(); - void show(char *msg, Dbt *key, Dbt *data); + void show(const char *msg, Dbt *key, Dbt *data); private: Db *dbp; @@ -51,7 +47,7 @@ BtRecExample::BtRecExample(FILE *fp) int ret; // Remove the previous database. - (void)unlink(DATABASE); + (void)remove(DATABASE); dbp = new Db(NULL, 0); @@ -60,7 +56,7 @@ BtRecExample::BtRecExample(FILE *fp) dbp->set_pagesize(1024); // 1K page sizes. dbp->set_flags(DB_RECNUM); // Record numbers. - dbp->open(DATABASE, NULL, DB_BTREE, DB_CREATE, 0664); + dbp->open(NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664); // // Insert records into the database, where the key is the word @@ -107,7 +103,7 @@ void BtRecExample::stats() { DB_BTREE_STAT *statp; - dbp->stat(&statp, NULL, 0); + dbp->stat(&statp, 0); cout << progname << ": database contains " << (u_long)statp->bt_ndata << " records\n"; @@ -191,34 +187,22 @@ void BtRecExample::run() // show -- // Display a key/data pair. // -void BtRecExample::show(char *msg, Dbt *key, Dbt *data) +void BtRecExample::show(const char *msg, Dbt *key, Dbt *data) { cout << msg << (char *)key->get_data() << " : " << (char *)data->get_data() << "\n"; } int -main(int argc, char *argv[]) +main() { - extern char *optarg; - extern int optind; FILE *fp; - int ch; - - while ((ch = getopt(argc, argv, "")) != EOF) - switch (ch) { - case '?': - default: - usage(); - } - argc -= optind; - argv += optind; // Open the word database. if ((fp = fopen(WORDLIST, "r")) == NULL) { fprintf(stderr, "%s: open %s: %s\n", progname, WORDLIST, db_strerror(errno)); - exit (1); + return (EXIT_FAILURE); } try { @@ -233,15 +217,8 @@ main(int argc, char *argv[]) } catch (DbException &dbe) { cerr << "Exception: " << dbe.what() << "\n"; - return dbe.get_errno(); + return (EXIT_FAILURE); } - return (0); -} - -void -usage() -{ - (void)fprintf(stderr, "usage: %s\n", progname); - exit(1); + return (EXIT_SUCCESS); } diff --git a/bdb/examples_cxx/EnvExample.cpp b/bdb/examples_cxx/EnvExample.cpp index bef1f3d1ace..4eeb9f115e2 100644 --- a/bdb/examples_cxx/EnvExample.cpp +++ b/bdb/examples_cxx/EnvExample.cpp @@ -1,28 +1,27 @@ /*- * See the file LICENSE for redistribution information. * - * Copyright (c) 1997, 1998, 1999, 2000 + * Copyright (c) 1997-2002 * Sleepycat Software. All rights reserved. * - * $Id: EnvExample.cpp,v 11.12 2000/10/27 20:32:00 dda Exp $ + * $Id: EnvExample.cpp,v 11.24 2002/01/11 15:52:15 bostic Exp $ */ -#include "db_config.h" - -#ifndef NO_SYSTEM_INCLUDES #include <sys/types.h> #include <errno.h> -#include <iostream.h> +#include <iostream> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#endif #include <db_cxx.h> +using std::ostream; +using std::cout; +using std::cerr; + #ifdef macintosh #define DATABASE_HOME ":database" #define CONFIG_DATA_DIR ":database" @@ -36,10 +35,10 @@ #endif #endif -void db_setup(char *, char *, ostream&); -void db_teardown(char *, char *, ostream&); +void db_setup(const char *, const char *, ostream&); +void db_teardown(const char *, const char *, ostream&); -char *progname = "EnvExample"; /* Program name. */ +const char *progname = "EnvExample"; /* Program name. */ // // An example of a program creating/configuring a Berkeley DB environment. @@ -54,7 +53,7 @@ main(int, char **) // and check error returns from all methods. // try { - char *data_dir, *home; + const char *data_dir, *home; // // All of the shared database files live in /home/database, @@ -64,21 +63,21 @@ main(int, char **) data_dir = CONFIG_DATA_DIR; cout << "Setup env\n"; - db_setup(DATABASE_HOME, data_dir, cerr); + db_setup(home, data_dir, cerr); cout << "Teardown env\n"; - db_teardown(DATABASE_HOME, data_dir, cerr); - return 0; + db_teardown(home, data_dir, cerr); + return (EXIT_SUCCESS); } catch (DbException &dbe) { - cerr << "AccessExample: " << dbe.what() << "\n"; - return 1; + cerr << "EnvExample: " << dbe.what() << "\n"; + return (EXIT_FAILURE); } } // Note that any of the db calls can throw DbException void -db_setup(char *home, char *data_dir, ostream& err_stream) +db_setup(const char *home, const char *data_dir, ostream& err_stream) { // // Create an environment object and initialize it for error @@ -98,7 +97,7 @@ db_setup(char *home, char *data_dir, ostream& err_stream) (void)dbenv->set_data_dir(data_dir); // Open the environment with full transactional support. - dbenv->open(DATABASE_HOME, + dbenv->open(home, DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0); // Do something interesting... @@ -108,7 +107,7 @@ db_setup(char *home, char *data_dir, ostream& err_stream) } void -db_teardown(char *home, char *data_dir, ostream& err_stream) +db_teardown(const char *home, const char *data_dir, ostream& err_stream) { // Remove the shared database regions. DbEnv *dbenv = new DbEnv(0); diff --git a/bdb/examples_cxx/LockExample.cpp b/bdb/examples_cxx/LockExample.cpp index cfab2868098..167900b9476 100644 --- a/bdb/examples_cxx/LockExample.cpp +++ b/bdb/examples_cxx/LockExample.cpp @@ -1,27 +1,26 @@ /*- * See the file LICENSE for redistribution information. * - * Copyright (c) 1997, 1998, 1999, 2000 + * Copyright (c) 1997-2002 * Sleepycat Software. All rights reserved. * - * $Id: LockExample.cpp,v 11.8 2001/01/04 14:23:30 dda Exp $ + * $Id: LockExample.cpp,v 11.22 2002/01/11 15:52:15 bostic Exp $ */ -#include "db_config.h" - -#ifndef NO_SYSTEM_INCLUDES #include <sys/types.h> #include <errno.h> -#include <iostream.h> +#include <iostream> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#endif #include <db_cxx.h> -char *progname = "LockExample"; // Program name. +using std::cin; +using std::cout; +using std::cerr; + +const char *progname = "LockExample"; // Program name. // // An example of a program using DBLock and related classes. @@ -30,18 +29,20 @@ class LockExample : public DbEnv { public: void run(); + int error_code() { return (ecode); } LockExample(const char *home, u_int32_t maxlocks, int do_unlink); private: static const char FileName[]; + int ecode; // no need for copy and assignment LockExample(const LockExample &); void operator = (const LockExample &); }; -static void usage(); // forward +static int usage(); // forward int main(int argc, char *argv[]) @@ -57,44 +58,51 @@ main(int argc, char *argv[]) for (int argnum = 1; argnum < argc; ++argnum) { if (strcmp(argv[argnum], "-h") == 0) { if (++argnum >= argc) - usage(); + return (usage()); home = argv[argnum]; } else if (strcmp(argv[argnum], "-m") == 0) { if (++argnum >= argc) - usage(); + return (usage()); if ((i = atoi(argv[argnum])) <= 0) - usage(); + return (usage()); maxlocks = (u_int32_t)i; /* XXX: possible overflow. */ } else if (strcmp(argv[argnum], "-u") == 0) { do_unlink = 1; } else { - usage(); + return (usage()); } } try { + int ecode; + if (do_unlink) { // Create an environment that immediately // removes all files. LockExample tmp(home, maxlocks, do_unlink); + if ((ecode = tmp.error_code()) != 0) + return (ecode); } LockExample app(home, maxlocks, do_unlink); + if ((ecode = app.error_code()) != 0) + return (ecode); app.run(); app.close(0); - return 0; + return (EXIT_SUCCESS); } catch (DbException &dbe) { cerr << "LockExample: " << dbe.what() << "\n"; - return 1; + return (EXIT_FAILURE); } } LockExample::LockExample(const char *home, u_int32_t maxlocks, int do_unlink) : DbEnv(0) +, ecode(0) { int ret; @@ -102,7 +110,7 @@ LockExample::LockExample(const char *home, u_int32_t maxlocks, int do_unlink) if ((ret = remove(home, DB_FORCE)) != 0) { cerr << progname << ": DbEnv::remove: " << strerror(errno) << "\n"; - exit (1); + ecode = EXIT_FAILURE; } } else { @@ -198,7 +206,7 @@ void LockExample::run() continue; } DbLock lock = locks[lockid]; - ret = lock.put(this); + ret = lock_put(&lock); did_get = 0; } @@ -228,9 +236,9 @@ void LockExample::run() delete locks; } -static void +static int usage() { cerr << "usage: LockExample [-u] [-h home] [-m maxlocks]\n"; - exit(1); + return (EXIT_FAILURE); } diff --git a/bdb/examples_cxx/MpoolExample.cpp b/bdb/examples_cxx/MpoolExample.cpp index cf0f5f7e6a4..276cb94d66d 100644 --- a/bdb/examples_cxx/MpoolExample.cpp +++ b/bdb/examples_cxx/MpoolExample.cpp @@ -1,43 +1,44 @@ /*- * See the file LICENSE for redistribution information. * - * Copyright (c) 1997, 1998, 1999, 2000 + * Copyright (c) 1997-2002 * Sleepycat Software. All rights reserved. * - * $Id: MpoolExample.cpp,v 11.9 2000/10/27 20:32:01 dda Exp $ + * $Id: MpoolExample.cpp,v 11.23 2002/01/11 15:52:15 bostic Exp $ */ -#include "db_config.h" - -#ifndef NO_SYSTEM_INCLUDES #include <sys/types.h> #include <errno.h> #include <fcntl.h> -#include <iostream.h> +#include <iostream> +#include <fstream> #include <stdlib.h> #include <string.h> #include <time.h> -#include <unistd.h> -#endif #include <db_cxx.h> +using std::cout; +using std::cerr; +using std::ios; +using std::ofstream; + #define MPOOL "mpool" -void init(char *, int, int); -void run(DB_ENV *, int, int, int); +int init(const char *, int, int); +int run(DB_ENV *, int, int, int); -static void usage(); +static int usage(); -char *progname = "MpoolExample"; // Program name. +const char *progname = "MpoolExample"; // Program name. class MpoolExample : public DbEnv { public: MpoolExample(); - void initdb(const char *home, int cachesize); - void run(int hits, int pagesize, int npages); + int initdb(const char *home, int cachesize); + int run(int hits, int pagesize, int npages); private: static const char FileName[]; @@ -49,6 +50,7 @@ private: int main(int argc, char *argv[]) { + int ret; int cachesize = 20 * 1024; int hits = 1000; int npages = 50; @@ -77,7 +79,8 @@ int main(int argc, char *argv[]) } // Initialize the file. - init(MPOOL, pagesize, npages); + if ((ret = init(MPOOL, pagesize, npages)) != 0) + return (ret); try { MpoolExample app; @@ -87,14 +90,16 @@ int main(int argc, char *argv[]) << "; pagesize: " << pagesize << "; N pages: " << npages << "\n"; - app.initdb(NULL, cachesize); - app.run(hits, pagesize, npages); + if ((ret = app.initdb(NULL, cachesize)) != 0) + return (ret); + if ((ret = app.run(hits, pagesize, npages)) != 0) + return (ret); cout << "MpoolExample: completed\n"; - return 0; + return (EXIT_SUCCESS); } catch (DbException &dbe) { cerr << "MpoolExample: " << dbe.what() << "\n"; - return 1; + return (EXIT_FAILURE); } } @@ -102,21 +107,16 @@ int main(int argc, char *argv[]) // init -- // Create a backing file. // -void -init(char *file, int pagesize, int npages) +int +init(const char *file, int pagesize, int npages) { - // // Create a file with the right number of pages, and store a page // number on each page. - // - int fd; - int flags = O_CREAT | O_RDWR | O_TRUNC; -#ifdef DB_WIN32 - flags |= O_BINARY; -#endif - if ((fd = open(file, flags, 0666)) < 0) { - cerr << "MpoolExample: " << file << ": " << strerror(errno) << "\n"; - exit(1); + ofstream of(file, ios::out | ios::binary); + + if (of.fail()) { + cerr << "MpoolExample: " << file << ": open failed\n"; + return (EXIT_FAILURE); } char *p = new char[pagesize]; memset(p, 0, pagesize); @@ -124,21 +124,22 @@ init(char *file, int pagesize, int npages) // The pages are numbered from 0. for (int cnt = 0; cnt <= npages; ++cnt) { *(db_pgno_t *)p = cnt; - if (write(fd, p, pagesize) != pagesize) { - cerr << "MpoolExample: " << file - << ": " << strerror(errno) << "\n"; - exit(1); + of.write(p, pagesize); + if (of.fail()) { + cerr << "MpoolExample: " << file << ": write failed\n"; + return (EXIT_FAILURE); } } delete [] p; + return (EXIT_SUCCESS); } -static void +static int usage() { cerr << "usage: MpoolExample [-c cachesize] " << "[-h hits] [-n npages] [-p pagesize]\n"; - exit(1); + return (EXIT_FAILURE); } // Note: by using DB_CXX_NO_EXCEPTIONS, we get explicit error returns @@ -150,61 +151,68 @@ MpoolExample::MpoolExample() { } -void MpoolExample::initdb(const char *home, int cachesize) +int MpoolExample::initdb(const char *home, int cachesize) { set_error_stream(&cerr); set_errpfx("MpoolExample"); set_cachesize(0, cachesize, 0); open(home, DB_CREATE | DB_INIT_MPOOL, 0); + return (EXIT_SUCCESS); } // // run -- // Get a set of pages. // -void +int MpoolExample::run(int hits, int pagesize, int npages) { db_pgno_t pageno; - int cnt; + int cnt, ret; void *p; - // Open the file in the pool. - DbMpoolFile *dbmfp; + // Open the file in the environment. + DbMpoolFile *mfp; - DbMpoolFile::open(this, MPOOL, 0, 0, pagesize, NULL, &dbmfp); + if ((ret = memp_fcreate(&mfp, 0)) != 0) { + cerr << "MpoolExample: memp_fcreate failed: " + << strerror(ret) << "\n"; + return (EXIT_FAILURE); + } + mfp->open(MPOOL, 0, 0, pagesize); cout << "retrieve " << hits << " random pages... "; srand((unsigned int)time(NULL)); for (cnt = 0; cnt < hits; ++cnt) { pageno = (rand() % npages) + 1; - if ((errno = dbmfp->get(&pageno, 0, &p)) != 0) { + if ((ret = mfp->get(&pageno, 0, &p)) != 0) { cerr << "MpoolExample: unable to retrieve page " << (unsigned long)pageno << ": " - << strerror(errno) << "\n"; - exit(1); + << strerror(ret) << "\n"; + return (EXIT_FAILURE); } if (*(db_pgno_t *)p != pageno) { cerr << "MpoolExample: wrong page retrieved (" << (unsigned long)pageno << " != " << *(int *)p << ")\n"; - exit(1); + return (EXIT_FAILURE); } - if ((errno = dbmfp->put(p, 0)) != 0) { + if ((ret = mfp->put(p, 0)) != 0) { cerr << "MpoolExample: unable to return page " << (unsigned long)pageno << ": " - << strerror(errno) << "\n"; - exit(1); + << strerror(ret) << "\n"; + return (EXIT_FAILURE); } } cout << "successful.\n"; // Close the pool. - if ((errno = close(0)) != 0) { - cerr << "MpoolExample: " << strerror(errno) << "\n"; - exit(1); + if ((ret = close(0)) != 0) { + cerr << "MpoolExample: " << strerror(ret) << "\n"; + return (EXIT_FAILURE); } + return (EXIT_SUCCESS); } diff --git a/bdb/examples_cxx/TpcbExample.cpp b/bdb/examples_cxx/TpcbExample.cpp index f4ca72df8e3..a57fa6aee8a 100644 --- a/bdb/examples_cxx/TpcbExample.cpp +++ b/bdb/examples_cxx/TpcbExample.cpp @@ -1,54 +1,35 @@ /*- * See the file LICENSE for redistribution information. * - * Copyright (c) 1997, 1998, 1999, 2000 + * Copyright (c) 1997-2002 * Sleepycat Software. All rights reserved. * - * $Id: TpcbExample.cpp,v 11.14 2000/10/27 20:32:01 dda Exp $ + * $Id: TpcbExample.cpp,v 11.30 2002/02/13 06:08:34 mjc Exp $ */ -#include "db_config.h" - -#ifndef NO_SYSTEM_INCLUDES #include <sys/types.h> -#if TIME_WITH_SYS_TIME -#include <sys/time.h> -#include <time.h> -#else -#if HAVE_SYS_TIME_H -#include <sys/time.h> -#else -#include <time.h> -#endif -#endif - #include <errno.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#endif - -#ifdef DB_WIN32 -#include <sys/types.h> -#include <sys/timeb.h> -#endif +#include <time.h> -#include <iostream.h> -#include <iomanip.h> +#include <iostream> +#include <iomanip> #include <db_cxx.h> -typedef enum { ACCOUNT, BRANCH, TELLER } FTYPE; +using std::cout; +using std::cerr; -void errExit(int err, const char *); // show err as errno and exit +typedef enum { ACCOUNT, BRANCH, TELLER } FTYPE; -void invarg(int, char *); +static int invarg(int, char *); u_int32_t random_id(FTYPE, u_int32_t, u_int32_t, u_int32_t); u_int32_t random_int(u_int32_t, u_int32_t); -static void usage(void); +static int usage(void); int verbose; -char *progname = "TpcbExample"; // Program name. +const char *progname = "TpcbExample"; // Program name. class TpcbExample : public DbEnv { @@ -58,7 +39,7 @@ public: int txn(Db *, Db *, Db *, Db *, int, int, int); void populateHistory(Db *, int, u_int32_t, u_int32_t, u_int32_t); - void populateTable(Db *, u_int32_t, u_int32_t, int, char *); + void populateTable(Db *, u_int32_t, u_int32_t, int, const char *); // Note: the constructor creates a DbEnv(), which is // not fully initialized until the DbEnv::open() method @@ -139,7 +120,8 @@ main(int argc, char *argv[]) unsigned long seed; int accounts, branches, tellers, history; int iflag, mpool, ntxns, txn_no_sync; - char *home, *endarg; + const char *home; + char *endarg; home = "TESTDIR"; accounts = branches = history = tellers = 0; @@ -147,24 +129,24 @@ main(int argc, char *argv[]) mpool = ntxns = 0; verbose = 0; iflag = 0; - seed = (unsigned long)getpid(); + seed = (unsigned long)time(NULL); for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "-a") == 0) { // Number of account records if ((accounts = atoi(argv[++i])) <= 0) - invarg('a', argv[i]); + return (invarg('a', argv[i])); } else if (strcmp(argv[i], "-b") == 0) { // Number of branch records if ((branches = atoi(argv[++i])) <= 0) - invarg('b', argv[i]); + return (invarg('b', argv[i])); } else if (strcmp(argv[i], "-c") == 0) { // Cachesize in bytes if ((mpool = atoi(argv[++i])) <= 0) - invarg('c', argv[i]); + return (invarg('c', argv[i])); } else if (strcmp(argv[i], "-f") == 0) { // Fast mode: no txn sync. @@ -181,30 +163,30 @@ main(int argc, char *argv[]) else if (strcmp(argv[i], "-n") == 0) { // Number of transactions if ((ntxns = atoi(argv[++i])) <= 0) - invarg('n', argv[i]); + return (invarg('n', argv[i])); } else if (strcmp(argv[i], "-S") == 0) { // Random number seed. seed = strtoul(argv[++i], &endarg, 0); if (*endarg != '\0') - invarg('S', argv[i]); + return (invarg('S', argv[i])); } else if (strcmp(argv[i], "-s") == 0) { // Number of history records if ((history = atoi(argv[++i])) <= 0) - invarg('s', argv[i]); + return (invarg('s', argv[i])); } else if (strcmp(argv[i], "-t") == 0) { // Number of teller records if ((tellers = atoi(argv[++i])) <= 0) - invarg('t', argv[i]); + return (invarg('t', argv[i])); } else if (strcmp(argv[i], "-v") == 0) { // Verbose option. verbose = 1; } else { - usage(); + return (usage()); } } @@ -216,9 +198,9 @@ main(int argc, char *argv[]) history = history == 0 ? HISTORY : history; if (verbose) - cout << (long)accounts << " Accounts " - << (long)branches << " Branches " - << (long)tellers << " Tellers " + cout << (long)accounts << " Accounts, " + << (long)branches << " Branches, " + << (long)tellers << " Tellers, " << (long)history << " History\n"; try { @@ -226,43 +208,44 @@ main(int argc, char *argv[]) // Must be done in within a try block, unless you // change the error model in the environment options. // - TpcbExample app(home, mpool, iflag, txn_no_sync ? DB_TXN_NOSYNC : 0); + TpcbExample app(home, mpool, iflag, + txn_no_sync ? DB_TXN_NOSYNC : 0); if (iflag) { if (ntxns != 0) - usage(); + return (usage()); app.populate(accounts, branches, history, tellers); } else { if (ntxns == 0) - usage(); + return (usage()); app.run(ntxns, accounts, branches, tellers); } app.close(0); - return 0; + return (EXIT_SUCCESS); } catch (DbException &dbe) { cerr << "TpcbExample: " << dbe.what() << "\n"; - return 1; + return (EXIT_FAILURE); } } -void +static int invarg(int arg, char *str) { cerr << "TpcbExample: invalid argument for -" << (char)arg << ": " << str << "\n"; - exit(1); + return (EXIT_FAILURE); } -static void +static int usage() { cerr << "usage: TpcbExample [-fiv] [-a accounts] [-b branches]\n" << " [-c cachesize] [-h home] [-n transactions ]\n" << " [-S seed] [-s history] [-t tellers]\n"; - exit(1); + return (EXIT_FAILURE); } TpcbExample::TpcbExample(const char *home, int cachesize, @@ -274,7 +257,11 @@ TpcbExample::TpcbExample(const char *home, int cachesize, set_error_stream(&cerr); set_errpfx("TpcbExample"); (void)set_cachesize(0, cachesize == 0 ? - 4 * 1024 * 1024 : (u_int32_t)cachesize, 0); + 4 * 1024 * 1024 : (u_int32_t)cachesize, 0); + + if (flags & (DB_TXN_NOSYNC)) + set_flags(DB_TXN_NOSYNC, 1); + flags &= ~(DB_TXN_NOSYNC); local_flags = flags | DB_CREATE | DB_INIT_MPOOL; if (!initializing) @@ -302,9 +289,10 @@ TpcbExample::populate(int accounts, int branches, int history, int tellers) dbp = new Db(this, 0); dbp->set_h_nelem((unsigned int)accounts); - if ((err = dbp->open("account", NULL, DB_HASH, - DB_CREATE | DB_TRUNCATE, 0644)) != 0) { - errExit(err, "Open of account file failed"); + if ((err = dbp->open(NULL, "account", NULL, DB_HASH, + DB_CREATE | DB_TRUNCATE, 0644)) != 0) { + DbException except("Account file create failed", err); + throw except; } start_anum = idnum; @@ -312,7 +300,8 @@ TpcbExample::populate(int accounts, int branches, int history, int tellers) idnum += accounts; end_anum = idnum - 1; if ((err = dbp->close(0)) != 0) { - errExit(err, "Account file close failed"); + DbException except("Account file close failed", err); + throw except; } delete dbp; if (verbose) @@ -329,16 +318,18 @@ TpcbExample::populate(int accounts, int branches, int history, int tellers) dbp->set_h_nelem((unsigned int)branches); dbp->set_pagesize(512); - if ((err = dbp->open("branch", NULL, DB_HASH, - DB_CREATE | DB_TRUNCATE, 0644)) != 0) { - errExit(err, "Branch file create failed"); + if ((err = dbp->open(NULL, "branch", NULL, DB_HASH, + DB_CREATE | DB_TRUNCATE, 0644)) != 0) { + DbException except("Branch file create failed", err); + throw except; } start_bnum = idnum; populateTable(dbp, idnum, balance, branches, "branch"); idnum += branches; end_bnum = idnum - 1; if ((err = dbp->close(0)) != 0) { - errExit(err, "Close of branch file failed"); + DbException except("Close of branch file failed", err); + throw except; } delete dbp; @@ -355,9 +346,10 @@ TpcbExample::populate(int accounts, int branches, int history, int tellers) dbp->set_h_nelem((unsigned int)tellers); dbp->set_pagesize(512); - if ((err = dbp->open("teller", NULL, DB_HASH, - DB_CREATE | DB_TRUNCATE, 0644)) != 0) { - errExit(err, "Teller file create failed"); + if ((err = dbp->open(NULL, "teller", NULL, DB_HASH, + DB_CREATE | DB_TRUNCATE, 0644)) != 0) { + DbException except("Teller file create failed", err); + throw except; } start_tnum = idnum; @@ -365,7 +357,8 @@ TpcbExample::populate(int accounts, int branches, int history, int tellers) idnum += tellers; end_tnum = idnum - 1; if ((err = dbp->close(0)) != 0) { - errExit(err, "Close of teller file failed"); + DbException except("Close of teller file failed", err); + throw except; } delete dbp; if (verbose) @@ -374,22 +367,24 @@ TpcbExample::populate(int accounts, int branches, int history, int tellers) dbp = new Db(this, 0); dbp->set_re_len(HISTORY_LEN); - if ((err = dbp->open("history", NULL, DB_RECNO, - DB_CREATE | DB_TRUNCATE, 0644)) != 0) { - errExit(err, "Create of history file failed"); + if ((err = dbp->open(NULL, "history", NULL, DB_RECNO, + DB_CREATE | DB_TRUNCATE, 0644)) != 0) { + DbException except("Create of history file failed", err); + throw except; } populateHistory(dbp, history, accounts, branches, tellers); if ((err = dbp->close(0)) != 0) { - errExit(err, "Close of history file failed"); + DbException except("Close of history file failed", err); + throw except; } delete dbp; } void TpcbExample::populateTable(Db *dbp, - u_int32_t start_id, u_int32_t balance, - int nrecs, char *msg) + u_int32_t start_id, u_int32_t balance, + int nrecs, const char *msg) { Defrec drec; memset(&drec.pad[0], 1, sizeof(drec.pad)); @@ -405,14 +400,15 @@ TpcbExample::populateTable(Db *dbp, dbp->put(NULL, &kdbt, &ddbt, DB_NOOVERWRITE)) != 0) { cerr << "Failure initializing " << msg << " file: " << strerror(err) << "\n"; - exit(1); + DbException except("failure initializing file", err); + throw except; } } } void -TpcbExample::populateHistory(Db *dbp, int nrecs, - u_int32_t accounts, u_int32_t branches, u_int32_t tellers) +TpcbExample::populateHistory(Db *dbp, int nrecs, u_int32_t accounts, + u_int32_t branches, u_int32_t tellers) { Histrec hrec; memset(&hrec.pad[0], 1, sizeof(hrec.pad)); @@ -430,7 +426,9 @@ TpcbExample::populateHistory(Db *dbp, int nrecs, int err; key = (db_recno_t)i; if ((err = dbp->put(NULL, &kdbt, &ddbt, DB_APPEND)) != 0) { - errExit(err, "Failure initializing history file"); + DbException except("failure initializing history file", + err); + throw except; } } } @@ -478,15 +476,6 @@ TpcbExample::run(int n, int accounts, int branches, int tellers) double gtps, itps; int failed, ifailed, ret, txns; time_t starttime, curtime, lasttime; -#ifndef DB_WIN32 - pid_t pid; - - pid = getpid(); -#else - int pid; - - pid = 0; -#endif // // Open the database files. @@ -494,20 +483,32 @@ TpcbExample::run(int n, int accounts, int branches, int tellers) int err; adb = new Db(this, 0); - if ((err = adb->open("account", NULL, DB_UNKNOWN, 0, 0)) != 0) - errExit(err, "Open of account file failed"); + if ((err = adb->open(NULL, "account", NULL, DB_UNKNOWN, + DB_AUTO_COMMIT, 0)) != 0) { + DbException except("Open of account file failed", err); + throw except; + } bdb = new Db(this, 0); - if ((err = bdb->open("branch", NULL, DB_UNKNOWN, 0, 0)) != 0) - errExit(err, "Open of branch file failed"); + if ((err = bdb->open(NULL, "branch", NULL, DB_UNKNOWN, + DB_AUTO_COMMIT, 0)) != 0) { + DbException except("Open of branch file failed", err); + throw except; + } tdb = new Db(this, 0); - if ((err = tdb->open("teller", NULL, DB_UNKNOWN, 0, 0)) != 0) - errExit(err, "Open of teller file failed"); + if ((err = tdb->open(NULL, "teller", NULL, DB_UNKNOWN, + DB_AUTO_COMMIT, 0)) != 0) { + DbException except("Open of teller file failed", err); + throw except; + } hdb = new Db(this, 0); - if ((err = hdb->open("history", NULL, DB_UNKNOWN, 0, 0)) != 0) - errExit(err, "Open of history file failed"); + if ((err = hdb->open(NULL, "history", NULL, DB_UNKNOWN, + DB_AUTO_COMMIT, 0)) != 0) { + DbException except("Open of history file failed", err); + throw except; + } txns = failed = ifailed = 0; starttime = time(NULL); @@ -527,10 +528,9 @@ TpcbExample::run(int n, int accounts, int branches, int tellers) // We use printf because it provides much simpler // formatting than iostreams. // - printf("[%d] %d txns %d failed ", (int)pid, - txns, failed); + printf("%d txns %d failed ", txns, failed); printf("%6.2f TPS (gross) %6.2f TPS (interval)\n", - gtps, itps); + gtps, itps); lasttime = curtime; ifailed = 0; } @@ -550,7 +550,7 @@ TpcbExample::run(int n, int accounts, int branches, int tellers) // int TpcbExample::txn(Db *adb, Db *bdb, Db *tdb, Db *hdb, - int accounts, int branches, int tellers) + int accounts, int branches, int tellers) { Dbc *acurs = NULL; Dbc *bcurs = NULL; @@ -560,7 +560,7 @@ TpcbExample::txn(Db *adb, Db *bdb, Db *tdb, Db *hdb, db_recno_t key; Defrec rec; Histrec hrec; - int account, branch, teller; + int account, branch, teller, ret; Dbt d_dbt; Dbt d_histdbt; @@ -628,11 +628,12 @@ TpcbExample::txn(Db *adb, Db *bdb, Db *tdb, Db *hdb, if (hdb->put(t, &k_histdbt, &d_histdbt, DB_APPEND) != 0) goto err; - if (acurs->close() != 0 || bcurs->close() != 0 || - tcurs->close() != 0) + if (acurs->close() != 0 || bcurs->close() != 0 || tcurs->close() != 0) goto err; - if (t->commit(0) != 0) + ret = t->commit(0); + t = NULL; + if (ret != 0) goto err; // END TIMING @@ -654,13 +655,3 @@ err: << " T=" << (long)teller << " failed\n"; return (-1); } - -void errExit(int err, const char *s) -{ - cerr << progname << ": "; - if (s != NULL) { - cerr << s << ": "; - } - cerr << strerror(err) << "\n"; - exit(1); -} |