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; }
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); }
static void b_inmem_op_tds(u_int ops, int update, u_int32_t env_flags, u_int32_t log_flags) { DB *dbp; DBT key, data; DB_ENV *dbenv; DB_MPOOL_STAT *gsp; DB_TXN *txn; char *keybuf, *databuf; DB_BENCH_ASSERT((keybuf = malloc(keysize)) != NULL); DB_BENCH_ASSERT((databuf = malloc(datasize)) != NULL); memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); key.data = keybuf; key.size = keysize; memset(keybuf, 'a', keysize); data.data = databuf; data.size = datasize; memset(databuf, 'b', datasize); DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0); dbenv->set_errfile(dbenv, stderr); /* General environment configuration. */ #ifdef DB_AUTO_COMMIT DB_BENCH_ASSERT(dbenv->set_flags(dbenv, DB_AUTO_COMMIT, 1) == 0); #endif if (env_flags != 0) DB_BENCH_ASSERT(dbenv->set_flags(dbenv, env_flags, 1) == 0); /* Logging configuration. */ if (log_flags != 0) #if DB_VERSION_MINOR >= 7 DB_BENCH_ASSERT( dbenv->log_set_config(dbenv, log_flags, 1) == 0); #else DB_BENCH_ASSERT(dbenv->set_flags(dbenv, log_flags, 1) == 0); #endif #ifdef DB_LOG_INMEMORY if (!(log_flags & DB_LOG_INMEMORY)) #endif #ifdef DB_LOG_IN_MEMORY if (!(log_flags & DB_LOG_IN_MEMORY)) #endif DB_BENCH_ASSERT(dbenv->set_lg_max(dbenv, logbufsize * 10) == 0); DB_BENCH_ASSERT(dbenv->set_lg_bsize(dbenv, logbufsize) == 0); DB_BENCH_ASSERT(dbenv->open(dbenv, "TESTDIR", DB_CREATE | DB_PRIVATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0666) == 0); DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0); DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0); DB_BENCH_ASSERT(dbp->open( dbp, NULL, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0); if (update) { (void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR); TIMER_START; for (; ops > 0; --ops) DB_BENCH_ASSERT( dbp->put(dbp, NULL, &key, &data, 0) == 0); TIMER_STOP; if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0) DB_BENCH_ASSERT(gsp->st_page_out == 0); } else { DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0); (void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR); TIMER_START; for (; ops > 0; --ops) { DB_BENCH_ASSERT( dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0); DB_BENCH_ASSERT( dbp->get(dbp, NULL, &key, &data, 0) == 0); DB_BENCH_ASSERT(txn->commit(txn, 0) == 0); } TIMER_STOP; if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0) DB_BENCH_ASSERT(gsp->st_cache_miss == 0); } DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0); DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 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; }