コード例 #1
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;
}
コード例 #2
0
ファイル: db.c プロジェクト: vogth/chandler
/*
 * csv_secondary_open --
 *	Open any secondary indices.
 */
int
csv_secondary_open()
{
    DB *sdb;
    DbField *f;
    int ret, (*fcmp)(DB *, const DBT *, const DBT *);

    /*
     * Create secondary database handles.
     */
    for (f = fieldlist; f->name != NULL; ++f) {
        if (f->indx == 0)
            continue;

        if ((ret = db_create(&sdb, dbenv, 0)) != 0) {
            dbenv->err(dbenv, ret, "db_create");
            return (1);
        }
        sdb->app_private = f;

        /* Keys are small, use a relatively small page size. */
        if ((ret = sdb->set_pagesize(sdb, 8 * 1024)) != 0) {
            dbenv->err(dbenv, ret, "DB->set_pagesize");
            return (1);
        }

        /*
         * Sort the database based on the underlying type.  Skip
         * strings, Berkeley DB defaults to lexicographic sort.
         */
        switch (f->type) {
        case DOUBLE:
            fcmp = compare_double;
            break;
        case ULONG:
            fcmp = compare_ulong;
            break;
        case NOTSET:
        case STRING:
        default:
            fcmp = NULL;
            break;
        }
        if (fcmp != NULL &&
                (ret = sdb->set_bt_compare(sdb, fcmp)) != 0) {
            dbenv->err(dbenv, ret, "DB->set_bt_compare");
            return (1);
        }

        /* Always configure secondaries for sorted duplicates. */
        if ((ret = sdb->set_flags(sdb, DB_DUPSORT)) != 0) {
            dbenv->err(dbenv, ret, "DB->set_flags");
            return (1);
        }
        if ((ret = sdb->set_dup_compare(sdb, compare_ulong)) != 0) {
            dbenv->err(dbenv, ret, "DB->set_dup_compare");
            return (1);
        }

        if ((ret = sdb->open(
                       sdb, NULL, f->name, NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
            dbenv->err(dbenv, ret, "DB->open: %s", f->name);
            return (1);
        }
        if ((ret = sdb->associate(
                       db, NULL, sdb, secondary_callback, DB_CREATE)) != 0) {
            dbenv->err(dbenv, ret, "DB->set_associate");
            return (1);
        }
        f->secondary = sdb;
    }

    return (0);
}
コード例 #3
0
int TestKeyExistErrorReturn(CuTest *ct) {
	DB *pdbp;
	DB *sdbp;
	DB_ENV *dbenv;

	const char *sec_db_file = "secondary.db";
	const char *pri_db_file = "primary.db";
	const char *env_dir = "TESTDIR";
	int i;
	thread_t writer_threads[NUMWRITERS];
	u_int32_t db_flags, env_flags;

	pdbp = sdbp = NULL;
	dbenv = NULL;
	db_flags = DB_CREATE | DB_AUTO_COMMIT | DB_READ_UNCOMMITTED;
	env_flags = DB_CREATE | DB_RECOVER | DB_INIT_LOCK | DB_INIT_LOG |
	    DB_INIT_MPOOL | DB_INIT_TXN | DB_THREAD;

	TestEnvConfigTestSetup(ct);

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

	dbenv->set_errfile(dbenv, stderr);
	dbenv->set_errpfx(dbenv, "TestKeyExistErrorReturn");

	/* Run deadlock detector on every lock conflict. */
	CuAssert(ct, "dbenv->set_lk_detect",
	    dbenv->set_lk_detect(dbenv, DB_LOCK_MINWRITE) == 0);

	CuAssert(ct, "dbenv->open",
	    dbenv->open(dbenv, env_dir, env_flags, 0) == 0);

	CuAssert(ct, "db_create", db_create(&pdbp, dbenv, 0) == 0);
	CuAssert(ct, "pdbp->open", pdbp->open(pdbp, NULL,
	    pri_db_file, NULL, DB_BTREE, db_flags, 0) == 0);

	CuAssert(ct, "db_create", db_create(&sdbp, dbenv, 0) == 0);
	CuAssert(ct, "sdbp->set_flags", sdbp->set_flags(sdbp,
	    DB_DUPSORT) == 0);
	CuAssert(ct, "sdbp->open", sdbp->open(sdbp, NULL, sec_db_file,
	    NULL, DB_BTREE, db_flags, 0) == 0);

	CuAssert(ct, "DB->associate", pdbp->associate(pdbp, NULL, sdbp,
	    assoc_callback, DB_AUTO_COMMIT) == 0);

	/* Initialize a mutex. Used to help provide thread ids. */
	(void)mutex_init(&thread_num_lock, NULL);

	for (i = 0; i < NUMWRITERS; ++i)
		(void)thread_create(&writer_threads[i], NULL,
		    writer_thread, (void *)pdbp);

	for (i = 0; i < NUMWRITERS; ++i)
		(void)thread_join(writer_threads[i], NULL);

	if (sdbp != NULL) 
		CuAssert(ct, "sdbp->close", sdbp->close(sdbp, 0) == 0);
	if (pdbp != NULL)
		CuAssert(ct, "pdbp->close", pdbp->close(pdbp, 0) == 0);
	if (dbenv != NULL)
		CuAssert(ct, "dbenv->close", dbenv->close(dbenv, 0) == 0);

	TestEnvConfigTestTeardown(ct);

	return (EXIT_SUCCESS);
}