Esempio n. 1
0
static int spoolbdb_open_rdonly(DB **db, DB_ENV *dbenv, char *dbfname,
				int f_mpool_nofile){
  /*
   * If f_mpool_nofile is set, we are opening a shared memory db, otherwise
   * it is a normal db.
   */
  DB *dbp = NULL;
  int status = 0;
  uint32_t dbflags = SPOOLBDB_FLAGS_RDONLY;

  if((dbenv == NULL) || (dbfname == NULL))
      return(EINVAL);

  status = db_create(&dbp, dbenv, 0);
  if(status != 0)
    return(status);

  if(f_mpool_nofile != 0)
    status = dbp->open(dbp, NULL, NULL, dbfname, DB_BTREE, dbflags, 0);
  else
    status = dbp->open(dbp, NULL, dbfname, NULL, DB_BTREE, dbflags, 0);

  if(status != 0){
    if(dbp != NULL)
    (void)dbp->close(dbp, 0);
  }else
    *db = dbp;

  return(status);
}
Esempio n. 2
0
DB *sg_open_db(DB_ENV *dbenv, char *filename,
	       int (*bt_compare_fcn)(DB *, const DBT *, const DBT *) )
{
    int ret;
    DB *dbp = NULL;

    if ((ret = db_create(&dbp, dbenv , 0)) != 0) {
	ci_debug_printf(1, "db_create: %s\n", db_strerror(ret));
	return NULL;
    }
    //     dbp->set_flags(dbp, DB_DUP);
    dbp->set_bt_compare(dbp, bt_compare_fcn);


#if(DB_VERSION_MINOR>=1)
    if ((ret = dbp->open( dbp, NULL, filename, NULL,
			  DB_BTREE, DB_RDONLY|DB_THREAD, 0)) != 0)
#else
	if ((ret = dbp->open( dbp, filename, NULL,
			      DB_BTREE, DB_RDONLY, 0)) != 0)
#endif
	{
	    ci_debug_printf(1, "open db %s: %s\n", filename, db_strerror(ret));
	    dbp->close(dbp, 0);
	    return NULL;
	}
    return dbp;
}
int TestPartOneKeyNoData(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	/* Allocate the memory from stack. */
	DBT keys[4];
	u_int32_t i;

	dbenv = NULL;
	dbp = NULL;
	nparts = 5;

	/* Do not assign any data to the first DBT. */
	memset(&keys[0], 0, sizeof(DBT));
	for (i = 1 ; i < (nparts - 1); i++) {
		memset(&keys[i], 0, sizeof(DBT));
		keys[i].data = &content[(i + 1) * (strlen(content) / nparts)];
		keys[i].size = sizeof(char);
	}

	/* Do not set any database flags. */
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	/*
	 * Verify that before the database is opened, DB->set_partition can
	 * be called multiple times regardless of its return code.
	 */
	keys[0].flags = DB_DBT_MALLOC;
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) != 0);
	keys[0].flags = 0;
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set DB_DUPSORT flags. */
	setup_envdir(TEST_ENV, 1);
	errfp = fopen("TESTDIR/errfile", "w");
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUPSORT) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set DB_DUP flags. */
	setup_envdir(TEST_ENV, 1);
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUP) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssertTrue(ct, put_data(dbp) == 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	return (0);
}
Esempio n. 4
0
int mail_cache_db_open(const char * filename,
    struct mail_cache_db ** pcache_db)
{
#if DBVERS >= 1
  DB * dbp;
#if DBVERS > 1
  int r;
#endif
  struct mail_cache_db * cache_db;

#if DB_VERSION_MAJOR >= 3
  r = db_create(&dbp, NULL, 0);
  if (r != 0)
    goto err;

#if (DB_VERSION_MAJOR >= 4) && ((DB_VERSION_MAJOR > 4) || (DB_VERSION_MINOR >= 1))
  r = dbp->open(dbp, NULL, filename, NULL, DB_BTREE, DB_CREATE,
      S_IRUSR | S_IWUSR);
#else
  r = dbp->open(dbp, filename, NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR);
#endif
  if (r != 0)
    goto close_db;
#else
#if DBVERS > 1  
  r = db_open(filename, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR,
      NULL, NULL, &dbp);
  if (r != 0)
    goto err;
#elif DBVERS == 1
  dbp = dbopen(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, DB_BTREE, NULL);
  if (dbp == NULL)
    goto err;
#else
  goto err;
#endif  
#endif

  cache_db = mail_cache_db_new(dbp);
  if (cache_db == NULL)
    goto close_db;
  
  * pcache_db = cache_db;

  return 0;

 close_db:
#if DBVERS > 1  
  dbp->close(dbp, 0);
#elif DBVERS == 1
  dbp->close(dbp);
#endif
 err:
  return -1;
#else
  return -1;
#endif
}
int TestPartUnsortedKey(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBT *keys;
	u_int32_t i, indx;

	dbenv = NULL;
	dbp = NULL;
	keys = NULL;
	nparts = 6;

	CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL);
	memset(keys, 0, (nparts - 1) * sizeof(DBT));
	/* Assign unsorted keys to the array. */
	for (i = 0, indx = 0; i < (nparts - 1); i++) {
		if (i == (nparts - 2) && i % 2 == 0)
			indx = i;
		else if (i % 2 != 0)
			indx = i - 1;
		else
			indx = i + 1;
		keys[i].data =
		    &content[(indx + 1) * (strlen(content) / nparts)];
		keys[i].size = sizeof(char);
	}

	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct,
	    dbp->set_partition(dbp, nparts - 1, &keys[1], NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssertTrue(ct, put_data(dbp) == 0);
	CuAssertTrue(ct, dbp->close(dbp, 0) == 0);

	/*
	 * Reconfig with a different partition number and
	 * re-open the database.
	 */
	CuAssertTrue(ct, db_create(&dbp, dbenv, 0) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, 0, 0644) != 0);
	CuAssertTrue(ct, dbp->close(dbp, 0) == 0);

	/*
	 * Reconfig with a different set of partition keys and
	 * re-open the database.
	 */
	CuAssertTrue(ct, db_create(&dbp, dbenv, 0) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts - 1, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, 0, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	free(keys);
	return (0);
}
int TestPartDuplicatedKey(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBT *keys;
	u_int32_t i;

	dbenv = NULL;
	dbp = NULL;
	keys = NULL;
	nparts = 5;

	CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL);
	memset(keys, 0, (nparts - 1) * sizeof(DBT));
	/* Assign the same data to the first 2 DBTs. */
	for (i = 0 ; i < (nparts - 1); i++) {
		if (i < 2)
			keys[i].data = &content[strlen(content) / nparts];
		else
			keys[i].data = &content[(i + 1) *
			    (strlen(content) / nparts)];
		keys[i].size = sizeof(char);
	}

	/* Do not set any database flags. */
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set DB_DUPSORT flags. */
	setup_envdir(TEST_ENV, 1);
	errfp = fopen("TESTDIR/errfile", "w");
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUPSORT) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set DB_DUP flags. */
	setup_envdir(TEST_ENV, 1);
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUP) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssertTrue(ct, put_data(dbp) == 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	free(keys);
	return (0);
}
Esempio n. 7
0
static int spoolbdb_open_create(DB **db, DB_ENV *dbenv,
				char *dbfname, int dbmode,
				int f_mpool_nofile){
  /*
   * If dbfname == NULL, then it is an in-memory (private) bdb.
   * If dbfname != NULL, then if the flag f_mpool_nofile is 1 the db
   * is configured as shared memory db or a normal db if the flag is 0.
   */
  DB *dbp = NULL;
  DB_MPOOLFILE *mpf = NULL;
  int status = 0;
  uint32_t dbflags = SPOOLBDB_FLAGS_CREATE;

  if(dbenv == NULL)
    return(EINVAL);

  /*
   * Let the user know that this combination is not valid.
   */
  if((dbfname == NULL) && (f_mpool_nofile == 0))
    return(EINVAL);

  status = db_create(&dbp, dbenv, 0);
  if(status != 0)
    return(status);

  if((dbfname == NULL) || (f_mpool_nofile == 1)){
    /*
     * Private or shared memory db.
     */
    mpf = dbp->get_mpf(dbp);
    status = mpf->set_flags(mpf, DB_MPOOL_NOFILE, 1);
  }

  if(status == 0){
    if(dbfname == NULL)
      status = dbp->open(dbp, NULL, NULL, NULL, DB_BTREE, dbflags, dbmode);
    else if(f_mpool_nofile != 0)
      status = dbp->open(dbp, NULL, NULL, dbfname, DB_BTREE, dbflags, dbmode);
    else
      status = dbp->open(dbp, NULL, dbfname, NULL, DB_BTREE, dbflags, dbmode);
  }

#if 0
  dbp->set_errcall(dbp, XXX_db_errcall_fcn); /* XXX */
#endif

  if(status != 0){
    if(dbp != NULL)
    (void)dbp->close(dbp, 0);
  }else
    *db = dbp;

  return(status);
}
Esempio n. 8
0
char *smf_lookup_db4_query(char *database, char *key) {
    DB *dbp;
    DBT db_key, db_value;
    int ret;
    char *db_res = NULL;

    /* initialize db4 */
    if ((ret = db_create(&dbp, NULL, 0)) != 0) {
        TRACE(TRACE_ERR, "db_create: %s\n", db_strerror(ret));
        return NULL;
    }

    TRACE(TRACE_LOOKUP, "[%p] lookup key [%s]", dbp,key);

    if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
        TRACE(TRACE_WARNING, "DB (%s): %s", database, db_strerror(ret));
    }
    if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0) {
        TRACE(TRACE_WARNING, "DB (%s): %s", database, db_strerror(ret));
    }

    /* open db */
#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR < 1
    if ((ret = dbp->open(dbp, database, NULL, DB_HASH, DB_RDONLY, 0)) != 0) {
        TRACE(TRACE_ERR, "DB (%s): %s", database, db_strerror(ret));
        return NULL;
    }
#else
    if ((ret = dbp->open(dbp, NULL, database, NULL, DB_HASH, DB_RDONLY, 0)) != 0) {
        TRACE(TRACE_ERR,"DB (%s): %s", database, db_strerror(ret));
        return NULL;
    }
#endif
    else {
        TRACE(TRACE_DEBUG, "DB (%s): open", database);
    }

    memset(&db_key, 0, sizeof(DBT));
    memset(&db_value, 0, sizeof(DBT));
    db_key.data = (void *)key;
    db_key.size = strlen(key) + 1;

    ret = dbp->get(dbp, NULL, &db_key, &db_value, 0);
    
    if (ret == 0) {
        if (asprintf(&db_res, "%s", (char *)db_value.data) != -1)
            TRACE(TRACE_LOOKUP, "[%p] found value [%s]", dbp, db_res);
    } else
        TRACE(TRACE_LOOKUP, "[%p] nothing found", dbp);

    if (dbp != NULL)
        dbp->close(dbp, 0);

    return db_res;
}
Esempio n. 9
0
int
b_curalloc(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind, __db_getopt_reset;
	DB *dbp;
	DBC *curp;
	int ch, i, count;

	count = 100000;
	__db_getopt_reset = 1;
	while ((ch = getopt(argc, argv, "c:")) != EOF)
		switch (ch) {
		case 'c':
			count = atoi(optarg);
			break;
		case '?':
		default:
			return (b_curalloc_usage());
		}
	argc -= optind;
	argv += optind;
	if (argc != 0)
		return (b_curalloc_usage());

	/* Create the database. */
	DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
	dbp->set_errfile(dbp, stderr);

#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
	DB_BENCH_ASSERT(dbp->open(
	    dbp, NULL, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
#else
	DB_BENCH_ASSERT(
	    dbp->open(dbp, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
#endif

	/* Allocate a cursor count times. */
	TIMER_START;
	for (i = 0; i < count; ++i) {
		DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &curp, 0) == 0);
		DB_BENCH_ASSERT(curp->c_close(curp) == 0);
	}
	TIMER_STOP;

	printf("# %d cursor allocations\n", count);
	TIMER_DISPLAY(count);

	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);

	return (0);
}
Esempio n. 10
0
_nc_db_open(const char *path, bool modify)
{
    DB *result = 0;
    int code;

#if HASHED_DB_API >= 4
    db_create(&result, NULL, 0);
    if ((code = result->open(result,
			     NULL,
			     path,
			     NULL,
			     DB_HASH,
			     modify ? DB_CREATE : DB_RDONLY,
			     0644)) != 0) {
	result = 0;
    }
#elif HASHED_DB_API >= 3
    db_create(&result, NULL, 0);
    if ((code = result->open(result,
			     path,
			     NULL,
			     DB_HASH,
			     modify ? DB_CREATE : DB_RDONLY,
			     0644)) != 0) {
	result = 0;
    }
#elif HASHED_DB_API >= 2
    if ((code = db_open(path,
			DB_HASH,
			modify ? DB_CREATE : DB_RDONLY,
			0644,
			(DB_ENV *) 0,
			(DB_INFO *) 0,
			&result)) != 0) {
	result = 0;
    }
#else
    if ((result = dbopen(path,
			 modify ? (O_CREAT | O_RDWR) : O_RDONLY,
			 0644,
			 DB_HASH,
			 NULL)) == 0) {
	code = errno;
    }
#endif
    if (result != 0) {
	T(("opened %s", path));
    } else {
	T(("cannot open %s: %s", path, strerror(code)));
    }
    return result;
}
Esempio n. 11
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);
}
Esempio n. 12
0
int TestPartNumber(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBT *keys;
	u_int32_t i;

	dbenv = NULL;
	dbp = NULL;
	keys = NULL;
	nparts = 5;

	CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL);
	memset(keys, 0, (nparts - 1) * sizeof(DBT));
	/* Assign data to the keys. */
	for (i = 0 ; i < (nparts - 1); i++) {
		keys[i].data = &content[(i + 1) * (strlen(content) / nparts)];
		keys[i].size = sizeof(char);
	}

	/* Partition number is less than 2. */
	CuAssertTrue(ct, create_db(&dbenv, &dbp, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, 1, keys, NULL) != 0);

	/* Partition number is less than key array length plus 1. */
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts - 1, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssertTrue(ct, put_data(dbp) == 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	free(keys);
	return (0);
}
Esempio n. 13
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);
}
Esempio n. 14
0
int userToSubname_open(struct userToSubnameDbFormat * userToSubnameDb, char mode) {
		unsigned int flags;
		int ret;
		int filemode;
		DB * db;

		debug("Using DB version %s\n", DB_VERSION_STRING);

		switch (mode) {
			case 'w':
				flags = DB_CREATE;    	/* If the database does not exist, * create it.*/
				filemode = 0664;
				break;
			case 'r':
				flags = DB_RDONLY;
				filemode = 0;
				break;
			default:
				errx(1, "%s %d Unknown open mode '%d'", __FILE__, __LINE__, mode);
		}

		if ((ret = db_create(&db, NULL, 0)) != 0)
			errx(1, "%s db_create: %s\n", __FILE__, db_strerror(ret));

		if ((ret = db->set_flags(db, DB_DUPSORT)) != 0)
			errx(1, "set dupsort flags, %s\n", db_strerror(ret));

		ret = db->open(db, NULL, bfile(userToSubnameDbFile), NULL, DB_BTREE, flags, filemode);
		if (ret != 0)
			errx(1, "db open error %s\n", db_strerror(ret));
	
		(*userToSubnameDb).dbp = db;

		return 1;
}
Esempio n. 15
0
int vdadisk_read (const char *db_name, LogicalSector *sector, tSectorId idToSearch) {
	DB *base;
	DBT key, data;
	int ret;

	if ((ret = db_create(&base, NULL, 0)) != 0)
		return ret;
	/* Associate DB with a file (create a btree)*/
	//if ((ret = base->open(base, NULL, concat( getGlobalHeap(), 2, db_name, ".db"), NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
	if ((ret = base->open(base, NULL, db_name, NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
		if(isDebugEnabled()) error( "[BDB] Fallo al abrir la base" );
		//base->err(base, ret, "DB open failed\n");
		base->close(base,0);
		return ret;
	}
	loadDBT (&key, &data, sector);
	ret = base->get(base, NULL, &key, &data, 0);
	if((ret = base->get(base, NULL, &key, &data, 0)) != 0) {
		if(isDebugEnabled()) 
			debug(concat( getGlobalHeap() , 2 , "[BDB] No se encuentra el sector " ,itoa_buf(sector->sectorId)));
		//base->err(base, ret, "Not found.");
	}
	base->close(base,0);
	return ret;
}
Esempio n. 16
0
int vdadisk_write (const char *db_name, LogicalSector *sector) {
	DB *base;
	DBT key, data;
	int ret;

	if ((ret = db_create(&base, NULL, 0)) != 0)
		return ret;
	/* Associate DB with a file (create a btree)*/
	//if ((ret = base->open(base, NULL,concat( getGlobalHeap(), 2, db_name, ".db"), NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
	if ((ret = base->open(base, NULL, db_name, NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
		if(isDebugEnabled()) error( "[BDB] Fallo al abrir la base" );
		//base->err(base, ret, "DB open failed\n");
		base->close(base,0);
		return ret;
	}
	loadDBT (&key, &data, sector);
	ret = base->put(base, NULL, &key, &data, DB_NOOVERWRITE);
	if (ret == DB_KEYEXIST) {
		//base->err(base, ret, "La clave %d ya existe!\n");*
		//_itoa_s(sector->sectorId, aux, 10, 10);
		if(isDebugEnabled()) debug(concat( getGlobalHeap() , 2 , "[BDB] Ya existe el sector ", itoa_buf(sector->sectorId)));
		if ((ret = base->del(base, NULL, &key, 0)) == 0 ){  /* Si existe lo borro y lo vuelvo a escribir */
			if(isDebugEnabled()) debug(concat( getGlobalHeap() , 2 , "[BDB] Reescribiendo el sector ", itoa_buf(sector->sectorId)));
			ret = base->put(base, NULL, &key, &data, DB_NOOVERWRITE);
		}
	}
	base->close(base,0);
	return ret;
}
Esempio n. 17
0
int main(int argc, char **argv)
{
  char *fname = argv[1];
  std::cout << "Opening: " << fname << std::endl;
  long ret;
#if 0
  DB *dbp;
  if(0 != (ret = db_create(&dbp, NULL, 0)))
    {
      chkerr(ret);
      exit(0);
    }
  else if(0 != (ret = dbp->open(dbp, NULL, fname, NULL, DB_BTREE, 0, 0)))
    {
      dbp->close(dbp, 0);
      chkerr(ret);
    }
#else
  void *dbp = (void*)lwbdb_open(fname);
  if(0 == dbp)
    {
      std::cerr << "Can't open database: " << fname << std::endl;
      exit(1);
    }
#endif
  char data[256];
  chkerr(lwbdb_get(dbp, "dog",3,data,sizeof(data)));
  std::cout << "dog -> " << data << std::endl;
  chkerr(lwbdb_get(dbp, "here",4,data,sizeof(data)));
  std::cout << "here -> " << data << std::endl;
  lwbdb_close(dbp);
  exit(0);
}
Esempio n. 18
0
void optimisticDb_open( OptimisticDb *optimisticDb )
{
	DB_MPOOLFILE 	*mpf;
	DB 				*dbp;
	u_int32_t 		openFlags;
	int 			ret;

	/* Creates the database handler */
	ret = db_create( &dbp, optimisticDb->envp, 0 );
	if( ret != 0 ) {
		printf( "Can't create database for optimistic storage\n" );
		exit( 1 );
	}

	mpf = dbp->get_mpf( dbp );
	mpf->set_flags( mpf, DB_MPOOL_NOFILE, 1 );
	
	/* Opens the database file */
	openFlags = DB_CREATE | DB_EXCL;

	ret = dbp->open( dbp, NULL, NULL, NULL, DB_BTREE, openFlags, 0 );
	if( ret != 0 ) {
		printf( "Failed to open database for optimistic storage\n" );
		exit( 1 );
	}

	optimisticDb->dbp = dbp;
	optimisticDb->isOpen = 1;

}
Esempio n. 19
0
int pctlmfdb_open(struct pctldb_st *pctldb,
		  struct pctldb_param_st *param, int *dberror){

  DB *mfdbp = NULL;
  int status;
  uint32_t dbflags = PCTLMFDB_FLAGS;

  status = db_create(&mfdbp, param->dbenv, 0);

  if(status == 0)
    status = mfdbp->set_bt_compare(mfdbp, timespec_cmp);

  if(status == 0)
    status = mfdbp->open(mfdbp, NULL, param->mfdbname, NULL, DB_BTREE, dbflags,
			 param->mode);

  if(status != 0){
    if(mfdbp != NULL)
      mfdbp->close(mfdbp, 0);

    *dberror = status;
    status = -1;
  }

  if(status == 0)
    pctldb->mfdbp = mfdbp;

  return(status);
}
Esempio n. 20
0
static void* berkely_registry_new(GError **error)
{
  Registry *s = g_memdup(&BerkelyRegistryImpl, sizeof(Registry));

  const gchar* db_filename = "my_db.db";

  g_debug("Opening DB %s", db_filename);
  DB *dbp;
  int ret;

  ret = db_create(&dbp, NULL, 0);
  if (ret != 0) {
    g_error("failed to create db");
  }

  ret = dbp->open(dbp, NULL, db_filename, NULL, DB_BTREE, DB_CREATE, 0);
  if (ret != 0) {
    g_error("failed to open db");
  }

  s->data = g_malloc(sizeof(Data));
  get_data(s)->db = dbp;

  g_assert(get_db(s) != NULL);
  return s;
}
Esempio n. 21
0
/* perform write command */
int dowrite(char *name, int rnum){
  DB *dbp;
  DBT key, data;
  int i, err, len;
  char buf[RECBUFSIZ];
  if(showprgr) printf("<Writing Test of Hash>\n  name=%s  rnum=%d\n\n", name, rnum);
  /* open a database */
  if(db_create(&dbp, NULL, 0) != 0){
    fprintf(stderr, "db_create failed\n");
    return 1;
  }
  if(dbp->set_pagesize(dbp, SMALL_PAGESIZE) != 0){
    fprintf(stderr, "DB->set_pagesize failed\n");
    dbp->close(dbp, 0);
    return 1;
  }
  if(dbp->set_cachesize(dbp, 0, BIG_CACHESIZE, 0) != 0){
    fprintf(stderr, "DB->set_cachesize failed\n");
    dbp->close(dbp, 0);
    return 1;
  }
  if(dbp->open(dbp, NULL, name, NULL, DB_HASH, DB_CREATE | DB_TRUNCATE, 00644) != 0){
    fprintf(stderr, "DB->open failed\n");
    dbp->close(dbp, 0);
    return 1;
  }
  err = FALSE;
  memset(&key, 0, sizeof(key));
  memset(&data, 0, sizeof(data));
  /* loop for each record */
  for(i = 1; i <= rnum; i++){
    /* store a record */
    len = sprintf(buf, "%08d", i);
    key.data = buf;
    key.size = len;
    data.data = buf;
    data.size = len;
    if(dbp->put(dbp, NULL, &key, &data, 0) != 0){
      fprintf(stderr, "DB->put failed\n");
      err = TRUE;
      break;
    }
    /* print progression */
    if(showprgr && rnum > 250 && i % (rnum / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == rnum || i % (rnum / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  /* close the database */
  if(dbp->close(dbp, 0) != 0){
    fprintf(stderr, "DB->close failed\n");
    err = TRUE;
  }
  if(showprgr && !err) printf("ok\n\n");
  return err ? 1 : 0;
}
Esempio n. 22
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);
}
Esempio n. 23
0
void
db_open(DB_ENV *dbenv, DB **dbp, char *name, int dups)
{
	DB *db;
	int ret;

	/* Create the database handle. */
	if ((ret = db_create(&db, dbenv, 0)) != 0) {
		dbenv->err(dbenv, ret, "db_create");
		exit (1);
	}

	/* Optionally, turn on duplicate data items. */
	/*	if (dups && (ret = db->set_flags(db, DB_DUP)) != 0) {
		dbenv->err(dbenv, ret, "db->set_flags: DB_DUP");
		exit (1);
		} */

	/*
	 * Open a database in the environment:
	 *	create if it doesn't exist
	 *	free-threaded handle
	 *	read/write owner only
	 */
	if ((ret = db->open(db, NULL, name, NULL, /*DB_BTREE*//* DB_RECNO */DB_HASH,
			    DB_AUTO_COMMIT | DB_CREATE | DB_THREAD, S_IRUSR | S_IWUSR)) != 0) {
		(void)db->close(db, 0);
		dbenv->err(dbenv, ret, "db->open: %s", name);
		exit (1);
	}

	*dbp = db;
}
Esempio n. 24
0
File: state.c Progetto: kbarber/cfng
void
DeletePersistentClass(char *name)
{
    int errno;
    DBT key,value;
    DB *dbp;
    struct CfState state;
    time_t now = time(NULL),expires;
    char filename[CF_BUFSIZE];

    snprintf(filename,CF_BUFSIZE,"%s/%s",g_vlockdir,CF_STATEDB_FILE);

    if ((errno = db_create(&dbp,NULL,0)) != 0) {
        snprintf(g_output, CF_BUFSIZE,
                "Couldn't open average database %s\n", filename);
        CfLog(cferror,g_output,"db_open");
        return;
    }

#ifdef CF_OLD_DB
    if ((errno = dbp->open(dbp,filename,NULL,DB_BTREE,
                    DB_CREATE,0644)) != 0)
#else
    if ((errno = dbp->open(dbp,NULL,filename,NULL,DB_BTREE,
                    DB_CREATE,0644)) != 0)
#endif
    {
    snprintf(g_output,CF_BUFSIZE,"Couldn't open average database %s\n",filename);
    CfLog(cferror,g_output,"db_open");
    return;
    }

    chmod(filename,0644);

    memset(&key,0,sizeof(key));
    memset(&value,0,sizeof(value));

    key.data = name;
    key.size = strlen(name)+1;

    if ((errno = dbp->del(dbp,NULL,&key,0)) != 0) {
        CfLog(cferror,"","db_store");
    }

    Debug("Deleted persistent state %s if found\n",name);
    dbp->close(dbp,0);
}
Esempio n. 25
0
int main() {
  DB *db;
  DB *uiddb;
  int ret;

  db_create(&db, NULL, 0);
  db->open(db, NULL, NULL, "data", DB_BTREE, DB_CREATE, 0);

  db_create(&uiddb, NULL, 0);
  uiddb->open(uiddb, NULL, NULL, "byuid", DB_BTREE, DB_CREATE, 0);

  ret = db->associate(db, NULL, uiddb, byuid_callback, 0);
  printf("associate: %d\n", ret);

  user_t u;
  strcpy(u.user, "jls");
  u.uid = 1000;
  strcpy(u.path, "/home/jls");

  DBT key, value;
  memset(&key, 0, sizeof(DBT));
  memset(&value, 0, sizeof(DBT));
  key.data = u.user;
  key.size = strlen(u.user);

  value.data = &u;
  value.size = sizeof(u);

  ret = db->put(db, NULL, &key, &value, 0);
  printf("put: %d\n", ret);

  memset(&value, 0, sizeof(value));
  ret = db->get(db, NULL, &key, &value, 0);
  printf("put: %d\n", ret);
  printf("Uid: %d\n", ((user_t*)value.data)->uid);

  memset(&value, 0, sizeof(value));
  int uid = 1000;
  key.data = &uid;
  key.size = sizeof(int);

  uiddb->get(uiddb, NULL, &key, &value, 0);
  printf("Secondary lookup: user=%s\n", ((user_t*)value.data)->user);

  return 0;
}
Esempio n. 26
0
/*
 * Opens the backend database.
 *
 * ARGUMENTS:
 *      backend         Pointer to pointer to backend structure.  Shall not be
 *                      NULL.  Upon successful return, "*backend" will be set.
 *                      The client should call "beClose(*backend)" when the
 *                      backend is no longer needed.
 *      dir             Pathname of the parent directory of the database.
 *                      Shall not be NULL.  The client can free it upon return.
 *      forWriting      Open the database for writing? 0 <=> no
 * RETURNS:
 *      0               Success.  "*backend" is set.
 *      ENOMEM          System error.  "log_start()" called.
 *      EIO             Backend database error.  "log_start()" called.
 */
RegStatus
beOpen(
    Backend** const     backend,
    const char* const   dir,
    int                 forWriting)
{
    RegStatus   status;
    Backend*    back = (Backend*)malloc(sizeof(Backend));

    assert(NULL != dir);

    if (NULL == back) {
        log_serror("Couldn't allocate %lu bytes", (long)sizeof(Backend));
        status = ENOMEM;
    }
    else {
        DB_ENV*         env;
        DB*             db;
        StringBuf*      path;

        if (0 == (status = sb_new(&path, PATH_MAX))) {
            if (0 == (status = sb_cat(path, dir, "/", DB_DIRNAME))) {
                if (0 == (status = createDbHandle(path, &env, &db))) {
                    if (status = db->open(db, NULL, DB_FILENAME, NULL,
                                          DB_BTREE, forWriting ? DB_CREATE : DB_RDONLY, 0)) {
                        log_add("Couldn't open database \"%s\" in \"%s\" "
                                "for %s", DB_FILENAME, path,
                                forWriting ? "writing" : "reading");
                        status = EIO;
                    }
                    else {
                        back->db = db;
                        back->cursor.dbCursor = NULL;
                        *backend = back;    /* success */
                    }                       /* "db" opened */

                    /*
                     * According to the documentation on DB->open(), if that
                     * call fails, then DB->close() must be called to discard
                     * the DB handle, so DB->close() is the termination
                     * counterpart of db_create() rather than of DB->open().
                     */
                    if (status) {
                        (void)db->close(db, 0);
                        (void)env->close(env, 0);
                    }
                }                       /* "env" allocated */
            }                           /* DB directory pathname created */

            sb_free(path);
        }                               /* "path" allocated */

        if (status)
            free(back);
    }                                   /* "back" allocated */

    return status;
}
Esempio n. 27
0
void createDB()
{
	DB *dbp = NULL;
	u_int32_t open_flags = DB_CREATE;
	int ret, i, j;

	ret = db_create(&dbp, NULL, 0);
	if (ret) {
		printf("db_create error - %d\n", ret);
		return;
	}

	ret = dbp->open(dbp, NULL, "vdd_sync_db.db", NULL, DB_HASH, open_flags, 0);
	if (ret) {
		printf("dbp->open error - %d.\n", ret);
		return;
	}

	DBT key, data;
	unsigned char a[KEY_SIZE];
	unsigned char b[BLOCK_INDEX_SIZE];

	/*****************  put  ********************/
	for (i = 0; i < 10; i++) {
		printf("inbuild ");
		for (j = 0; j < KEY_SIZE; ++j) {
			a[j] = i;
			printf("%u ", a[j]);
		}
		for (j = 0; j < BLOCK_INDEX_SIZE; ++j) {
			b[j] = 0;
			if (j == 0)
				b[j] = i;
			if (j == 4)
				b[j] = 1;
			printf("%u ", b[j]);
		}
		printf("\n");

		memset(&key, 0, sizeof(DBT));
		key.data = a;
		key.size = KEY_SIZE;

		memset(&data, 0, sizeof(DBT));
		data.data = b;
		data.size = BLOCK_INDEX_SIZE;

		ret = dbp->put(dbp, NULL, &(key), &(data), DB_NOOVERWRITE);
		//ret = dbp->put(dbp, NULL, &key, &data, 0);
		if (ret == DB_KEYEXIST){
			printf("key exsit\n");
		} else if (ret) {
			printf("dbp->put error\n");
		}
		dbp->sync(dbp, 0);//toooooo slow
	}
}
Esempio n. 28
0
int main() {
	DB *dbp;
	DB_ENV *dbenv;
	DB_TXN *xid;
	DBT key, data;
	const unsigned int INSERT_NUM = 100;
	char value[22]; /* should be log INSERT_NUM */
	int ret, i, t_ret;

        env_dir_create();
        env_open(&dbenv);

	if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
		exit (1);
	}

	dbenv->txn_begin(dbenv, NULL, &xid, 0);
	if ((ret = dbp->open(dbp,
					xid, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
		dbp->err(dbp, ret, "%s", DATABASE);
		goto err;
	}

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));

	key.size = sizeof(int);
	key.data = malloc(sizeof(int));
	data.data = value;

	for( i = 0; i < INSERT_NUM; i++ ) {
		*((int*)key.data) = i;
		data.size = sizeof(char)*strlen(data.data);
		sprintf(value, "value: %u\n", i);
		dbp->put(dbp, xid, &key, &data, 0);
	}

	xid->commit(xid, 0);
	dbenv->txn_begin(dbenv, NULL, &xid, 0);
	
	for( i = 0; i < INSERT_NUM; i++ ) {
		*((int*)key.data) = i;
		dbp->get(dbp, xid, &key, &data, 0);
		printf("db: %u: key retrieved: data was %s.\n", *((int*)key.data), (char *)data.data);
	}

	xid->abort(xid);

err:    if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
			ret = t_ret; 


	return 0;

}
Esempio n. 29
0
/*
 * Returns a valid 'DB' handle.
 *
 * Requires:
 *    'file' is a valid non-existant path.
 */
DB *bdb_init (const char *file)
{
    DB *db;

    REQUIRE (db_create (&db, NULL, 0) == 0);
    REQUIRE (db->set_flags (db, DB_DUP) == 0);
    REQUIRE (db->open (db, file, NULL, DB_HASH, DB_CREATE | DB_EXCL, 0) == 0);

    return db;
}
Esempio n. 30
0
static void
b_inmem_op_ds(u_int ops, int update)
{
	DB_ENV *dbenv;
	char *letters = "abcdefghijklmnopqrstuvwxuz";
	DB *dbp;
	DBT key, data;
	char *keybuf, *databuf;
	DB_MPOOL_STAT  *gsp;

	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_create(&dbp, NULL, 0) == 0);
	dbenv = dbp->dbenv;
	dbp->set_errfile(dbp, stderr);

	DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0);
	DB_BENCH_ASSERT(dbp->open(
	    dbp, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0666) == 0);

	(void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);

	if (update) {
		TIMER_START;
		for (; ops > 0; --ops) {
			keybuf[(ops % keysize)] = letters[(ops % 26)];
			DB_BENCH_ASSERT(
			    dbp->put(dbp, NULL, &key, &data, 0) == 0);
		}
		TIMER_STOP;
	} else {
		DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
		TIMER_START;
		for (; ops > 0; --ops)
			DB_BENCH_ASSERT(
			    dbp->get(dbp, NULL, &key, &data, 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);
}