/** * librdf_hash_bdb_cursor_init - Initialise a new bdb cursor * @cursor_context: hash cursor context * @hash_context: hash to operate over * * Return value: non 0 on failure **/ static int librdf_hash_bdb_cursor_init(void *cursor_context, void *hash_context) { librdf_hash_bdb_cursor_context *cursor=(librdf_hash_bdb_cursor_context*)cursor_context; #ifdef HAVE_BDB_CURSOR DB* db; #endif cursor->hash=(librdf_hash_bdb_context*)hash_context; #ifdef HAVE_BDB_CURSOR db=cursor->hash->db; #ifdef HAVE_BDB_CURSOR_4_ARGS /* V3 prototype: * int DB->cursor(DB *db, DB_TXN *txnid, DBC **cursorp, u_int32_t flags); */ if(db->cursor(db, NULL, &cursor->cursor, 0)) return 1; #else /* V2 prototype: * int DB->cursor(DB *db, DB_TXN *txnid, DBC **cursorp); */ if(db->cursor(db, NULL, &cursor->cursor)) return 1; #endif #endif return 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); }
int hrai_list(const char *db_name) { DBT key, data; DBC *it; DB *dbp = hrai_db_open(); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); dbp->cursor(dbp, NULL, &it, 0); int ret; while ((ret = it->get(it, &key, &data, DB_NEXT)) == 0) { struct hrai_entry *entry = (struct hrai_entry *)data.data; time_t *keystamp = (time_t *)key.data; printf("%ld: %4d-%02d-%02d [%u] %s\n", *keystamp , (entry->date).tm_year + 1900 , (entry->date).tm_mon + 1 , (entry->date).tm_mday , entry->amount , entry->description ); } if (ret != DB_NOTFOUND) return 1; if (it != NULL) it->close(it); hrai_db_close(dbp); return 0; }
/** * e_dbhash_foreach_key: * @edbh: an #EDbHash * @func: a callback function * @user_data: data to pass to @func * * Calls @func for each database object. **/ void e_dbhash_foreach_key (EDbHash *edbh, EDbHashFunc func, gpointer user_data) { DB *db; DBT dkey; DBT ddata; DBC *dbc; gint db_error = 0; g_return_if_fail (edbh != NULL); g_return_if_fail (edbh->priv != NULL); g_return_if_fail (func != NULL); db = edbh->priv->db; db_error = db->cursor (db, NULL, &dbc, 0); if (db_error != 0) { return; } memset (&dkey, 0, sizeof (DBT)); memset (&ddata, 0, sizeof (DBT)); db_error = dbc->c_get (dbc, &dkey, &ddata, DB_FIRST); while (db_error == 0) { (*func) ((const gchar *) dkey.data, user_data); db_error = dbc->c_get (dbc, &dkey, &ddata, DB_NEXT); } dbc->c_close (dbc); }
static int pctlmfdb_stat(struct pctldb_st *pctldb, int *dberror){ int status = 0; DB* mfdbp = pctldb->mfdbp; DBC *cursor; DBT key, data; uint32_t memfile_size; *dberror = mfdbp->cursor(mfdbp, NULL, &cursor, 0); if(*dberror != 0) return(-1); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); pctldb->mf_total_size = 0; while(status == 0){ status = cursor->c_get(cursor, &key, &data, DB_NEXT); if(status == 0){ memfile_size = data.size; pctldb->mf_total_size += memfile_size; } } cursor->c_close(cursor); if(status == DB_NOTFOUND) status = 0; else{ *dberror = status; status = -1; } return(status); }
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); }
static int db3copen(dbiIndex dbi, DB_TXN * txnid, DBC ** dbcp, unsigned int dbiflags) { DB * db = dbi->dbi_db; DBC * dbcursor = NULL; int flags; int rc; /* XXX DB_WRITECURSOR cannot be used with sunrpc dbenv. */ assert(db != NULL); if ((dbiflags & DB_WRITECURSOR) && (dbi->dbi_eflags & DB_INIT_CDB) && !(dbi->dbi_oflags & DB_RDONLY)) { flags = DB_WRITECURSOR; } else flags = 0; rc = db->cursor(db, txnid, &dbcursor, flags); rc = cvtdberr(dbi, "db->cursor", rc, _debug); if (dbcp) *dbcp = dbcursor; else (void) db3cclose(dbi, dbcursor, 0); return rc; }
void bdb_dump_users_score(const char *dbpath) { struct passwd *p; uid_t uid; int mailcount; DBT key, data; DB *map = _bdb_open_database(dbpath); DBC *cursor; map->cursor(map, NULL, &cursor, 0); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); data.data = &mailcount; data.ulen = sizeof(int); data.flags = DB_DBT_USERMEM; while (cursor->get(cursor, &key, &data, DB_NEXT) == 0) { uid = *((uid_t *)key.data); if((p = getpwuid(uid)) == NULL) { warn("Can't find user for uid : %d\n", uid); continue; } printf("%s\t%d\n", p->pw_name, mailcount); } cursor->close(cursor); map->close(map, 0); }
int mail_cache_db_clean_up(struct mail_cache_db * cache_db, chash * exist) { DB * dbp; int r; DBC * dbcp; DBT db_key; DBT db_data; dbp = cache_db->internal_database; #if DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6 r = dbp->cursor(dbp, NULL, &dbcp); #else r = dbp->cursor(dbp, NULL, &dbcp, 0); #endif if (r != 0) return -1; memset(&db_key, 0, sizeof(db_key)); memset(&db_data, 0, sizeof(db_data)); while (1) { chashdatum hash_key; chashdatum hash_data; r = dbcp->c_get(dbcp, &db_key, &db_data, DB_NEXT); if (r != 0) break; hash_key.data = db_key.data; hash_key.len = db_key.size; r = chash_get(exist, &hash_key, &hash_data); if (r < 0) { r = dbcp->c_del(dbcp, 0); if (r != 0) return -1; } } r = dbcp->c_close(dbcp); if (r != 0) return -1; return 0; }
int hdb_dn2id_parent( Operation *op, DB_TXN *txn, EntryInfo *ei, ID *idp ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; DB *db = bdb->bi_dn2id->bdi_db; DBT key, data; DBC *cursor; int rc = 0; diskNode *d; char *ptr; ID nid; DBTzero(&key); key.size = sizeof(ID); key.data = &nid; key.ulen = sizeof(ID); key.flags = DB_DBT_USERMEM; BDB_ID2DISK( ei->bei_id, &nid ); DBTzero(&data); data.flags = DB_DBT_USERMEM; rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags ); if ( rc ) return rc; data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2); d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx ); data.data = d; rc = cursor->c_get( cursor, &key, &data, DB_SET ); if ( rc == 0 ) { if (d->nrdnlen[0] & 0x80) { rc = LDAP_OTHER; } else { db_recno_t dkids; ptr = (char *) data.data + data.size - sizeof(ID); BDB_DISK2ID( ptr, idp ); ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1]; ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn ); ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - ei->bei_nrdn.bv_len; ptr = d->nrdn + ei->bei_nrdn.bv_len + 1; ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn ); /* How many children does this node have? */ cursor->c_count( cursor, &dkids, 0 ); ei->bei_dkids = dkids; } } cursor->c_close( cursor ); op->o_tmpfree( d, op->o_tmpmemctx ); return rc; }
int bdb_dn2id( Operation *op, struct berval *dn, EntryInfo *ei, DB_TXN *txn, DB_LOCK *lock ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; DB *db = bdb->bi_dn2id->bdi_db; DBC *cursor; int rc; DBT key, data; ID nid; Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 ); DBTzero( &key ); key.size = dn->bv_len + 2; key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx ); ((char *)key.data)[0] = DN_BASE_PREFIX; AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 ); /* store the ID */ DBTzero( &data ); data.data = &nid; data.ulen = sizeof(ID); data.flags = DB_DBT_USERMEM; rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags ); if ( rc ) goto func_leave; rc = bdb_dn2id_lock( bdb, dn, 0, txn, lock ); if ( rc ) goto nolock; /* fetch it */ rc = cursor->c_get( cursor, &key, &data, DB_SET ); nolock: cursor->c_close( cursor ); func_leave: if( rc != 0 ) { Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n", db_strerror( rc ), rc, 0 ); } else { BDB_DISK2ID( &nid, &ei->bei_id ); Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%lx\n", ei->bei_id, 0, 0 ); } op->o_tmpfree( key.data, op->o_tmpmemctx ); return rc; }
/* FIXME should this be wrapped in an explicit transaction? */ static void do_dump_triplets() { DB *db; DBC *dbcp; int rc; DBT key = { 0 }, data = { 0 }; rc = get_db(&db, 1); if (rc) { fprintf(stderr, "DBD-%d: failed to open database\n", rc); return; } rc = db->cursor(db, 0, &dbcp, 0); if (rc) fprintf(stderr, "DBD-%d: db->cursor failed: %s\n", rc, db_strerror(rc)); else { while (! exit_requested && (rc = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0) { const char *const start = key.data, *s = start; const struct triplet_data *t = data.data; printf("%d\t", t->crypted); printf("%s\t", db_key_ntop(s)); switch ((enum dbkey_type_enum)*s++) { case DBKEY_T_RAW: s += strlen(s) + 1; break; case DBKEY_T_IP4: s += IPV4_BITS / 8; break; case DBKEY_T_IP6: s += IPV6_BITS / 8; break; } printf("%s\t", s); s += strlen(s) + 1; fwrite(s, 1, key.size - (s - start), stdout); putchar('\t'); write_ctime(&t->create_time); putchar('\t'); write_ctime(&t->access_time); printf("\t%lu\t%lu\n", t->block_count, t->pass_count); } if (rc && rc != DB_NOTFOUND) fprintf(stderr, "DBD-%d: dbcp->c_get failed: %s\n", rc, db_strerror(rc)); rc = dbcp->c_close(dbcp); if (rc) fprintf(stderr, "DBD-%d: dbcp->c_close failed: %s\n", rc, db_strerror(rc)); } }
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 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; }
Iterator* PerconaFTEngine::Find(Context& ctx, const KeyObject& key) { PerconaFTLocalContext& local_ctx = g_local_ctx.GetValue(); DB_TXN* txn = local_ctx.transc.Get(); PerconaFTIterator* iter = NULL; NEW(iter, PerconaFTIterator(this,key.GetNameSpace())); DB* db = GetFTDB(ctx, key.GetNameSpace(), false); if (NULL == db) { iter->MarkValid(false); return iter; } int r = 0; DBC* c = NULL; CHECK_EXPR(r = db->cursor(db, txn, &c, 0)); if (0 != r) { local_ctx.transc.Release(false); iter->MarkValid(false); return iter; } iter->SetCursor(db, txn, c); if (key.GetType() > 0) { if (!ctx.flags.iterate_multi_keys) { if (!ctx.flags.iterate_no_upperbound) { KeyObject& upperbound_key = iter->IterateUpperBoundKey(); upperbound_key.SetNameSpace(key.GetNameSpace()); if (key.GetType() == KEY_META) { upperbound_key.SetType(KEY_END); } else { upperbound_key.SetType(key.GetType() + 1); } upperbound_key.SetKey(key.GetKey()); upperbound_key.CloneStringPart(); } } iter->Jump(key); } else { iter->JumpToFirst(); } return iter; }
int hdb_dn2id_children( Operation *op, DB_TXN *txn, Entry *e ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; DB *db = bdb->bi_dn2id->bdi_db; DBT key, data; DBC *cursor; int rc; ID id; diskNode d; DBTzero(&key); key.size = sizeof(ID); key.data = &e->e_id; key.flags = DB_DBT_USERMEM; BDB_ID2DISK( e->e_id, &id ); /* IDL cache is in host byte order */ if ( bdb->bi_idl_cache_size ) { rc = bdb_idl_cache_get( bdb, db, &key, NULL ); if ( rc != LDAP_NO_SUCH_OBJECT ) { return rc; } } key.data = &id; DBTzero(&data); data.data = &d; data.ulen = sizeof(d); data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL; data.dlen = sizeof(d); rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags ); if ( rc ) return rc; rc = cursor->c_get( cursor, &key, &data, DB_SET ); if ( rc == 0 ) { db_recno_t dkids; rc = cursor->c_count( cursor, &dkids, 0 ); if ( rc == 0 ) { BEI(e)->bei_dkids = dkids; if ( dkids < 2 ) rc = DB_NOTFOUND; } } cursor->c_close( cursor ); return rc; }
static bool dir_is_empty(DB_TXN *txn, const struct nfs_inode *ino) { int rc; DBC *cur = NULL; DB *dirent = srv.fsdb.dirent; DBT pkey, pval; struct fsdb_de_key key; nfsino_t rnum = 0; uint64_t db_dummy; rc = dirent->cursor(dirent, txn, &cur, 0); if (rc) { dirent->err(dirent, rc, "dirent->cursor"); return false; } key.inum = inum_encode(ino->inum); memset(&pkey, 0, sizeof(pkey)); pkey.data = &key; pkey.size = sizeof(key); pkey.flags = DB_DBT_MALLOC; memset(&pval, 0, sizeof(pval)); pval.data = &db_dummy; pval.ulen = sizeof(db_dummy); pval.flags = DB_DBT_USERMEM; rc = cur->get(cur, &pkey, &pval, DB_SET_RANGE); if (rc == 0) { struct fsdb_de_key *rkey = pkey.data; rnum = inum_decode(rkey->inum); free(rkey); } else if (rc != DB_NOTFOUND) dirent->err(dirent, rc, "dir_is_empty cur->get"); rc = cur->close(cur); if (rc) { dirent->err(dirent, rc, "dirent->cursor close"); return false; } if (rnum == ino->inum) return false; return true; }
int Db::cursor(DbTxn *txnid, Dbc **cursorp, u_int32_t flags) { DB *db = unwrap(this); DBC *dbc = 0; int err; if ((err = db->cursor(db, unwrap(txnid), &dbc, flags)) != 0) { DB_ERROR("Db::cursor", err, error_policy()); return (err); } // The following cast implies that Dbc can be no larger than DBC *cursorp = (Dbc*)dbc; return (0); }
static dbiCursor db3_dbiCursorInit(dbiIndex dbi, unsigned int flags) { dbiCursor dbc = NULL; if (dbi && dbi->dbi_db) { DB * db = dbi->dbi_db; DBC * cursor; int cflags; int rc = 0; uint32_t eflags = db_envflags(db); /* DB_WRITECURSOR requires CDB and writable db */ if ((flags & DBC_WRITE) && (eflags & DB_INIT_CDB) && !(dbi->dbi_flags & DBI_RDONLY)) { cflags = DB_WRITECURSOR; } else cflags = 0; /* * Check for stale locks which could block writes "forever". * XXX: Should we also do this on reads? Reads are less likely * to get blocked so it seems excessive... * XXX: On DB_RUNRECOVER, we should abort everything. Now * we'll just fail to open a cursor again and again and again. */ if (cflags & DB_WRITECURSOR) { DB_ENV *dbenv = db->get_env(db); rc = dbenv->failchk(dbenv, 0); rc = cvtdberr(dbi, "dbenv->failchk", rc, _debug); } if (rc == 0) { rc = db->cursor(db, NULL, &cursor, cflags); rc = cvtdberr(dbi, "db->cursor", rc, _debug); } if (rc == 0) { dbc = xcalloc(1, sizeof(*dbc)); dbc->cursor = cursor; dbc->dbi = dbi; dbc->flags = flags; } } return dbc; }
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; }
int Db::cursor(DbTxn *txnid, Dbc **cursorp, u_int32_t flags) { DB *db = unwrap(this); int err; if (!db) { DB_ERROR("Db::cursor", EINVAL); return EINVAL; } DBC *dbc = 0; if ((err = db->cursor(db, unwrap(txnid), &dbc, flags)) != 0) { DB_ERROR("Db::cursor", err); return err; } // The following cast implies that Dbc can be no larger than DBC *cursorp = (Dbc*)dbc; return 0; }
int mail_cache_db_get_keys(struct mail_cache_db * cache_db, chash * keys) { DB * dbp; int r; DBC * dbcp; DBT db_key; DBT db_data; dbp = cache_db->internal_database; r = dbp->cursor(dbp, NULL, &dbcp, 0); if (r != 0) return -1; memset(&db_key, 0, sizeof(db_key)); memset(&db_data, 0, sizeof(db_data)); while (1) { chashdatum hash_key; chashdatum hash_data; r = dbcp->c_get(dbcp, &db_key, &db_data, DB_NEXT); if (r != 0) break; hash_key.data = db_key.data; hash_key.len = db_key.size; hash_data.data = NULL; hash_data.len = 0; r = chash_set(keys, &hash_key, &hash_data, NULL); if (r < 0) { return -1; } } r = dbcp->c_close(dbcp); if (r != 0) return -1; return 0; }
void bdb_zero_database(const char *dbpath, int blacklist) { DB *map; DBC *cursor; DBT key, data; int score; map = _bdb_open_database(dbpath); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); map->cursor(map, NULL, &cursor, 0); data.data = &score; data.ulen = sizeof(int); data.flags = DB_DBT_USERMEM; while(cursor->get(cursor, &key, &data, DB_NEXT) == 0) { if(score != blacklist) cursor->del(cursor, 0); } cursor->close(cursor); map->close(map, 0); }
/* * Initializes the cursor. * * ARGUMENTS: * backend Pointer to the backend database. Shall have been * set by beOpen(). Shall not be NULL. * RETURNS * 0 Success. * EINVAL The backend database already has an active cursor. * EIO Backend database error. "log_start()" called. * ENOMEM System error. "log_start()" called. */ RegStatus beInitCursor( Backend* const backend) { RegStatus status; assert(NULL != backend); assert(NULL != backend->db); if (backend->cursor.dbCursor) { log_start("Cursor already active for backend database \"%s\"", getPath(backend->db)); status = EINVAL; } else { DB* db = backend->db; DBC* dbCursor; /* * Because this function is only used for reading, the Berkeley * cursor needn't be transactionally protected. */ if (status = db->cursor(db, NULL, &dbCursor, 0)) { log_add("Couldn't create cursor for database \"%s\"", getPath(db)); status = EIO; } else { (void)memset(&backend->cursor.key, 0, sizeof(DBT)); (void)memset(&backend->cursor.value, 0, sizeof(DBT)); backend->cursor.key.data = NULL; backend->cursor.value.data = NULL; backend->cursor.key.flags |= DB_DBT_REALLOC; backend->cursor.value.flags |= DB_DBT_REALLOC; backend->cursor.dbCursor = dbCursor; } /* "dbCursor" allocated */ } return status; }
/* {{{ rberkeley_db_cursor */ SEXP rberkeley_db_cursor (SEXP _dbp, SEXP _txnid, SEXP _flags) { DB *dbp; DBC *dbc; DB_TXN *txnid; int ret; u_int32_t flags = INTEGER(_flags)[0]; dbp = R_ExternalPtrAddr(_dbp); if(R_ExternalPtrTag(_dbp) != install("DB") || dbp == NULL) error("invalid 'db' handle"); if(!isNull(_txnid)) { txnid = R_ExternalPtrAddr(_txnid); } else txnid = NULL; ret = dbp->cursor(dbp, txnid, &dbc, flags); if(ret != 0) return ScalarInteger(ret); return R_MakeExternalPtr(dbc, install("DBC"), ScalarLogical(TRUE)); }
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); }
/*@ * 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); }
int b_curwalk(int argc, char *argv[]) { extern char *optarg; extern int optind, __db_getopt_reset; DB *dbp; DBTYPE type; DBC *dbc; 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, pagesize, walkflags; int ch, i, count, dupcount, j; int prev, ret, skipdupwalk, sorted, walkcount; char *ts, dbuf[32], kbuf[32]; type = DB_BTREE; cachesize = 10 * MEGABYTE; pagesize = 16 * 1024; count = 100000; dupcount = prev = skipdupwalk = sorted = 0; walkcount = 1000; ts = "Btree"; __db_getopt_reset = 1; while ((ch = getopt(argc, argv, "C:c:d:P:pSst:w:")) != EOF) switch (ch) { case 'C': cachesize = (u_int32_t)atoi(optarg); break; case 'c': count = atoi(optarg); break; case 'd': dupcount = atoi(optarg); break; case 'P': pagesize = (u_int32_t)atoi(optarg); break; case 'p': prev = 1; break; case 'S': skipdupwalk = 1; break; case 's': sorted = 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_curwalk_usage()); } break; case 'w': walkcount = atoi(optarg); break; case '?': default: return (b_curwalk_usage()); } argc -= optind; argv += optind; if (argc != 0) return (b_curwalk_usage()); /* * Queue and Recno don't support duplicates. */ #if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2) if (dupcount != 0 && (type == DB_QUEUE || type == DB_RECNO || type == DB_HEAP)) { fprintf(stderr, "b_curwalk: Queue, Recno and Heap don't support duplicates\n"); return (b_curwalk_usage()); } #else if (dupcount != 0 && (type == DB_QUEUE || type == DB_RECNO)) { fprintf(stderr, "b_curwalk: Queue and Recno don't support duplicates\n"); return (b_curwalk_usage()); } #endif #if DB_VERSION_MAJOR < 3 || DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0 #define DB_PREV_NODUP 0 /* * DB_PREV_NODUP wasn't available until after 3.0.55. * * For some reason, testing sorted duplicates doesn't work either. * I don't really care about 3.0.55 any more, just ignore it. */ 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); DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 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); /* Set duplicates flag. */ if (dupcount != 0) DB_BENCH_ASSERT( dbp->set_flags(dbp, sorted ? DB_DUPSORT : DB_DUP) == 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. */ data.data = dbuf; data.size = 20; if (type == DB_BTREE || type == DB_HASH) { key.size = 10; key.data = kbuf; for (i = 0; i < count; ++i) { (void)snprintf(kbuf, sizeof(kbuf), "%010d", i); for (j = 0; j <= dupcount; ++j) { (void)snprintf(dbuf, sizeof(dbuf), "%020d", j); 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); } walkflags = prev ? (skipdupwalk ? DB_PREV_NODUP : DB_PREV) : (skipdupwalk ? DB_NEXT_NODUP : DB_NEXT); /* Walk the cursor through the tree N times. */ TIMER_START; for (i = 0; i < walkcount; ++i) { DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &dbc, 0) == 0); while ((ret = dbc->c_get(dbc, &key, &data, walkflags)) == 0) ; DB_BENCH_ASSERT(ret == DB_NOTFOUND); DB_BENCH_ASSERT(dbc->c_close(dbc) == 0); } TIMER_STOP; printf("# %d %s %s cursor of %d 10/20 byte key/data items", walkcount, ts, prev ? (skipdupwalk ? "DB_PREV_NODUP" : "DB_PREV") : (skipdupwalk ? "DB_NEXT_NODUP" : "DB_NEXT"), count); if (dupcount != 0) printf(" with %d dups", dupcount); printf("\n"); /* * An "operation" is traversal of a single key/data pair -- not a * return of the key/data pair, since some versions of this test * skip duplicate key/data pairs. * * Use a "double" so we don't overflow. */ TIMER_DISPLAY((double)count * walkcount); DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0); return (EXIT_SUCCESS); }
int hdb_dn2id( Operation *op, struct berval *in, EntryInfo *ei, DB_TXN *txn, DBC **cursor ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; DB *db = bdb->bi_dn2id->bdi_db; DBT key, data; int rc = 0, nrlen; diskNode *d; char *ptr; unsigned char dlen[2]; ID idp, parentID; Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id(\"%s\")\n", in->bv_val, 0, 0 ); nrlen = dn_rdnlen( op->o_bd, in ); if (!nrlen) nrlen = in->bv_len; DBTzero(&key); key.size = sizeof(ID); key.data = &idp; key.ulen = sizeof(ID); key.flags = DB_DBT_USERMEM; parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0; BDB_ID2DISK( parentID, &idp ); DBTzero(&data); data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1; data.ulen = data.size * 3; data.dlen = data.ulen; data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL; rc = db->cursor( db, txn, cursor, bdb->bi_db_opflags ); if ( rc ) return rc; d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx ); d->nrdnlen[1] = nrlen & 0xff; d->nrdnlen[0] = (nrlen >> 8) | 0x80; dlen[0] = d->nrdnlen[0]; dlen[1] = d->nrdnlen[1]; ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen ); *ptr = '\0'; data.data = d; rc = (*cursor)->c_get( *cursor, &key, &data, DB_GET_BOTH_RANGE ); if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] || strncmp( d->nrdn, in->bv_val, nrlen ))) { rc = DB_NOTFOUND; } if ( rc == 0 ) { ptr = (char *) data.data + data.size - sizeof(ID); BDB_DISK2ID( ptr, &ei->bei_id ); ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen; ptr = d->nrdn + nrlen + 1; ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn ); if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) { db_recno_t dkids; /* How many children does the parent have? */ /* FIXME: do we need to lock the parent * entryinfo? Seems safe... */ (*cursor)->c_count( *cursor, &dkids, 0 ); ei->bei_parent->bei_dkids = dkids; } } op->o_tmpfree( d, op->o_tmpmemctx ); if( rc != 0 ) { Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: get failed: %s (%d)\n", db_strerror( rc ), rc, 0 ); } else { Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id: got id=0x%lx\n", ei->bei_id, 0, 0 ); } return rc; }
int hdb_dn2id_delete( Operation *op, DB_TXN *txn, EntryInfo *eip, Entry *e ) { struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private; DB *db = bdb->bi_dn2id->bdi_db; DBT key, data; DBC *cursor; diskNode *d; int rc; ID nid; unsigned char dlen[2]; Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n", e->e_id, e->e_ndn, 0 ); DBTzero(&key); key.size = sizeof(ID); key.ulen = key.size; key.flags = DB_DBT_USERMEM; BDB_ID2DISK( eip->bei_id, &nid ); DBTzero(&data); data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1; data.ulen = data.size; data.dlen = data.size; data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL; key.data = &nid; d = op->o_tmpalloc( data.size, op->o_tmpmemctx ); d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff; d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80; dlen[0] = d->nrdnlen[0]; dlen[1] = d->nrdnlen[1]; memcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val, BEI(e)->bei_nrdn.bv_len+1 ); data.data = d; rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags ); if ( rc ) goto func_leave; /* Delete our ID from the parent's list */ rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE ); if ( rc == 0 ) { if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] && !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val )) rc = cursor->c_del( cursor, 0 ); else rc = DB_NOTFOUND; } /* Delete our ID from the tree. With sorted duplicates, this * will leave any child nodes still hanging around. This is OK * for modrdn, which will add our info back in later. */ if ( rc == 0 ) { BDB_ID2DISK( e->e_id, &nid ); rc = cursor->c_get( cursor, &key, &data, DB_SET ); if ( rc == 0 ) rc = cursor->c_del( cursor, 0 ); } cursor->c_close( cursor ); func_leave: op->o_tmpfree( d, op->o_tmpmemctx ); /* Delete IDL cache entries */ if ( rc == 0 && bdb->bi_idl_cache_size ) { ID tmp[2]; char *ptr = ((char *)&tmp[1])-1; key.data = ptr; key.size = sizeof(ID)+1; tmp[1] = eip->bei_id; *ptr = DN_ONE_PREFIX; bdb_idl_cache_del_id( bdb, db, &key, e->e_id ); if ( eip ->bei_parent ) { *ptr = DN_SUBTREE_PREFIX; for (; eip && eip->bei_parent->bei_id; eip = eip->bei_parent) { tmp[1] = eip->bei_id; bdb_idl_cache_del_id( bdb, db, &key, e->e_id ); } /* Handle DB with empty suffix */ if ( !op->o_bd->be_suffix[0].bv_len && eip ) { tmp[1] = eip->bei_id; bdb_idl_cache_del_id( bdb, db, &key, e->e_id ); } } } Debug( LDAP_DEBUG_TRACE, "<= hdb_dn2id_delete 0x%lx: %d\n", e->e_id, rc, 0 ); return rc; }