void bdb_open(void) { DB *db; DBC *dbc; DB_ENV *dbenv; assert(db_env_create(&dbenv, 0) == 0); dbenv->set_errpfx(dbenv, "bdb"); dbenv->set_errfile(dbenv, stderr); assert(dbenv->mutex_set_max(dbenv, 10000) == 0); assert(dbenv->set_cachesize(dbenv, 0, 50 * 1024 * 1024, 1) == 0); assert(dbenv->open(dbenv, NULL, DB_CREATE | (g.c_delete_pct == 0 && g.c_insert_pct == 0 && g.c_write_pct == 0 ? 0 : DB_INIT_LOCK) | DB_INIT_MPOOL | DB_PRIVATE, 0) == 0); assert(db_create(&db, dbenv, 0) == 0); if (g.c_file_type == ROW && g.c_reverse) assert(db->set_bt_compare(db, bdb_compare_reverse) == 0); assert(db->open(db, NULL, "__bdb", NULL, DB_BTREE, DB_CREATE, 0) == 0); g.bdb = db; assert(db->cursor(db, NULL, &dbc, 0) == 0); g.dbc = dbc; key_gen_setup(&keybuf); }
void bdb_open(void) { DB *db; DBC *dbc; DB_ENV *dbenv; assert(db_env_create(&dbenv, 0) == 0); dbenv->set_errpfx(dbenv, "bdb"); dbenv->set_errfile(dbenv, stderr); assert(dbenv->mutex_set_max(dbenv, 10000) == 0); assert(dbenv->set_cachesize(dbenv, 0, 50 * 1024 * 1024, 1) == 0); assert(dbenv->open(dbenv, NULL, DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL | DB_PRIVATE, 0) == 0); assert(db_create(&db, dbenv, 0) == 0); if (g.c_reverse) assert(db->set_bt_compare(db, bdb_compare_reverse) == 0); assert(db->open( db, NULL, g.home_bdb, NULL, DB_BTREE, DB_CREATE, 0) == 0); g.bdb = db; assert(db->cursor(db, NULL, &dbc, 0) == 0); g.dbc = dbc; key_gen_init(&keyitem); }
int nbsp_open_dbenv(void){ DB_ENV *dbenv = NULL; int status = 0; uint32_t mb; uint32_t dbenv_flags = DBENV_FLAGS; mb = (1024 * 1024) * g.dbcache_mb; status = db_env_create(&dbenv, 0); if(status == 0) status = dbenv->set_cachesize(dbenv, 0, mb, 0); if(status == 0) status = dbenv->open(dbenv, g.dbhome, dbenv_flags, g.dbfile_mode); if(status != 0){ log_errx("Cannot initialize db environment. %s", db_strerror(status)); status = -1; if(dbenv != NULL) (void)dbenv->close(dbenv, 0); }else g.dbenv = dbenv; return(status); }
static DB_ENV *dict_db_new_env(const char *db_path) { VSTRING *db_home_buf; DB_ENV *dbenv; u_int32_t cache_size_gbytes; u_int32_t cache_size_bytes; int ncache; if ((errno = db_env_create(&dbenv, 0)) != 0) msg_fatal("create DB environment: %m"); #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 7) if ((errno = dbenv->get_cachesize(dbenv, &cache_size_gbytes, &cache_size_bytes, &ncache)) != 0) msg_fatal("get DB cache size: %m"); if (cache_size_gbytes == 0 && cache_size_bytes < dict_db_cache_size) { if ((errno = dbenv->set_cache_max(dbenv, cache_size_gbytes, dict_db_cache_size)) != 0) msg_fatal("set DB max cache size %d: %m", dict_db_cache_size); if ((errno = dbenv->set_cachesize(dbenv, cache_size_gbytes, dict_db_cache_size, ncache)) != 0) msg_fatal("set DB cache size %d: %m", dict_db_cache_size); } #endif /* XXX db_home is also the default directory for the .db file. */ db_home_buf = vstring_alloc(100); if ((errno = dbenv->open(dbenv, sane_dirname(db_home_buf, db_path), DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0)) != 0) msg_fatal("open DB environment: %m"); vstring_free(db_home_buf); return (dbenv); }
DB_ENV *ldbm_initialize_env(const char *home, int dbcachesize, int *envdirok) { DB_ENV *env = NULL; int err; u_int32_t envFlags; err = db_env_create( &env, 0 ); if ( err ) { #ifdef LDAP_SYSLOG syslog( LOG_INFO, "ldbm_initialize_env(): " "FATAL error in db_env_create() : %s (%d)\n", db_strerror( err ), err ); #endif return NULL; } #if DB_VERSION_X >= 0x030300 /* This interface appeared in 3.3 */ env->set_alloc( env, ldbm_malloc, NULL, NULL ); #endif env->set_errcall( env, ldbm_db_errcall ); env->set_errpfx( env, "==>" ); if (dbcachesize) { env->set_cachesize( env, 0, dbcachesize, 0 ); } envFlags = DB_CREATE | DB_INIT_MPOOL | DB_USE_ENVIRON; #ifdef DB_PRIVATE envFlags |= DB_PRIVATE; #endif #ifdef DB_MPOOL_PRIVATE envFlags |= DB_MPOOL_PRIVATE; #endif #ifdef HAVE_BERKELEY_DB_THREAD envFlags |= DB_THREAD; #endif #if DB_VERSION_X >= 0x030100 err = env->open( env, home, envFlags, 0 ); #else /* 3.0.x requires an extra argument */ err = env->open( env, home, NULL, envFlags, 0 ); #endif if ( err != 0 ) { #ifdef LDAP_SYSLOG syslog( LOG_INFO, "ldbm_initialize_env(): " "FATAL error in dbEnv->open() : %s (%d)\n", db_strerror( err ), err ); #endif env->close( env, 0 ); return NULL; } *envdirok = 1; return env; }
int bdblib_create_dbenv(DB_ENV **_dbenv, char* _home) { DB_ENV *env; char *progname; int rc, flags; progname = "kamailio"; /* Create an environment and initialize it for additional error * reporting. */ if ((rc = db_env_create(&env, 0)) != 0) { ERR("db_env_create failed! bdb error: %s.\n", db_strerror(rc)); return (rc); } env->set_errpfx(env, progname); /* Specify the shared memory buffer pool cachesize */ if ((rc = env->set_cachesize(env, 0, _bdb_parms->cache_size, 0)) != 0) { ERR("dbenv set_cachsize failed! bdb error: %s.\n", db_strerror(rc)); env->err(env, rc, "set_cachesize"); goto err; } /* Concurrent Data Store flags */ flags = DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL | DB_THREAD; /* Transaction Data Store flags ; not supported yet */ /* flags = DB_CREATE | DB_RECOVER | DB_INIT_LOG | DB_INIT_LOCK | DB_INIT_MPOOL | DB_THREAD | DB_INIT_TXN; */ /* Open the environment */ if ((rc = env->open(env, _home, flags, 0)) != 0) { ERR("dbenv is not initialized! bdb error: %s.\n",db_strerror(rc)); env->err(env, rc, "environment open: %s", _home); goto err; } *_dbenv = env; return (0); err: (void)env->close(env, 0); return (rc); }
int TestSetCachesize(CuTest *ct) { DB_ENV *dbenv; int ncache; u_int32_t a, b; dbenv = NULL; /* cache size: NOT reset at run-time. */ ENV CuAssertTrue(ct, dbenv->set_cachesize(dbenv, 1, 131072, 3) == 0); CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_CREATE | DB_INIT_MPOOL, 0666) == 0); CuAssertTrue(ct, dbenv->get_cachesize(dbenv, &a, &b, &ncache) == 0); CuAssertTrue(ct, dbenv->get_cachesize(dbenv, &a, &b, &ncache) == 0); CuAssertTrue(ct, a == 1 && b == 131072 && ncache == 3); ENV CuAssertTrue(ct, dbenv->set_cachesize(dbenv, 2, 262144, 1) == 0); CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0); CuAssertTrue(ct, dbenv->get_cachesize(dbenv, &a, &b, &ncache) == 0); CuAssertTrue(ct, a == 1 && b == 131072 && ncache == 3); 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); // match lladd's defaults... dbenv->set_lg_bsize(dbenv, 1024*1024); // match lladd's defaults... dbenv->set_cachesize(dbenv, 0, 8204288, 0); /* 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); }*/ dbenv->set_tx_max(dbenv, 32000); unsigned int max; dbenv->get_tx_max(dbenv, &max); printf("Max xact count: %d\n", max); /* * 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_PRIVATE | 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; }
OptimisticDb* optimisticDb_create() { OptimisticDb *optimisticDb; DB_ENV *envp; u_int32_t envFlags; int ret; /* Allocate memory */ optimisticDb = malloc( sizeof( OptimisticDb ) ); ret = db_env_create( &envp, 0 ); if( ret != 0 ) { printf( "Failed to create anvironment for optimistic database store\n" ); exit(1); } envFlags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_INIT_MPOOL | DB_PRIVATE /* Don't put region files on disk */ ; /* Store database logs entirely in memory */ envp->log_set_config( envp, DB_LOG_IN_MEMORY, 1 ); /* Increase the cache size */ envp->set_cachesize( envp, 0, 100 * 1024 * 1024, 1 ); envp->set_errfile( envp, stderr ); ret = envp->open( envp, NULL, envFlags, 0 ); if( ret != 0 ) { printf( "Failed to create environment for optimistic database store\n"); exit( 1 ); } optimisticDb->envp = envp; optimisticDb->isOpen = 0; return optimisticDb; }
/* * env_init -- * Initialize the environment. */ DB_ENV * env_init( char *home, char *prefix, int cachesize) { DB_ENV *dbenv; int ret; if ((ret = db_env_create(&dbenv, 0)) != 0) { dbenv->err(dbenv, ret, "db_env_create"); return (NULL); } dbenv->set_errfile(dbenv, stderr); dbenv->set_errpfx(dbenv, prefix); if ((ret = dbenv->set_cachesize(dbenv, 0, cachesize, 0)) != 0) { dbenv->err(dbenv, ret, "DB_ENV->set_cachesize"); return (NULL); } if ((ret = dbenv->open(dbenv, home, DB_CREATE | DB_INIT_MPOOL | DB_INIT_TXN | DB_INIT_LOCK, 0)) != 0) { dbenv->err(dbenv, ret, "DB_ENV->open: %s", home); (void)dbenv->close(dbenv, 0); return (NULL); } return (dbenv); }
/* * db_init -- * Initialize the environment. */ int db_init(char *home, DB_ENV **dbenvp) { int ret; DB_ENV *dbenv; if ((ret = db_env_create(&dbenv, 0)) != 0) return (ret); /* dbenv->set_lk_max(dbenv, 10000); */ dbenv->set_cachesize(dbenv, 0, 16 * 1024 * 1024, 0); /* new version only ... dbenv->set_flags(dbenv, DB_CDB_ALLDB, 1); */ if (home == NULL) { home = getenv("CHESHIRE_DB_HOME"); if (home == NULL) { fprintf(stderr, "CHESHIRE_DB_HOME must be set OR config file <DBENV> set\n"); fprintf(LOGFILE, "CHESHIRE_DB_HOME must be set OR config file <DBENV> set\n"); return(1); } } if ((ret = dbenv->open(dbenv, home, DB_INIT_MPOOL | DB_INIT_CDB | DB_CREATE | DB_USE_ENVIRON, 0)) == 0) { *dbenvp = dbenv; dbenv->set_errfile(dbenv, LOGFILE); dbenv->set_errpfx(dbenv, "BerkeleyDB"); return (0); } /* this goes to stdout and screws up z39.50 connections -- hangs them */ /* dbenv->err(dbenv, ret, "Could not open DB environment: %s", home); */ fprintf(LOGFILE,"Could not open DB environment: %s\n",home); (void)dbenv->close(dbenv, 0); return (ret); }
/* * This should be done before the program is open for business. * As such, it does not need to be reentrant. */ int open_db_env(char *topdir) { DB_ENV *tmpenv = NULL; int st; struct stat64 sbuf; char logdir[MAXPATHLEN+1]; char tmpdir[MAXPATHLEN+1]; char *dirarr[3]; int i; if (topdir == NULL) { return (-1); } snprintf(logdir, sizeof (tmpdir), "%s/.logs", topdir); snprintf(tmpdir, sizeof (tmpdir), "%s/.tmp", topdir); dirarr[0] = topdir; dirarr[1] = logdir; dirarr[2] = tmpdir; /* first, set up the environment */ st = db_env_create(&tmpenv, 0); if (st != 0) { return (st); } /* make sure the directories exist */ for (i = 0; i < 3; i++) { st = stat64(dirarr[i], &sbuf); if ((st != 0) && (errno == ENOENT)) { st = mkdirp(dirarr[i], 0744); if (st == 0) { st = stat64(dirarr[i], &sbuf); } } if ((st == 0) && (!S_ISDIR(sbuf.st_mode))) { st = -1; break; } } if (st != 0) { return (st); } st = tmpenv->set_data_dir(tmpenv, topdir); if (st != 0) { return (st); } st = tmpenv->set_lg_dir(tmpenv, logdir); if (st != 0) { return (st); } st = tmpenv->set_tmp_dir(tmpenv, tmpdir); if (st != 0) { return (st); } st = tmpenv->set_flags(tmpenv, env_fl, 1); if (st != 0) { return (st); } /* overall database cache size */ st = tmpenv->set_cachesize(tmpenv, 0, (60 * MEGA), 1); st = tmpenv->set_shm_key(tmpenv, FSM_SHM_MASTER_KEY); if (st != 0) { return (st); } /* log buffer in memory */ st = tmpenv->set_lg_bsize(tmpenv, (30 * MEGA)); if (st != 0) { return (st); } /* set up additional error logging */ tmpenv->set_errcall(tmpenv, fsmdb_log_err); /* Increase the number of locks available */ tmpenv->set_lk_max_locks(tmpenv, 10000); tmpenv->set_lk_max_lockers(tmpenv, 10000); tmpenv->set_lk_max_objects(tmpenv, 10000); /* Increase the number of concurrent transactions available */ /* Note: Default in 4.4-20 is '20'. In later versions it's 100 */ tmpenv->set_tx_max(tmpenv, 100); st = tmpenv->open(tmpenv, topdir, env_ofl, 0644); if (st != 0) { /* check for a major failure */ if (st == DB_RUNRECOVERY) { st = tmpenv->open(tmpenv, topdir, env_ffl, 0644); } /* log catastrophic failure and remove all db files. */ if (st == DB_RUNRECOVERY) { fsmdb_log_err(dbEnv, NULL, "Database files corrupt, cannot recover. " "Files will be removed. Please re-index any " "recovery points to regenerate the database."); fsmdb_remove_all(topdir); } } if (st != 0) { return (st); } /* clear out unneeded log files */ tmpenv->log_archive(tmpenv, NULL, DB_ARCH_REMOVE); /* all set, ready to use */ dbEnv = tmpenv; return (st); }
int b_recover(int argc, char *argv[]) { extern char *optarg; extern int optind; DB *dbp; DBT key, data; DB_ENV *dbenv; DB_TXN *txn; u_int32_t cachesize; int ch, i, count; /* * Recover was too slow before release 4.0 that it's not worth * running the test. */ #if DB_VERSION_MAJOR < 4 return (0); #endif cachesize = MEGABYTE; count = 1000; while ((ch = getopt(argc, argv, "C:c:")) != EOF) switch (ch) { case 'C': cachesize = (u_int32_t)atoi(optarg); break; case 'c': count = atoi(optarg); break; case '?': default: return (usage()); } argc -= optind; argv += optind; if (argc != 0) return (usage()); /* Create the environment. */ DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0); dbenv->set_errfile(dbenv, stderr); DB_BENCH_ASSERT(dbenv->set_cachesize(dbenv, 0, cachesize, 0) == 0); #define OFLAGS \ (DB_CREATE | DB_INIT_LOCK | \ DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE) #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0 DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, NULL, OFLAGS, 0666) == 0); #endif #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1 DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, OFLAGS, 0666) == 0); #endif #if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1 DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, OFLAGS, 0666) == 0); #endif /* Create the database. */ DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0); #if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1 DB_BENCH_ASSERT(dbp->open(dbp, NULL, TESTFILE, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0666) == 0); #else DB_BENCH_ASSERT( dbp->open(dbp, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0); #endif /* Initialize the data. */ memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); key.size = data.size = 20; key.data = data.data = "01234567890123456789"; /* Start/commit a transaction count times. */ for (i = 0; i < count; ++i) { #if DB_VERSION_MAJOR < 4 DB_BENCH_ASSERT( txn_begin(dbenv, NULL, &txn, DB_TXN_NOSYNC) == 0); DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0); DB_BENCH_ASSERT(txn_commit(txn, 0) == 0); #else DB_BENCH_ASSERT( dbenv->txn_begin(dbenv, NULL, &txn, DB_TXN_NOSYNC) == 0); DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0); DB_BENCH_ASSERT(txn->commit(txn, 0) == 0); #endif } DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0); DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0); /* Create a new DB_ENV handle. */ DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0); dbenv->set_errfile(dbenv, stderr); DB_BENCH_ASSERT( dbenv->set_cachesize(dbenv, 0, 1048576 /* 1MB */, 0) == 0); /* Now run recovery. */ TIMER_START; #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0 DB_BENCH_ASSERT(dbenv->open( dbenv, TESTDIR, NULL, OFLAGS | DB_RECOVER, 0666) == 0); #endif #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1 DB_BENCH_ASSERT( dbenv->open(dbenv, TESTDIR, OFLAGS | DB_RECOVER, 0666) == 0); #endif #if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1 DB_BENCH_ASSERT( dbenv->open(dbenv, TESTDIR, OFLAGS | DB_RECOVER, 0666) == 0); #endif TIMER_STOP; /* * We divide the time by the number of transactions, so an "operation" * is the recovery of a single transaction. */ printf("# recovery after %d transactions\n", count); TIMER_DISPLAY(count); DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0); return (0); }
static int db_init(dbiIndex dbi, const char * dbhome, const char * dbfile, const char * dbsubfile, DB_ENV ** dbenvp) { rpmdb rpmdb = dbi->dbi_rpmdb; DB_ENV *dbenv = NULL; int eflags; int rc; if (dbenvp == NULL) return 1; /* XXX HACK */ if (rpmdb->db_errfile == NULL) rpmdb->db_errfile = stderr; eflags = (dbi->dbi_oeflags | dbi->dbi_eflags); if (eflags & DB_JOINENV) eflags &= DB_JOINENV; if (dbfile) { char *dbiflags = prDbiOpenFlags(eflags, 1); rpmlog(RPMLOG_DEBUG, "opening db environment %s/%s %s\n", dbhome, dbfile, dbiflags); free(dbiflags); } /* XXX Can't do RPC w/o host. */ if (dbi->dbi_host == NULL) dbi->dbi_ecflags &= ~DB_CLIENT; /* XXX Set a default shm_key. */ if ((dbi->dbi_eflags & DB_SYSTEM_MEM) && dbi->dbi_shmkey == 0) { #if defined(HAVE_FTOK) dbi->dbi_shmkey = ftok(dbhome, 0); #else dbi->dbi_shmkey = 0x44631380; #endif } rc = db_env_create(&dbenv, dbi->dbi_ecflags); rc = cvtdberr(dbi, "db_env_create", rc, _debug); if (dbenv == NULL || rc) goto errxit; { int xx; /* 4.1: dbenv->set_app_dispatch(???) */ /* 4.1: dbenv->set_alloc(???) */ /* 4.1: dbenv->set_data_dir(???) */ /* 4.1: dbenv->set_encrypt(???) */ dbenv->set_errcall(dbenv, (void *) rpmdb->db_errcall); dbenv->set_errfile(dbenv, rpmdb->db_errfile); dbenv->set_errpfx(dbenv, rpmdb->db_errpfx); /* 4.1: dbenv->set_feedback(???) */ /* 4.1: dbenv->set_flags(???) */ /* dbenv->set_paniccall(???) */ #if (DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 5) /* * These enable automatic stale lock removal. * thread_count 8 is some kind of "magic minimum" value... */ dbenv->set_thread_count(dbenv, 8); dbenv->set_isalive(dbenv, db3isalive); #endif if ((dbi->dbi_ecflags & DB_CLIENT) && dbi->dbi_host) { const char * home; int retry = 0; if ((home = strrchr(dbhome, '/')) != NULL) dbhome = ++home; while (retry++ < 5) { xx = dbenv->set_rpc_server(dbenv, NULL, dbi->dbi_host, dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0); xx = cvtdberr(dbi, "dbenv->set_server", xx, _debug); if (!xx) break; (void) sleep(15); } } else { #if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3) xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT, (dbi->dbi_verbose & DB_VERB_CHKPOINT)); #endif xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, (dbi->dbi_verbose & DB_VERB_DEADLOCK)); xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, (dbi->dbi_verbose & DB_VERB_RECOVERY)); xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR, (dbi->dbi_verbose & DB_VERB_WAITSFOR)); if (dbi->dbi_mmapsize) { xx = dbenv->set_mp_mmapsize(dbenv, dbi->dbi_mmapsize); xx = cvtdberr(dbi, "dbenv->set_mp_mmapsize", xx, _debug); } if (dbi->dbi_tmpdir) { const char * root; char * tmpdir; root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root); if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone) root = NULL; tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL); xx = dbenv->set_tmp_dir(dbenv, tmpdir); xx = cvtdberr(dbi, "dbenv->set_tmp_dir", xx, _debug); tmpdir = _free(tmpdir); } } /* dbenv->set_lk_conflicts(???) */ /* dbenv->set_lk_detect(???) */ /* 4.1: dbenv->set_lk_max_lockers(???) */ /* 4.1: dbenv->set_lk_max_locks(???) */ /* 4.1: dbenv->set_lk_max_objects(???) */ /* 4.1: dbenv->set_lg_bsize(???) */ /* 4.1: dbenv->set_lg_dir(???) */ /* 4.1: dbenv->set_lg_max(???) */ /* 4.1: dbenv->set_lg_regionmax(???) */ if (dbi->dbi_cachesize) { xx = dbenv->set_cachesize(dbenv, 0, dbi->dbi_cachesize, 0); xx = cvtdberr(dbi, "dbenv->set_cachesize", xx, _debug); } /* 4.1 dbenv->set_timeout(???) */ /* dbenv->set_tx_max(???) */ /* 4.1: dbenv->set_tx_timestamp(???) */ /* dbenv->set_tx_recover(???) */ /* dbenv->set_rep_transport(???) */ /* dbenv->set_rep_limit(???) */ if (dbi->dbi_no_fsync) { xx = db_env_set_func_fsync(db3_fsync_disable); xx = cvtdberr(dbi, "db_env_set_func_fsync", xx, _debug); } if (dbi->dbi_shmkey) { xx = dbenv->set_shm_key(dbenv, dbi->dbi_shmkey); xx = cvtdberr(dbi, "dbenv->set_shm_key", xx, _debug); } } rc = (dbenv->open)(dbenv, dbhome, eflags, dbi->dbi_perms); rc = cvtdberr(dbi, "dbenv->open", rc, _debug); if (rc) goto errxit; #if (DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 5) /* stale lock removal */ rc = dbenv->failchk(dbenv, 0); rc = cvtdberr(dbi, "dbenv->failchk", rc, _debug); if (rc) goto errxit; #endif *dbenvp = dbenv; return 0; errxit: if (dbenv) { int xx; xx = dbenv->close(dbenv, 0); xx = cvtdberr(dbi, "dbenv->close", xx, _debug); } return rc; }
int main(int argc, char **argv) { std::string path2DbEnv; std::string theContainer = "simpleExampleData.dbxml"; for ( int i=1; i<argc; i++ ) { if ( argv[i][0] == '-' ) { switch(argv[i][1]) { case 'h': path2DbEnv = argv[++i]; break; default: usage(); } } } if (! path2DbEnv.length() ) usage(); // 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 (64 MB). The default is quite small u_int32_t envCacheSize = 64*1024*1024; // 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); dberr = dbEnv->open(dbEnv, path2DbEnv.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; } //Have the XmlManager adopt the db environment XmlManager db(dbEnv, DBXML_ADOPT_DBENV); //Configure the container to use transactions XmlContainerConfig config; config.setTransactional(true); //Open a container in the db environment XmlContainer container = db.openContainer(theContainer, config); // Get an XmlUpdateContext. Useful from a performance perspective. XmlUpdateContext updateContext = db.createUpdateContext(); //Get a transaction XmlTransaction txn = db.createTransaction(); std::string document1 = "<aDoc><title>doc1</title><color>green</color></aDoc>"; std::string document2 = "<aDoc><title>doc2</title><color>yellow</color></aDoc>"; //Add the documents XmlDocument myXMLDoc = db.createDocument(); /* Set the XmlDocument to the relevant string and then put it into the container. * Using the flag DBXML_GEN_NAME means that a generated name will be assigned * to the document if it does not have one. An exception will be thrown if * a document is inserted without a name or the DBXML_GEN_NAME flag. */ myXMLDoc.setContent( document1 ); container.putDocument(txn, myXMLDoc, updateContext, DBXML_GEN_NAME); //do it again for the second document myXMLDoc.setContent( document2 ); container.putDocument(txn, myXMLDoc, updateContext, DBXML_GEN_NAME); //Normally we would use a try/catch block to trap any exceptions. // In the catch, we should call txn->abort() to avoid leaving the // database in an indeterminate state in the event of an error. // However, this simple example avoids error handling so as to // highlite basic concepts, so that step if omitted here as well. //Commit the writes. This causes the container write operations // to be saved to the container. txn.commit(); return 0; }
static int db_init(rpmdb rdb, const char * dbhome) { DB_ENV *dbenv = NULL; int rc, xx; int retry_open = 2; int lockfd = -1; struct dbConfig_s * cfg = &rdb->cfg; /* This is our setup, thou shall not have other setups before us */ uint32_t eflags = (DB_CREATE|DB_INIT_MPOOL|DB_INIT_CDB); if (rdb->db_dbenv != NULL) { rdb->db_opens++; return 0; } else { /* On first call, set backend description to something... */ free(rdb->db_descr); rasprintf(&rdb->db_descr, "db%u", DB_VERSION_MAJOR); } /* * Both verify and rebuild are rather special, if for different reasons: * On rebuild we dont want to be affected by eg paniced environment, and * CDB only slows things down there. Verify is a quirky beast unlike * anything else in BDB, and does not like shared env or CDB. */ if (rdb->db_flags & (RPMDB_FLAG_VERIFYONLY|RPMDB_FLAG_REBUILD)) { eflags |= DB_PRIVATE; eflags &= ~DB_INIT_CDB; } rc = db_env_create(&dbenv, 0); rc = dbapi_err(rdb, "db_env_create", rc, _debug); if (dbenv == NULL || rc) goto errxit; dbenv->set_alloc(dbenv, rmalloc, rrealloc, NULL); dbenv->set_errcall(dbenv, NULL); dbenv->set_errpfx(dbenv, _errpfx); dbenv->set_msgcall(dbenv, warnlog); /* * These enable automatic stale lock removal. * thread_count 8 is some kind of "magic minimum" value... */ dbenv->set_thread_count(dbenv, 8); dbenv->set_isalive(dbenv, isalive); dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, (cfg->db_verbose & DB_VERB_DEADLOCK)); dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, (cfg->db_verbose & DB_VERB_RECOVERY)); dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR, (cfg->db_verbose & DB_VERB_WAITSFOR)); if (cfg->db_mmapsize) { xx = dbenv->set_mp_mmapsize(dbenv, cfg->db_mmapsize); xx = dbapi_err(rdb, "dbenv->set_mp_mmapsize", xx, _debug); } if (cfg->db_cachesize) { xx = dbenv->set_cachesize(dbenv, 0, cfg->db_cachesize, 0); xx = dbapi_err(rdb, "dbenv->set_cachesize", xx, _debug); } /* * Serialize shared environment open (and clock) via fcntl() lock. * Otherwise we can end up calling dbenv->failchk() while another * process is joining the environment, leading to transient * DB_RUNRECOVER errors. Also prevents races wrt removing the * environment (eg chrooted operation). Silently fall back to * private environment on failure to allow non-privileged queries * to "work", broken as it might be. */ if (!(eflags & DB_PRIVATE)) { lockfd = serialize_env(dbhome); if (lockfd < 0) { eflags |= DB_PRIVATE; retry_open--; rpmlog(RPMLOG_DEBUG, "serialize failed, using private dbenv\n"); } } /* * Actually open the environment. Fall back to private environment * if we dont have permission to join/create shared environment or * system doesn't support it.. */ while (retry_open) { char *fstr = prDbiOpenFlags(eflags, 1); rpmlog(RPMLOG_DEBUG, "opening db environment %s %s\n", dbhome, fstr); free(fstr); rc = (dbenv->open)(dbenv, dbhome, eflags, rdb->db_perms); if ((rc == EACCES || rc == EROFS) || (rc == EINVAL && errno == rc)) { eflags |= DB_PRIVATE; retry_open--; } else { retry_open = 0; } } rc = dbapi_err(rdb, "dbenv->open", rc, _debug); if (rc) goto errxit; dbenv->set_errcall(dbenv, errlog); /* stale lock removal */ rc = dbenv->failchk(dbenv, 0); rc = dbapi_err(rdb, "dbenv->failchk", rc, _debug); if (rc) goto errxit; rdb->db_dbenv = dbenv; rdb->db_opens = 1; if (lockfd >= 0) close(lockfd); return 0; errxit: if (dbenv) { int xx; xx = dbenv->close(dbenv, 0); xx = dbapi_err(rdb, "dbenv->close", xx, _debug); } if (lockfd >= 0) close(lockfd); return rc; }
static int bdb_init_tx_handle(struct storage* s, char* db_env_path) { int result; DB_ENV* dbenv; //Create environment handle result = db_env_create(&dbenv, 0); if (result != 0) { paxos_log_error("DB_ENV creation failed: %s", db_strerror(result)); return -1; } //Durability mode if (!paxos_config.bdb_sync) result = dbenv->set_flags(dbenv, DB_TXN_WRITE_NOSYNC, 1); if (result != 0) { paxos_log_error("DB_ENV set_flags failed: %s", db_strerror(result)); return -1; } //Redirect errors to sdout dbenv->set_errfile(dbenv, stdout); //Set the size of the memory cache result = dbenv->set_cachesize(dbenv, 0, paxos_config.bdb_cachesize, 1); if (result != 0) { paxos_log_error("DB_ENV set_cachesize failed: %s", db_strerror(result)); return -1; } //TODO see page size impact //Set page size for this db // result = dbp->set_pagesize(dbp, pagesize); // assert(result == 0); //FIXME set log size // Environment open flags int flags; flags = DB_CREATE | /* Create if not existing */ 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. */ DB_THREAD | /* Cause the environment to be free-threaded */ DB_REGISTER | DB_INIT_MPOOL; /* Initialize the memory pool (in-memory cache) */ //Open the DB environment result = dbenv->open(dbenv, db_env_path, /* Environment directory */ flags, /* Open flags */ 0); /* Default file permissions */ if (result != 0) { paxos_log_error("DB_ENV open failed: %s", db_strerror(result)); return -1; } paxos_log_info("Berkeley DB storage opened successfully"); s->env = dbenv; return 0; }
static struct nb_db * nb_db_berkeleydb_open(const struct nb_db_opts *opts) { struct nb_db_berkeleydb *berkeleydb = malloc(sizeof(*berkeleydb)); if (berkeleydb == NULL) { fprintf(stderr, "malloc(%zu) failed", sizeof(*berkeleydb)); goto error_1; } int r; r = mkdir(opts->path, 0777); if (r != 0 && errno != EEXIST) { fprintf(stderr, "mkdir: %d\n", r); goto error_1; } DB_ENV *env; DB *db; int env_flags = DB_REGION_INIT; r = db_env_create(&env, 0); if (r != 0) { fprintf(stderr, "db_env_create: %s\n", db_strerror(r)); goto error_1; } r = env->set_cachesize(env, 0, 0, 1); if (r != 0) { fprintf(stderr, "set_cachesize: %s\n", db_strerror(r)); goto error_2; } env_flags |= DB_TXN_WRITE_NOSYNC; r = env->set_flags(env, env_flags, 1); if (r != 0) { fprintf(stderr, "set_flags: %s\n", db_strerror(r)); goto error_2; } int log_flags = DB_LOG_AUTO_REMOVE; r = env->log_set_config(env, log_flags, 1); int env_open_flags = DB_CREATE|DB_INIT_MPOOL; r = env->open(env, opts->path, env_open_flags, 0666); if (r != 0) { fprintf(stderr, "env->open: %s\n", db_strerror(r)); goto error_2; } r = db_create(&db, env, 0); if (r != 0) { fprintf(stderr, "db_create: %s\n", db_strerror(r)); goto error_2; } int open_flags = DB_CREATE; r = db->open(db, NULL, "data.bdb", NULL, DB_BTREE, open_flags, 0664); if (r != 0) { fprintf(stderr, "db->open: %s\n", db_strerror(r)); goto error_3; } berkeleydb->env = env; berkeleydb->db = db; return (struct nb_db *) berkeleydb; error_3: db->close(db, 0); error_2: env->close(env, 0); error_1: return NULL; }
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) { std::string path2DbEnv; std::string theContainer = "simpleExampleData.dbxml"; for ( int i=1; i<argc; i++ ) { if ( argv[i][0] == '-' ) { switch(argv[i][1]) { case 'h': path2DbEnv = argv[++i]; break; default: usage(); } } } if (! path2DbEnv.length() ) usage(); // 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 (64 MB). The default is quite small u_int32_t envCacheSize = 64*1024*1024; // 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); dberr = dbEnv->open(dbEnv, path2DbEnv.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; } //Have the XmlManager adopt the db environment XmlManager db(dbEnv, DBXML_ADOPT_DBENV); //Configure the container to use transactions XmlContainerConfig config; config.setTransactional(true); //Open a container in the db environment XmlContainer container = db.openContainer(theContainer, config); //Create a transaction XmlTransaction txn = db.createTransaction(); //perform the queries //find all the Vendor documents in the database doQuery( txn, db, container, "/vendor" ); //find all the vendors that are wholesale shops doQuery( txn, db, container, "/vendor[@type=\"wholesale\"]"); //find the product document for "Lemon Grass" doQuery( txn, db, container, "/product/item[.=\"Lemon Grass\"]"); //find all the products where the price is less than or equal to 0.11 doQuery( txn, db, container, "/product/inventory[price<=0.11]"); //find all the vegetables where the price is less than or equal to 0.11 doQuery( txn, db, container, "/product[inventory/price<=0.11 and category=\"vegetables\"]"); //commit the transaction txn.commit(); return 0; }
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 main(int argc, char **argv) { std::string path2DbEnv; std::string theContainer = "namespaceExampleData.dbxml"; for ( int i=1; i<argc; i++ ) { if ( argv[i][0] == '-' ) { switch(argv[i][1]) { case 'h': path2DbEnv = argv[++i]; break; default: usage(); } } } if (! path2DbEnv.length() ) usage(); // 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 (64 MB). The default is quite small u_int32_t envCacheSize = 64*1024*1024; // 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); dberr = dbEnv->open(dbEnv, path2DbEnv.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; } //Have the XmlManager adopt the db environment XmlManager mgr(dbEnv, DBXML_ADOPT_DBENV); //Configure the container to use transactions XmlContainerConfig config; config.setTransactional(true); //Open a container in the db environment XmlContainer container = mgr.openContainer(theContainer, config); //create a transaction XmlTransaction txn = mgr.createTransaction(); //create a context and declare the namespaces XmlQueryContext context = mgr.createQueryContext(); context.setNamespace( "fruits", "http://groceryItem.dbxml/fruits"); context.setNamespace( "vegetables", "http://groceryItem.dbxml/vegetables"); context.setNamespace( "desserts", "http://groceryItem.dbxml/desserts"); //Query for documents by their document names. doContextQuery( txn, mgr, container.getName(), "/*[dbxml:metadata('dbxml:name')='ZuluNut.xml']", context ); doContextQuery( txn, mgr, container.getName(), "/*[dbxml:metadata('dbxml:name')='TrifleOrange.xml']", context ); doContextQuery( txn, mgr, container.getName(), "/*[dbxml:metadata('dbxml:name')='TriCountyProduce.xml']", context ); //Get the document from the container using the document name doGetDocument(txn, container, "ZuluNut.xml"); doGetDocument(txn, container, "TrifleOrange.xml"); doGetDocument(txn, container, "TriCountyProduce.xml"); //commit the transaction txn.commit(); return 0; }
static void open_db(bdb_drv_t* pdrv, ErlIOVec *ev) { drv_cfg* pcfg; DB* pdb; DB_ENV* penv; u_int32_t open_flags, page_size_bytes, cache_size_bytes, bulk_get_buffer_size_bytes; int ret; ErlDrvBinary* data = ev->binv[1]; char *bytes = data->orig_bytes; int txn_enabled = bytes[1]; int db_type = bytes[2]; cache_size_bytes = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4) )); page_size_bytes = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4 + 4) )); bulk_get_buffer_size_bytes = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4 + 4 + 4) )); char* lv_start = bytes + 4 + 4 + 4 + 4; char *db_name_len_bytes = lv_start; char *db_name_bytes = lv_start + 4; uint32_t db_name_length = (uint32_t) ntohl(* ((uint32_t*) db_name_len_bytes) ); char *data_dir_len_bytes = db_name_bytes + db_name_length; char *data_dir_bytes = data_dir_len_bytes + 4; uint32_t data_dir_length = (uint32_t) ntohl(* ((uint32_t*) data_dir_len_bytes) ); pcfg = (drv_cfg*) malloc(sizeof(drv_cfg)); pcfg->buffer = (char*) malloc(bulk_get_buffer_size_bytes); if (pcfg->buffer == NULL) { return_error_tuple(pdrv, "Could not allocate memory for operation!"); return; } pcfg->penv = NULL; pcfg->pdb = NULL; pcfg->txn_enabled = txn_enabled; pcfg->data_dir = malloc(data_dir_length + 1); pcfg->db_name = malloc(db_name_length + 1); pcfg->page_size_bytes = page_size_bytes; pcfg->data_dir[data_dir_length] = 0; pcfg->db_name[db_name_length] = 0; if (db_type == 'B') { pcfg->db_type = DB_BTREE; } else { pcfg->db_type = DB_HASH; } memcpy(pcfg->data_dir, data_dir_bytes, data_dir_length); memcpy(pcfg->db_name, db_name_bytes, db_name_length); pcfg->bulk_get_buffer_size_bytes = bulk_get_buffer_size_bytes; ret = db_env_create(&penv, 0); if (ret != 0) { return_error_tuple(pdrv, db_strerror(ret)); return; } penv->app_private = pcfg; open_flags = DB_CREATE | DB_INIT_MPOOL; if (pcfg->txn_enabled) { open_flags |= DB_INIT_LOCK | DB_THREAD | DB_INIT_TXN | DB_INIT_LOG | DB_REGISTER | DB_RECOVER; } //penv->set_msgcall(penv, (FILE*)stderr); //penv->set_verbose(penv, DB_VERB_DEADLOCK | DB_VERB_RECOVERY, 1); ret = penv->set_cachesize(penv, 0, cache_size_bytes, 1); if (ret != 0) { return_error_tuple(pdrv, db_strerror(ret)); return; } ret = penv->open(penv, pcfg->data_dir, open_flags, 0); if (ret != 0) { return_error_tuple(pdrv, db_strerror(ret)); return; } //Only open the table if not in replication mode.... for rep mode the table will be opened in event handler ... ret = db_create(&pdb, penv, 0); if (ret != 0) { return_error_tuple(pdrv, db_strerror(ret)); return; } if (pcfg->db_type == DB_BTREE) { ret = pdb->set_flags(pdb, DB_RECNUM); if (ret != 0) { return_error_tuple(pdrv, db_strerror(ret)); return; } } ret = pdb->set_pagesize(pdb, page_size_bytes); if (ret != 0) { return_error_tuple(pdrv, db_strerror(ret)); return; } pdb->set_errpfx(pdb, pcfg->db_name); pcfg->db_open_flags = DB_CREATE; if (pcfg->txn_enabled) { pcfg->db_open_flags |= DB_THREAD; } if ((ret = pdb->open(pdb, NULL, pcfg->db_name, pcfg->db_name, pcfg->db_type, pcfg->db_open_flags, 0)) != 0) { return_error_tuple(pdrv, db_strerror(ret)); return; } pcfg->pdb = pdb; pcfg->penv = penv; pdrv->pcfg = pcfg; return_ok(pdrv); return; }
int main(int argc, char **argv) { std::string path2DbEnv; std::string theContainer = "namespaceExampleData.dbxml"; for ( int i=1; i<argc; i++ ) { if ( argv[i][0] == '-' ) { switch(argv[i][1]) { case 'h': path2DbEnv = argv[++i]; break; default: usage(); } } } if (! path2DbEnv.length() ) usage(); // 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 (64 MB). The default is quite small u_int32_t envCacheSize = 64*1024*1024; // 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); dberr = dbEnv->open(dbEnv, path2DbEnv.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; } //Have the XmlManager adopt the db environment XmlManager db(dbEnv, DBXML_ADOPT_DBENV); //Configure the container to use transactions XmlContainerConfig config; config.setTransactional(true); XmlContainer container = db.openContainer(theContainer, config); //Get a transaction XmlTransaction txn = db.createTransaction(); XmlUpdateContext uc = db.createUpdateContext(); //Add an string equality index for the "product" element node. addIndex( container, "", "product", "node-element-equality-string", txn, uc ); //Add an edge presence index for the product node addIndex( container, "", "product", "edge-element-presence", txn, uc ); //commit the index adds txn.commit(); return 0; }
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; }
int b_put(int argc, char *argv[]) { extern char *optarg; extern int optind, __db_getopt_reset; DB_ENV *dbenv; DB *dbp, **second; DBTYPE type; DBT key, data; db_recno_t recno; u_int32_t cachesize, dsize; int ch, i, count, secondaries; char *ts, buf[64]; second = NULL; type = DB_BTREE; cachesize = MEGABYTE; dsize = 20; count = 100000; secondaries = 0; ts = "Btree"; __db_getopt_reset = 1; while ((ch = getopt(argc, argv, "C:c:d:s:t:")) != EOF) switch (ch) { case 'C': cachesize = (u_int32_t)atoi(optarg); break; case 'c': count = atoi(optarg); break; case 'd': dsize = (u_int32_t)atoi(optarg); break; case 's': secondaries = atoi(optarg); break; case 't': switch (optarg[0]) { case 'B': case 'b': ts = "Btree"; type = DB_BTREE; break; case 'H': case 'h': if (b_util_have_hash()) return (0); ts = "Hash"; type = DB_HASH; break; case 'Q': case 'q': if (b_util_have_queue()) return (0); ts = "Queue"; type = DB_QUEUE; break; case 'R': case 'r': ts = "Recno"; type = DB_RECNO; break; default: return (b_put_usage()); } break; case '?': default: return (b_put_usage()); } argc -= optind; argv += optind; if (argc != 0) return (b_put_usage()); #if DB_VERSION_MAJOR < 3 || DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3 /* * Secondaries were added after DB 3.2.9. */ if (secondaries) return (0); #endif /* Create the environment. */ DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0); dbenv->set_errfile(dbenv, stderr); DB_BENCH_ASSERT(dbenv->set_cachesize(dbenv, 0, cachesize, 0) == 0); #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 1 DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, NULL, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0); #else DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0); #endif /* * Create the database. * Optionally set the record length for Queue. */ DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0); if (type == DB_QUEUE) DB_BENCH_ASSERT(dbp->set_re_len(dbp, dsize) == 0); #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1) DB_BENCH_ASSERT( dbp->open(dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0); #else DB_BENCH_ASSERT( dbp->open(dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0); #endif /* Optionally create the secondaries. */ if (secondaries != 0) { DB_BENCH_ASSERT((second = calloc(sizeof(DB *), (size_t)secondaries)) != NULL); for (i = 0; i < secondaries; ++i) { DB_BENCH_ASSERT(db_create(&second[i], dbenv, 0) == 0); (void)snprintf(buf, sizeof(buf), "%d.db", i); #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1) DB_BENCH_ASSERT(second[i]->open(second[i], NULL, buf, NULL, DB_BTREE, DB_CREATE, 0600) == 0); #else DB_BENCH_ASSERT(second[i]->open(second[i], buf, NULL, DB_BTREE, DB_CREATE, 0600) == 0); #endif #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1) /* * The DB_TXN argument to Db.associate was added in * 4.1.25. */ DB_BENCH_ASSERT(dbp->associate( dbp, NULL, second[i], b_put_secondary, 0) == 0); #else DB_BENCH_ASSERT(dbp->associate( dbp, second[i], b_put_secondary, 0) == 0); #endif } } /* Store a key/data pair. */ memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); switch (type) { case DB_BTREE: case DB_HASH: key.data = "01234567890123456789"; key.size = 20; break; case DB_QUEUE: case DB_RECNO: recno = 1; key.data = &recno; key.size = sizeof(recno); break; case DB_UNKNOWN: b_util_abort(); break; } data.size = dsize; DB_BENCH_ASSERT( (data.data = malloc((size_t)dsize)) != NULL); /* Store the key/data pair count times. */ TIMER_START; for (i = 0; i < count; ++i) { /* Change data value so the secondaries are updated. */ (void)snprintf(data.data, data.size, "%10lu", (u_long)i); DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0); } TIMER_STOP; if (type == DB_BTREE || type == DB_HASH) printf( "# %d %s database put of 10 byte key, %lu byte data", count, ts, (u_long)dsize); else printf("# %d %s database put of key, %lu byte data", count, ts, (u_long)dsize); if (secondaries) printf(" with %d secondaries", secondaries); printf("\n"); TIMER_DISPLAY(count); if (second != NULL) { for (i = 0; i < secondaries; ++i) DB_BENCH_ASSERT(second[i]->close(second[i], 0) == 0); free(second); } DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0); DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0); return (0); }
/////////////////// // // // Main Function // // // /////////////////// int main(int argc, char *argv[]) { /* Declare local vars */ /***********************************/ DB_ENV *dbenv; /* pointer to database environment */ DB *dbp1; /* pointer to 1st database */ DB *dbp2; /* pointer to 2nd database */ DBT key1, data1; /* Database key and data variables */ DBT key2, data2; /* Database key and data variables */ int ret; /* return code/message */ /* Creates the DB environment */ if ((ret = db_env_create(&dbenv, 0)) != 0) { fprintf(stderr, "%s: %s\n", argv[0], db_strerror(ret)); return(1); }; dbenv->set_errfile(dbenv, stderr); dbenv->set_errpfx(dbenv, argv[0]); /* Set shared memory buffer pool cachesize: 5MB. */ /* Databases are in a subdirectory of the environment home. */ if ((ret = dbenv->set_cachesize(dbenv, 0, 5 * 1024 * 1024, 0)) != 0) { dbenv->err(dbenv, ret, "set_cachesize"); dbenv->close(dbenv, 0); return(1); }; if ((ret = dbenv->set_data_dir(dbenv, MY_DBENV_DIR_DATA)) != 0) { dbenv->err(dbenv, ret, "set_data_dir: %s", MY_DBENV_DIR_DATA); dbenv->close(dbenv, 0); return(1); }; /* Open the environment with full transactional support. */ dbenv->open( dbenv, MY_DBENV_DIR_HOME, DB_CREATE | DB_INIT_LOG | DB_INIT_LOCK | DB_INIT_MPOOL | DB_INIT_TXN, 0 ); if (ret != 0) { dbenv->err(dbenv, ret, "environment open: %s", MY_DBENV_DIR_HOME); dbenv->close(dbenv, 0); return(1); }; /* Create first Database handle */ if ((ret = db_create(&dbp1, dbenv, 0)) != 0) { dbenv->err(dbenv, ret, "database create"); dbenv->close(dbenv, 0); return(1); }; /* Opens first database */ if ((ret = dbp1->open(dbp1, NULL, MY_DB1_FILE, NULL, DB_BTREE, DB_CREATE, 0640)) != 0) { dbenv->err(dbenv, ret, "DB->open: %s", MY_DB1_FILE); dbp1->close(dbp1, 0); dbenv->close(dbenv, 0); return (ret); }; /* Create second Database handle */ if ((ret = db_create(&dbp2, dbenv, 0)) != 0) { dbenv->err(dbenv, ret, "database create"); dbp1->close(dbp1, 0); dbenv->close(dbenv, 0); return(1); }; /* Opens second database */ if ((ret = dbp2->open(dbp2, NULL, MY_DB2_FILE, NULL, DB_BTREE, DB_CREATE, 0640)) != 0) { dbenv->err(dbenv, ret, "DB->open: %s", MY_DB2_FILE); dbp1->close(dbp1, 0); dbp2->close(dbp1, 0); dbenv->close(dbenv, 0); return (ret); }; /* Clears variables to avoid garbage */ memset(&key1, 0, sizeof(key1)); memset(&data1, 0, sizeof(data1)); memset(&key2, 0, sizeof(key2)); memset(&data2, 0, sizeof(data2)); /* Preps data to be added to first database */ key1.data = "fruit"; key1.size = sizeof("fruit"); data1.data = "apple"; data1.size = sizeof("apple"); /* pushes data into first database */ if ((ret = dbp1->put(dbp1, NULL, &key1, &data1, 0)) == 0) { printf("db: %s: key stored.\n", (char *)key1.data); } else { dbp1->err(dbp1, ret, "DB->put"); dbp1->close(dbp1, 0); dbp2->close(dbp2, 0); dbenv->close(dbenv, 0); return(1); }; /* Preps data for second database */ key2 = data1; data2.data = "red"; data2.size = sizeof("red"); /* pushes data into second database */ if ((ret = dbp2->put(dbp2, NULL, &key2, &data2, 0)) == 0) { printf("db: %s: key stored.\n", (char *)key2.data); } else { dbp1->err(dbp2, ret, "DB->put"); dbp1->close(dbp1, 0); dbp2->close(dbp2, 0); dbenv->close(dbenv, 0); return(1); }; /* Pulls data from database */ if ((ret = dbp1->get(dbp1, NULL, &key1, &data1, 0)) == 0) { printf("db: %s: key retrieved: data was %s.\n", (char *)key1.data, (char *)data1.data); if ((ret = dbp1->get(dbp2, NULL, &data1, &data2, 0)) == 0) { printf("db: %s: key retrieved: data was %s.\n", (char *)data1.data, (char *)data2.data); } else { dbp1->err(dbp2, ret, "DB->get"); dbp1->close(dbp1, 0); dbp2->close(dbp2, 0); dbenv->close(dbenv, 0); return(1); }; } else { dbp1->err(dbp2, ret, "DB->get"); dbp1->close(dbp1, 0); dbp2->close(dbp2, 0); dbenv->close(dbenv, 0); return(1); }; /* Exit function */ dbp1->close(dbp1, 0); dbp2->close(dbp2, 0); dbenv->close(dbenv, 0); return(0); };