void bdb_close(void) { DB *db; DBC *dbc; DB_ENV *dbenv; dbc = g.dbc; db = g.bdb; dbenv = db->dbenv; assert(dbc->close(dbc) == 0); assert(db->close(db, 0) == 0); assert(dbenv->close(dbenv, 0) == 0); free(keybuf); keybuf = NULL; }
int Dbc::dup(Dbc** cursorp, u_int32_t _flags) { int ret; DBC *dbc = this; DBC *new_cursor = 0; ret = dbc->dup(dbc, &new_cursor, _flags); if (DB_RETOK_STD(ret)) // The following cast implies that Dbc can be no larger than DBC *cursorp = (Dbc*)new_cursor; else DB_ERROR(DbEnv::get_DbEnv(dbc->dbenv), "Dbc::dup", ret, ON_ERROR_UNKNOWN); return (ret); }
int bdb_last_id( BackendDB *be, DB_TXN *tid ) { struct bdb_info *bdb = (struct bdb_info *) be->be_private; int rc; ID id = 0; unsigned char idbuf[sizeof(ID)]; DBT key, data; DBC *cursor; DBTzero( &key ); key.flags = DB_DBT_USERMEM; key.data = (char *) idbuf; key.ulen = sizeof( idbuf ); DBTzero( &data ); data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL; /* Get a read cursor */ rc = bdb->bi_id2entry->bdi_db->cursor( bdb->bi_id2entry->bdi_db, tid, &cursor, 0 ); if (rc == 0) { rc = cursor->c_get(cursor, &key, &data, DB_LAST); cursor->c_close(cursor); } switch(rc) { case DB_NOTFOUND: rc = 0; break; case 0: BDB_DISK2ID( idbuf, &id ); break; default: Debug( LDAP_DEBUG_ANY, "=> bdb_last_id: get failed: %s (%d)\n", db_strerror(rc), rc, 0 ); goto done; } bdb->bi_lastid = id; done: return rc; }
bool Bigram::save_db(const char * dbfile){ DB * tmp_db = NULL; int ret = unlink(dbfile); if ( ret != 0 && errno != ENOENT) return false; ret = db_create(&tmp_db, NULL, 0); assert(ret == 0); if (NULL == tmp_db) return false; ret = tmp_db->open(tmp_db, NULL, dbfile, NULL, DB_HASH, DB_CREATE, 0600); if ( ret != 0 ) return false; DBC * cursorp = NULL; DBT key, data; /* Get a cursor */ m_db->cursor(m_db, NULL, &cursorp, 0); if (NULL == cursorp) return false; /* Initialize our DBTs. */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); /* Iterate over the database, retrieving each record in turn. */ while ((ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0) { int ret = tmp_db->put(tmp_db, NULL, &key, &data, 0); assert(ret == 0); } assert (ret == DB_NOTFOUND); /* Cursors must be closed */ if ( cursorp != NULL ) cursorp->c_close(cursorp); if ( tmp_db != NULL ) tmp_db->close(tmp_db, 0); return true; }
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 Dbc::put(Dbt* key, Dbt *data, u_int32_t flags_arg) { DBC *cursor = this; int err; if ((err = cursor->c_put(cursor, key, data, flags_arg)) != 0) { // DB_KEYEXIST is a "normal" return, so should not be // thrown as an error // if (err != DB_KEYEXIST) { DB_ERROR("Db::put", err, ON_ERROR_UNKNOWN); return (err); } } return (err); }
int Dbc::get(Dbt* key, Dbt *data, u_int32_t flags_arg) { DBC *cursor = this; int err; if ((err = cursor->c_get(cursor, key, data, flags_arg)) != 0) { // DB_NOTFOUND and DB_KEYEMPTY are "normal" returns, // so should not be thrown as an error // if (err != DB_NOTFOUND && err != DB_KEYEMPTY) { DB_ERROR("Db::get", err, ON_ERROR_UNKNOWN); return (err); } } return (err); }
int Dbc::del(u_int32_t flags_arg) { DBC *cursor = this; int err; if ((err = cursor->c_del(cursor, flags_arg)) != 0) { // DB_KEYEMPTY is a "normal" return, so should not be // thrown as an error // if (err != DB_KEYEMPTY) { DB_ERROR("Db::del", err, ON_ERROR_UNKNOWN); return (err); } } return (err); }
void bdb_update(const void *arg_key, size_t arg_key_size, const void *arg_value, size_t arg_value_size) { DBC *dbc = g.dbc; int ret; key.data = (void *)arg_key; key.size = (u_int32_t)arg_key_size; value.data = (void *)arg_value; value.size = (u_int32_t)arg_value_size; if ((ret = dbc->put(dbc, &key, &value, DB_KEYFIRST)) != 0) bdb_die(ret, "dbc.put: DB_KEYFIRST: {%.*s}{%.*s}", (int)key.size, (char *)key.data, (int)value.size, (char *)value.data); }
int count_records(DB *dbp, DB_TXN *txn) { DBT key, data; DBC *cursorp; int count, ret; cursorp = NULL; count = 0; /* Get the cursor */ ret = dbp->cursor(dbp, txn, &cursorp, DB_READ_UNCOMMITTED); if (ret != 0) { dbp->err(dbp, ret, "count_records: cursor open failed."); goto cursor_err; } /* Get the key DBT used for the database read */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); do { ret = cursorp->get(cursorp, &key, &data, DB_NEXT); switch (ret) { case 0: count++; break; case DB_NOTFOUND: break; default: dbp->err(dbp, ret, "Count records unspecified error"); goto cursor_err; } } while (ret == 0); cursor_err: if (cursorp != NULL) { ret = cursorp->close(cursorp); if (ret != 0) { dbp->err(dbp, ret, "count_records: cursor close failed."); } } return (count); }
static int dbiCursorPut(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags) { int rc = EINVAL; int sane = (key->data != NULL && key->size > 0 && data->data != NULL && data->size > 0); if (dbc && sane) { DBC * cursor = dbc->cursor; rpmdb rdb = dbc->dbi->dbi_rpmdb; rpmswEnter(&rdb->db_putops, (ssize_t) 0); rc = cursor->c_put(cursor, key, data, DB_KEYLAST); rc = cvtdberr(dbc->dbi, "dbcursor->c_put", rc, _debug); rpmswExit(&rdb->db_putops, (ssize_t) data->size); } return rc; }
int Dbc::get(Dbt* key, Dbt *data, u_int32_t _flags) { int ret; DBC *dbc = this; ret = dbc->c_get(dbc, key, data, _flags); if (!DB_RETOK_DBCGET(ret)) { if (ret == ENOMEM && DB_OVERFLOWED_DBT(key)) DB_ERROR_DBT("Dbc::get", key, ON_ERROR_UNKNOWN); else if (ret == ENOMEM && DB_OVERFLOWED_DBT(data)) DB_ERROR_DBT("Dbc::get", data, ON_ERROR_UNKNOWN); else DB_ERROR("Dbc::get", ret, ON_ERROR_UNKNOWN); } return (ret); }
/* * Sets a cursor. * * Arguments: * cursor Pointer to the cursor. Shall not be NULL. * mode The mode for setting the cursor. One of DB_SET_RANGE * or DB_NEXT. * Returns: * 0 Success. "*cursor" is set. * ENOENT No such entry. * EIO Backend database error. "log_start()" called. */ static RegStatus setCursor( Cursor* const cursor, int mode) { DBC* dbCursor = cursor->dbCursor; DBT* keyDbt = &cursor->key; DBT* valueDbt = &cursor->value; int status = dbCursor->get(dbCursor, keyDbt, valueDbt, mode); if (DB_NOTFOUND == status) { status = ENOENT; } else if (0 != status) { status = EIO; } return status; }
void DBBase::dump (QString &d) { QFile outFile(d); if (! outFile.open(QIODevice::WriteOnly)) return; Q3TextStream outStream(&outFile); DBT key, data; DBC *cur; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); db->cursor(db, NULL, &cur, 0); while (! cur->c_get(cur, &key, &data, DB_NEXT)) outStream << (char *) key.data << "=" << (char *) data.data << "\n"; cur->c_close(cur); outFile.close(); }
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; }
int Dbc::pget(Dbt* key, Dbt *pkey, Dbt *data, u_int32_t _flags) { int ret; DBC *dbc = this; ret = dbc->c_pget(dbc, key, pkey, data, _flags); /* Logic is the same as for Dbc::get - reusing macro. */ if (!DB_RETOK_DBCGET(ret)) { if (ret == ENOMEM && DB_OVERFLOWED_DBT(key)) DB_ERROR_DBT("Dbc::pget", key, ON_ERROR_UNKNOWN); else if (ret == ENOMEM && DB_OVERFLOWED_DBT(data)) DB_ERROR_DBT("Dbc::pget", data, ON_ERROR_UNKNOWN); else DB_ERROR("Dbc::pget", ret, ON_ERROR_UNKNOWN); } return (ret); }
static int kvs_cursor_remove(WT_CURSOR *wtcursor) { CURSOR_SOURCE *cursor; DBC *dbc; DBT *key, *value; WT_EXTENSION_API *wt_api; WT_SESSION *session; int ret = 0; session = wtcursor->session; cursor = (CURSOR_SOURCE *)wtcursor; wt_api = cursor->wt_api; dbc = cursor->dbc; key = &cursor->key; value = &cursor->value; /* * WiredTiger's "remove" of a bitfield is really an update with a value * of a single byte of zero. */ if (cursor->config_bitfield) { wtcursor->value.size = 1; wtcursor->value.data = "\0"; return (kvs_cursor_update(wtcursor)); } if ((ret = copyin_key(wtcursor)) != 0) return (ret); if ((ret = dbc->get(dbc, key, value, DB_SET)) != 0) { if (ret == DB_NOTFOUND || ret == DB_KEYEMPTY) return (WT_NOTFOUND); ERET(wt_api, session, WT_ERROR, "DbCursor.get: %s", db_strerror(ret)); } if ((ret = dbc->del(dbc, 0)) != 0) ERET(wt_api, session, WT_ERROR, "DbCursor.del: %s", db_strerror(ret)); return (0); }
void DbPlugin::getLastBar (Bar &bar) { DBT key, data; DBC *cur; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); DBBar dbbar; memset(&dbbar, 0, sizeof(DBBar)); data.data = &dbbar; data.ulen = sizeof(DBBar); data.flags = DB_DBT_USERMEM; db->cursor(db, NULL, &cur, 0); cur->c_get(cur, &key, &data, DB_PREV); QString k = (char *) key.data; getBar(dbbar, k, bar); cur->c_close(cur); }
void DbPlugin::dump (QString &d, bool f) { QFile outFile(d); if (! outFile.open(IO_WriteOnly)) return; QTextStream outStream(&outFile); DBT key, data; DBC *cur; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); DBBar dbbar; memset(&dbbar, 0, sizeof(DBBar)); data.data = &dbbar; data.ulen = sizeof(DBBar); data.flags = DB_DBT_USERMEM; db->cursor(db, NULL, &cur, 0); QFileInfo fi(symbol); while (! cur->c_get(cur, &key, &data, DB_NEXT)) { if (f) { outStream << fi.fileName() << "," << (char *) key.data << "," << QString::number(dbbar.open) << "," << QString::number(dbbar.high) << "," << QString::number(dbbar.low) << "," << QString::number(dbbar.close) << "," << QString::number(dbbar.volume, 'f', 0) << "," << QString::number(dbbar.oi) << "\n"; } else { outStream << (char *) key.data << "=" << QString::number(dbbar.open) << "," << QString::number(dbbar.high) << "," << QString::number(dbbar.low) << "," << QString::number(dbbar.close) << "," << QString::number(dbbar.volume, 'f', 0) << "," << QString::number(dbbar.oi) << "\n"; } } cur->c_close(cur); outFile.close(); }
static void do_acl_list(void) { int rc; DBC *cur = NULL; DBT key, val; unsigned long count = 0; struct db_acl_ent *ent; memset(&key, 0, sizeof(key)); memset(&val, 0, sizeof(val)); val.flags = DB_DBT_MALLOC; rc = tdb.acls->cursor(tdb.acls, NULL, &cur, 0); if (rc) { tdb.acls->err(tdb.acls, rc, "cursor create"); exit(1); } printf("bucket\tgrantee\tperm\tkey\n"); while (1) { rc = cur->get(cur, &key, &val, DB_NEXT); if (rc) break; ent = val.data; printf("%s\t%s\t%s\t%s\n", ent->bucket, ent->grantee, ent->perm, ent->key); count++; free(ent); } fprintf(stderr, "%lu records\n", count); cur->close(cur); }
void bdb_remove(uint64_t keyno, int *notfoundp) { DBC *dbc = g.dbc; int ret; key.data = keybuf; key_gen(key.data, &key.size, keyno, 0); bdb_read(keyno, &value.data, &value.size, notfoundp); if (*notfoundp) return; if ((ret = dbc->del(dbc, 0)) != 0) { if (ret != DB_NOTFOUND) die(ret, "dbc.del: {%.*s}", (int)key.size, (char *)key.data); *notfoundp = 1; } }
static int count_blocks(DB *sdb){ DBC *cursor; DBT key, data; db_recno_t dup_count; char *filename = "big_blocks.txt"; FILE *fp; memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); fp = fopen(filename, "w"); sdb->cursor(sdb, NULL, &cursor, 0); while(DB_NOTFOUND != cursor->get(cursor, &key, &data, DB_NEXT_NODUP)){ cursor->count(cursor, &dup_count, 0); if((int)dup_count > 500) fprintf(fp, "%s, %lu\n", (char*)key.data, (size_t)dup_count); } fclose(fp); return 0; }
/* {{{ rberkeley_dbcursor_count */ SEXP rberkeley_dbcursor_count (SEXP _dbc, SEXP _flags) { DBC *dbc; db_recno_t countp; u_int32_t flags; int ret; flags = (u_int32_t)INTEGER(_flags)[0]; /* unused by API, set to 0L */ dbc = R_ExternalPtrAddr(_dbc); if(R_ExternalPtrTag(_dbc) != install("DBC") || dbc == NULL) error("invalid 'dbc' handle"); ret = dbc->count(dbc, &countp, flags); if(ret != 0) return ScalarInteger(ret); return ScalarInteger((u_int32_t)countp); }
void bdb_read(uint64_t keyno, void *valuep, uint32_t *valuesizep, int *notfoundp) { DBC *dbc = g.dbc; int ret; key.data = keybuf; key_gen(key.data, &key.size, keyno, 0); *notfoundp = 0; if ((ret = dbc->get(dbc, &key, &value, DB_SET)) != 0) { if (ret != DB_NOTFOUND) die(ret, "dbc.get: DB_SET: {%.*s}", (int)key.size, (char *)key.data); *notfoundp = 1; } else { *(void **)valuep = value.data; *valuesizep = value.size; } }
static void do_bucket_list(void) { int rc; DBC *cur = NULL; DBT key, val; unsigned long count = 0; struct db_bucket_ent *ent; memset(&key, 0, sizeof(key)); memset(&val, 0, sizeof(val)); val.flags = DB_DBT_MALLOC; rc = tdb.buckets->cursor(tdb.buckets, NULL, &cur, 0); if (rc) { tdb.buckets->err(tdb.buckets, rc, "cursor create"); exit(1); } printf("name\towner\ttime_created\n"); while (1) { rc = cur->get(cur, &key, &val, DB_NEXT); if (rc) break; ent = val.data; printf("%s\t%s\t%llu\n", ent->name, ent->owner, (unsigned long long) GUINT64_FROM_LE(ent->time_create)); count++; free(ent); } fprintf(stderr, "%lu records\n", count); cur->close(cur); }
static void do_obj_cnt(void) { DBC *cur = NULL; DBT key, val; uint64_t cntbuf; /* LE */ uint64_t objcount; /* Host order */ int rc; rc = tdb.oids->cursor(tdb.oids, NULL, &cur, 0); if (rc) { tdb.oids->err(tdb.oids, rc, "cursor create"); exit(1); } memset(&key, 0, sizeof(key)); memset(&val, 0, sizeof(val)); val.flags = DB_DBT_USERMEM; val.data = &cntbuf; val.ulen = sizeof(uint64_t); /* read existing counter, if any */ rc = cur->get(cur, &key, &val, DB_NEXT); if (rc == DB_NOTFOUND) { printf("-\n"); } else if (rc) { printf("\n"); fprintf(stderr, "objid get error %d\n", rc); exit(1); } else { if (val.size != sizeof(uint64_t)) { printf("\n"); fprintf(stderr, "objid_init got size %d\n", val.size); exit(1); } objcount = GUINT64_FROM_LE(*(uint64_t *)val.data); printf("%llu\n", (unsigned long long) objcount); } cur->close(cur); }
int Dbc::get(Dbt* key, Dbt *data, u_int32_t _flags) { int ret; DBC *dbc = this; ret = dbc->get(dbc, key, data, _flags); if (!DB_RETOK_DBCGET(ret)) { if (ret == DB_BUFFER_SMALL && DB_OVERFLOWED_DBT(key)) DB_ERROR_DBT(DbEnv::get_DbEnv(dbc->dbenv), "Dbc::get", key, ON_ERROR_UNKNOWN); else if (ret == DB_BUFFER_SMALL && DB_OVERFLOWED_DBT(data)) DB_ERROR_DBT(DbEnv::get_DbEnv(dbc->dbenv), "Dbc::get", data, ON_ERROR_UNKNOWN); else DB_ERROR(DbEnv::get_DbEnv(dbc->dbenv), "Dbc::get", ret, ON_ERROR_UNKNOWN); } return (ret); }
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); }
local void walk_db(int (*func)(DBT *key, DBT *val), int write) { DBC *cursor; DBT key, val; if (db->cursor(db, NULL, &cursor, write ? DB_WRITECURSOR : 0)) { if (write) fputs("couldn't get db cursor for writing. is the db read-only?\n", stderr); else fputs("couldn't get db cursor.\n", stderr); return; } memset(&key, 0, sizeof(key)); memset(&val, 0, sizeof(val)); while (cursor->c_get(cursor, &key, &val, DB_NEXT) == 0) if (func(&key, &val)) cursor->c_del(cursor, 0); cursor->c_close(cursor); }
void bdb_update(const void *arg_key, uint32_t arg_key_size, const void *arg_value, uint32_t arg_value_size, int *notfoundp) { DBC *dbc = g.dbc; int ret; key.data = (void *)arg_key; key.size = arg_key_size; value.data = (void *)arg_value; value.size = arg_value_size; *notfoundp = 0; if ((ret = dbc->put(dbc, &key, &value, DB_KEYFIRST)) != 0) { if (ret != DB_NOTFOUND) { die(ret, "dbc.put: DB_KEYFIRST: {%.*s}{%.*s}", (int)key.size, (char *)key.data, (int)value.size, (char *)value.data); } *notfoundp = 1; } }