int TestSetLockDetect(CuTest *ct) { DB_ENV *dbenv; u_int32_t v; dbenv = NULL; /* lk_detect: NOT reset at run-time. */ ENV CuAssertTrue(ct, dbenv->set_lk_detect(dbenv, DB_LOCK_MAXLOCKS) == 0); CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_CREATE | DB_INIT_LOCK, 0666) == 0); CuAssertTrue(ct, dbenv->get_lk_detect(dbenv, &v) == 0); CuAssertTrue(ct, v == DB_LOCK_MAXLOCKS); ENV CuAssertTrue(ct, dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT) == 0); CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0); CuAssertTrue(ct, dbenv->get_lk_detect(dbenv, &v) == 0); CuAssertTrue(ct, v == DB_LOCK_MAXLOCKS); return (0); }
void env_open(DB_ENV **dbenvp) { DB_ENV *dbenv; int ret; /* Create the environment handle. */ if ((ret = db_env_create(&dbenv, 0)) != 0) { fprintf(stderr, "txnapp: db_env_create: %s\n", db_strerror(ret)); exit (1); } /* Set up error handling. */ dbenv->set_errpfx(dbenv, "txnapp"); dbenv->set_errfile(dbenv, stderr); /* Do deadlock detection internally. */ if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT)) != 0) { dbenv->err(dbenv, ret, "set_lk_detect: DB_LOCK_DEFAULT"); exit (1); } /* * Open a transactional environment: * create if it doesn't exist * free-threaded handle * run recovery * read/write owner only */ if ((ret = dbenv->open(dbenv, ENV_DIRECTORY, DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_RECOVER | DB_THREAD, S_IRUSR | S_IWUSR)) != 0) { dbenv->err(dbenv, ret, "dbenv->open: %s", ENV_DIRECTORY); exit (1); } *dbenvp = dbenv; }
int dbfe_initialize_dbenv (DB_ENV **dbep, str filename, bool join, unsigned int cachesize = 1024) { int r (-1); r = mkdir (filename, 0755); if (r < 0 && errno != EEXIST) { fatal ("Couldn't mkdir for database %s: %m", filename.cstr ()); } r = db_env_create (dbep, 0); if (r) return r; DB_ENV *dbe = *dbep; // Enable verbose dead lock detection. dbe->set_verbose (dbe, DB_VERB_DEADLOCK, 1); dbe->set_verbose (dbe, DB_VERB_WAITSFOR, 1); dbe->set_lk_detect (dbe, DB_LOCK_DEFAULT); dbe->set_errfile (dbe, stderr); if (!join) { // Force the latest parameters strbuf db_config_path ("%s/DB_CONFIG", filename.cstr ()); dbfe_generate_config (db_config_path, cachesize); // We use all the fixings r = dbe->open (dbe, filename, DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER , 0); } else { r = dbe->open (dbe, filename, DB_JOINENV, 0); } return r; }
int main(void) { /* Initialize our handles */ DB *dbp = NULL; DB_ENV *envp = NULL; thread_t writer_threads[NUMWRITERS]; int i, ret, ret_t; u_int32_t env_flags; /* Application name */ const char *prog_name = "txn_guide_inmemory"; /* Create the environment */ ret = db_env_create(&envp, 0); if (ret != 0) { fprintf(stderr, "Error creating environment handle: %s\n", db_strerror(ret)); goto err; } env_flags = DB_CREATE | /* Create the environment if it does not exist */ DB_INIT_LOCK | /* Initialize the locking subsystem */ DB_INIT_LOG | /* Initialize the logging subsystem */ DB_INIT_TXN | /* Initialize the transactional subsystem. This * also turns on logging. */ DB_INIT_MPOOL | /* Initialize the memory pool (in-memory cache) */ DB_PRIVATE | /* Region files are backed by heap memory. */ DB_THREAD; /* Cause the environment to be free-threaded */ /* Specify in-memory logging */ ret = envp->log_set_config(envp, DB_LOG_IN_MEMORY, 1); if (ret != 0) { fprintf(stderr, "Error setting log subsystem to in-memory: %s\n", db_strerror(ret)); goto err; } /* * Specify the size of the in-memory log buffer. */ ret = envp->set_lg_bsize(envp, 10 * 1024 * 1024); if (ret != 0) { fprintf(stderr, "Error increasing the log buffer size: %s\n", db_strerror(ret)); goto err; } /* * Specify the size of the in-memory cache. */ ret = envp->set_cachesize(envp, 0, 10 * 1024 * 1024, 1); if (ret != 0) { fprintf(stderr, "Error increasing the cache size: %s\n", db_strerror(ret)); goto err; } /* * Indicate that we want db to perform lock detection internally. * Also indicate that the transaction with the fewest number of * write locks will receive the deadlock notification in * the event of a deadlock. */ ret = envp->set_lk_detect(envp, DB_LOCK_MINWRITE); if (ret != 0) { fprintf(stderr, "Error setting lock detect: %s\n", db_strerror(ret)); goto err; } /* Now actually open the environment */ ret = envp->open(envp, NULL, env_flags, 0); if (ret != 0) { fprintf(stderr, "Error opening environment: %s\n", db_strerror(ret)); goto err; } /* * If we had utility threads (for running checkpoints or * deadlock detection, for example) we would spawn those * here. However, for a simple example such as this, * that is not required. */ /* Open the database */ ret = open_db(&dbp, prog_name, NULL, envp, DB_DUPSORT); if (ret != 0) goto err; /* Initialize a mutex. Used to help provide thread ids. */ (void)mutex_init(&thread_num_lock, NULL); /* Start the writer threads. */ for (i = 0; i < NUMWRITERS; i++) (void)thread_create( &writer_threads[i], NULL, writer_thread, (void *)dbp); /* Join the writers */ for (i = 0; i < NUMWRITERS; i++) (void)thread_join(writer_threads[i], NULL); err: /* Close our database handle, if it was opened. */ if (dbp != NULL) { ret_t = dbp->close(dbp, 0); if (ret_t != 0) { fprintf(stderr, "%s database close failed.\n", db_strerror(ret_t)); ret = ret_t; } } /* Close our environment, if it was opened. */ if (envp != NULL) { ret_t = envp->close(envp, 0); if (ret_t != 0) { fprintf(stderr, "environment close failed: %s\n", db_strerror(ret_t)); ret = ret_t; } } /* Final status message and return. */ printf("I'm all done.\n"); return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); }
int main(int argc, char *argv[]) { /* Initialize our handles */ DB *dbp = NULL; DB_ENV *envp = NULL; thread_t writer_threads[NUMWRITERS]; int ch, i, ret, ret_t; u_int32_t env_flags; char *db_home_dir; /* Application name */ const char *prog_name = "txn_guide"; /* Database file name */ const char *file_name = "mydb.db"; /* Parse the command line arguments */ #ifdef _WIN32 db_home_dir = ".\\"; #else db_home_dir = "./"; #endif while ((ch = getopt(argc, argv, "h:")) != EOF) switch (ch) { case 'h': db_home_dir = optarg; break; case '?': default: return (usage()); } /* Create the environment */ ret = db_env_create(&envp, 0); if (ret != 0) { fprintf(stderr, "Error creating environment handle: %s\n", db_strerror(ret)); goto err; } /* * Indicate that we want db to perform lock detection internally. * Also indicate that the transaction with the fewest number of * write locks will receive the deadlock notification in * the event of a deadlock. */ ret = envp->set_lk_detect(envp, DB_LOCK_MINWRITE); if (ret != 0) { fprintf(stderr, "Error setting lock detect: %s\n", db_strerror(ret)); goto err; } env_flags = DB_CREATE | /* Create the environment if it does not exist */ DB_RECOVER | /* Run normal recovery. */ DB_INIT_LOCK | /* Initialize the locking subsystem */ DB_INIT_LOG | /* Initialize the logging subsystem */ DB_INIT_TXN | /* Initialize the transactional subsystem. This * also turns on logging. */ DB_INIT_MPOOL | /* Initialize the memory pool (in-memory cache) */ DB_THREAD; /* Cause the environment to be free-threaded */ /* Now actually open the environment */ ret = envp->open(envp, db_home_dir, env_flags, 0); if (ret != 0) { fprintf(stderr, "Error opening environment: %s\n", db_strerror(ret)); goto err; } /* * If we had utility threads (for running checkpoints or * deadlock detection, for example) we would spawn those * here. However, for a simple example such as this, * that is not required. */ /* Open the database */ ret = open_db(&dbp, prog_name, file_name, envp, DB_DUPSORT); if (ret != 0) goto err; /* Initialize a mutex. Used to help provide thread ids. */ (void)mutex_init(&thread_num_lock, NULL); /* Start the writer threads. */ for (i = 0; i < NUMWRITERS; i++) (void)thread_create( &writer_threads[i], NULL, writer_thread, (void *)dbp); /* Join the writers */ for (i = 0; i < NUMWRITERS; i++) (void)thread_join(writer_threads[i], NULL); err: /* Close our database handle, if it was opened. */ if (dbp != NULL) { ret_t = dbp->close(dbp, 0); if (ret_t != 0) { fprintf(stderr, "%s database close failed: %s\n", file_name, db_strerror(ret_t)); ret = ret_t; } } /* Close our environment, if it was opened. */ if (envp != NULL) { ret_t = envp->close(envp, 0); if (ret_t != 0) { fprintf(stderr, "environment close failed: %s\n", db_strerror(ret_t)); ret = ret_t; } } /* Final status message and return. */ printf("I'm all done.\n"); return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); }
int main(int argc, char **argv) { // This program uses a named container, which will apear // on disk std::string containerName = "people.dbxml"; std::string content = "<people><person><name>joe</name></person><person><name>mary</name></person></people>"; std::string docName = "people"; // Note that the query uses a variable, which must be set // in the query context std::string queryString = "collection('people.dbxml')/people/person[name=$name]"; std::string environmentDir = "."; // Berkeley DB environment flags u_int32_t envFlags = DB_RECOVER|DB_CREATE|DB_INIT_MPOOL| DB_INIT_LOCK|DB_INIT_TXN|DB_INIT_LOG; // Berkeley DB cache size (25 MB). The default is quite small u_int32_t envCacheSize = 25*1024*1024; // argument parsing should really use getopt(), but // avoid it for platform-independence if (argc == 3) { if (std::string(argv[1]) != std::string("-h")) usage(argv[0]); environmentDir = argv[2]; } else if (argc != 1) usage(argv[0]); // Create and open a Berkeley DB Transactional Environment. int dberr; DB_ENV *dbEnv = 0; dberr = db_env_create(&dbEnv, 0); if (dberr == 0) { dbEnv->set_cachesize(dbEnv, 0, envCacheSize, 1); dbEnv->set_errcall(dbEnv, errcall); // set error callback dbEnv->set_lk_detect(dbEnv, DB_LOCK_DEFAULT); // handle deadlocks dberr = dbEnv->open(dbEnv, environmentDir.c_str(), envFlags, 0); } if (dberr) { std::cout << "Unable to create environment handle due to the following error: " << db_strerror(dberr) << std::endl; if (dbEnv) dbEnv->close(dbEnv, 0); return -1; } try { // All BDB XML programs require an XmlManager instance. // Create it from the DbEnv XmlManager mgr(dbEnv, DBXML_ADOPT_DBENV); // Because the container will exist on disk, remove it // first if it exists if (mgr.existsContainer(containerName)) mgr.removeContainer(containerName); /* Create a container that is transactional. The container * type is NodeContainer, which is the default container type, * and the index is on nodes, which is the default for a * NodeContainer container. XmlContainerConfig can be used * to set the container type and index type. */ XmlContainerConfig config; config.setTransactional(true); XmlContainer cont = mgr.createContainer( containerName, config); // All Container modification operations need XmlUpdateContext XmlUpdateContext uc = mgr.createUpdateContext(); // The following putDocument call will auto-transact // and will abort/cleanup if the operation deadlocks or // otherwise fails cont.putDocument(docName, content, uc); // Querying requires an XmlQueryContext XmlQueryContext qc = mgr.createQueryContext(); // Add a variable to the query context, used by the query qc.setVariableValue("name", "mary"); // Use try/catch and while to handle deadlock/retry int retry = 0; while (retry < 5) { // hard-code 5 retries // Create a new transaction for the query XmlTransaction txn = mgr.createTransaction(); try { // o Note the passing of txn to both methods // o Often the XmlQueryExpression object will be created and // saved for reuse in order to amortize the cost of parsing a query XmlQueryExpression expr = mgr.prepare(txn, queryString, qc); XmlResults res = expr.execute(txn, qc); // Note use of XmlQueryExpression::getQuery() and // XmlResults::size() std::cout << "The query, '" << expr.getQuery() << "' returned " << (unsigned int)res.size() << " result(s)" << std::endl; // Process results -- just print them XmlValue value; std::cout << "Result: " << std::endl; while (res.next(value)) { std::cout << "\t" << value.asString() << std::endl; } // done with the transaction txn.commit(); break; } catch (XmlException &x) { txn.abort(); // unconditional if ((x.getExceptionCode() == XmlException::DATABASE_ERROR) && (x.getDbErrno() == DB_LOCK_DEADLOCK)) { ++retry; continue; // try again } throw; // re-throw -- not deadlock } } // In C++, resources are released as objects go out // of scope. } catch (XmlException &xe) { std::cout << "XmlException: " << xe.what() << std::endl; } return 0; }
int main() { const u_int8_t *lk_conflicts; DB_ENV *dbenv; db_timeout_t timeout; u_int32_t a, b, v; int lk_modes, ncache, nmodes; u_int8_t conflicts[40]; dbenv = NULL; /* tx_max: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_tx_max(dbenv, 37) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_TXN, 0666) == 0); assert(dbenv->get_tx_max(dbenv, &v) == 0); assert(v == 37); ENV assert(dbenv->set_tx_max(dbenv, 63) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_tx_max(dbenv, &v) == 0); assert(v == 37); /* lg_max: reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_lg_max(dbenv, 37 * 1024 * 1024) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOG, 0666) == 0); assert(dbenv->get_lg_max(dbenv, &v) == 0); assert(v == 37 * 1024 * 1024); ENV assert(dbenv->set_lg_max(dbenv, 63 * 1024 * 1024) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_lg_max(dbenv, &v) == 0); assert(v == 63 * 1024 * 1024); /* lg_bsize: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_lg_bsize(dbenv, 37 * 1024) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOG, 0666) == 0); assert(dbenv->get_lg_bsize(dbenv, &v) == 0); assert(v == 37 * 1024); ENV assert(dbenv->set_lg_bsize(dbenv, 63 * 1024) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_lg_bsize(dbenv, &v) == 0); assert(v == 37 * 1024); /* lg_regionmax: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_lg_regionmax(dbenv, 137 * 1024) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOG, 0666) == 0); assert(dbenv->get_lg_regionmax(dbenv, &v) == 0); assert(v == 137 * 1024); ENV assert(dbenv->set_lg_regionmax(dbenv, 163 * 1024) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_lg_regionmax(dbenv, &v) == 0); assert(v == 137 * 1024); /* lk_get_lk_conflicts: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV memset(conflicts, 'a', sizeof(conflicts)); nmodes = 6; assert(dbenv->set_lk_conflicts(dbenv, conflicts, nmodes) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOCK, 0666) == 0); assert(dbenv->get_lk_conflicts(dbenv, &lk_conflicts, &lk_modes) == 0); assert(lk_conflicts[0] == 'a'); assert(lk_modes == 6); ENV memset(conflicts, 'b', sizeof(conflicts)); nmodes = 8; assert(dbenv->set_lk_conflicts(dbenv, conflicts, nmodes) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_lk_conflicts(dbenv, &lk_conflicts, &lk_modes) == 0); assert(lk_conflicts[0] == 'a'); assert(lk_modes == 6); /* lk_detect: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_lk_detect(dbenv, DB_LOCK_MAXLOCKS) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOCK, 0666) == 0); assert(dbenv->get_lk_detect(dbenv, &v) == 0); assert(v == DB_LOCK_MAXLOCKS); ENV assert(dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_lk_detect(dbenv, &v) == 0); assert(v == DB_LOCK_MAXLOCKS); /* lk_max_locks: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_lk_max_locks(dbenv, 37) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOCK, 0666) == 0); assert(dbenv->get_lk_max_locks(dbenv, &v) == 0); assert(v == 37); ENV assert(dbenv->set_lk_max_locks(dbenv, 63) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_lk_max_locks(dbenv, &v) == 0); assert(v == 37); /* lk_max_lockers: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_lk_max_lockers(dbenv, 37) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOCK, 0666) == 0); assert(dbenv->get_lk_max_lockers(dbenv, &v) == 0); assert(v == 37); ENV assert(dbenv->set_lk_max_lockers(dbenv, 63) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_lk_max_lockers(dbenv, &v) == 0); assert(v == 37); /* lk_max_objects: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_lk_max_objects(dbenv, 37) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOCK, 0666) == 0); assert(dbenv->get_lk_max_objects(dbenv, &v) == 0); assert(v == 37); ENV assert(dbenv->set_lk_max_objects(dbenv, 63) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_lk_max_objects(dbenv, &v) == 0); assert(v == 37); /* lock timeout: reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_timeout(dbenv, 37, DB_SET_LOCK_TIMEOUT) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOCK, 0666) == 0); assert(dbenv->get_timeout(dbenv, &timeout, DB_SET_LOCK_TIMEOUT) == 0); assert(timeout == 37); ENV assert(dbenv->set_timeout(dbenv, 63, DB_SET_LOCK_TIMEOUT) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_timeout(dbenv, &timeout, DB_SET_LOCK_TIMEOUT) == 0); assert(timeout == 63); /* txn timeout: reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_timeout(dbenv, 37, DB_SET_TXN_TIMEOUT) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_LOCK, 0666) == 0); assert(dbenv->get_timeout(dbenv, &timeout, DB_SET_TXN_TIMEOUT) == 0); assert(timeout == 37); ENV assert(dbenv->set_timeout(dbenv, 63, DB_SET_TXN_TIMEOUT) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_timeout(dbenv, &timeout, DB_SET_TXN_TIMEOUT) == 0); assert(timeout == 63); /* cache size: NOT reset at run-time. */ system("rm -rf TESTDIR; mkdir TESTDIR"); ENV assert(dbenv->set_cachesize(dbenv, 1, 131072, 3) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_INIT_MPOOL, 0666) == 0); assert(dbenv->get_cachesize(dbenv, &a, &b, &ncache) == 0); assert(dbenv->get_cachesize(dbenv, &a, &b, &ncache) == 0); assert(a == 1 && b == 131072 && ncache == 3); ENV assert(dbenv->set_cachesize(dbenv, 2, 262144, 1) == 0); assert(dbenv->open(dbenv, "TESTDIR", DB_JOINENV, 0666) == 0); assert(dbenv->get_cachesize(dbenv, &a, &b, &ncache) == 0); assert(a == 1 && b == 131072 && ncache == 3); return (0); }
int TestKeyExistErrorReturn(CuTest *ct) { DB *pdbp; DB *sdbp; DB_ENV *dbenv; const char *sec_db_file = "secondary.db"; const char *pri_db_file = "primary.db"; const char *env_dir = "TESTDIR"; int i; thread_t writer_threads[NUMWRITERS]; u_int32_t db_flags, env_flags; pdbp = sdbp = NULL; dbenv = NULL; db_flags = DB_CREATE | DB_AUTO_COMMIT | DB_READ_UNCOMMITTED; env_flags = DB_CREATE | DB_RECOVER | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_THREAD; TestEnvConfigTestSetup(ct); CuAssert(ct, "db_env_create", db_env_create(&dbenv, 0) == 0); dbenv->set_errfile(dbenv, stderr); dbenv->set_errpfx(dbenv, "TestKeyExistErrorReturn"); /* Run deadlock detector on every lock conflict. */ CuAssert(ct, "dbenv->set_lk_detect", dbenv->set_lk_detect(dbenv, DB_LOCK_MINWRITE) == 0); CuAssert(ct, "dbenv->open", dbenv->open(dbenv, env_dir, env_flags, 0) == 0); CuAssert(ct, "db_create", db_create(&pdbp, dbenv, 0) == 0); CuAssert(ct, "pdbp->open", pdbp->open(pdbp, NULL, pri_db_file, NULL, DB_BTREE, db_flags, 0) == 0); CuAssert(ct, "db_create", db_create(&sdbp, dbenv, 0) == 0); CuAssert(ct, "sdbp->set_flags", sdbp->set_flags(sdbp, DB_DUPSORT) == 0); CuAssert(ct, "sdbp->open", sdbp->open(sdbp, NULL, sec_db_file, NULL, DB_BTREE, db_flags, 0) == 0); CuAssert(ct, "DB->associate", pdbp->associate(pdbp, NULL, sdbp, assoc_callback, DB_AUTO_COMMIT) == 0); /* Initialize a mutex. Used to help provide thread ids. */ (void)mutex_init(&thread_num_lock, NULL); for (i = 0; i < NUMWRITERS; ++i) (void)thread_create(&writer_threads[i], NULL, writer_thread, (void *)pdbp); for (i = 0; i < NUMWRITERS; ++i) (void)thread_join(writer_threads[i], NULL); if (sdbp != NULL) CuAssert(ct, "sdbp->close", sdbp->close(sdbp, 0) == 0); if (pdbp != NULL) CuAssert(ct, "pdbp->close", pdbp->close(pdbp, 0) == 0); if (dbenv != NULL) CuAssert(ct, "dbenv->close", dbenv->close(dbenv, 0) == 0); TestEnvConfigTestTeardown(ct); return (EXIT_SUCCESS); }
int main(int argc, char **argv) { // Deal with command line arguments const char *path2DbEnv = 0; u_int32_t envFlags = (DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL); u_int32_t txnEnvFlags = (DB_INIT_TXN|DB_INIT_LOCK|DB_INIT_LOG); u_int32_t dbxmlFlags = DBXML_ALLOW_EXTERNAL_ACCESS; vector<string> scripts; int verbose = 0; bool transactionMode = false; bool dbPrivate = false; bool envCreate = false; const char *progName = argv[0]; const char *password = 0; int cacheSize = 64; int ch; int ret = 0; while ((ch = getopt(argc, argv, "?h:hs:tvxVP:cz:")) != -1) { switch (ch) { case 'h': { path2DbEnv = optarg; break; } case 'z': { cacheSize = atoi(optarg); break; } case 'c': { envFlags &= ~DB_PRIVATE; envCreate = true; break; } case 'x': { dbxmlFlags &= ~DBXML_ALLOW_EXTERNAL_ACCESS; break; } case 't': { transactionMode = true; envFlags |= txnEnvFlags; break; } case 's': { scripts.push_back(optarg); break; } case 'v': { ++verbose; break; } case 'V': { printf("%s\n", DbXml::dbxml_version(NULL, NULL, NULL)); printf("%s\n", db_version(NULL, NULL, NULL)); exit(0); } case 'P': { password = optarg; break; } case '?': default: { usage(progName, 0); break; } } } // Turn on logging if extra verbose is specified if(verbose > 1) { setLogLevel(LEVEL_ALL, true); setLogCategory(CATEGORY_ALL, true); setLogCategory(CATEGORY_NODESTORE, verbose > 2); verboseErrors = true; } SigBlock sb; // block signals, resend at end of scope try { // Create a DB environment, and XmlManager DB_ENV *dbenv; int dberr = 0; dberr = db_env_create(&dbenv, 0); if (dberr) { cout << "Error creating environment: " << dberr << endl; exit(-1); } if (password) dbenv->set_encrypt(dbenv, password, DB_ENCRYPT_AES); dbenv->set_errcall(dbenv, errcall); dbenv->set_cachesize(dbenv, 0, cacheSize * 1024 * 1024, 1); dbenv->set_lk_max_lockers(dbenv, 10000); dbenv->set_lk_max_locks(dbenv, 10000); dbenv->set_lk_max_objects(dbenv, 10000); if (!dbPrivate) { dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT); if (verbose && !envCreate) { cout << "Attempting to join environment: " << (path2DbEnv ? path2DbEnv : ".") << endl; } dberr = dbenv->open(dbenv, path2DbEnv, DB_USE_ENVIRON, 0); if (dberr != 0) { if (dberr == DB_VERSION_MISMATCH) { cerr << "Error opening environment " << (path2DbEnv ? path2DbEnv : ".") << ": " << "environment version mismatch" << endl; exit(-1); } if (verbose) { if(envCreate) { cerr << "Creating environment: " << (path2DbEnv ? path2DbEnv : ".") << endl; } else { cerr << "Unable to join environment " << (path2DbEnv ? path2DbEnv : ".") << ", creating a DB_PRIVATE environment" << endl; } } dberr = dbenv->open(dbenv, path2DbEnv, envFlags, 0); } else { cout << "Joined existing environment" << endl; u_int32_t eflags = 0; dbenv->get_open_flags(dbenv, &eflags); if (eflags & DB_INIT_TXN) transactionMode = true; else { if (verbose && (transactionMode == true)) cout << "Joined a non-transactional environment, turning off transaction mode" << endl; transactionMode = false; } } } else { dberr = dbenv->open(dbenv, path2DbEnv, envFlags, 0); } if (dberr != 0) { cerr << "Error opening environment " << (path2DbEnv ? path2DbEnv : ".") << ", error is " << dberr << endl; exit(-1); } XmlManager db(dbenv, dbxmlFlags|DBXML_ADOPT_DBENV); // Create the environment Environment env(db, sb); env.transactions() = transactionMode; // Create the Shell object DefaultShell shell; // Run scripts, if specified if(!scripts.empty()) { env.interactive() = false; env.verbose() = (verbose != 0); for(vector<string>::iterator i = scripts.begin(); i != scripts.end() && !env.quit(); ++i) { ifstream scriptFile(i->c_str(), ios::in); if(!scriptFile) { cerr << progName << ": cannot open script file: " << *i << endl; } else { env.streamName() = *i; env.lineNo() = 0; shell.mainLoop(scriptFile, env); scriptFile.close(); } } } // Perform the queries if(!env.quit()) { env.interactive() = true; env.verbose() = true; env.streamName() = "stdin"; env.lineNo() = 0; do { shell.mainLoop(cin, env); if(env.sigBlock().isInterrupted()) env.sigBlock().reset(); } while(!env.quit() && !cin.eof()); } } catch(exception &e) { cerr << progName << ": error at lowest level: " << e.what() << endl; ret = 1; } catch(...) { cerr << progName << ": error at lowest level: " << endl; ret = 1; } return ret; }