Example #1
0
static int
bdb_dump(WT_CURSOR *wtcursor, WT_SESSION *session, const char *tag)
{
	CURSOR_SOURCE *cursor;
	DB *db;
	DBC *dbc;
	DBT *key, *value;
	WT_EXTENSION_API *wt_api;
	int ret = 0;

	cursor = (CURSOR_SOURCE *)wtcursor;
	wt_api = cursor->wt_api;

	db = cursor->db;
	key = &cursor->key;
	value = &cursor->value;

	if ((ret = db->cursor(db, NULL, &dbc, 0)) != 0)
		ERET(wt_api,
		    session, WT_ERROR, "Db.cursor: %s", db_strerror(ret));
	printf("==> %s\n", tag);
	while ((ret = dbc->get(dbc, key, value, DB_NEXT)) == 0)
		if (cursor->config_recno)
			printf("\t%llu/%.*s\n",
			    (unsigned long long)*(db_recno_t *)key->data,
			    (int)value->size, (char *)value->data);
		else
			printf("\t%.*s/%.*s\n",
			    (int)key->size, (char *)key->data,
			    (int)value->size, (char *)value->data);

	if (ret != DB_NOTFOUND)
		ERET(wt_api,
		    session, WT_ERROR, "DbCursor.get: %s", db_strerror(ret));

	return (0);
}
Example #2
0
static int
kvs_cursor_close(WT_CURSOR *wtcursor)
{
	CURSOR_SOURCE *cursor;
	DATA_SOURCE *ds;
	DB *db;
	DBC *dbc;
	WT_EXTENSION_API *wt_api;
	WT_SESSION *session;
	int ret = 0;

	session = wtcursor->session;
	cursor = (CURSOR_SOURCE *)wtcursor;
	ds = cursor->ds;
	wt_api = cursor->wt_api;

	dbc = cursor->dbc;
	cursor->dbc = NULL;
	if (dbc != NULL && (ret = dbc->close(dbc)) != 0)
		ERET(wt_api, session, WT_ERROR,
		    "DbCursor.close: %s", db_strerror(ret));

	db = cursor->db;
	cursor->db = NULL;
	if (db != NULL && (ret = db->close(db, 0)) != 0)
		ERET(wt_api,
		    session, WT_ERROR, "Db.close: %s", db_strerror(ret));
	free(wtcursor);

	if ((ret = writelock(wt_api, session, &ds->rwlock)) != 0)
		return (ret);
	--ds->open_cursors;
	if ((ret = unlock(wt_api, session, &ds->rwlock)) != 0)
		return (ret);

	return (0);
}
Example #3
0
static int
kvs_session_create(WT_DATA_SOURCE *wtds,
    WT_SESSION *session, const char *uri, WT_CONFIG_ARG *config)
{
	DATA_SOURCE *ds;
	DB *db;
	DBTYPE type;
	WT_CONFIG_ITEM v;
	WT_EXTENSION_API *wt_api;
	int ret = 0;
	const char *name;

	ds = (DATA_SOURCE *)wtds;
	wt_api = ds->wt_api;
						/* Get the object name */
	if ((ret = uri2name(wt_api, session, uri, &name)) != 0)
		return (ret);
						/* Check key/value formats */
	if ((ret =
	    wt_api->config_get(wt_api, session, config, "key_format", &v)) != 0)
		ERET(wt_api, session, ret,
		    "key_format configuration: %s",
		    wt_api->strerror(wt_api, session, ret));
	type = v.len == 1 && v.str[0] == 'r' ? DB_RECNO : DB_BTREE;

	/* Create the Berkeley DB table */
	if ((ret = db_create(&db, ds->dbenv, 0)) != 0)
		ERET(wt_api,
		    session, WT_ERROR, "db_create: %s", db_strerror(ret));
	if ((ret = db->open(db, NULL, name, NULL, type, DB_CREATE, 0)) != 0)
		ERET(wt_api,
		    session, WT_ERROR, "Db.open: %s", uri, db_strerror(ret));
	if ((ret = db->close(db, 0)) != 0)
		ERET(wt_api, session, WT_ERROR, "Db.close", db_strerror(ret));

	return (0);
}
Example #4
0
File: db3.c Project: xrg/RPM
static int db3stat(dbiIndex dbi, unsigned int flags)
{
    DB * db = dbi->dbi_db;
#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
    DB_TXN * txnid = NULL;
#endif
    int rc = 0;

    assert(db != NULL);
#if defined(DB_FAST_STAT)
    if (flags)
	flags = DB_FAST_STAT;
    else
#endif
	flags = 0;
    dbi->dbi_stats = _free(dbi->dbi_stats);
#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
    rc = db->stat(db, txnid, &dbi->dbi_stats, flags);
#else
    rc = db->stat(db, &dbi->dbi_stats, flags);
#endif
    rc = cvtdberr(dbi, "db->stat", rc, _debug);
    return rc;
}
Example #5
0
static int
databaseRemove(const char* file,
               const char* database,
               int         flags)
{
#ifdef TRACE_FUNC_CALLS
    LOG_TRACE_IN;
#endif

    DB* localDbHandle;
    int ret;

    // Removing the database file
    db_create(&localDbHandle, NULL, 0);

    ret = localDbHandle->remove(localDbHandle,
                                file,
                                database,
                                flags);
#ifdef TRACE_FUNC_CALLS
    LOG_TRACE_OUT;
#endif
    return ret;
}
Example #6
0
/*  opening the MindMap Database */
static int
ooMindMap_open(struct ooMindMap *self, const char *dbpath)
{
    int ret;
    DB *dbp = self->_storage;

    if (!dbp) return oo_FAIL;

    /* open the database */
    if ((ret = dbp->open(dbp,        /* DB structure pointer */ 
			 NULL,       /* Transaction pointer */ 
			 dbpath,     /* On-disk file that holds the database. */
			 NULL,       /* Optional logical database name */
			 DB_BTREE,   /* Database access method */
			 DB_CREATE,  /* Open flags */
			 0664))      /* File mode (using defaults) */
                      != oo_OK) {
	dbp->err(dbp, ret, "%s: open", dbpath);
	(void)dbp->close(dbp, 0);
	return oo_FAIL;
    }

    return 0;
}
Example #7
0
/* {{{ rberkeley_db_compact */
SEXP rberkeley_db_compact (SEXP _dbp, SEXP _txnid, SEXP _start,
                           SEXP _stop, SEXP _c_data, SEXP _flags)
{
  DB *dbp;
  DB_TXN *txnid;
  DBT start, stop, end;
  /*DB_COMPACT c_data;*/
  u_int32_t flags;
  int ret;

  if(isNull(_txnid)) {
    txnid = R_ExternalPtrAddr(_txnid);
  } else {
    txnid = NULL;
  }
  if(!isNull(_start)) {
    memset(&start, 0, sizeof(DBT));
    start.data = (unsigned char *)RAW(_start);
    start.size = length(_start);
  }
  if(!isNull(_stop)) {
    memset(&stop, 0, sizeof(DBT));
    stop.data = (unsigned char *)RAW(_stop);
    stop.size = length(_stop);
  }
  flags = (u_int32_t)INTEGER(_flags)[0];
  /*memset(&end, 0, sizeof(end));*/

  dbp = R_ExternalPtrAddr(_dbp);
  if(R_ExternalPtrTag(_dbp) != RBerkeley_DB || dbp == NULL)
    error("invalid 'db' handle");

  ret = dbp->compact(dbp, txnid, &start, &stop, NULL, flags, &end); 

  return ScalarInteger(ret);
}
Example #8
0
void var_register(const  QString& id,unsigned short* p,bool persist)
{
	//产生内存变量
	struct varUShort t;
	unsigned short val= 0;
	
	//id是str
	t.id = id;
	
	t.p = p;

	//persist false普通变量 true 保持变量
	if(persist)
	{

		//在sql中查找是否有变量,如果没有则产生条目,否者将变量值读入到内存变量
		if(mydb.GetValue(id,val))
		{
			*p = val;
			t.sqlval = val;
		}
		else
		{
			printf("newval\n");
			mydb.NewId(id,*p,"");
			t.sqlval = *p;
		}
	}
	else
	{
		t.sqlval = *p;
	}
	//内存列表
	varUShortlist.append(t);

}
Example #9
0
/*@
 * Walk through the database and return the highest iid we have seen. 
 * If this is a RECNO database this can probably be done more cheaply
 * but for now we will make this usable regardless of the index impl
 */
iid_t 
storage_get_max_iid(struct storage * s)
{
	int ret;
	DB *dbp = s->db;
	DBC *dbcp;
	DBT key, data;
	iid_t max_iid = 0;
	
	/* Acquire a cursor for the database. */
	if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
		dbp->err(dbp, ret, "DB->cursor");
		return (1);
	}
	
	/* Re-initialize the key/data pair. */ 
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	
	/* Walk through the database and print out the key/data pairs. */
	while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0) {
		assert(data.size = sizeof(iid_t));
		max_iid = *(iid_t *)key.data;
	}
	
	if (ret != DB_NOTFOUND) {
		dbp->err(dbp, ret, "DBcursor->get");
		return 0;
	}
	
	/* Close the cursor. */
	if ((ret = dbcp->c_close(dbcp)) != 0) {
		dbp->err(dbp, ret, "DBcursor->close");
	}
	return (max_iid);
}
Example #10
0
static void cleanup(pam_handle_t *pamh, void *data, int err) {
    DB* db;
    if (NULL != data) {
        abl_context *context = data;
        log_debug(context->args, "In cleanup, err is %08x", err); 

        if (err && (err & PAM_DATA_REPLACE) == 0) {
            log_debug(context->args, "record returned %d",record_attempt(context));
        }
        config_free(context->args);
        free(context->args);

        db = context->info->utdb;
        db->close(db,0);
        db = context->info->usdb;
        db->close(db,0);
        db = context->info->htdb;
        db->close(db,0);
        db = context->info->hsdb;
        db->close(db,0);
        free(context->info);
        free(context);
    }
}
Example #11
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;
}
Example #12
0
static char *
map_db_get_entry(void *hdl, char *key, size_t *len)
{
	int ret;
	DBT dbk;
	DBT dbv;
	DB *db = hdl;
	char *result = NULL;

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

	if ((ret = db->get(db, &dbk, &dbv, 0)) != 0)
		return NULL;

	result = calloc(dbv.size, 1);
	if (result == NULL)
		fatal("calloc");
	(void)strlcpy(result, dbv.data, dbv.size);

	*len = dbv.size;

	return result;
}
Example #13
0
QString UtenteBasic::userSearch(DB &d) const
{
    int contautenti=1;
    QString risposta="";
    for (std::map<QString,Utente*>::iterator it=d.begin(); it!=d.end(); ++it)
    {
        if (it->first==un.login)
            continue;
        if (rete->is_in(it->second->un.login))
        {
            risposta+="\n\nutente amico, puoi vedere il profilo intero anche se sei basic: "+it->second->un.login+ "\n"; contautenti++;
            risposta+=it->second->info->visualizza();
            risposta+=it->second->rete->get_rete();
        }
        else
        {
            risposta+="\n\nutente: "+QString::number(contautenti)+ "\n"; contautenti++;
            risposta+=("Nome: " +(it->second->info->Nome)+"\n");
            risposta+=("Cognome: "+(it->second->info->Cognome)+"\n");
        }
    }
    if (risposta=="") return "Spiacente, nessun risultato :(";
    return risposta;
}
Example #14
0
/* {{{ rberkeley_db_set_msgfile */
SEXP rberkeley_db_set_msgfile (SEXP _dbp, SEXP _msgfile)
{
  DB *dbp;
  FILE *msgfile = NULL;

  dbp = R_ExternalPtrAddr(_dbp);
  if(R_ExternalPtrTag(_dbp) != RBerkeley_DB || dbp == NULL)
    error("invalid 'db' handle");

  if(!isNull(_msgfile)) {
    msgfile = fopen(CHAR(STRING_ELT(_msgfile,0)),"w");
    if(msgfile == NULL)
      error("open failed!\n");
  } else msgfile = NULL;

  dbp->set_msgfile(dbp, msgfile);
  if(msgfile == NULL) {
    return R_NilValue;
  } else {
  SEXP ptr = R_MakeExternalPtr(msgfile, install("msgfile"), ScalarLogical(TRUE));
  R_RegisterCFinalizer(ptr, (R_CFinalizer_t) rberkeley_fclose);
  return ptr;
  } 
}
Example #15
0
int put (void **db, void *key, int key_len, void *value, int value_len) {
    int ret;
    DBT db_key, db_data;

    DB *dbp = *db;


    memset(&db_key, 0, sizeof(db_key));
    memset(&db_data, 0, sizeof(db_data));
    db_key.data = key;
    db_key.size = key_len;
    db_data.data = value;
    db_data.size = value_len;


    if ((ret = dbp->put(dbp, NULL, &db_key, &db_data, 0)) == 0)
        printf("db: %s: key stored.\n", (char *)db_key.data);
    else {
        dbp->err(dbp, ret, "DB->put");
        exit(1);
    }

    return ES_OK;
}
Example #16
0
static krb5_error_code
ctx_unlock(krb5_context context, krb5_db2_context *dbc)
{
    krb5_error_code retval;
    DB *db;

    retval = osa_adb_release_lock(dbc->policy_db);
    if (retval)
        return retval;

    if (!dbc->db_locks_held) /* lock already unlocked */
        return KRB5_KDB_NOTLOCKED;

    db = dbc->db;
    if (--(dbc->db_locks_held) == 0) {
        db->close(db);
        dbc->db = NULL;
        dbc->db_lock_mode = 0;

        retval = krb5_lock_file(context, dbc->db_lf_file,
                                KRB5_LOCKMODE_UNLOCK);
    }
    return retval;
}
Example #17
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;

}
Example #18
0
File: db3.c Project: xrg/RPM
static int db3cget(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
		unsigned int flags)
{
    DB * db = dbi->dbi_db;
    int _printit;
    int rc;

    assert(db != NULL);
    if (dbcursor == NULL) {
	/* XXX duplicates require cursors. */
	rc = db->get(db, dbi->dbi_txnid, key, data, 0);
	/* XXX DB_NOTFOUND can be returned */
	_printit = (rc == DB_NOTFOUND ? 0 : _debug);
	rc = cvtdberr(dbi, "db->get", rc, _printit);
    } else {
	/* XXX db3 does DB_FIRST on uninitialized cursor */
	rc = dbcursor->c_get(dbcursor, key, data, flags);
	/* XXX DB_NOTFOUND can be returned */
	_printit = (rc == DB_NOTFOUND ? 0 : _debug);
	rc = cvtdberr(dbi, "dbcursor->c_get", rc, _printit);
    }

    return rc;
}
Example #19
0
int uriindex_delete (char uri[], char subname[]) {
        DB dbpArray;

        DB *dbp = NULL;

        DBT key;
        int ret;
	int forreturn = 1;
	struct uriindexFormat uriindex;

        if (!uriindex_open(&dbp,subname, DB_CREATE)) {
		fprintf(stderr,"can't open uriindex\n");
		return 0;
	}


        //resetter minne
        memset(&key, 0, sizeof(DBT));

	//legger inn datane i bdb strukturen
        key.data = uri;
        key.size = strlen(uri);

        if ((ret = dbp->del(dbp, NULL, &key, 0)) == 0) {
		forreturn = 1;
        }
        else {
                dbp->err(dbp, ret, "DBcursor->get");
                forreturn = 0;
        }

	uriindex_close(&dbp);

	return forreturn;

}
Example #20
0
// Celest
int guild_getexp(struct map_session_data *sd,int exp)
{
	struct guild *g;
	struct guild_expcache *c;
	nullpo_retr(0, sd);

	if (sd->status.guild_id == 0 || (g = guild_search(sd->status.guild_id)) == NULL)
		return 0;

	c = guild_expcache_db->ensure(guild_expcache_db, i2key(sd->status.char_id), create_expcache, sd);
	if (c->exp > UINT_MAX - exp)
		c->exp = UINT_MAX;
	else
		c->exp += exp;
	return exp;
}
Example #21
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);
}
int TestPartNumber(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBT *keys;
	u_int32_t i;

	dbenv = NULL;
	dbp = NULL;
	keys = NULL;
	nparts = 1000000;

	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++) {
		CuAssertTrue(ct,
		    (keys[i].data = malloc(sizeof(u_int32_t))) != NULL);
		memcpy(keys[i].data, &i, sizeof(u_int32_t));
		keys[i].size = sizeof(u_int32_t);
	}

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

	/* Partition number is bigger than the limit 1000000. */
	CuAssertTrue(ct,
	    dbp->set_partition(dbp, nparts + 1, keys, NULL) == EINVAL);

	/* Partition number is equal to the limit 1000000. */
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);

	/* Partition keys do not fix into a single database page. */
	CuAssertTrue(ct, dbp->set_pagesize(dbp, 512) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, 800, 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);

	for (i = 0 ; i < (nparts - 1); i++)
		free(keys[i].data);
	free(keys);
	return (0);
}
Example #23
0
int smf_lookup_db4_update(const char *database, const char *key, const char *value) {
    DB *dbp;
    DBT db_key, db_data;
    int ret;

    /* Initialize the structure. This database is not opened in an environment,
     * so the environment pointer is NULL. */
    if ((ret = db_create(&dbp, NULL, 0)) != 0) {
        TRACE(TRACE_ERR, "db_create: %s\n", db_strerror(ret));
        return -1;
    }

    /* set page- and cachesize */
    if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
        printf("db->open: %s\n", db_strerror(ret));
        return(-1);
    }
    if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0) {
        printf("db->open: %s\n", db_strerror(ret));
        return(-1);
    }

    /* open the database */
    ret = dbp->open(dbp, NULL, database, NULL, DB_HASH, DB_CREATE, 0);
    if (ret != 0) {
        TRACE(TRACE_ERR,"DB: %s",db_strerror(ret));
        return -1;
    }

    memset(&db_key, 0, sizeof(DBT));
    db_key.data = (char*)key;
    db_key.size = strlen(key) + 1;
    memset(&db_data, 0, sizeof(DBT));
    db_data.data = (char*)value;
    db_data.size = strlen(value) + 1;

    ret = dbp->put(dbp, NULL, &db_key, &db_data, 0);
    if (ret == 0) {
        TRACE(TRACE_DEBUG, "[%p] update was successful [%s: %s]", dbp, key, value);
    } else {
        TRACE(TRACE_ERR, "[%p] failed to update database [%s: %s]: %s",
              dbp, key, value, db_strerror(ret));
    }

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

    return (ret == 0) ? ret : -1;
}
Example #24
0
static int
ooMindMap_keys(struct ooMindMap *self,
	       mindmap_size_t  **ids,
	       mindmap_size_t    batch_size,
	       mindmap_size_t    batch_start)
{   DBT key, data;
    DB *dbp;
    DBC *dbcp;
    int ret;

    dbp = self->_storage;

    /* initialize the key/data pair */
    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    /* acquire a cursor for the database */
    if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
	dbp->err(dbp, ret, "DB->cursor");
	goto error;
    }

    /* TODO: batching */

    /* walk through the database and print out the key/data pairs. */
    while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0)
	printf("%.*s : %.*s\n",
	       (int)key.size, (char *)key.data,
	       (int)data.size, (char *)data.data);
    if (ret != DB_NOTFOUND)
    {	dbp->err(dbp, ret, "DBcursor->get");
	goto error;
    }

    /* close everything down */
    if ((ret = dbcp->c_close(dbcp)) != 0) {
	dbp->err(dbp, ret, "DBcursor->close");
	goto error;
    }

    return oo_OK;

error:	
    (void)dbp->close(dbp, 0);
    return oo_FAIL;
}
Example #25
0
int MovieDB::InsertMovie(Movie clsMovie)
{
	DB db;
	db.Connect();
	CString csSQL;
	CStringArray csaMaxID;
	db.Select("select top 1 MovieID from MovieTable order by MovieID desc",csaMaxID);	
    db.CloseRecordset();
	csSQL.Format("insert into MovieTable values(,%d,%d,%d,%d,'%s','%s','%s','%s')",atoi(csaMaxID.GetAt(0))+1,
		clsMovie.iTimeLength,clsMovie.iLength,clsMovie.iWidth,clsMovie.CStrYear,clsMovie.CStrMovieType,clsMovie.CStrPath,clsMovie.CStrFileName);
    if(db.Execute(csSQL)==FALSE)
	{
		return 0;
	}
	db.DisConnect();
	return 1;
}
Example #26
0
int
open_db(DB** pDb, const char* pcszDBName, int nClear, int nDuplicate){
	DB*	db;
	int	nRet;
	
	*pDb = NULL;
 opendb:
	/* Initialize BDB handle */
	if ( db_create(&db, NULL, 0) ){
		write_log(LT_BOTH, "Error: Can't init BDB,"
			  "when open database %s!\n", pcszDBName ) ;
		return 0;
	}

	/* if need clear table */
	if ( nClear == 1 ){
		db->remove(db, pcszDBName, NULL, 0);
		nClear = 0;
		goto opendb;
	}

	/* set up error handling for this database */
	db->set_errcall(db, db_err_handler);
	db->set_errpfx(db, "jau_db");
	if ( nDuplicate == 1 )
		db->set_flags(db, DB_DUPSORT);
	
	/* Now open the database */
	nRet = db->open(db, NULL, pcszDBName,
			     NULL, DB_BTREE, DB_CREATE, 0);
	if (nRet != 0) {
		write_log(LT_BOTH, "Error: Can't open database %s!\n",
			  pcszDBName);
		db->close(db, 0);
		return 0;
	}
	*pDb = db;
	
	return 1;
}
Example #27
0
int
dbfe_opendb (DB_ENV *dbe, DB **dbp, str filename, int flags, int mode = 0664, bool dups = false)
{
  int r (-1);
  r = db_create (dbp, dbe, 0);
  if (r) return r;

  DB *db = *dbp;
  db->set_pagesize (db, 16 * 1024);

  /* Secondary databases, for example, require duplicates */
  if (dups && (r = db->set_flags (db, DB_DUPSORT)) != 0) {
    (void)db->close(db, 0);
    dbe->err (dbe, r, "db->set_flags: DB_DUP");
    return r;
  }

  /* the below seems to cause the db to grow much larger. */
  // db->set_bt_minkey(db, 60);

#if ((DB_VERSION_MAJOR < 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR < 1)))
  r = db->open (db, filename.cstr (), NULL, DB_BTREE, flags, mode);
#else
  if (!dbe) {
    r = db->open (db, NULL, filename.cstr (), NULL, DB_BTREE, flags, mode);
  } else {
    // Sleepycat 4.1 and greater force us to open the DB inside a
    // transaction the open suceeds in either case, but if the open
    // isn't surrounded by a transaction, later calls that use a
    // transaction will fail
    DB_TXN *t = NULL;
    r = dbe->txn_begin (dbe, NULL, &t, 0);
    if (r || !t) return r;
    r = db->open (db, t, filename.cstr (), NULL, DB_BTREE, flags, mode);
    r = t->commit (t, 0);
#endif
  }
  return r;
}
Example #28
0
static int
db_servent(void *retval, void *mdata, va_list ap)
{
	char buf[BUFSIZ];
	DBT key, data, *result;
	DB *db;

	struct db_state *st;
	int rv;
	int stayopen;

	enum nss_lookup_type how;
	char *name;
	char *proto;
	int port;

	struct servent *serv;
	char *buffer;
	size_t bufsize;
	int *errnop;

	name = NULL;
	proto = NULL;
	how = (enum nss_lookup_type)mdata;
	switch (how) {
	case nss_lt_name:
		name = va_arg(ap, char *);
		proto = va_arg(ap, char *);
		break;
	case nss_lt_id:
		port = va_arg(ap, int);
		proto = va_arg(ap, char *);
		break;
	case nss_lt_all:
		break;
	default:
		return NS_NOTFOUND;
	};

	serv = va_arg(ap, struct servent *);
	buffer  = va_arg(ap, char *);
	bufsize = va_arg(ap, size_t);
	errnop = va_arg(ap,int *);

	*errnop = db_getstate(&st);
	if (*errnop != 0)
		return (NS_UNAVAIL);

	if (how == nss_lt_all && st->keynum < 0)
		return (NS_NOTFOUND);

	if (st->db == NULL) {
		st->db = dbopen(_PATH_SERVICES_DB, O_RDONLY, 0, DB_HASH, NULL);
		if (st->db == NULL) {
			*errnop = errno;
			return (NS_UNAVAIL);
		}
	}

	stayopen = (how == nss_lt_all) ? 1 : st->stayopen;
	db = st->db;

	do {
		switch (how) {
		case nss_lt_name:
			key.data = buf;
			if (proto == NULL)
				key.size = snprintf(buf, sizeof(buf),
				    "\376%s", name);
			else
				key.size = snprintf(buf, sizeof(buf),
				    "\376%s/%s", name, proto);
			key.size++;
			if (db->get(db, &key, &data, 0) != 0 ||
			    db->get(db, &data, &key, 0) != 0) {
				rv = NS_NOTFOUND;
				goto db_fin;
			}
			result = &key;
			break;
		case nss_lt_id:
			key.data = buf;
			port = htons(port);
			if (proto == NULL)
				key.size = snprintf(buf, sizeof(buf),
				    "\377%d", port);
			else
				key.size = snprintf(buf, sizeof(buf),
				    "\377%d/%s", port, proto);
			key.size++;
			if (db->get(db, &key, &data, 0) != 0 ||
			    db->get(db, &data, &key, 0) != 0) {
				rv = NS_NOTFOUND;
				goto db_fin;
			}
			result = &key;
			break;
		case nss_lt_all:
			key.data = buf;
			key.size = snprintf(buf, sizeof(buf), "%d",
			    st->keynum++);
			key.size++;
			if (db->get(db, &key, &data, 0) != 0) {
				st->keynum = -1;
				rv = NS_NOTFOUND;
				goto db_fin;
			}
			result = &data;
			break;
		}

		rv = parse_result(serv, buffer, bufsize, result->data,
		    result->size - 1, errnop);

	} while (!(rv & NS_TERMINATE) && how == nss_lt_all);

db_fin:
	if (!stayopen && st->db != NULL) {
		db->close(db);
		st->db = NULL;
	}

	if (rv == NS_SUCCESS && retval != NULL)
		*(struct servent **)retval = serv;

	return (rv);
}
Example #29
0
File: db.c Project: 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);
}
Example #30
0
int
b_load(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind, __db_getopt_reset;
	DB *dbp;
	DBTYPE type;
	DBT key, data;
#if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
	DB_HEAP_RID rid;
#endif
	db_recno_t recno;
	u_int32_t cachesize;
	int ch, i, count, duplicate;
	char *ts, buf[32];

	type = DB_BTREE;
	cachesize = MEGABYTE;
	count = 100000;
	duplicate = 0;
	ts = "Btree";
	__db_getopt_reset = 1;
	while ((ch = getopt(argc, argv, "C:c:dt:")) != EOF)
		switch (ch) {
		case 'C':
			cachesize = (u_int32_t)atoi(optarg);
			break;
		case 'c':
			count = atoi(optarg);
			break;
		case 'd':
			duplicate = 1;
			break;
		case 't':
			switch (optarg[0]) {
			case 'B': case 'b':
				ts = "Btree";
				type = DB_BTREE;
				break;
			case 'H': case 'h':
				if (optarg[1] == 'E' || optarg[1] == 'e') {
#if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
					if (b_util_have_heap())
						return (0);
					ts = "Heap";
					type = DB_HEAP;
#else
					fprintf(stderr,
				"b_curwalk: Heap is not supported! \n");
					return (EXIT_SUCCESS);
#endif
				} else {
					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_load_usage());
			}
			break;
		case '?':
		default:
			return (b_load_usage());
		}
	argc -= optind;
	argv += optind;
	if (argc != 0)
		return (b_load_usage());

	/* Usage. */
#if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
	if (duplicate && 
	    (type == DB_QUEUE || type == DB_RECNO || type == DB_HEAP)) {
		fprintf(stderr,
	"b_load: Queue, Recno and Heap don't support duplicates\n");
		return (b_load_usage());
	}
#else
	if (duplicate && (type == DB_QUEUE || type == DB_RECNO)) {
		fprintf(stderr,
		    "b_load: Queue an Recno don't support duplicates\n");
		return (b_load_usage());
	}
#endif

#if DB_VERSION_MAJOR < 3 || DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 1
	/*
	 * DB versions prior to 3.1.17 didn't have off-page duplicates, so
	 * this test can run forever.
	 */
	if (duplicate)
		return (0);
#endif

	/* Create the database. */
	DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
	DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 0) == 0);
	if (duplicate)
		DB_BENCH_ASSERT(dbp->set_flags(dbp, DB_DUP) == 0);
	dbp->set_errfile(dbp, stderr);

	/* Set record length for Queue. */
	if (type == DB_QUEUE)
		DB_BENCH_ASSERT(dbp->set_re_len(dbp, 20) == 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

	/* Initialize the data. */
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));

	/* Insert count in-order key/data pairs. */
	TIMER_START;
	if (duplicate) {
		key.size = 10;
		key.data = "01234567890123456789";
		data.data = buf;
		data.size = 20;
		for (i = 0; i < count; ++i) {
			(void)snprintf(buf, sizeof(buf), "%020d", i);
			DB_BENCH_ASSERT(
			    dbp->put(dbp, NULL, &key, &data, 0) == 0);
		}
	} else {
		data.data = buf;
		data.size = 20;
		if (type == DB_BTREE || type == DB_HASH) {
			key.size = 10;
			key.data = buf;
			for (i = 0; i < count; ++i) {
				(void)snprintf(buf, sizeof(buf), "%010d", i);
				DB_BENCH_ASSERT(
				    dbp->put(dbp, NULL, &key, &data, 0) == 0);
			}
#if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
		} else if (type == DB_HEAP) {
			key.data = &rid;
			key.size = sizeof(rid);
			for (i = 0; i < count; ++i)
				DB_BENCH_ASSERT(dbp->put(dbp, 
				    NULL, &key, &data, DB_APPEND) == 0);
#endif
		} else {
			key.data = &recno;
			key.size = sizeof(recno);
			for (i = 0, recno = 1; i < count; ++i, ++recno)
				DB_BENCH_ASSERT(
				    dbp->put(dbp, NULL, &key, &data, 0) == 0);
		}
	}

	TIMER_STOP;

	printf("# %d %s database in-order put of 10/20 byte key/data %sitems\n",
	    count, ts, duplicate ? "duplicate " : "");
	TIMER_DISPLAY(count);

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

	return (0);
}