summaryrefslogtreecommitdiff
path: root/bdb/examples_cxx
diff options
context:
space:
mode:
Diffstat (limited to 'bdb/examples_cxx')
-rw-r--r--bdb/examples_cxx/AccessExample.cpp136
-rw-r--r--bdb/examples_cxx/BtRecExample.cpp224
-rw-r--r--bdb/examples_cxx/EnvExample.cpp121
-rw-r--r--bdb/examples_cxx/LockExample.cpp244
-rw-r--r--bdb/examples_cxx/MpoolExample.cpp218
-rw-r--r--bdb/examples_cxx/TpcbExample.cpp657
6 files changed, 0 insertions, 1600 deletions
diff --git a/bdb/examples_cxx/AccessExample.cpp b/bdb/examples_cxx/AccessExample.cpp
deleted file mode 100644
index 921463b3a54..00000000000
--- a/bdb/examples_cxx/AccessExample.cpp
+++ /dev/null
@@ -1,136 +0,0 @@
-/*-
- * See the file LICENSE for redistribution information.
- *
- * Copyright (c) 1997-2002
- * Sleepycat Software. All rights reserved.
- *
- * $Id: AccessExample.cpp,v 11.18 2002/01/23 15:33:20 bostic Exp $
- */
-
-#include <sys/types.h>
-
-#include <iostream>
-#include <iomanip>
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <db_cxx.h>
-
-using std::cin;
-using std::cout;
-using std::cerr;
-
-class AccessExample
-{
-public:
- AccessExample();
- void run();
-
-private:
- static const char FileName[];
-
- // no need for copy and assignment
- AccessExample(const AccessExample &);
- void operator = (const AccessExample &);
-};
-
-int main()
-{
- // 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
- // that error codes are returned for all Berkeley DB methods.
- //
- try {
- AccessExample app;
- app.run();
- return (EXIT_SUCCESS);
- }
- catch (DbException &dbe) {
- cerr << "AccessExample: " << dbe.what() << "\n";
- return (EXIT_FAILURE);
- }
-}
-
-const char AccessExample::FileName[] = "access.db";
-
-AccessExample::AccessExample()
-{
-}
-
-void AccessExample::run()
-{
- // Remove the previous database.
- (void)remove(FileName);
-
- // Create the database object.
- // There is no environment for this simple example.
- Db db(0, 0);
-
- db.set_error_stream(&cerr);
- db.set_errpfx("AccessExample");
- db.set_pagesize(1024); /* Page size: 1K. */
- db.set_cachesize(0, 32 * 1024, 0);
- db.open(NULL, FileName, NULL, DB_BTREE, DB_CREATE, 0664);
-
- //
- // Insert records into the database, where the key is the user
- // input and the data is the user input in reverse order.
- //
- char buf[1024];
- char rbuf[1024];
- char *t;
- char *p;
- int ret;
- int len;
-
- for (;;) {
- cout << "input> ";
- cout.flush();
-
- cin.getline(buf, sizeof(buf));
- if (cin.eof())
- break;
-
- if ((len = strlen(buf)) <= 0)
- continue;
- for (t = rbuf, p = buf + (len - 1); p >= buf;)
- *t++ = *p--;
- *t++ = '\0';
-
- Dbt key(buf, len + 1);
- Dbt data(rbuf, len + 1);
-
- ret = db.put(0, &key, &data, DB_NOOVERWRITE);
- if (ret == DB_KEYEXIST) {
- cout << "Key " << buf << " already exists.\n";
- }
- }
- cout << "\n";
-
- // We put a try block around this section of code
- // to ensure that our database is properly closed
- // in the event of an error.
- //
- try {
- // Acquire a cursor for the table.
- Dbc *dbcp;
- db.cursor(NULL, &dbcp, 0);
-
- // Walk through the table, printing the key/data pairs.
- Dbt key;
- Dbt data;
- while (dbcp->get(&key, &data, DB_NEXT) == 0) {
- char *key_string = (char *)key.get_data();
- char *data_string = (char *)data.get_data();
- cout << key_string << " : " << data_string << "\n";
- }
- dbcp->close();
- }
- catch (DbException &dbe) {
- cerr << "AccessExample: " << dbe.what() << "\n";
- }
-
- db.close(0);
-}
diff --git a/bdb/examples_cxx/BtRecExample.cpp b/bdb/examples_cxx/BtRecExample.cpp
deleted file mode 100644
index b56c3fe5837..00000000000
--- a/bdb/examples_cxx/BtRecExample.cpp
+++ /dev/null
@@ -1,224 +0,0 @@
-/*-
- * See the file LICENSE for redistribution information.
- *
- * Copyright (c) 1997-2002
- * Sleepycat Software. All rights reserved.
- *
- * $Id: BtRecExample.cpp,v 11.21 2002/01/23 15:33:20 bostic Exp $
- */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <iostream>
-#include <iomanip>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <db_cxx.h>
-
-using std::cout;
-using std::cerr;
-
-#define DATABASE "access.db"
-#define WORDLIST "../test/wordlist"
-
-const char *progname = "BtRecExample"; // Program name.
-
-class BtRecExample
-{
-public:
- BtRecExample(FILE *fp);
- ~BtRecExample();
- void run();
- void stats();
- void show(const char *msg, Dbt *key, Dbt *data);
-
-private:
- Db *dbp;
- Dbc *dbcp;
-};
-
-BtRecExample::BtRecExample(FILE *fp)
-{
- char *p, *t, buf[1024], rbuf[1024];
- int ret;
-
- // Remove the previous database.
- (void)remove(DATABASE);
-
- dbp = new Db(NULL, 0);
-
- dbp->set_error_stream(&cerr);
- dbp->set_errpfx(progname);
- dbp->set_pagesize(1024); // 1K page sizes.
-
- dbp->set_flags(DB_RECNUM); // Record numbers.
- dbp->open(NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664);
-
- //
- // Insert records into the database, where the key is the word
- // preceded by its record number, and the data is the same, but
- // in reverse order.
- //
-
- for (int cnt = 1; cnt <= 1000; ++cnt) {
- (void)sprintf(buf, "%04d_", cnt);
- if (fgets(buf + 4, sizeof(buf) - 4, fp) == NULL)
- break;
- u_int32_t len = strlen(buf);
- buf[len - 1] = '\0';
- for (t = rbuf, p = buf + (len - 2); p >= buf;)
- *t++ = *p--;
- *t++ = '\0';
-
- // As a convenience for printing, we include the null terminator
- // in the stored data.
- //
- Dbt key(buf, len);
- Dbt data(rbuf, len);
-
- if ((ret = dbp->put(NULL, &key, &data, DB_NOOVERWRITE)) != 0) {
- dbp->err(ret, "Db::put");
- if (ret != DB_KEYEXIST)
- throw DbException(ret);
- }
- }
-}
-
-BtRecExample::~BtRecExample()
-{
- if (dbcp != 0)
- dbcp->close();
- dbp->close(0);
- delete dbp;
-}
-
-//
-// Print out the number of records in the database.
-//
-void BtRecExample::stats()
-{
- DB_BTREE_STAT *statp;
-
- dbp->stat(&statp, 0);
- cout << progname << ": database contains "
- << (u_long)statp->bt_ndata << " records\n";
-
- // Note: must use free, not delete.
- // This struct is allocated by C.
- //
- free(statp);
-}
-
-void BtRecExample::run()
-{
- db_recno_t recno;
- int ret;
- char buf[1024];
-
- // Acquire a cursor for the database.
- dbp->cursor(NULL, &dbcp, 0);
-
- //
- // Prompt the user for a record number, then retrieve and display
- // that record.
- //
- for (;;) {
- // Get a record number.
- cout << "recno #> ";
- cout.flush();
- if (fgets(buf, sizeof(buf), stdin) == NULL)
- break;
- recno = atoi(buf);
-
- //
- // Start with a fresh key each time,
- // the dbp->get() routine returns
- // the key and data pair, not just the key!
- //
- Dbt key(&recno, sizeof(recno));
- Dbt data;
-
- if ((ret = dbcp->get(&key, &data, DB_SET_RECNO)) != 0) {
- dbp->err(ret, "DBcursor->get");
- throw DbException(ret);
- }
-
- // Display the key and data.
- show("k/d\t", &key, &data);
-
- // Move the cursor a record forward.
- if ((ret = dbcp->get(&key, &data, DB_NEXT)) != 0) {
- dbp->err(ret, "DBcursor->get");
- throw DbException(ret);
- }
-
- // Display the key and data.
- show("next\t", &key, &data);
-
- //
- // Retrieve the record number for the following record into
- // local memory.
- //
- data.set_data(&recno);
- data.set_size(sizeof(recno));
- data.set_ulen(sizeof(recno));
- data.set_flags(data.get_flags() | DB_DBT_USERMEM);
-
- if ((ret = dbcp->get(&key, &data, DB_GET_RECNO)) != 0) {
- if (ret != DB_NOTFOUND && ret != DB_KEYEMPTY) {
- dbp->err(ret, "DBcursor->get");
- throw DbException(ret);
- }
- }
- else {
- cout << "retrieved recno: " << (u_long)recno << "\n";
- }
- }
-
- dbcp->close();
- dbcp = NULL;
-}
-
-//
-// show --
-// Display a key/data pair.
-//
-void BtRecExample::show(const char *msg, Dbt *key, Dbt *data)
-{
- cout << msg << (char *)key->get_data()
- << " : " << (char *)data->get_data() << "\n";
-}
-
-int
-main()
-{
- FILE *fp;
-
- // Open the word database.
- if ((fp = fopen(WORDLIST, "r")) == NULL) {
- fprintf(stderr, "%s: open %s: %s\n",
- progname, WORDLIST, db_strerror(errno));
- return (EXIT_FAILURE);
- }
-
- try {
- BtRecExample app(fp);
-
- // Close the word database.
- (void)fclose(fp);
- fp = NULL;
-
- app.stats();
- app.run();
- }
- catch (DbException &dbe) {
- cerr << "Exception: " << dbe.what() << "\n";
- return (EXIT_FAILURE);
- }
-
- return (EXIT_SUCCESS);
-}
diff --git a/bdb/examples_cxx/EnvExample.cpp b/bdb/examples_cxx/EnvExample.cpp
deleted file mode 100644
index 4eeb9f115e2..00000000000
--- a/bdb/examples_cxx/EnvExample.cpp
+++ /dev/null
@@ -1,121 +0,0 @@
-/*-
- * See the file LICENSE for redistribution information.
- *
- * Copyright (c) 1997-2002
- * Sleepycat Software. All rights reserved.
- *
- * $Id: EnvExample.cpp,v 11.24 2002/01/11 15:52:15 bostic Exp $
- */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <iostream>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <db_cxx.h>
-
-using std::ostream;
-using std::cout;
-using std::cerr;
-
-#ifdef macintosh
-#define DATABASE_HOME ":database"
-#define CONFIG_DATA_DIR ":database"
-#else
-#ifdef DB_WIN32
-#define DATABASE_HOME "\\tmp\\database"
-#define CONFIG_DATA_DIR "\\database\\files"
-#else
-#define DATABASE_HOME "/tmp/database"
-#define CONFIG_DATA_DIR "/database/files"
-#endif
-#endif
-
-void db_setup(const char *, const char *, ostream&);
-void db_teardown(const char *, const char *, ostream&);
-
-const char *progname = "EnvExample"; /* Program name. */
-
-//
-// An example of a program creating/configuring a Berkeley DB environment.
-//
-int
-main(int, char **)
-{
- //
- // Note: it may be easiest to put all Berkeley DB operations in a
- // try block, as seen here. Alternatively, you can change the
- // ErrorModel in the DbEnv so that exceptions are never thrown
- // and check error returns from all methods.
- //
- try {
- const char *data_dir, *home;
-
- //
- // All of the shared database files live in /home/database,
- // but data files live in /database.
- //
- home = DATABASE_HOME;
- data_dir = CONFIG_DATA_DIR;
-
- cout << "Setup env\n";
- db_setup(home, data_dir, cerr);
-
- cout << "Teardown env\n";
- db_teardown(home, data_dir, cerr);
- return (EXIT_SUCCESS);
- }
- catch (DbException &dbe) {
- cerr << "EnvExample: " << dbe.what() << "\n";
- return (EXIT_FAILURE);
- }
-}
-
-// Note that any of the db calls can throw DbException
-void
-db_setup(const char *home, const char *data_dir, ostream& err_stream)
-{
- //
- // Create an environment object and initialize it for error
- // reporting.
- //
- DbEnv *dbenv = new DbEnv(0);
- dbenv->set_error_stream(&err_stream);
- dbenv->set_errpfx(progname);
-
- //
- // We want to specify the shared memory buffer pool cachesize,
- // but everything else is the default.
- //
- dbenv->set_cachesize(0, 64 * 1024, 0);
-
- // Databases are in a subdirectory.
- (void)dbenv->set_data_dir(data_dir);
-
- // Open the environment with full transactional support.
- dbenv->open(home,
- DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0);
-
- // Do something interesting...
-
- // Close the handle.
- dbenv->close(0);
-}
-
-void
-db_teardown(const char *home, const char *data_dir, ostream& err_stream)
-{
- // Remove the shared database regions.
- DbEnv *dbenv = new DbEnv(0);
-
- dbenv->set_error_stream(&err_stream);
- dbenv->set_errpfx(progname);
-
- (void)dbenv->set_data_dir(data_dir);
- dbenv->remove(home, 0);
- delete dbenv;
-}
diff --git a/bdb/examples_cxx/LockExample.cpp b/bdb/examples_cxx/LockExample.cpp
deleted file mode 100644
index 167900b9476..00000000000
--- a/bdb/examples_cxx/LockExample.cpp
+++ /dev/null
@@ -1,244 +0,0 @@
-/*-
- * See the file LICENSE for redistribution information.
- *
- * Copyright (c) 1997-2002
- * Sleepycat Software. All rights reserved.
- *
- * $Id: LockExample.cpp,v 11.22 2002/01/11 15:52:15 bostic Exp $
- */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <iostream>
-#include <stdlib.h>
-#include <string.h>
-
-#include <db_cxx.h>
-
-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.
-//
-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 int usage(); // forward
-
-int
-main(int argc, char *argv[])
-{
- const char *home;
- int do_unlink;
- u_int32_t maxlocks;
- int i;
-
- home = "TESTDIR";
- maxlocks = 0;
- do_unlink = 0;
- for (int argnum = 1; argnum < argc; ++argnum) {
- if (strcmp(argv[argnum], "-h") == 0) {
- if (++argnum >= argc)
- return (usage());
- home = argv[argnum];
- }
- else if (strcmp(argv[argnum], "-m") == 0) {
- if (++argnum >= argc)
- return (usage());
- if ((i = atoi(argv[argnum])) <= 0)
- return (usage());
- maxlocks = (u_int32_t)i; /* XXX: possible overflow. */
- }
- else if (strcmp(argv[argnum], "-u") == 0) {
- do_unlink = 1;
- }
- else {
- 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 (EXIT_SUCCESS);
- }
- catch (DbException &dbe) {
- cerr << "LockExample: " << dbe.what() << "\n";
- return (EXIT_FAILURE);
- }
-}
-
-LockExample::LockExample(const char *home, u_int32_t maxlocks, int do_unlink)
-: DbEnv(0)
-, ecode(0)
-{
- int ret;
-
- if (do_unlink) {
- if ((ret = remove(home, DB_FORCE)) != 0) {
- cerr << progname << ": DbEnv::remove: "
- << strerror(errno) << "\n";
- ecode = EXIT_FAILURE;
- }
- }
- else {
- set_error_stream(&cerr);
- set_errpfx("LockExample");
- if (maxlocks != 0)
- set_lk_max_locks(maxlocks);
- open(home, DB_CREATE | DB_INIT_LOCK, 0);
- }
-}
-
-void LockExample::run()
-{
- long held;
- u_int32_t len, locker;
- int did_get, ret;
- DbLock *locks = 0;
- int lockcount = 0;
- char objbuf[1024];
- int lockid = 0;
-
- //
- // Accept lock requests.
- //
- lock_id(&locker);
- for (held = 0;;) {
- cout << "Operation get/release [get]> ";
- cout.flush();
-
- char opbuf[16];
- cin.getline(opbuf, sizeof(opbuf));
- if (cin.eof())
- break;
- if ((len = strlen(opbuf)) <= 1 || strcmp(opbuf, "get") == 0) {
- // Acquire a lock.
- cout << "input object (text string) to lock> ";
- cout.flush();
- cin.getline(objbuf, sizeof(objbuf));
- if (cin.eof())
- break;
- if ((len = strlen(objbuf)) <= 0)
- continue;
-
- char lockbuf[16];
- do {
- cout << "lock type read/write [read]> ";
- cout.flush();
- cin.getline(lockbuf, sizeof(lockbuf));
- if (cin.eof())
- break;
- len = strlen(lockbuf);
- } while (len >= 1 &&
- strcmp(lockbuf, "read") != 0 &&
- strcmp(lockbuf, "write") != 0);
-
- db_lockmode_t lock_type;
- if (len <= 1 || strcmp(lockbuf, "read") == 0)
- lock_type = DB_LOCK_READ;
- else
- lock_type = DB_LOCK_WRITE;
-
- Dbt dbt(objbuf, strlen(objbuf));
-
- DbLock lock;
- ret = lock_get(locker, DB_LOCK_NOWAIT, &dbt,
- lock_type, &lock);
- did_get = 1;
- lockid = lockcount++;
- if (locks == NULL) {
- locks = new DbLock[1];
- }
- else {
- DbLock *newlocks = new DbLock[lockcount];
- for (int lockno = 0; lockno < lockid; lockno++) {
- newlocks[lockno] = locks[lockno];
- }
- delete locks;
- locks = newlocks;
- }
- locks[lockid] = lock;
- } else {
- // Release a lock.
- do {
- cout << "input lock to release> ";
- cout.flush();
- cin.getline(objbuf, sizeof(objbuf));
- if (cin.eof())
- break;
- } while ((len = strlen(objbuf)) <= 0);
- lockid = strtol(objbuf, NULL, 16);
- if (lockid < 0 || lockid >= lockcount) {
- cout << "Lock #" << lockid << " out of range\n";
- continue;
- }
- DbLock lock = locks[lockid];
- ret = lock_put(&lock);
- did_get = 0;
- }
-
- switch (ret) {
- case 0:
- cout << "Lock #" << lockid << " "
- << (did_get ? "granted" : "released")
- << "\n";
- held += did_get ? 1 : -1;
- break;
- case DB_LOCK_NOTGRANTED:
- cout << "Lock not granted\n";
- break;
- case DB_LOCK_DEADLOCK:
- cerr << "LockExample: lock_"
- << (did_get ? "get" : "put")
- << ": " << "returned DEADLOCK";
- break;
- default:
- cerr << "LockExample: lock_get: %s",
- strerror(errno);
- }
- }
- cout << "\n";
- cout << "Closing lock region " << held << " locks held\n";
- if (locks != 0)
- delete locks;
-}
-
-static int
-usage()
-{
- cerr << "usage: LockExample [-u] [-h home] [-m maxlocks]\n";
- return (EXIT_FAILURE);
-}
diff --git a/bdb/examples_cxx/MpoolExample.cpp b/bdb/examples_cxx/MpoolExample.cpp
deleted file mode 100644
index 276cb94d66d..00000000000
--- a/bdb/examples_cxx/MpoolExample.cpp
+++ /dev/null
@@ -1,218 +0,0 @@
-/*-
- * See the file LICENSE for redistribution information.
- *
- * Copyright (c) 1997-2002
- * Sleepycat Software. All rights reserved.
- *
- * $Id: MpoolExample.cpp,v 11.23 2002/01/11 15:52:15 bostic Exp $
- */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <fcntl.h>
-#include <iostream>
-#include <fstream>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include <db_cxx.h>
-
-using std::cout;
-using std::cerr;
-using std::ios;
-using std::ofstream;
-
-#define MPOOL "mpool"
-
-int init(const char *, int, int);
-int run(DB_ENV *, int, int, int);
-
-static int usage();
-
-const char *progname = "MpoolExample"; // Program name.
-
-class MpoolExample : public DbEnv
-{
-public:
- MpoolExample();
- int initdb(const char *home, int cachesize);
- int run(int hits, int pagesize, int npages);
-
-private:
- static const char FileName[];
-
- // no need for copy and assignment
- MpoolExample(const MpoolExample &);
- void operator = (const MpoolExample &);
-};
-
-int main(int argc, char *argv[])
-{
- int ret;
- int cachesize = 20 * 1024;
- int hits = 1000;
- int npages = 50;
- int pagesize = 1024;
-
- for (int i = 1; i < argc; ++i) {
- if (strcmp(argv[i], "-c") == 0) {
- if ((cachesize = atoi(argv[++i])) < 20 * 1024)
- usage();
- }
- else if (strcmp(argv[i], "-h") == 0) {
- if ((hits = atoi(argv[++i])) <= 0)
- usage();
- }
- else if (strcmp(argv[i], "-n") == 0) {
- if ((npages = atoi(argv[++i])) <= 0)
- usage();
- }
- else if (strcmp(argv[i], "-p") == 0) {
- if ((pagesize = atoi(argv[++i])) <= 0)
- usage();
- }
- else {
- usage();
- }
- }
-
- // Initialize the file.
- if ((ret = init(MPOOL, pagesize, npages)) != 0)
- return (ret);
-
- try {
- MpoolExample app;
-
- cout << progname
- << ": cachesize: " << cachesize
- << "; pagesize: " << pagesize
- << "; N pages: " << npages << "\n";
-
- if ((ret = app.initdb(NULL, cachesize)) != 0)
- return (ret);
- if ((ret = app.run(hits, pagesize, npages)) != 0)
- return (ret);
- cout << "MpoolExample: completed\n";
- return (EXIT_SUCCESS);
- }
- catch (DbException &dbe) {
- cerr << "MpoolExample: " << dbe.what() << "\n";
- return (EXIT_FAILURE);
- }
-}
-
-//
-// init --
-// Create a backing file.
-//
-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.
- 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);
-
- // The pages are numbered from 0.
- for (int cnt = 0; cnt <= npages; ++cnt) {
- *(db_pgno_t *)p = cnt;
- of.write(p, pagesize);
- if (of.fail()) {
- cerr << "MpoolExample: " << file << ": write failed\n";
- return (EXIT_FAILURE);
- }
- }
- delete [] p;
- return (EXIT_SUCCESS);
-}
-
-static int
-usage()
-{
- cerr << "usage: MpoolExample [-c cachesize] "
- << "[-h hits] [-n npages] [-p pagesize]\n";
- return (EXIT_FAILURE);
-}
-
-// Note: by using DB_CXX_NO_EXCEPTIONS, we get explicit error returns
-// from various methods rather than exceptions so we can report more
-// information with each error.
-//
-MpoolExample::MpoolExample()
-: DbEnv(DB_CXX_NO_EXCEPTIONS)
-{
-}
-
-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.
-//
-int
-MpoolExample::run(int hits, int pagesize, int npages)
-{
- db_pgno_t pageno;
- int cnt, ret;
- void *p;
-
- // Open the file in the environment.
- DbMpoolFile *mfp;
-
- 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 ((ret = mfp->get(&pageno, 0, &p)) != 0) {
- cerr << "MpoolExample: unable to retrieve page "
- << (unsigned long)pageno << ": "
- << strerror(ret) << "\n";
- return (EXIT_FAILURE);
- }
- if (*(db_pgno_t *)p != pageno) {
- cerr << "MpoolExample: wrong page retrieved ("
- << (unsigned long)pageno << " != "
- << *(int *)p << ")\n";
- return (EXIT_FAILURE);
- }
- if ((ret = mfp->put(p, 0)) != 0) {
- cerr << "MpoolExample: unable to return page "
- << (unsigned long)pageno << ": "
- << strerror(ret) << "\n";
- return (EXIT_FAILURE);
- }
- }
-
- cout << "successful.\n";
-
- // Close the pool.
- 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
deleted file mode 100644
index a57fa6aee8a..00000000000
--- a/bdb/examples_cxx/TpcbExample.cpp
+++ /dev/null
@@ -1,657 +0,0 @@
-/*-
- * See the file LICENSE for redistribution information.
- *
- * Copyright (c) 1997-2002
- * Sleepycat Software. All rights reserved.
- *
- * $Id: TpcbExample.cpp,v 11.30 2002/02/13 06:08:34 mjc Exp $
- */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include <iostream>
-#include <iomanip>
-#include <db_cxx.h>
-
-using std::cout;
-using std::cerr;
-
-typedef enum { ACCOUNT, BRANCH, TELLER } FTYPE;
-
-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 int usage(void);
-
-int verbose;
-const char *progname = "TpcbExample"; // Program name.
-
-class TpcbExample : public DbEnv
-{
-public:
- void populate(int, int, int, int);
- void run(int, int, int, int);
- 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, const char *);
-
- // Note: the constructor creates a DbEnv(), which is
- // not fully initialized until the DbEnv::open() method
- // is called.
- //
- TpcbExample(const char *home, int cachesize,
- int initializing, int flags);
-
-private:
- static const char FileName[];
-
- // no need for copy and assignment
- TpcbExample(const TpcbExample &);
- void operator = (const TpcbExample &);
-};
-
-//
-// This program implements a basic TPC/B driver program. To create the
-// TPC/B database, run with the -i (init) flag. The number of records
-// with which to populate the account, history, branch, and teller tables
-// is specified by the a, s, b, and t flags respectively. To run a TPC/B
-// test, use the n flag to indicate a number of transactions to run (note
-// that you can run many of these processes in parallel to simulate a
-// multiuser test run).
-//
-#define TELLERS_PER_BRANCH 100
-#define ACCOUNTS_PER_TELLER 1000
-#define HISTORY_PER_BRANCH 2592000
-
-/*
- * The default configuration that adheres to TPCB scaling rules requires
- * nearly 3 GB of space. To avoid requiring that much space for testing,
- * we set the parameters much lower. If you want to run a valid 10 TPS
- * configuration, define VALID_SCALING.
- */
-#ifdef VALID_SCALING
-#define ACCOUNTS 1000000
-#define BRANCHES 10
-#define TELLERS 100
-#define HISTORY 25920000
-#endif
-
-#ifdef TINY
-#define ACCOUNTS 1000
-#define BRANCHES 10
-#define TELLERS 100
-#define HISTORY 10000
-#endif
-
-#if !defined(VALID_SCALING) && !defined(TINY)
-#define ACCOUNTS 100000
-#define BRANCHES 10
-#define TELLERS 100
-#define HISTORY 259200
-#endif
-
-#define HISTORY_LEN 100
-#define RECLEN 100
-#define BEGID 1000000
-
-struct Defrec {
- u_int32_t id;
- u_int32_t balance;
- u_int8_t pad[RECLEN - sizeof(u_int32_t) - sizeof(u_int32_t)];
-};
-
-struct Histrec {
- u_int32_t aid;
- u_int32_t bid;
- u_int32_t tid;
- u_int32_t amount;
- u_int8_t pad[RECLEN - 4 * sizeof(u_int32_t)];
-};
-
-int
-main(int argc, char *argv[])
-{
- unsigned long seed;
- int accounts, branches, tellers, history;
- int iflag, mpool, ntxns, txn_no_sync;
- const char *home;
- char *endarg;
-
- home = "TESTDIR";
- accounts = branches = history = tellers = 0;
- txn_no_sync = 0;
- mpool = ntxns = 0;
- verbose = 0;
- iflag = 0;
- 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)
- return (invarg('a', argv[i]));
- }
- else if (strcmp(argv[i], "-b") == 0) {
- // Number of branch records
- if ((branches = atoi(argv[++i])) <= 0)
- return (invarg('b', argv[i]));
- }
- else if (strcmp(argv[i], "-c") == 0) {
- // Cachesize in bytes
- if ((mpool = atoi(argv[++i])) <= 0)
- return (invarg('c', argv[i]));
- }
- else if (strcmp(argv[i], "-f") == 0) {
- // Fast mode: no txn sync.
- txn_no_sync = 1;
- }
- else if (strcmp(argv[i], "-h") == 0) {
- // DB home.
- home = argv[++i];
- }
- else if (strcmp(argv[i], "-i") == 0) {
- // Initialize the test.
- iflag = 1;
- }
- else if (strcmp(argv[i], "-n") == 0) {
- // Number of transactions
- if ((ntxns = atoi(argv[++i])) <= 0)
- return (invarg('n', argv[i]));
- }
- else if (strcmp(argv[i], "-S") == 0) {
- // Random number seed.
- seed = strtoul(argv[++i], &endarg, 0);
- if (*endarg != '\0')
- return (invarg('S', argv[i]));
- }
- else if (strcmp(argv[i], "-s") == 0) {
- // Number of history records
- if ((history = atoi(argv[++i])) <= 0)
- return (invarg('s', argv[i]));
- }
- else if (strcmp(argv[i], "-t") == 0) {
- // Number of teller records
- if ((tellers = atoi(argv[++i])) <= 0)
- return (invarg('t', argv[i]));
- }
- else if (strcmp(argv[i], "-v") == 0) {
- // Verbose option.
- verbose = 1;
- }
- else {
- return (usage());
- }
- }
-
- srand((unsigned int)seed);
-
- accounts = accounts == 0 ? ACCOUNTS : accounts;
- branches = branches == 0 ? BRANCHES : branches;
- tellers = tellers == 0 ? TELLERS : tellers;
- history = history == 0 ? HISTORY : history;
-
- if (verbose)
- cout << (long)accounts << " Accounts, "
- << (long)branches << " Branches, "
- << (long)tellers << " Tellers, "
- << (long)history << " History\n";
-
- try {
- // Initialize the database environment.
- // 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);
-
- if (iflag) {
- if (ntxns != 0)
- return (usage());
- app.populate(accounts, branches, history, tellers);
- }
- else {
- if (ntxns == 0)
- return (usage());
- app.run(ntxns, accounts, branches, tellers);
- }
-
- app.close(0);
- return (EXIT_SUCCESS);
- }
- catch (DbException &dbe) {
- cerr << "TpcbExample: " << dbe.what() << "\n";
- return (EXIT_FAILURE);
- }
-}
-
-static int
-invarg(int arg, char *str)
-{
- cerr << "TpcbExample: invalid argument for -"
- << (char)arg << ": " << str << "\n";
- return (EXIT_FAILURE);
-}
-
-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";
- return (EXIT_FAILURE);
-}
-
-TpcbExample::TpcbExample(const char *home, int cachesize,
- int initializing, int flags)
-: DbEnv(0)
-{
- u_int32_t local_flags;
-
- set_error_stream(&cerr);
- set_errpfx("TpcbExample");
- (void)set_cachesize(0, 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)
- local_flags |= DB_INIT_TXN | DB_INIT_LOCK | DB_INIT_LOG;
- open(home, local_flags, 0);
-}
-
-//
-// Initialize the database to the specified number of accounts, branches,
-// history records, and tellers.
-//
-void
-TpcbExample::populate(int accounts, int branches, int history, int tellers)
-{
- Db *dbp;
-
- int err;
- u_int32_t balance, idnum;
- u_int32_t end_anum, end_bnum, end_tnum;
- u_int32_t start_anum, start_bnum, start_tnum;
-
- idnum = BEGID;
- balance = 500000;
-
- dbp = new Db(this, 0);
- dbp->set_h_nelem((unsigned int)accounts);
-
- 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;
- populateTable(dbp, idnum, balance, accounts, "account");
- idnum += accounts;
- end_anum = idnum - 1;
- if ((err = dbp->close(0)) != 0) {
- DbException except("Account file close failed", err);
- throw except;
- }
- delete dbp;
- if (verbose)
- cout << "Populated accounts: "
- << (long)start_anum << " - " << (long)end_anum << "\n";
-
- dbp = new Db(this, 0);
- //
- // Since the number of branches is very small, we want to use very
- // small pages and only 1 key per page. This is the poor-man's way
- // of getting key locking instead of page locking.
- //
- dbp->set_h_ffactor(1);
- dbp->set_h_nelem((unsigned int)branches);
- dbp->set_pagesize(512);
-
- 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) {
- DbException except("Close of branch file failed", err);
- throw except;
- }
- delete dbp;
-
- if (verbose)
- cout << "Populated branches: "
- << (long)start_bnum << " - " << (long)end_bnum << "\n";
-
- dbp = new Db(this, 0);
- //
- // In the case of tellers, we also want small pages, but we'll let
- // the fill factor dynamically adjust itself.
- //
- dbp->set_h_ffactor(0);
- dbp->set_h_nelem((unsigned int)tellers);
- dbp->set_pagesize(512);
-
- 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;
- populateTable(dbp, idnum, balance, tellers, "teller");
- idnum += tellers;
- end_tnum = idnum - 1;
- if ((err = dbp->close(0)) != 0) {
- DbException except("Close of teller file failed", err);
- throw except;
- }
- delete dbp;
- if (verbose)
- cout << "Populated tellers: "
- << (long)start_tnum << " - " << (long)end_tnum << "\n";
-
- dbp = new Db(this, 0);
- dbp->set_re_len(HISTORY_LEN);
- 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) {
- 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, const char *msg)
-{
- Defrec drec;
- memset(&drec.pad[0], 1, sizeof(drec.pad));
-
- Dbt kdbt(&drec.id, sizeof(u_int32_t));
- Dbt ddbt(&drec, sizeof(drec));
-
- for (int i = 0; i < nrecs; i++) {
- drec.id = start_id + (u_int32_t)i;
- drec.balance = balance;
- int err;
- if ((err =
- dbp->put(NULL, &kdbt, &ddbt, DB_NOOVERWRITE)) != 0) {
- cerr << "Failure initializing " << msg << " file: "
- << strerror(err) << "\n";
- 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)
-{
- Histrec hrec;
- memset(&hrec.pad[0], 1, sizeof(hrec.pad));
- hrec.amount = 10;
- db_recno_t key;
-
- Dbt kdbt(&key, sizeof(u_int32_t));
- Dbt ddbt(&hrec, sizeof(hrec));
-
- for (int i = 1; i <= nrecs; i++) {
- hrec.aid = random_id(ACCOUNT, accounts, branches, tellers);
- hrec.bid = random_id(BRANCH, accounts, branches, tellers);
- hrec.tid = random_id(TELLER, accounts, branches, tellers);
-
- int err;
- key = (db_recno_t)i;
- if ((err = dbp->put(NULL, &kdbt, &ddbt, DB_APPEND)) != 0) {
- DbException except("failure initializing history file",
- err);
- throw except;
- }
- }
-}
-
-u_int32_t
-random_int(u_int32_t lo, u_int32_t hi)
-{
- u_int32_t ret;
- int t;
-
- t = rand();
- ret = (u_int32_t)(((double)t / ((double)(RAND_MAX) + 1)) *
- (hi - lo + 1));
- ret += lo;
- return (ret);
-}
-
-u_int32_t
-random_id(FTYPE type, u_int32_t accounts, u_int32_t branches, u_int32_t tellers)
-{
- u_int32_t min, max, num;
-
- max = min = BEGID;
- num = accounts;
- switch(type) {
- case TELLER:
- min += branches;
- num = tellers;
- // Fallthrough
- case BRANCH:
- if (type == BRANCH)
- num = branches;
- min += accounts;
- // Fallthrough
- case ACCOUNT:
- max = min + num - 1;
- }
- return (random_int(min, max));
-}
-
-void
-TpcbExample::run(int n, int accounts, int branches, int tellers)
-{
- Db *adb, *bdb, *hdb, *tdb;
- double gtps, itps;
- int failed, ifailed, ret, txns;
- time_t starttime, curtime, lasttime;
-
- //
- // Open the database files.
- //
-
- int err;
- adb = new Db(this, 0);
- 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(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(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(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);
- lasttime = starttime;
- while (n-- > 0) {
- txns++;
- ret = txn(adb, bdb, tdb, hdb, accounts, branches, tellers);
- if (ret != 0) {
- failed++;
- ifailed++;
- }
- if (n % 5000 == 0) {
- curtime = time(NULL);
- gtps = (double)(txns - failed) / (curtime - starttime);
- itps = (double)(5000 - ifailed) / (curtime - lasttime);
-
- // We use printf because it provides much simpler
- // formatting than iostreams.
- //
- printf("%d txns %d failed ", txns, failed);
- printf("%6.2f TPS (gross) %6.2f TPS (interval)\n",
- gtps, itps);
- lasttime = curtime;
- ifailed = 0;
- }
- }
-
- (void)adb->close(0);
- (void)bdb->close(0);
- (void)tdb->close(0);
- (void)hdb->close(0);
-
- cout << (long)txns << " transactions begun "
- << (long)failed << " failed\n";
-}
-
-//
-// XXX Figure out the appropriate way to pick out IDs.
-//
-int
-TpcbExample::txn(Db *adb, Db *bdb, Db *tdb, Db *hdb,
- int accounts, int branches, int tellers)
-{
- Dbc *acurs = NULL;
- Dbc *bcurs = NULL;
- Dbc *tcurs = NULL;
- DbTxn *t = NULL;
-
- db_recno_t key;
- Defrec rec;
- Histrec hrec;
- int account, branch, teller, ret;
-
- Dbt d_dbt;
- Dbt d_histdbt;
- Dbt k_dbt;
- Dbt k_histdbt(&key, sizeof(key));
-
- //
- // XXX We could move a lot of this into the driver to make this
- // faster.
- //
- account = random_id(ACCOUNT, accounts, branches, tellers);
- branch = random_id(BRANCH, accounts, branches, tellers);
- teller = random_id(TELLER, accounts, branches, tellers);
-
- k_dbt.set_size(sizeof(int));
-
- d_dbt.set_flags(DB_DBT_USERMEM);
- d_dbt.set_data(&rec);
- d_dbt.set_ulen(sizeof(rec));
-
- hrec.aid = account;
- hrec.bid = branch;
- hrec.tid = teller;
- hrec.amount = 10;
- // Request 0 bytes since we're just positioning.
- d_histdbt.set_flags(DB_DBT_PARTIAL);
-
- // START TIMING
- if (txn_begin(NULL, &t, 0) != 0)
- goto err;
-
- if (adb->cursor(t, &acurs, 0) != 0 ||
- bdb->cursor(t, &bcurs, 0) != 0 ||
- tdb->cursor(t, &tcurs, 0) != 0)
- goto err;
-
- // Account record
- k_dbt.set_data(&account);
- if (acurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
- goto err;
- rec.balance += 10;
- if (acurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
- goto err;
-
- // Branch record
- k_dbt.set_data(&branch);
- if (bcurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
- goto err;
- rec.balance += 10;
- if (bcurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
- goto err;
-
- // Teller record
- k_dbt.set_data(&teller);
- if (tcurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
- goto err;
- rec.balance += 10;
- if (tcurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
- goto err;
-
- // History record
- d_histdbt.set_flags(0);
- d_histdbt.set_data(&hrec);
- d_histdbt.set_ulen(sizeof(hrec));
- if (hdb->put(t, &k_histdbt, &d_histdbt, DB_APPEND) != 0)
- goto err;
-
- if (acurs->close() != 0 || bcurs->close() != 0 || tcurs->close() != 0)
- goto err;
-
- ret = t->commit(0);
- t = NULL;
- if (ret != 0)
- goto err;
-
- // END TIMING
- return (0);
-
-err:
- if (acurs != NULL)
- (void)acurs->close();
- if (bcurs != NULL)
- (void)bcurs->close();
- if (tcurs != NULL)
- (void)tcurs->close();
- if (t != NULL)
- (void)t->abort();
-
- if (verbose)
- cout << "Transaction A=" << (long)account
- << " B=" << (long)branch
- << " T=" << (long)teller << " failed\n";
- return (-1);
-}