void BDBFile::dumpDB() { cout << "\n\n\n===== dumping contents of " << bdbFileName << " =====\n" << endl; // We put a try block around this section of code // to ensure that our database is properly closed // in the event of an error. // try { // Acquire a cursor for the table. Dbc *dbcp; dbHandle->cursor(NULL, &dbcp, 0); // Walk through the table, printing the key/data pairs. Dbt key; Dbt data; int rtrnCode = 0; int count = 0; while ((rtrnCode = dbcp->get(&key, &data, DB_NEXT) == 0)) { char *key_string = (char *)key.get_data(); //char *data_string = (char *)data.get_data(); char* data_string = (char *)data.get_data(); cout << key_string << " : " << *(data_string) << "\n"; ++count; } if (rtrnCode > 0) dbHandle->err(rtrnCode, "BDBFile::dumpDB"); dbcp->close(); cout << "\n\n\n===== total of " << count << " record(s) =====\n" << endl; } catch (DbException &dbe) { cout << "BDBFile::dumpDB " << dbe.what() << "\n"; } }
void DatabaseManager::printEntireDB() { Dbc *cursor = NULL; try { Dbt dbKey; Dbt dbData; m_pimpl->checkTSSInit(); m_pimpl->m_database->cursor(&(**m_pimpl->m_transaction), &cursor, m_pimpl->m_cursorFlags); int ret = cursor->get(&dbKey, &dbData, DB_SET_RANGE); while(ret != DB_NOTFOUND) { StringPimpl dataString = StringPimpl((char *) dbData.get_data()); StringPimpl keyString = StringPimpl((char *) dbKey.get_data()); cout << "key :" << keyString.c_str() << endl << "data:" << dataString.c_str() << endl << endl; ret = cursor->get(&dbKey, &dbData, DB_NEXT); } cursor->close(); } catch (DbException &e) { std::cerr << "Error in deleteRecordRange: " << e.what() << e.get_errno() << std::endl; if(cursor != NULL) { cursor->close(); } } }
Header* MultiPartHeader::partDbFind(Db* pdb, quint16 partNo) { int ret = 0; Header* h = 0; Dbc *cursorp = 0; Dbt key; Dbt data; key.set_data(&multiPartKey); key.set_size(sizeof(quint64)); data.set_data(&partNo); data.set_size(sizeof(quint16)); // Get a cursor pdb->cursor(NULL, &cursorp, 0); // Position the cursor to the first record in the database whose // key matches the search key and whose data begins with the search // data. if (!(ret = cursorp->get(&key, &data, DB_GET_BOTH_RANGE))) { h = new Header((char*)data.get_data(), (char*)key.get_data()); } // Close the cursor if (cursorp != NULL) cursorp->close(); return h; }
bool FileRecord::RemoveLock(const std::string& lock_id, std::list<std::pair<std::string,std::string> >& ids) { if(!valid_) return false; Glib::Mutex::Lock lock(lock_); Dbc* cur = NULL; if(!dberr("removelock:cursor",db_lock_->cursor(NULL,&cur,DB_WRITECURSOR))) return false; Dbt key; Dbt data; make_string(lock_id,key); void* pkey = key.get_data(); if(!dberr("removelock:get1",cur->get(&key,&data,DB_SET))) { // TODO: handle errors ::free(pkey); cur->close(); return false; }; for(;;) { std::string id; std::string owner; uint32_t size = data.get_size(); void* buf = data.get_data(); buf = parse_string(id,buf,size); buf = parse_string(owner,buf,size); ids.push_back(std::pair<std::string,std::string>(id,owner)); if(!dberr("removelock:del",cur->del(0))) { ::free(pkey); cur->close(); return false; }; if(!dberr("removelock:get2",cur->get(&key,&data,DB_NEXT_DUP))) break; }; db_lock_->sync(0); ::free(pkey); cur->close(); return true; }
AntiCacheBlock AntiCacheDB::readBlockBerkeleyDB(std::string tableName, int16_t blockId) { Dbt key; key.set_data(&blockId); key.set_size(sizeof(int16_t)); Dbt value; value.set_flags(DB_DBT_MALLOC); VOLT_DEBUG("Reading evicted block with id %d", blockId); int ret_value = m_db->get(NULL, &key, &value, 0); if (ret_value != 0) { VOLT_ERROR("Invalid anti-cache blockId '%d' for table '%s'", blockId, tableName.c_str()); throw UnknownBlockAccessException(tableName, blockId); } else { // m_db->del(NULL, &key, 0); // if we have this the benchmark won't end assert(value.get_data() != NULL); } AntiCacheBlock block(blockId, static_cast<char*>(value.get_data()), value.get_size()); return (block); }
HeaderGroup* BulkHeaderGroup::getGroup(NewsGroup* ng, QString& articleIndex) { HeaderGroup *hg = 0; int ret; Dbt groupkey; Dbt groupdata; memset(&groupkey, 0, sizeof(groupkey)); memset(&groupdata, 0, sizeof(groupdata)); groupdata.set_flags(DB_DBT_MALLOC); QByteArray ba = articleIndex.toLocal8Bit(); const char *k= ba.constData(); groupkey.set_data((void*)k); groupkey.set_size(articleIndex.length()); Db* groupsDb = ng->getGroupingDb(); ret=groupsDb->get(NULL, &groupkey, &groupdata, 0); if (ret != 0) //key not found { qDebug() << "Failed to find group with key " << articleIndex; } else { qDebug() << "Found group with key " << articleIndex; hg=new HeaderGroup(articleIndex.length(), (char*)k, (char*)groupdata.get_data()); void* ptr = groupdata.get_data(); Q_FREE(ptr); } return hg; }
bool FileRecord::Modify(const std::string& id, const std::string& owner, const std::list<std::string>& meta) { if(!valid_) return false; Glib::Mutex::Lock lock(lock_); Dbt key; Dbt data; make_key(id,owner,key); void* pkey = key.get_data(); if(!dberr("Failed to retrieve record from database",db_rec_->get(NULL,&key,&data,0))) { ::free(pkey); return false; }; std::string uid; std::string id_tmp; std::string owner_tmp; std::list<std::string> meta_tmp; parse_record(uid,id_tmp,owner_tmp,meta_tmp,key,data); ::free(pkey); make_record(uid,id,owner,meta,key,data); if(!dberr("Failed to store record to database",db_rec_->put(NULL,&key,&data,0))) { ::free(key.get_data()); ::free(data.get_data()); return false; }; db_rec_->sync(0); ::free(key.get_data()); ::free(data.get_data()); return true; }
bool operator==(const Dbt&d1, const Dbt&d2) { if (d1.get_size() != d2.get_size()) return false; return memcmp(d1.get_data(), d2.get_data(), d2.get_size()) == 0; }
static int dbdump(const char *env_dir, const char *dbfile, const char *dbname) { int r; #if defined(USE_ENV) && USE_ENV DbEnv env(DB_CXX_NO_EXCEPTIONS); r = env.set_redzone(0); assert(r==0); r = env.open(env_dir, DB_INIT_MPOOL + DB_CREATE + DB_PRIVATE, 0777); assert(r == 0); Db db(&env, DB_CXX_NO_EXCEPTIONS); #else Db db(0, DB_CXX_NO_EXCEPTIONS); #endif r = db.open(0, dbfile, dbname, DB_UNKNOWN, 0, 0777); if (r != 0) { printf("cant open %s:%s %d:%s\n", dbfile, dbname, r, db_strerror(r)); #if defined(USE_ENV) && USE_ENV r = env.close(0); assert(r == 0); #endif return 1; } u_int32_t dbflags; r = db.get_flags(&dbflags); assert(r == 0); #ifndef TOKUDB if (dbflags & DB_DUP) printf("duplicates=1\n"); if (dbflags & DB_DUPSORT) printf("dupsort=1\n"); #endif #if 0 u_int32_t nodesize; r = db.get_nodesize(&nodesize); assert(r == 0); printf("nodesize=%d\n", nodesize); #endif Dbc *cursor; r = db.cursor(0, &cursor, 0); assert(r == 0); Dbt key; key.set_flags(DB_DBT_REALLOC); Dbt val; val.set_flags(DB_DBT_REALLOC); for (;;) { r = cursor->get(&key, &val, DB_NEXT); if (r != 0) break; // printf("%.*s\n", key.get_size(), (char *)key.get_data()); hexdump(&key); // printf("%.*s\n", val.get_size(), (char *)val.get_data()); hexdump(&val); } if (key.get_data()) toku_free(key.get_data()); if (val.get_data()) toku_free(val.get_data()); r = cursor->close(); assert(r == 0); r = db.close(0); assert(r == 0); #if defined(USE_ENV) && USE_ENV r = env.close(0); assert(r == 0); #endif return 0; }
vector<string> Freeze::EvictorIBase::allDbs() const { vector<string> result; try { Db db(_dbEnv->getEnv(), 0); // // Berkeley DB expects file paths to be UTF8 encoded. // db.open(0, nativeToUTF8(_filename, getProcessStringConverter()).c_str(), 0, DB_UNKNOWN, DB_RDONLY, 0); Dbc* dbc = 0; db.cursor(0, &dbc, 0); Dbt dbKey; dbKey.set_flags(DB_DBT_MALLOC); Dbt dbValue; dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL); bool more = true; while(more) { more = (dbc->get(&dbKey, &dbValue, DB_NEXT) == 0); if(more) { string dbName(static_cast<char*>(dbKey.get_data()), dbKey.get_size()); if(dbName.find(indexPrefix) != 0) { result.push_back(dbName); } free(dbKey.get_data()); } } dbc->close(); db.close(0); } catch(const DbException& dx) { if(dx.get_errno() != ENOENT) { DatabaseException ex(__FILE__, __LINE__); ex.message = dx.what(); throw ex; } } return result; }
void berkeleydb_store<Object>::get_range(const timestamp& from, const timestamp& to, const long lp_id, std::vector<obj_ptr>& ret_obj) { boost::lock_guard<boost::mutex> guard(get_mutex_); if (from > to) return; /* generate key */ std::vector<char> from_char; key_lpid_to_char(from, lp_id, from_char); Dbt db_key = Dbt(&from_char[0], from_char.size()); /* prepare data */ char* binary; Dbt data; data.set_data(binary); data.set_flags(DB_DBT_MALLOC); /* seek to from */ Dbc* cursorp; db->cursor(NULL, &cursorp, 0); int ret = cursorp->get(&db_key, &data, DB_SET_RANGE); if (ret) { if (cursorp != NULL) cursorp->close(); return; } std::string get_data_binary = std::string((char*) data.get_data(), data.get_size()); std::stringstream from_ss; from_ss << get_data_binary; boost::archive::binary_iarchive iar(from_ss); Object obj; iar >> obj; ret_obj.push_back(boost::make_shared<Object>(obj)); /* get until end */ while ((ret = cursorp->get(&db_key, &data, DB_NEXT)) == 0) { timestamp tmstmp; long id; char_to_key_lpid((char*) db_key.get_data(), tmstmp, id); if (tmstmp > to || id != lp_id) break; get_data_binary = std::string((char*) data.get_data(), data.get_size()); std::stringstream ss; ss << get_data_binary; boost::archive::binary_iarchive iar(ss); Object deserialized_obj; iar >> deserialized_obj; ret_obj.push_back(boost::make_shared<Object>(deserialized_obj)); } if (cursorp != NULL) cursorp->close(); };
bool DbMultipleDataIterator::next(Dbt &data) { if (*p_ == (u_int32_t)-1) { data.set_data(0); data.set_size(0); p_ = 0; } else { data.set_data(data_ + *p_--); data.set_size(*p_--); if (data.get_size() == 0 && data.get_data() == data_) data.set_data(0); } return (data.get_data() != 0); }
int main(int argc, char *argv[]) { try { Db *db = new Db(NULL, 0); db->open(NULL, "my.db", NULL, DB_BTREE, DB_CREATE, 0644); // populate our massive database. // all our strings include null for convenience. // Note we have to cast for idiomatic // usage, since newer gcc requires it. Dbt *keydbt = new Dbt((char *)"key", 4); Dbt *datadbt = new Dbt((char *)"data", 5); db->put(NULL, keydbt, datadbt, 0); // Now, retrieve. We could use keydbt over again, // but that wouldn't be typical in an application. Dbt *goodkeydbt = new Dbt((char *)"key", 4); Dbt *badkeydbt = new Dbt((char *)"badkey", 7); Dbt *resultdbt = new Dbt(); resultdbt->set_flags(DB_DBT_MALLOC); int ret; if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) { cout << "get: " << DbEnv::strerror(ret) << "\n"; } else { char *result = (char *)resultdbt->get_data(); cout << "got data: " << result << "\n"; } if ((ret = db->get(NULL, badkeydbt, resultdbt, 0)) != 0) { // We expect this... cout << "get using bad key: " << DbEnv::strerror(ret) << "\n"; } else { char *result = (char *)resultdbt->get_data(); cout << "*** got data using bad key!!: " << result << "\n"; } cout << "finished test\n"; } catch (DbException &dbe) { cerr << "Db Exception: " << dbe.what(); } return 0; }
void* BDBFile::readLastRec(void* key_, unsigned int keySize_) { assert(dbHandle != NULL); Dbt key; Dbt data; int rtrnCode = this->cursorRead(key, data, DB_LAST); if (rtrnCode > 0) { key_ = NULL; keySize_ = 0; return NULL; } else { key_ = key.get_data(); keySize_ = key.get_size(); return data.get_data(); } }
bool FileRecord::Remove(const std::string& id, const std::string& owner) { if(!valid_) return false; Glib::Mutex::Lock lock(lock_); Dbt key; Dbt data; make_key(id,owner,key); void* pkey = key.get_data(); if(dberr("",db_locked_->get(NULL,&key,&data,0))) { ::free(pkey); error_str_ = "Record has active locks"; return false; // have locks }; if(!dberr("Failed to retrieve record from database",db_rec_->get(NULL,&key,&data,0))) { ::free(pkey); return false; // No such record? }; std::string uid; std::string id_tmp; std::string owner_tmp; std::list<std::string> meta; parse_record(uid,id_tmp,owner_tmp,meta,key,data); if(!uid.empty()) { ::unlink(uid_to_path(uid).c_str()); // TODO: handle error }; if(!dberr("Failed to delete record from database",db_rec_->del(NULL,&key,0))) { // TODO: handle error ::free(pkey); return false; }; db_rec_->sync(0); ::free(pkey); return true; }
void MultiPartHeader::getAllArticleNums(Db* pDB, PartNumMap* serverArticleNos, QMap<quint16, quint64>* partSize,QMap<quint16, QString>* partMsgId) { quint64 multiPartKey = getMultiPartKey(); Header* h = 0; // Walk the headers to get the PartNumMap Dbc *cursorp = 0; // Get a cursor pDB->cursor(NULL, &cursorp, 0); // Set up our DBTs Dbt key(&multiPartKey, sizeof(quint64)); Dbt data; int ret = cursorp->get(&key, &data, DB_SET); while (ret != DB_NOTFOUND) { h = new Header((char*)data.get_data(), (char*)key.get_data()); serverArticleNos->insert(h->getPartNo(), h->getServerArticlemap()); partSize->insert(h->getPartNo(), h->getSize()); partMsgId->insert(h->getPartNo(), h->getMessageId()); Q_DELETE(h); ret = cursorp->get(&key, &data, DB_NEXT_DUP); } if (cursorp != NULL) cursorp->close(); }
void berkeleydb_store<Object>::get(const timestamp& time, const long lp_id, obj_ptr& ret) { boost::lock_guard<boost::mutex> guard(get_mutex_); /* generate key */ std::vector<char> key; key_lpid_to_char(time, lp_id, key); Dbt db_key(&key[0], key.size()); /* get data */ char* event_binary; Dbt data; data.set_data(event_binary); data.set_flags(DB_DBT_MALLOC); db->get(NULL, &db_key, &data, 0); /* deserialize the data */ std::string binary = std::string((char*) data.get_data(), data.get_size()); std::stringstream from_ss; from_ss << binary; boost::archive::binary_iarchive iar(from_ss); Object obj; iar >> obj; ret = boost::make_shared<Object>(obj); };
/* This function searches region name db according to relation ID * and put the region name and namespace strings into the last twqo arguments. * at the end of region name in parenthesis. BY default it is false. * Returns -1 or -2 for exceptions; * returns 0 if record is not found; * returns -3 for any other errors; * returns 1 if record is found and variables are assisgned successfully. */ int getRegionName(string dbHome, string rDbName, long rID_in, string & rName_out, string & space_out) { mydb rDB(dbHome, rDbName, DB_RDONLY); Dbc *cursorp; Dbt key(&rID_in, sizeof(rID_in)); Dbt data; long foo = -3; try { rDB.getDb().cursor(NULL, &cursorp, 0); int ret = cursorp->get(&key, &data, DB_SET); if (!ret) { regionRec rData(data.get_data()); rName_out = rData.getName(); space_out = rData.getNameSpace(); foo = 1; } else if (ret == DB_NOTFOUND) foo = 0; } catch(DbException &e) { rDB.getDb().err(e.get_errno(), "Error in getRegionName()"); foo = -1; } catch(exception &e) { rDB.getDb().errx("Error in getRegionName(): %s", e.what()); foo = -2; } cursorp->close(); return foo; }
void* BDBFile::readFirstRec(void* key_, unsigned int keySize_) { assert(dbHandle != NULL && dbCursor != NULL); Dbt key; Dbt data; int rtrnCode = dbCursor->get(&key, &data, DB_FIRST); if (rtrnCode > 0) { key_ = NULL; keySize_ = 0; return NULL; } else { key_ = key.get_data(); keySize_ = key.get_size(); return data.get_data(); } }
bool CDBStrMultiMap::get( const string &key, vector<string> &values ) { if( db_is_open == false ) { cout<<"==== Can not get items from BDB.BDB has not been opened."<<endl; return false; } char key1[1024]; memset(key1, 0, 1024); strcpy(key1, key.c_str()); Dbt dbt_key( key1, key.length() +1 ); try{ Dbc *dbcp; db->cursor(NULL, &dbcp, 0); Dbt data; while( dbcp->get(&dbt_key, &data, DB_SET ) == 0){ char *data_string = (char*)data.get_data(); values.push_back( data_string ); dbcp->del(0); } dbcp->close(); } catch( DbException &e) { cerr<<"!! Exception: "<<e.what()<<endl; return false; } return true; }
void test2() { int numberOfKeysToWrite= 10000; Db db(0,DB_CXX_NO_EXCEPTIONS); db.set_flags(DB_DUP); db.set_pagesize(512); int err= db.open(0, "test2.db", 0, DB_BTREE, DB_CREATE, 0); { int i= 0; int k= 0; Dbt key(&k,sizeof(k)); Dbt data(&i,sizeof(i)); for(;i<numberOfKeysToWrite;++i) { err= db.put(0,&key,&data,0); } } { Dbc *dbc; err= db.cursor(0,&dbc,0); char buffer[8192]; int numberOfKeysRead= 0; Dbt multikey(&numberOfKeysRead,sizeof(numberOfKeysRead)); Dbt multidata(&buffer,sizeof(buffer)); multidata.set_flags(DB_DBT_USERMEM); multidata.set_ulen(sizeof(buffer)); err= 0; while(err==0) { err= dbc->get(&multikey,&multidata,DB_NEXT|DB_MULTIPLE); if(err==0) { Dbt data; DbMultipleDataIterator i(multidata); while(err==0 && i.next(data)) { int actualData; memmove(&actualData, data.get_data(), sizeof(actualData)); if(numberOfKeysRead!=actualData) { std::cout << "Error: key/data mismatch. " << numberOfKeysRead << "!=" << actualData << std::endl; err= -1; } numberOfKeysRead++; } } else if(err!=DB_NOTFOUND) std::cout << "Error: dbc->get: " << db_strerror(err) << std::endl; } if(numberOfKeysRead!=numberOfKeysToWrite) { std::cout << "Error: key count mismatch. " << numberOfKeysRead << "!=" << numberOfKeysToWrite << std::endl; } dbc->close(); } db.close(0); }
void test3() { int numberOfKeysToWrite= 10000; Db db(0,DB_CXX_NO_EXCEPTIONS); db.set_pagesize(512); int err= db.open(0, "test3.db", 0, DB_RECNO, DB_CREATE, 0); { int i= 0; Dbt key; Dbt data(&i,sizeof(i)); for(;i<numberOfKeysToWrite;++i) { err= db.put(0,&key,&data,DB_APPEND); } } { Dbc *dbc; err= db.cursor(0,&dbc,0); char buffer[8192]; int numberOfKeysRead= 0; Dbt multikey(&numberOfKeysRead,sizeof(numberOfKeysRead)); Dbt multidata(&buffer,sizeof(buffer)); multidata.set_flags(DB_DBT_USERMEM); multidata.set_ulen(sizeof(buffer)); err= 0; while(err==0) { err= dbc->get(&multikey,&multidata,DB_NEXT|DB_MULTIPLE_KEY); if(err==0) { u_int32_t recno= 0; Dbt data; DbMultipleRecnoDataIterator i(multidata); while(err==0 && i.next(recno,data)) { int actualData= *((int*)data.get_data()); if(recno!=actualData+1) { std::cout << "Error: recno/data mismatch. " << recno << "!=" << actualData << "+1" << std::endl; err= -1; } numberOfKeysRead++; } } else if(err!=DB_NOTFOUND) std::cout << "Error: dbc->get: " << db_strerror(err) << std::endl; } if(numberOfKeysRead!=numberOfKeysToWrite) { std::cout << "Error: key count mismatch. " << numberOfKeysRead << "!=" << numberOfKeysToWrite << std::endl; } dbc->close(); } db.close(0); }
static void parse_record(std::string& uid, std::string& id, std::string& owner, std::list<std::string>& meta, const Dbt& key, const Dbt& data) { uint32_t size = 0; void* d = NULL; d = (void*)key.get_data(); size = (uint32_t)key.get_size(); d = parse_string(id,d,size); d = parse_string(owner,d,size); d = (void*)data.get_data(); size = (uint32_t)data.get_size(); d = parse_string(uid,d,size); for(;size;) { std::string s; d = parse_string(s,d,size); meta.push_back(s); }; }
std::string FileRecord::Add(std::string& id, const std::string& owner, const std::list<std::string>& meta) { if(!valid_) return ""; Glib::Mutex::Lock lock(lock_); Dbt key; Dbt data; std::string uid = rand_uid64().substr(4); make_record(uid,(id.empty())?uid:id,owner,meta,key,data); void* pkey = key.get_data(); void* pdata = data.get_data(); if(!dberr("Failed to add record to database",db_rec_->put(NULL,&key,&data,DB_NOOVERWRITE))) { ::free(pkey); ::free(pdata); return ""; }; db_rec_->sync(0); ::free(pkey); ::free(pdata); if(id.empty()) id = uid; return uid_to_path(uid); }
std::set<std::string> ClicDb::get(const std::string& usr) { Dbt key(const_cast<char*>(usr.c_str()), usr.size()); Dbt value; if (db.get(NULL, &key, &value, 0) == DB_NOTFOUND) return std::set<std::string>(); std::string str((char*)value.get_data(), value.get_size()); std::set<std::string> result; boost::algorithm::split(result, str, boost::algorithm::is_any_of("\t")); return result; }
void* BDBFile::readRec(void* key_, unsigned int keySize_) { assert(dbHandle != NULL); Dbt key(key_, keySize_); Dbt data; int rtrnCode = dbCursor->get(&key, &data, DB_SET); if (rtrnCode > 0) { return NULL; } else { return data.get_data(); } }
std::set<std::string> ClicDb::get(const std::string& usr) { Dbt key(const_cast<char*>(usr.c_str()), usr.size()); Dbt value; if (db.get(NULL, &key, &value, 0) == DB_NOTFOUND) return std::set<std::string>(); std::string str((char*)value.get_data(), value.get_size()); std::set<std::string> res; for (const auto &i : split(str, '\t')) res.insert(i); return res; }
void getWord(const std::string & word, unsigned int & good_count, unsigned int & bad_count) { Dbt key(const_cast<void *>(reinterpret_cast<const void *>(word.c_str())), word.size()); Dbt data; BayesDBData bdata; good_count = 0; bad_count = 0; if(db.get(NULL, &key, &data, 0) != DB_NOTFOUND) { int s = data.get_size(); if(s != sizeof(bdata)) throw std::runtime_error("Bayesian DB returned wrong record size"); void * vdata = data.get_data(); memcpy((void *)&bdata, vdata, s); good_count = bdata.good_edits; bad_count = bdata.bad_edits; } }
// Shows the records in the inventory database that // have a specific item name. For each inventory record // shown, the appropriate vendor record is also displayed. int show_item(MyDb &itemnameSDB, MyDb &vendorDB, std::string &itemName) { // Get a cursor to the itemname secondary db Dbc *cursorp; try { itemnameSDB.getDb().cursor(NULL, &cursorp, 0); // Get the search key. This is the name on the inventory // record that we want to examine. std::cout << "Looking for " << itemName << std::endl; Dbt key((void *)itemName.c_str(), (u_int32_t)itemName.length() + 1); Dbt data; // Position the cursor to the first record in the secondary // database that has the appropriate key. int ret = cursorp->get(&key, &data, DB_SET); if (!ret) { do { InventoryData inventoryItem(data.get_data()); inventoryItem.show(); show_vendor(vendorDB, inventoryItem.getVendor().c_str()); } while (cursorp->get(&key, &data, DB_NEXT_DUP) == 0); } else { std::cerr << "No records found for '" << itemName << "'" << std::endl; } } catch (DbException &e) { itemnameSDB.getDb().err(e.get_errno(), "Error in show_item"); cursorp->close(); throw e; } catch (std::exception &e) { itemnameSDB.getDb().errx("Error in show_item: %s", e.what()); cursorp->close(); throw e; } cursorp->close(); return (0); }
bool DbMultipleKeyDataIterator::next(Dbt &key, Dbt &data) { if (*p_ == (u_int32_t)-1) { key.set_data(0); key.set_size(0); data.set_data(0); data.set_size(0); p_ = 0; } else { key.set_data(data_ + *p_--); key.set_size(*p_--); data.set_data(data_ + *p_--); data.set_size(*p_--); } return (data.get_data() != 0); }