void bdb_qlist_db_open(void){ int ret; DBC *cursorp = NULL; DB_TXN *txnp = NULL; /* Create queue.list db handle */ ret = db_create(&qlist_dbp, envp, 0); CHECK_DB_RET(ret); /* Open and Iterate */ ret = envp->txn_begin(envp, NULL, &txnp, 0); CHECK_DB_RET(ret); ret = qlist_dbp->open(qlist_dbp, txnp, "queue.list", NULL, DB_BTREE, DB_CREATE, 0664); CHECK_DB_RET(ret); ret = qlist_dbp->cursor(qlist_dbp, txnp, &cursorp, 0); CHECK_DB_RET(ret); DBT dbkey, dbdata; char qname[512]; qstats_t qs; BDB_CLEANUP_DBT(); memset(qname, 0, 512); memset(&qs, 0, sizeof(qs)); dbkey.data = (void *)qname; dbkey.ulen = 512; dbkey.flags = DB_DBT_USERMEM; dbdata.data = (void *)&qs; dbdata.ulen = sizeof(qs); dbdata.flags = DB_DBT_USERMEM; while ((ret = cursorp->get(cursorp, &dbkey, &dbdata, DB_NEXT)) == 0) { open_exsited_queue_db(txnp, qname, &qs); } if (ret != DB_NOTFOUND) { goto dberr; } ret = cursorp->close(cursorp); CHECK_DB_RET(ret); ret = txnp->commit(txnp, 0); CHECK_DB_RET(ret); return; dberr: if (cursorp != NULL){ cursorp->close(cursorp); } if (txnp != NULL){ txnp->abort(txnp); } fprintf(stderr, "bdb_qlist_db_open: %s\n", db_strerror(ret)); exit(EXIT_FAILURE); }
static int kvs_cursor_reset(WT_CURSOR *wtcursor) { CURSOR_SOURCE *cursor; DBC *dbc; WT_EXTENSION_API *wt_api; WT_SESSION *session; int ret = 0; session = wtcursor->session; cursor = (CURSOR_SOURCE *)wtcursor; wt_api = cursor->wt_api; /* Close and re-open the Berkeley DB cursor */ if ((dbc = cursor->dbc) != NULL) { cursor->dbc = NULL; if ((ret = dbc->close(dbc)) != 0) ERET(wt_api, session, WT_ERROR, "DbCursor.close: %s", db_strerror(ret)); if ((ret = cursor->db->cursor(cursor->db, NULL, &dbc, 0)) != 0) ERET(wt_api, session, WT_ERROR, "Db.cursor: %s", db_strerror(ret)); cursor->dbc = dbc; } return (0); }
/* * terane_Index_iter_segments: iterate through all segments. * * callspec: Index.iter_segments(txn) * parameters: * txn (Txn): A Txn object to wrap the operation in, or None * returns: a new Iter object. Each iteration returns a long representing the * segment id. * exceptions: * terane.outputs.store.backend.Error: A db error occurred when trying to create the iterator */ PyObject * terane_Index_iter_segments (terane_Index *self, PyObject *args) { terane_Txn *txn = NULL; DBC *cursor = NULL; PyObject *iter = NULL; terane_Iter_ops ops = { .next = _Index_next_segment }; int dbret; /* parse parameters */ if (!PyArg_ParseTuple (args, "O", &txn)) return NULL; if ((PyObject *) txn == Py_None) txn = NULL; if (txn && txn->ob_type != &terane_TxnType) return PyErr_Format (PyExc_TypeError, "txn must be a Txn or None"); /* create a new cursor */ dbret = self->segments->cursor (self->segments, txn? txn->txn : NULL, &cursor, 0); /* if cursor allocation failed, return Error */ if (dbret != 0) { PyErr_Format (terane_Exc_Error, "Failed to allocate segment cursor: %s", db_strerror (dbret)); return NULL; } iter = terane_Iter_new ((PyObject *) self, cursor, &ops, 0); if (iter == NULL) cursor->close (cursor); return iter; }
bool Bdb::put(void* data, uint dlen, void* key, uint klen) { DBT dbkey, dbval; memset(&dbkey, 0, sizeof(DBT)); memset(&dbval, 0, sizeof(DBT)); dbkey.data = key; dbkey.size = klen; dbval.data = data; dbval.size = dlen; if (dup) { DBC *cursor = NULL; DBT tmpval; memset(&tmpval, 0, sizeof(DBT)); int ret = d_ptr->db->cursor(d_ptr->db, NULL, &cursor, 0); if (ret) return false; ret = cursor->get(cursor, &dbkey, &tmpval, DB_SET); if (ret) { ret = d_ptr->db->put(d_ptr->db, NULL, &dbkey, &dbval, DB_OVERWRITE_DUP); } else { ret = cursor->put(cursor, &dbkey, &dbval, DB_AFTER); } cursor->close(cursor); return ret == 0; } int ret = d_ptr->db->put(d_ptr->db, NULL, &dbkey, &dbval, DB_OVERWRITE_DUP); return ret == 0; }
bool Bdb::get(void* key, uint klen, void** data, uint* pdlen) { DBT dbkey, dbval; memset(&dbkey, 0, sizeof(DBT)); memset(&dbval, 0, sizeof(DBT)); dbkey.data = key; dbkey.size = klen; dbval.flags = DB_DBT_MALLOC; int ret; // for each data DBC *cursor = NULL; ret = d_ptr->db->cursor(d_ptr->db, NULL, &cursor, 0); if (ret) { return false; } ret = cursor->get(cursor, &dbkey, &dbval, DB_SET); if (ret == DB_NOTFOUND) { return false; } *data = dbval.data; *pdlen = dbval.size; cursor->close(cursor); return true; }
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; }
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 print_elements_print(DB **dbp, DB_ENV **envp,FILE *fp){ int ret; DBT key, data; DBC *cursorp; (*dbp)->cursor(*dbp, NULL, &cursorp, 0); /* 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->get(cursorp, &key, &data, DB_NEXT)) == 0) { /* Do interesting things with the DBTs here. */ fprintf(fp,"%llu %llu\n",(unsigned long long int)*((uint64_t *)key.data),(unsigned long long int)((struct hash_value *)data.data)->cont); //printf("%llu %llu\n",(unsigned long long int)*((uint64_t *)key.data),(unsigned long long int)((struct hash_value *)data.data)->cont); } if (ret != DB_NOTFOUND) { /* Error handling goes here */ } if (cursorp != NULL) cursorp->close(cursorp); return 0; }
static void list_db(DB *dbp) { g_message("Listing database"); DBC *cursorp; dbp->cursor(dbp, NULL, &cursorp, 0); DBT key, data; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); int ret; while ((ret = cursorp->get(cursorp, &key, &data, DB_NEXT)) == 0) { g_debug("Listing: key.size=%i, strlen(key.data)=%i", key.size, (int)strlen(key.data)); g_assert(strlen(key.data) + 1 == key.size); g_message("Key=%s", (char*)key.data); Info *info = bab_info_unmarshall(key.data); bab_info_log("Unmarshalled", info); } if (ret != DB_NOTFOUND) { g_error("Got DB error"); } if (cursorp != NULL) { cursorp->close(cursorp); } }
/** @brief @return true (k1,k2) existed, remove success false (k1,k2) not existed, nothing done */ bool kmapdset_base::remove_impl(const void* k1, const void* k2, DB_TXN* txn, const char* func) { PortableDataOutput<AutoGrownMemIO> oKey1, oData; oData.resize(4*1024); save_key1(oKey1, k1); save_key2(oData, k2); DBT tk1; memset(&tk1, 0, sizeof(DBT)); tk1.data = oKey1.begin(); tk1.size = oKey1.tell(); DBT tdd; memset(&tdd, 0, sizeof(DBT)); tdd.data = oData.begin(); tdd.size = oData.tell(); DBC* curp = NULL; int ret = m_db->cursor(m_db, txn, &curp, 0); if (0 == ret) { ret = curp->get(curp, &tk1, &tdd, DB_GET_BOTH); if (0 == ret) { ret = curp->del(curp, 0); } curp->close(curp); return 0 == ret; } else { string_appender<> oss; oss << db_strerror(ret) << "... at: " << func << "\n" ; throw std::runtime_error(oss.str()); } }
bool Bdb::over(void* data, uint dlen, void* key, uint klen) { DBT dbkey, dbval; memset(&dbkey, 0, sizeof(DBT)); memset(&dbval, 0, sizeof(DBT)); dbkey.data = key; dbkey.size = klen; dbval.data = data; dbval.size = dlen; DBT tmpval; memset(&tmpval, 0, sizeof(DBT)); DBC* cursor = NULL; if (d_ptr->db->cursor(d_ptr->db, NULL, &cursor, 0)) return false; if (cursor->get(cursor, &dbkey, &tmpval, DB_SET)) { int ret = d_ptr->db->put(d_ptr->db, NULL, &dbkey, &dbval, DB_OVERWRITE_DUP); cursor->close(cursor); return ret == 0; } int ret = cursor->put(cursor, &dbkey, &dbval, DB_CURRENT); return ret == 0; }
/** @brief replace OR insert a record @note if not thrown an exception, always success @return true replace the record false insert the record @throws exception, failed */ bool kmapdset_base::replace_impl(const void* k1, const void* d, DB_TXN* txn, const char* func) { PortableDataOutput<AutoGrownMemIO> oKey1, oData; try { save_key1(oKey1, k1); save_data(oData, d); } catch (const IOException& exp) { string_appender<> oss; oss << exp.what() << "... at: " << func; throw std::runtime_error(oss.str()); } DBT tk1; memset(&tk1, 0, sizeof(DBT)); tk1.data = oKey1.begin(); tk1.size = oKey1.tell(); DBT tdd; memset(&tdd, 0, sizeof(DBT)); tdd.data = oData.begin(); tdd.size = oData.tell(); DBC* curp = NULL; int ret = m_db->cursor(m_db, txn, &curp, 0); if (0 == ret) { ret = curp->get(curp, &tk1, &tdd, DB_GET_BOTH); if (0 == ret) { tk1.data = oKey1.begin(); tk1.size = oKey1.tell(); tdd.data = oData.begin(); tdd.size = oData.tell(); ret = curp->put(curp, &tk1, &tdd, DB_CURRENT); curp->close(curp); if (0 == ret) return true; } else if (DB_NOTFOUND == ret) { ret = curp->put(curp, &tk1, &tdd, DB_NODUPDATA); curp->close(curp); if (0 == ret) return false; } } string_appender<> oss; oss << db_strerror(ret) << "... at: " << func << "\n" ; throw std::runtime_error(oss.str()); }
int gen_output(DB **dbpor,DB_ENV **envpor,DB **dbprint,DB_ENV **envprint){ //Iterate through original DB and insert in print DB int ret; DBT key, data; DBC *cursorp; (*dbpor)->cursor(*dbpor, NULL, &cursorp, 0); // 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->get(cursorp, &key, &data, DB_NEXT)) == 0) { //get hash from berkley db struct hash_value hvalue; uint64_t ndups = (unsigned long long int)((struct hash_value *)data.data)->cont; ndups=ndups-1; //char ndups[25]; //key //sprintf(ndups,"%llu",(unsigned long long int)((struct hash_value *)data.data)->cont); //see if entry already exists and //Insert new value in hash for print number_of_duplicates->numberof blocks int retprint = get_db_print(&ndups,&hvalue,dbprint,envprint); //if hash entry does not exist if(retprint == DB_NOTFOUND){ hvalue.cont=1; //insert into into the hashtable put_db_print(&ndups,&hvalue,dbprint,envprint); } else{ //increase counter hvalue.cont++; //insert counter in the right entry put_db_print(&ndups,&hvalue,dbprint,envprint); } } if (ret != DB_NOTFOUND) { perror("failed while iterating"); } if (cursorp != NULL) cursorp->close(cursorp); return 0; }
int store_list(const char *key, int (*add_file)(const char *, void *), void *opaque, struct acrd_txid *txid) { DBT db_key, db_data; int ret; DB_TXN *tid = NULL; DBC *cursor; if (txid) tid = txid->tid; ret = dbp->cursor(dbp, tid, &cursor, DB_DIRTY_READ); if (ret) { eprintf("failed open a cursor\n"); return ACRD_ERR_UNKNOWN; } memset(&db_key, 0, sizeof(db_key)); memset(&db_data, 0, sizeof(db_data)); if (key) { db_key.data = (void *)key; db_key.size = strlen(key) + 1; ret = cursor->c_get(cursor, &db_key, &db_data, DB_SET_RANGE); } else ret = cursor->c_get(cursor, &db_key, &db_data, DB_FIRST); while (ret == 0) { if (key && strncmp(db_key.data, key, strlen(key)) != 0) /* prefix doesn't match */ break; ret = add_file(db_key.data, opaque); if (ret != 0) break; ret = cursor->c_get(cursor, &db_key, &db_data, DB_NEXT); }; if (ret == 0 || ret == DB_NOTFOUND) ret = ACRD_SUCCESS; else { envp->err(envp, ret, "store_list failed\n"); ret = ACRD_ERR_UNKNOWN; } cursor->close(cursor); return ret; }
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 count_records(DB *dbp, DB_TXN *txn) { DBT key, value; 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(&value, 0, sizeof(DBT)); do { ret = cursorp->get(cursorp, &key, &value, 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); }
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); key_gen_teardown(&keyitem); }
void Bdb::_walk(BDBWalkFunc func) { DBC *cursor; DBT key, data; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); d_ptr->db->cursor(d_ptr->db, NULL, &cursor, 0); while (0 == (cursor->get(cursor, &key, &data, DB_NEXT))) { if (false == (*func)(&key, &data, cursor)) { break; } } cursor->close(cursor); }
/* {{{ rberkeley_dbcursor_close */ SEXP rberkeley_dbcursor_close (SEXP _dbc) { DBC *dbc; int ret; dbc = R_ExternalPtrAddr(_dbc); if(R_ExternalPtrTag(_dbc) != install("DBC") || dbc == NULL) error("invalid 'dbc' handle"); ret = dbc->close(dbc); if(ret == 0) { R_ClearExternalPtr(_dbc); } return ScalarInteger(ret); }
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; }
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); }
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); }
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); }
static void do_obj_list(void) { int rc; DBC *cur = NULL; DBT key, val; unsigned long count = 0; memset(&key, 0, sizeof(key)); memset(&val, 0, sizeof(val)); val.flags = DB_DBT_MALLOC; rc = tdb.objs->cursor(tdb.objs, NULL, &cur, 0); if (rc) { tdb.objs->err(tdb.objs, rc, "cursor create"); exit(1); } printf("bucket\towner\tmd5\tsize|addr\tn_str\n"); while (1) { struct db_obj_ent *obj; rc = cur->get(cur, &key, &val, DB_NEXT); if (rc) break; obj = val.data; print_obj(obj); count++; free(obj); } fprintf(stderr, "%lu records\n", count); cur->close(cur); }
size_t kmapdset_base::count_impl(const void* k1, DB_TXN* txn, const char* func) { PortableDataOutput<AutoGrownMemIO> oKey1; try { save_key1(oKey1, k1); } catch (const IOException& exp) { string_appender<> oss; oss << exp.what() << "... at: " << func; throw std::runtime_error(oss.str()); } DBT tk1; memset(&tk1, 0, sizeof(DBT)); tk1.data = oKey1.begin(); tk1.size = oKey1.tell(); DBT tdd; memset(&tdd, 0, sizeof(DBT)); DBC* curp = NULL; int ret = m_db->cursor(m_db, txn, &curp, 0); if (0 == ret) { ret = curp->get(curp, &tk1, &tdd, DB_SET); db_recno_t count = 0; if (0 == ret) ret = curp->count(curp, &count, 0); else if (DB_NOTFOUND == ret) count = 0, ret = 0; // clear error curp->close(curp); if (0 != ret) goto ErrorL; return count; } ErrorL: string_appender<> oss; oss << db_strerror(ret) << "... at: " << func << "\n" ; throw std::runtime_error(oss.str()); }
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); }
static void do_user_list(void) { int rc; DBC *cur = NULL; DBT key, val; unsigned long count = 0; memset(&key, 0, sizeof(key)); memset(&val, 0, sizeof(val)); val.flags = DB_DBT_MALLOC; rc = tdb.passwd->cursor(tdb.passwd, NULL, &cur, 0); if (rc) { tdb.passwd->err(tdb.passwd, rc, "cursor create"); exit(1); } printf("username\tpassword\n"); while (1) { rc = cur->get(cur, &key, &val, DB_NEXT); if (rc) break; printf("%s\t%s\n", (char *) key.data, (char *) val.data); count++; free(val.data); } fprintf(stderr, "%lu records\n", count); cur->close(cur); }
static void tags_cache_gc (struct tags_cache *c) { DBC *cur; DBT key; DBT serialized_cache_rec; int ret; char *last_referenced = NULL; time_t last_referenced_atime = time (NULL) + 1; int nitems = 0; c->db->cursor (c->db, NULL, &cur, 0); memset (&key, 0, sizeof(key)); memset (&serialized_cache_rec, 0, sizeof(serialized_cache_rec)); key.flags = DB_DBT_MALLOC; serialized_cache_rec.flags = DB_DBT_MALLOC; while (true) { struct cache_record rec; #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 6 ret = cur->c_get (cur, &key, &serialized_cache_rec, DB_NEXT); #else ret = cur->get (cur, &key, &serialized_cache_rec, DB_NEXT); #endif if (ret != 0) break; if (cache_record_deserialize (&rec, serialized_cache_rec.data, serialized_cache_rec.size, 1) && rec.atime < last_referenced_atime) { last_referenced_atime = rec.atime; if (last_referenced) free (last_referenced); last_referenced = (char *)xmalloc (key.size + 1); memcpy (last_referenced, key.data, key.size); last_referenced[key.size] = '\0'; } // TODO: remove objects with serialization error. nitems++; free (key.data); free (serialized_cache_rec.data); } if (ret != DB_NOTFOUND) logit ("Searching for element to remove failed (cursor): %s", db_strerror (ret)); #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 6 cur->c_close (cur); #else cur->close (cur); #endif debug ("Elements in cache: %d (limit %d)", nitems, c->max_items); if (last_referenced) { if (nitems >= c->max_items) tags_cache_remove_rec (c, last_referenced); free (last_referenced); } else debug ("Cache empty"); }
int list_keys(struct bdb_t *bdbp, void *start_key, size_t key_len, void **key_ptr, size_t *key_count) { (void) start_key; (void) key_len; DBC *cursor; int ret; if ((ret = bdbp->dbp->cursor(bdbp->dbp, NULL, &cursor, DB_CURSOR_BULK)) != 0) return ret; DBT key, value; memset(&key, 0, sizeof(DBT)); memset(&value, 0, sizeof(DBT)); key.flags = DB_DBT_MALLOC; key.data = start_key; if (start_key) key.size = key_len; // printf("start_key, key_len = %p %zu\n", start_key, key_len); value.flags = DB_DBT_REALLOC; size_t count = 0; for (ret = cursor->get(cursor, &key, &value, DB_SET_RANGE); ret != DB_NOTFOUND; ret = cursor->get(cursor, &key, &value, DB_NEXT)) { if (ret != 0) { for (size_t i = 0; i < count; i++) free(key_ptr[i]); goto err; } if (count >= *key_count) { free(key.data); break; } if (key.size == key_len) { if (start_key && memcmp(start_key, key.data, key_len) == 0) { free(key.data); continue; } key_ptr[count++] = key.data; } else { REPORT_ERR("invalid key size %"PRIu32", should be %zu", key.size, key_len); } } ret = 0; *key_count = count; err: cursor->close(cursor); free(value.data); return ret; }