Example #1
0
int TestGetLockConflicts(CuTest *ct) {
	DB_ENV *dbenv;
	const u_int8_t *lk_conflicts;
	int lk_modes, nmodes;
	u_int8_t conflicts[40];

	dbenv = NULL;
	/* lk_get_lk_conflicts: NOT reset at run-time. */
	ENV
	memset(conflicts, 'a', sizeof(conflicts));
	nmodes = 6;
	CuAssertTrue(ct,
	    dbenv->set_lk_conflicts(dbenv, conflicts, nmodes) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv,
	    TEST_ENV, DB_CREATE | DB_INIT_LOCK, 0666) == 0);
	CuAssertTrue(ct,
	    dbenv->get_lk_conflicts(dbenv, &lk_conflicts, &lk_modes) == 0);
	CuAssertTrue(ct, lk_conflicts[0] == 'a');
	CuAssertTrue(ct, lk_modes == 6);
	ENV
	memset(conflicts, 'b', sizeof(conflicts));
	nmodes = 8;
	CuAssertTrue(ct,
	    dbenv->set_lk_conflicts(dbenv, conflicts, nmodes) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0);
	CuAssertTrue(ct,
	    dbenv->get_lk_conflicts(dbenv, &lk_conflicts, &lk_modes) == 0);
	CuAssertTrue(ct, lk_conflicts[0] == 'a');
	CuAssertTrue(ct, lk_modes == 6);

	return (0);
}
Example #2
0
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;
}
Example #3
0
int TestSetThreadCount(CuTest *ct) { /* SKIP */
/* Run this test only when hash is supported. */
#ifdef HAVE_HASH
	DB_ENV *dbenv;
	DB *db;

	CuAssert(ct, "db_env_create", db_env_create(&dbenv, 0) == 0);

	dbenv->set_errpfx(dbenv, "TestSetThreadCount");
	CuAssert(ct, "set_thread_count", dbenv->set_thread_count(dbenv, 2) == 0);
	CuAssert(ct, "env->open", dbenv->open(dbenv, ".",
			DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
			DB_INIT_TXN | DB_PRIVATE | DB_THREAD, 0) == 0);

	CuAssert(ct, "db_create", db_create(&db, dbenv, 0) == 0);
	CuAssert(ct, "DB->open", db->open(
	    db, NULL, NULL, "TestSetThreadCount", DB_HASH, DB_CREATE, 0) == 0);

	db->close(db, 0);
	dbenv->close(dbenv, 0);
#else
	printf("TestSetThreadCount is not supported by the build.\n");
#endif /* HAVE_HASH */
	return (0);
}
Example #4
0
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);
}
Example #5
0
/*
 * Opens the database environment.
 *
 * ARGUMENTS:
 *      path            Pathname of the database directory.  Shall not be NULL.
 *                      The client can free it upon return.
 *      dbEnv           Pointer to a pointer to the database environment.  Shall
 *                      not be NULL.  "*dbEnv" is set upon successful return.
 * RETURNS:
 *      0               Success.  "*dbEnv" is set.
 *      ENOMEM          System error.  "log_start()" called.
 *      EIO             Backend database error.  "log_start()" called.
 */
static RegStatus
openEnvironment(
    const char* const   path,
    DB_ENV** const      dbEnv)
{
    RegStatus   status;
    DB_ENV*     env;

    assert(NULL != path);

    log_clear();

    if (0 == (status = createEnvHandle(path, &env))) {
        /*
         * The database is configured for "concurrent data store"
         * access rather than for transactional access because the
         * former is faster and sufficient.
         */
        status = env->open(env, path, DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL,
                           0);

        if (status) {
            log_add("Couldn't open environment for database \"%s\"", path);
            status = EIO;
        }
        else {
            *dbEnv = env;
        }

        if (status)
            (void)env->close(env, 0);
    }                                   /* "env" allocated */

    return status;
}
Example #6
0
void BDB_RecoverEnv(const string& path,
                    bool          fatal_recover)
{
    DB_ENV  *dbenv;
    int      ret;
    if ((ret = db_env_create(&dbenv, 0)) != 0) {
        string msg =
            "Cannot create environment " + string(db_strerror(ret));
        BDB_THROW(eInvalidOperation, msg);
    }

    dbenv->set_errfile(dbenv, stderr);
    //if (verbose)
    //  (void)dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);

    u_int32_t flags = 0;
    flags |= DB_CREATE | DB_INIT_LOG |
           DB_INIT_MPOOL | DB_INIT_TXN | DB_USE_ENVIRON;
    flags |= fatal_recover ? DB_RECOVER_FATAL : DB_RECOVER;
    flags |= DB_PRIVATE;

    if ((ret = dbenv->open(dbenv, path.c_str(), flags, 0)) != 0) {
        dbenv->close(dbenv, 0);
        string msg =
            "Cannot open environment " + string(db_strerror(ret));
        BDB_THROW(eInvalidOperation, msg);
    }
    ret = dbenv->close(dbenv, 0);
}
Example #7
0
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);
}
Example #8
0
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);
}
Example #9
0
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);
}
Example #10
0
int TestSetTransactionTimeout(CuTest *ct) {
	DB_ENV *dbenv;
	struct context *info;
	FILE *msgfile;
	char *path;
	db_timeout_t timeout;

	dbenv = NULL;
	if ((path = calloc(100, sizeof(char))) == NULL)
		return (ENOMEM);
	snprintf(path, 100, "%s%c%s", TEST_ENV, PATH_SEPARATOR[0], "msgfile");
	if ((msgfile = fopen(path, "w")) == NULL)
		return (EINVAL);
	info = ct->context;
	info->fp = msgfile;
	info->path = path;
	/* txn timeout: reset at run-time. */
	ENV
	CuAssertTrue(ct,
	    dbenv->set_timeout(dbenv, 37, DB_SET_TXN_TIMEOUT) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv,
	    TEST_ENV, DB_CREATE | DB_INIT_LOCK, 0666) == 0);
	CuAssertTrue(ct,
	    dbenv->get_timeout(dbenv, &timeout, DB_SET_TXN_TIMEOUT) == 0);
	CuAssertTrue(ct, timeout == 37);
	ENV
	/* New transaction timeout is ignored when joining the environment. */
	CuAssertTrue(ct,
	    dbenv->set_timeout(dbenv, 63, DB_SET_TXN_TIMEOUT) == 0);
	/* Redirect the error message to suppress the warning. */
	dbenv->set_msgfile(dbenv, msgfile);
	CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0);
	CuAssertTrue(ct,
	    dbenv->get_timeout(dbenv, &timeout, DB_SET_TXN_TIMEOUT) == 0);
	CuAssertTrue(ct, timeout == 37);
	/* Direct the error message back to the standard output. */
	dbenv->set_msgfile(dbenv, NULL);
	/* Re-config the transaction timeout after opening the environment. */
	CuAssertTrue(ct,
	    dbenv->set_timeout(dbenv, 63, DB_SET_TXN_TIMEOUT) == 0);
	CuAssertTrue(ct,
	    dbenv->get_timeout(dbenv, &timeout, DB_SET_TXN_TIMEOUT) == 0);
	CuAssertTrue(ct, timeout == 63);
	return (0);
}
Example #11
0
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);
}
Example #12
0
db* initialize_db(const char* db_name, uint32_t flag){
    db* db_ptr = NULL;
    DB* b_db;
    DB_ENV* dbenv;
    int ret;
    char* full_path = NULL;
    if((ret = mkdir(db_dir,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0){
        if(errno!=EEXIST){
            err_log("DB : Dir Creation Failed\n");
            goto db_init_return;
        }
    }
    full_path = (char*)malloc(strlen(db_dir) + strlen(db_name) + 2);
    mk_path(full_path, db_dir, db_name);
#ifdef ENV
    if ((ret = db_env_create(&dbenv, 0)) != 0) {
        dbenv->err(dbenv, ret, "Environment Created: %s", db_dir);
        goto db_init_return;
    }
    if ((ret = dbenv->open(dbenv, db_dir, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL|DB_THREAD, 0)) != 0) {
        //dbenv->err(dbenv, ret, "Environment Open: %s", db_dir);
        goto db_init_return;
    }
    /* Initialize the DB handle */
    if((ret = db_create(&b_db,dbenv,flag)) != 0){
        err_log("DB : %s.\n", db_strerror(ret));
        goto db_init_return;                                                                                                                                                   
    }                                                                                                                                                                          
#else                                                                                                                                                                          
    /* Initialize the DB handle */                                                                                                                                             
    if((ret = db_create(&b_db,NULL,flag)) != 0){                                                                                                                               
        err_log("DB : %s.\n", db_strerror(ret));                                                                                                                               
        goto db_init_return;                                                                                                                                                   
    }                                                                                                                                                                          
#endif                                                                                                                                                                         
    if((ret = b_db->set_pagesize(b_db, pagesize)) != 0){                                                                                                                       
        err_log("DB : %s.\n", db_strerror(ret));                                                                                                                               
        goto db_init_return;                                                                                                                                                   
    }                                                                                                                                                                          
                                                                                                                                                                               
    if((ret = b_db->open(b_db, NULL, db_name, NULL, DB_BTREE, DB_THREAD|DB_CREATE,0)) != 0){ // db_name is the on-disk file that holds the database                            
        //b_db->err(b_db,ret,"%s","test.db");                                                                                                                                  
        goto db_init_return;                                                                                                                                                   
    }                                                                                                                                                                          
    db_ptr = (db*)(malloc(sizeof(db)));                                                                                                                                        
    db_ptr->bdb_ptr = b_db;                                                                                                                                                    
                                                                                                                                                                               
db_init_return:                                                                                                                                                                
    if(full_path != NULL){                                                                                                                                                     
        free(full_path);                                                                                                                                                       
    }                                                                                                                                                                          
    if(db_ptr != NULL){                                                                                                                                                        
        ;                                                                                                                                                                      
    }                                                                                                                                                                          
    return db_ptr;                                                                                                                                                             
}
Example #13
0
int TestSetLockMaxObjects(CuTest *ct) {
	DB_ENV *dbenv;
	u_int32_t v;

	dbenv = NULL;
	/* lk_max_objects: NOT reset at run-time. */
	ENV
	CuAssertTrue(ct, dbenv->set_lk_max_objects(dbenv, 1037) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv,
	    TEST_ENV, DB_CREATE | DB_INIT_LOCK, 0666) == 0);
	CuAssertTrue(ct, dbenv->get_lk_max_objects(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 1037);
	ENV
	CuAssertTrue(ct, dbenv->set_lk_max_objects(dbenv, 1063) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0);
	CuAssertTrue(ct, dbenv->get_lk_max_objects(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 1037);
	return (0);
}
Example #14
0
int TestSetTxMax(CuTest *ct) {
	DB_ENV *dbenv;
	u_int32_t v;

	dbenv = NULL;
	/* tx_max: NOT reset at run-time. */
	ENV
	CuAssertTrue(ct, dbenv->set_tx_max(dbenv, 37) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv,
	    TEST_ENV, DB_CREATE | DB_INIT_TXN, 0666) == 0);
	CuAssertTrue(ct, dbenv->get_tx_max(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 37);
	ENV
	CuAssertTrue(ct, dbenv->set_tx_max(dbenv, 63) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0);
	CuAssertTrue(ct, dbenv->get_tx_max(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 37);
	return (0);
}
Example #15
0
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);
}
Example #16
0
int TestSetLogRegionSize(CuTest *ct) {
	DB_ENV *dbenv;
	u_int32_t v;

	dbenv = NULL;
	/* lg_regionmax: NOT reset at run-time. */
	ENV
	CuAssertTrue(ct, dbenv->set_lg_regionmax(dbenv, 137 * 1024) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv,
	    TEST_ENV, DB_CREATE | DB_INIT_LOG, 0666) == 0);
	CuAssertTrue(ct, dbenv->get_lg_regionmax(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 137 * 1024);
	ENV
	CuAssertTrue(ct, dbenv->set_lg_regionmax(dbenv, 163 * 1024) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0);
	CuAssertTrue(ct, dbenv->get_lg_regionmax(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 137 * 1024);
	return (0);
}
Example #17
0
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);
}
Example #18
0
// If an error occurred during the constructor, report it now.
// Otherwise, call the underlying DB->open method.
//
int DbEnv::open(const char *db_home, u_int32_t flags, int mode)
{
	DB_ENV *env = unwrap(this);
	int err;

	if ((err = construct_error_) != 0)
		DB_ERROR("Db::open", err, error_policy());
	else if ((err = env->open(env, db_home, flags, mode)) != 0)
		DB_ERROR("DbEnv::open", err, error_policy());

	return (err);
}
Example #19
0
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;
}
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;
}
Example #21
0
int TestSetLogMax(CuTest *ct) {
	DB_ENV *dbenv;
	struct context *info;
	FILE *msgfile;
	char *path;
	u_int32_t v;

	dbenv = NULL;
	if ((path = calloc(100, sizeof(char))) == NULL)
		return (ENOMEM);
	snprintf(path, 100, "%s%c%s", TEST_ENV, PATH_SEPARATOR[0], "msgfile");
	if ((msgfile = fopen(path, "w")) == NULL)
		return (EINVAL);
	info = ct->context;
	info->fp = msgfile;
	info->path = path;
	/* lg_max: reset at run-time. */
	ENV
	CuAssertTrue(ct, dbenv->set_lg_max(dbenv, 37 * 1024 * 1024) == 0);
	CuAssertTrue(ct, dbenv->open(dbenv,
	    TEST_ENV, DB_CREATE | DB_INIT_LOG, 0666) == 0);
	CuAssertTrue(ct, dbenv->get_lg_max(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 37 * 1024 * 1024);
	ENV
	/* New log maximum size is ignored when joining the environment. */
	CuAssertTrue(ct, dbenv->set_lg_max(dbenv, 63 * 1024 * 1024) == 0);
	/* Redirect the error message to suppress the warning. */
	dbenv->set_msgfile(dbenv, msgfile);
	CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0);
	CuAssertTrue(ct, dbenv->get_lg_max(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 37 * 1024 * 1024);
	/* Direct the error message back to the standard output. */
	dbenv->set_msgfile(dbenv, NULL);
	/* Re-config the log maximum size after opening the environment. */
	CuAssertTrue(ct, dbenv->set_lg_max(dbenv, 63 * 1024 * 1024) == 0);
	CuAssertTrue(ct, dbenv->get_lg_max(dbenv, &v) == 0);
	CuAssertTrue(ct, v == 63 * 1024 * 1024);
	return (0);
}
Example #22
0
int main(int argc,char * argv[])
{
	int rc;
	DB_ENV *env;
	DB *dbi;
	DBT key, data;
	DB_TXN *txn;
	DBC *cursor;
	char sval[32], kval[32];

#define FLAGS (DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_INIT_MPOOL|DB_CREATE|DB_THREAD)
	rc = db_env_create(&env, 0);
	rc = env->open(env, "./testdb", FLAGS, 0664);
	rc = db_create(&dbi, env, 0);
	rc = env->txn_begin(env, NULL, &txn, 0);
	rc = dbi->open(dbi, txn, "test.bdb", NULL, DB_BTREE, DB_CREATE, 0664);

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	key.size = sizeof(int);
	key.data = sval;
	data.size = sizeof(sval);
	data.data = sval;

	sprintf(sval, "%03x %d foo bar", 32, 3141592);
	rc = dbi->put(dbi, txn, &key, &data, 0);
	rc = txn->commit(txn, 0);
	if (rc) {
		fprintf(stderr, "txn->commit: (%d) %s\n", rc, db_strerror(rc));
		goto leave;
	}
	rc = env->txn_begin(env, NULL, &txn, 0);
	rc = dbi->cursor(dbi, txn, &cursor, 0);
	key.flags = DB_DBT_USERMEM;
	key.data = kval;
	key.ulen = sizeof(kval);
	data.flags = DB_DBT_USERMEM;
	data.data = sval;
	data.ulen = sizeof(sval);
	while ((rc = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
		printf("key: %p %.*s, data: %p %.*s\n",
			key.data,  (int) key.size,  (char *) key.data,
			data.data, (int) data.size, (char *) data.data);
	}
	rc = cursor->c_close(cursor);
	rc = txn->abort(txn);
leave:
	rc = dbi->close(dbi, 0);
	rc = env->close(env, 0);
	return rc;
}
Example #23
0
int TestSetTransactionTimeout(CuTest *ct) {
    DB_ENV *dbenv;
    db_timeout_t timeout;

    dbenv = NULL;
    /* txn timeout: reset at run-time. */
    ENV
    CuAssertTrue(ct,
                 dbenv->set_timeout(dbenv, 37, DB_SET_TXN_TIMEOUT) == 0);
    CuAssertTrue(ct, dbenv->open(dbenv,
                                 TEST_ENV, DB_CREATE | DB_INIT_LOCK, 0666) == 0);
    CuAssertTrue(ct,
                 dbenv->get_timeout(dbenv, &timeout, DB_SET_TXN_TIMEOUT) == 0);
    CuAssertTrue(ct, timeout == 37);
    ENV
    CuAssertTrue(ct,
                 dbenv->set_timeout(dbenv, 63, DB_SET_TXN_TIMEOUT) == 0);
    CuAssertTrue(ct, dbenv->open(dbenv, TEST_ENV, DB_JOINENV, 0666) == 0);
    CuAssertTrue(ct,
                 dbenv->get_timeout(dbenv, &timeout, DB_SET_TXN_TIMEOUT) == 0);
    CuAssertTrue(ct, timeout == 63);
    return (0);
}
Example #24
0
// If an error occurred during the constructor, report it now.
// Otherwise, call the underlying DB->open method.
//
int DbEnv::open(const char *db_home, u_int32_t flags, int mode)
{
	int ret;
	DB_ENV *env = unwrap(this);

	if (construct_error_ != 0)
		ret = construct_error_;
	else
		ret = env->open(env, db_home, flags, mode);

	if (!DB_RETOK_STD(ret))
		DB_ERROR(this, "DbEnv::open", ret, error_policy());

	return (ret);
}
Example #25
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);




        /*
         * 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 ,
            S_IRUSR | S_IWUSR)) != 0) {
                (void)dbenv->close(dbenv, 0);
                fprintf(stderr, "dbenv->open: %s: %s\n",
                    ENV_DIRECTORY, db_strerror(ret));
                exit (1);
        }




        *dbenvp = dbenv;
}
Example #26
0
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 TestSequencePreOpenSetterAndGetter(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DB_SEQUENCE *seq;
	u_int32_t seq_flags;

	CuAssert(ct, "db_env_create", create_dbenv_handle(&dbenv) == 0);
	CuAssert(ct, "dbenv->open", dbenv->open(dbenv,
	    TEST_ENV, DB_CREATE | DB_INIT_MPOOL, 0644) == 0);
	CuAssert(ct, "db_create", create_db_handle(&dbp, dbenv) == 0);
	CuAssert(ct, "dbp->open", dbp->open(dbp,
	    NULL, "seq.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssert(ct, "db_sequence_create",
	    create_seq_handle(&seq, dbp) == 0);

	/* Test DB_SEQUENCE->set_cachesize(), DB_SEQUENCE->get_cachesize(). */
	CHECK_1_DIGIT_VALUE(seq, set_cachesize, get_cachesize,
	    u_int32_t, rand());

	/* Test DB_SEQUENCE->set_flags(), DB_SEQUENCE->get_flags(). */
	seq_flags = 0;
	CHECK_1_DIGIT_VALUE(seq, set_flags, get_flags,
	    u_int32_t, DB_SEQ_DEC | DB_SEQ_WRAP);
	/* We make sure the DB_SEQ_DEC is cleared if we set DB_SEQ_INC. */
	CuAssert(ct, "seq->set_flags", seq->set_flags(seq, DB_SEQ_INC) == 0);
	CuAssert(ct, "seq->get_flags", seq->get_flags(seq, &seq_flags) == 0);
	CuAssert(ct, "check seq flags",
	    seq_flags == (DB_SEQ_INC | DB_SEQ_WRAP));

	/*
	 * Test DB_SEQUENCE->set_range(), DB_SEQUENCE->get_range().
	 * The max should be bigger than min.
	 */
	CHECK_2_DIGIT_VALUES(seq, set_range, get_range,
	    db_seq_t, 2, db_seq_t, 1048576);
	
	CuAssert(ct, "seq->close", close_seq_handle(seq) == 0);
	CuAssert(ct, "dbp->close", close_db_handle(dbp) == 0);
	CuAssert(ct, "dbenv->close", close_dbenv_handle(dbenv) == 0);

	return (0);
}
Example #28
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;
}
/** run recovery, open environment and keep the exclusive lock */
static DB_ENV *dbe_recover_open(bfpath *bfp, u_int32_t flags)
{
    const u_int32_t local_flags = flags | DB_CREATE;
    DB_ENV *dbe;
    int e;

    if (DEBUG_DATABASE(0))
        fprintf(dbgout, "trying to lock database directory\n");
    db_try_glock(bfp, F_WRLCK, F_SETLKW); /* wait for exclusive lock */

    /* run recovery */
    bf_dbenv_create(&dbe);

    if (DEBUG_DATABASE(0))
        fprintf(dbgout, "running regular data base recovery%s\n",
	       flags & DB_PRIVATE ? " and removing environment" : "");

    /* quirk: DB_RECOVER requires DB_CREATE and cannot work with DB_JOINENV */

    /*
     * Hint from Keith Bostic, SleepyCat support, 2004-11-29,
     * we can use the DB_PRIVATE flag, that rebuilds the database
     * environment in heap memory, so we don't need to remove it.
     */

    e = dbe->open(dbe, bfp->dirname,
		  dbenv_defflags | local_flags | DB_RECOVER, DS_MODE);
    if (e != 0) {
	print_error(__FILE__, __LINE__, "Cannot recover environment \"%s\": %s",
		bfp->dirname, db_strerror(e));
	if (e == DB_RUNRECOVERY)
	    diag_dbeopen(flags, bfp);
	exit(EX_ERROR);
    }

    return dbe;
}
Example #30
0
DB_ENV *db_setup(char *home)
{
    DB_ENV *dbenv;
    int ret;

    /* * Create an environment and initialize it for additional error * reporting. */ 
    if ((ret = db_env_create(&dbenv, 0)) != 0) { 
	return (NULL); 
    } 
    ci_debug_printf(5,"Environment created OK.\n");


//	dbenv->set_data_dir(dbenv, "");
    dbenv->set_data_dir(dbenv, home);
    ci_debug_printf(5,"Data dir set to %s.\n", home);
    /*
      dbenv->set_shm_key(dbenv, 888888L);
      ci_debug_printf(5,"Shared memory set.\n");
    */
    /* * Specify the 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"); 
    //	  goto err; 
    //     } 

    /* Open the environment  */ 
    if ((ret = dbenv->open(dbenv, home, DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL|DB_THREAD /*| DB_SYSTEM_MEM*/, 0)) != 0){ 
	ci_debug_printf(1, "Environment open failed: %s\n", db_strerror(ret));
	dbenv->close(dbenv, 0); 
	return NULL; 
    }
    ci_debug_printf(5,"DB setup OK.\n");


    return (dbenv);
}