本文整理汇总了C++中Dbc类的典型用法代码示例。如果您正苦于以下问题:C++ Dbc类的具体用法?C++ Dbc怎么用?C++ Dbc使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dbc类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: db
void TestKeyRange::run()
{
// Remove the previous database.
(void)unlink(FileName);
// Create the database object.
// There is no environment for this simple example.
Db db(0, 0);
db.set_error_stream(&cerr);
db.set_errpfx("TestKeyRange");
db.set_pagesize(1024); /* Page size: 1K. */
db.set_cachesize(0, 32 * 1024, 0);
db.open(NULL, FileName, NULL, DB_BTREE, DB_CREATE, 0664);
//
// Insert records into the database, where the key is the user
// input and the data is the user input in reverse order.
//
char buf[1024];
char rbuf[1024];
char *t;
char *p;
int ret;
int len;
Dbt *firstkey = NULL;
char firstbuf[1024];
for (;;) {
cout << "input>";
cout.flush();
cin.getline(buf, sizeof(buf));
if (cin.eof())
break;
if ((len = strlen(buf)) <= 0)
continue;
for (t = rbuf, p = buf + (len - 1); p >= buf;)
*t++ = *p--;
*t++ = '\0';
Dbt key(buf, len + 1);
Dbt data(rbuf, len + 1);
if (firstkey == NULL) {
strcpy(firstbuf, buf);
firstkey = new Dbt(firstbuf, len + 1);
}
ret = db.put(0, &key, &data, DB_NOOVERWRITE);
if (ret == DB_KEYEXIST) {
cout << "Key " << buf << " already exists.\n";
}
cout << "\n";
}
// 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;
db.cursor(NULL, &dbcp, 0);
/*ADDED...*/
DB_KEY_RANGE range;
memset(&range, 0, sizeof(range));
db.key_range(NULL, firstkey, &range, 0);
printf("less: %f\n", range.less);
printf("equal: %f\n", range.equal);
printf("greater: %f\n", range.greater);
/*end ADDED*/
Dbt key;
Dbt data;
// Walk through the table, printing the key/data pairs.
while (dbcp->get(&key, &data, DB_NEXT) == 0) {
char *key_string = (char *)key.get_data();
char *data_string = (char *)data.get_data();
cout << key_string << " : " << data_string << "\n";
}
dbcp->close();
}
catch (DbException &dbe) {
cerr << "TestKeyRange: " << dbe.what() << "\n";
}
db.close(0);
}
示例2: VisitBackward
bool Table::VisitBackward(TableVisitor &tv)
{
ByteString bsKey, bsValue;
Dbc* cursor = NULL;
bool ret = true;
u_int32_t flags = DB_PREV;
// TODO call tv.OnComplete() or error handling
if (db->cursor(NULL, &cursor, 0) != 0)
return false;
Dbt key, value;
if (tv.GetStartKey() && tv.GetStartKey()->length > 0)
{
key.set_data(tv.GetStartKey()->buffer);
key.set_size(tv.GetStartKey()->length);
flags = DB_SET_RANGE;
// as DB_SET_RANGE finds the smallest key greater than or equal to the
// specified key, in order to visit the database backwards, move to the
// first matching elem, then move backwards
if (cursor->get(&key, &value, flags) != 0)
{
// end of database
cursor->close();
if (db->cursor(NULL, &cursor, 0) != 0)
return false;
}
else
{
// if there is a match, call the acceptor, otherwise move to the
// previous elem in the database
if (memcmp(tv.GetStartKey()->buffer, key.get_data(),
MIN(tv.GetStartKey()->length, key.get_size())) == 0)
{
bsKey.size = key.get_size();
bsKey.length = key.get_size();
bsKey.buffer = (char*) key.get_data();
bsValue.size = value.get_size();
bsValue.length = value.get_size();
bsValue.buffer = (char*) value.get_data();
ret = tv.Accept(bsKey, bsValue);
}
}
}
flags = DB_PREV;
while (ret && cursor->get(&key, &value, flags) == 0)
{
bsKey.size = key.get_size();
bsKey.length = key.get_size();
bsKey.buffer = (char*) key.get_data();
bsValue.size = value.get_size();
bsValue.length = value.get_size();
bsValue.buffer = (char*) value.get_data();
ret = tv.Accept(bsKey, bsValue);
if (!ret)
break;
flags = DB_PREV;
}
cursor->close();
tv.OnComplete();
return ret;
}
示例3: while
bool CDB::Rewrite(const string& strFile, const char* pszSkip)
{
while (true)
{
{
LOCK(bitdb.cs_db);
if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0)
{
// Flush log data to the dat file
bitdb.CloseDb(strFile);
bitdb.CheckpointLSN(strFile);
bitdb.mapFileUseCount.erase(strFile);
bool fSuccess = true;
printf("Rewriting %s...\n", strFile.c_str());
string strFileRes = strFile + ".rewrite";
{ // surround usage of db with extra {}
CDB db(strFile.c_str(), "r");
Db* pdbCopy = new Db(&bitdb.dbenv, 0);
int ret = pdbCopy->open(NULL, // Txn pointer
strFileRes.c_str(), // Filename
"main", // Logical db name
DB_BTREE, // Database type
DB_CREATE, // Flags
0);
if (ret > 0)
{
printf("Cannot create database file %s\n", strFileRes.c_str());
fSuccess = false;
}
Dbc* pcursor = db.GetCursor();
if (pcursor)
while (fSuccess)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT);
if (ret == DB_NOTFOUND)
{
pcursor->close();
break;
}
else if (ret != 0)
{
pcursor->close();
fSuccess = false;
break;
}
if (pszSkip &&
strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
continue;
if (strncmp(&ssKey[0], "\x07version", 8) == 0)
{
// Update version:
ssValue.clear();
ssValue << CLIENT_VERSION;
}
Dbt datKey(&ssKey[0], ssKey.size());
Dbt datValue(&ssValue[0], ssValue.size());
int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE);
if (ret2 > 0)
fSuccess = false;
}
if (fSuccess)
{
db.Close();
bitdb.CloseDb(strFile);
if (pdbCopy->close(0))
fSuccess = false;
delete pdbCopy;
}
}
if (fSuccess)
{
Db dbA(&bitdb.dbenv, 0);
if (dbA.remove(strFile.c_str(), NULL, 0))
fSuccess = false;
Db dbB(&bitdb.dbenv, 0);
if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
fSuccess = false;
}
if (!fSuccess)
printf("Rewriting of %s FAILED!\n", strFileRes.c_str());
return fSuccess;
}
}
MilliSleep(100);
}
return false;
}
示例4: transformDb
static void
transformDb(bool evictor, const Ice::CommunicatorPtr& communicator,
const FreezeScript::ObjectFactoryPtr& objectFactory,
DbEnv& dbEnv, DbEnv& dbEnvNew, const string& dbName,
const Freeze::ConnectionPtr& connectionNew, vector<Db*>& dbs,
const Slice::UnitPtr& oldUnit, const Slice::UnitPtr& newUnit,
DbTxn* txnNew, bool purgeObjects, bool suppress, string descriptors)
{
if(evictor)
{
//
// The evictor database file contains multiple databases. We must first
// determine the names of those databases, ignoring any whose names
// begin with "$index:". Each database represents a separate facet, with
// the facet name used as the database name. The database named "$default"
// represents the main object.
//
vector<string> dbNames;
{
Db db(&dbEnv, 0);
db.open(0, dbName.c_str(), 0, DB_UNKNOWN, DB_RDONLY, 0);
Dbt dbKey, dbValue;
dbKey.set_flags(DB_DBT_MALLOC);
dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL);
Dbc* dbc = 0;
db.cursor(0, &dbc, 0);
while(dbc->get(&dbKey, &dbValue, DB_NEXT) == 0)
{
string s(static_cast<char*>(dbKey.get_data()), dbKey.get_size());
if(s.find("$index:") != 0)
{
dbNames.push_back(s);
}
free(dbKey.get_data());
}
dbc->close();
db.close(0);
}
//
// Transform each database. We must delay closing the new databases
// until after the transaction is committed or aborted.
//
for(vector<string>::iterator p = dbNames.begin(); p != dbNames.end(); ++p)
{
string name = p->c_str();
Db db(&dbEnv, 0);
db.open(0, dbName.c_str(), name.c_str(), DB_BTREE, DB_RDONLY, FREEZE_SCRIPT_DB_MODE);
Db* dbNew = new Db(&dbEnvNew, 0);
dbs.push_back(dbNew);
dbNew->open(txnNew, dbName.c_str(), name.c_str(), DB_BTREE, DB_CREATE | DB_EXCL, FREEZE_SCRIPT_DB_MODE);
//
// Execute the transformation descriptors.
//
istringstream istr(descriptors);
string facet = (name == "$default" ? string("") : name);
FreezeScript::transformDatabase(communicator, objectFactory, oldUnit, newUnit, &db, dbNew, txnNew, 0,
dbName, facet, purgeObjects, cerr, suppress, istr);
db.close(0);
}
Freeze::Catalog catalogNew(connectionNew, Freeze::catalogName());
Freeze::CatalogData catalogData = { true, "::Ice::Identity", "Object" };
catalogNew.put(Freeze::Catalog::value_type(dbName, catalogData));
}
else
{
//
// Transform a map database.
//
Db db(&dbEnv, 0);
db.open(0, dbName.c_str(), 0, DB_BTREE, DB_RDONLY, FREEZE_SCRIPT_DB_MODE);
Db* dbNew = new Db(&dbEnvNew, 0);
dbs.push_back(dbNew);
dbNew->open(txnNew, dbName.c_str(), 0, DB_BTREE, DB_CREATE | DB_EXCL, FREEZE_SCRIPT_DB_MODE);
//
// Execute the transformation descriptors.
//
istringstream istr(descriptors);
FreezeScript::transformDatabase(communicator, objectFactory, oldUnit, newUnit, &db, dbNew, txnNew,
connectionNew, dbName, "", purgeObjects, cerr, suppress, istr);
db.close(0);
}
}
示例5: test1
void test1()
{
int numberOfKeysToWrite= 10000;
Db db(0,DB_CXX_NO_EXCEPTIONS);
db.set_pagesize(512);
int err= db.open(0, "test1.db", 0, DB_BTREE, DB_CREATE, 0);
{
int i= 0;
Dbt key(&i,sizeof(i));
Dbt data(&i,sizeof(i));
for(;i<numberOfKeysToWrite;++i)
{
db.put(0,&key,&data,0);
}
}
{
Dbc *dbc;
err= db.cursor(0,&dbc,0);
char *check= (char*)calloc(numberOfKeysToWrite,1);
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)
{
Dbt key, data;
DbMultipleKeyDataIterator i(multidata);
while(err==0 && i.next(key,data))
{
int actualKey;
int actualData;
memmove(&actualKey, key.get_data(), sizeof(actualKey));
memmove(&actualData, data.get_data(), sizeof(actualData));
if(actualKey!=actualData)
{
std::cout << "Error: key/data mismatch. " << actualKey << "!=" << actualData << std::endl;
err= -1;
}
else
{
check[actualKey]++;
}
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;
}
for(int n=0;n<numberOfKeysToWrite;++n)
{
if(check[n]!=1)
{
std::cout << "Error: key " << n << " was written to the database, but not read back." << std::endl;
}
}
free(check);
dbc->close();
}
db.close(0);
}
示例6: loadData
void QueueScheduler::loadData(Db* _schedDb, bool createStartup)
{
Dbc *cursor = 0;
Dbt key, data;
uchar *keymem=new uchar[KEYMEM_SIZE];
uchar *datamem=new uchar[DATAMEM_SIZE];
key.set_flags(DB_DBT_USERMEM);
key.set_data(keymem);
key.set_ulen(KEYMEM_SIZE);
data.set_flags(DB_DBT_USERMEM);
data.set_ulen(DATAMEM_SIZE);
data.set_data(datamem);
int maxId = 0;
int ret = 0;
schedDb = _schedDb;
schedDb->cursor(0, &cursor, DB_WRITECURSOR);
QueueSchedule* queueSchedule = 0;
while (ret == 0)
{
if ((ret = cursor->get(&key, &data, DB_NEXT)) == 0)
{
// qDebug() << "Just read schedule from Db, key = : " << key.get_data();
queueSchedule = new QueueSchedule(schedDb, (uchar*)data.get_data(), this);
queueSchedules.append(queueSchedule);
currentSchedule = queueSchedule;
if (queueSchedule->getIsActive())
enabledSchedules.insert(queueSchedule->getPriority(), queueSchedule);
}
else if (ret == DB_BUFFER_SMALL)
{
ret = 0;
qDebug("Insufficient memory");
qDebug("Size required is: %d", data.get_size());
uchar *p=datamem;
datamem=new uchar[data.get_size()+1000];
data.set_ulen(data.get_size()+1000);
data.set_data(datamem);
Q_DELETE_ARRAY(p);
}
}
cursor->close();
if (createStartup)
{
queueSchedule = new QueueSchedule(schedDb, tr("Un-metered"), this);
queueSchedule->setIsActive(false);
queueSchedule->setPriority(2);
queueSchedule->addElement( 480 * 60, (1439 * 60) + 59, 128*1024, 0);
queueSchedule->addElement(1920 * 60, (2879 * 60) + 59, 128*1024, 0);
queueSchedule->addElement(3360 * 60, (4319 * 60) + 59, 128*1024, 0);
queueSchedule->addElement(4800 * 60, (5759 * 60) + 59, 128*1024, 0);
maxId = queueSchedule->addElement(6240 * 60, (7199 * 60) + 59, 128*1024, 0);
queueSchedule->setMaxId(maxId);
queueSchedules.append(queueSchedule);
currentSchedule = queueSchedule;
queueSchedule->dbSave();
}
managePeriods();
Q_DELETE_ARRAY(keymem);
Q_DELETE_ARRAY(datamem);
}
示例7: run_bdb_btree_big
//.........这里部分代码省略.........
STXXL_MSG("Records in map: " << dbstat->bt_ndata);
STXXL_MSG("Construction elapsed time: " << (Timer.mseconds() / 1000.) <<
" seconds : " << (double(n) / (Timer.mseconds() / 1000.)) << " key/data pairs per sec");
db.stat_print(0);
db.get_env()->memp_stat_print(DB_STAT_CLEAR);
////////////////////////////////////////
Timer.reset();
Timer.start();
for (i = 0; i < n_inserts; ++i)
{
//key1_storage.keybuf[KEYPOS] = letters[VALUE];
rand_key(i, key1_storage);
db.put(NULL, &key1, &data1, DB_NOOVERWRITE);
}
Timer.stop();
db.stat(NULL, &dbstat, 0);
STXXL_MSG("Records in map: " << dbstat->bt_ndata);
STXXL_MSG("Insertions elapsed time: " << (Timer.mseconds() / 1000.) <<
" seconds : " << (double(n_inserts) / (Timer.mseconds() / 1000.)) << " key/data pairs per sec");
db.stat_print(0);
db.get_env()->memp_stat_print(DB_STAT_CLEAR);
/////////////////////////////////////////
Timer.reset();
Timer.start();
Dbc * cursorp;
db.cursor(NULL, &cursorp, 0);
for (i = 0; i < n_locates; ++i)
{
//key1_storage.keybuf[KEYPOS] = letters[VALUE];
rand_key(i, key1_storage);
Dbt keyx(key1_storage.keybuf, KEY_SIZE);
Dbt datax(data1_storage.databuf, DATA_SIZE);
cursorp->get(&keyx, &datax, DB_SET_RANGE);
}
Timer.stop();
STXXL_MSG("Locates elapsed time: " << (Timer.mseconds() / 1000.) <<
" seconds : " << (double(ops) / (Timer.mseconds() / 1000.)) << " key/data pairs per sec");
db.stat_print(0);
db.get_env()->memp_stat_print(DB_STAT_CLEAR);
////////////////////////////////////
Timer.reset();
Timer.start();
stxxl::int64 n_scanned = 0;
for (i = 0; i < n_range_queries; ++i)
{
//key1_storage.keybuf[KEYPOS] = letters[VALUE];
rand_key(i, key1_storage);
my_key last_key = key1_storage;
//key1_storage.keybuf[KEYPOS] = letters[VALUE];
示例8: rundb
// Check that key/data for 0 - count-1 are already present,
// and write a key/data for count. The key and data are
// both "0123...N" where N == count-1.
//
// For some reason on Windows, we need to open using the full pathname
// of the file when there is no environment, thus the 'has_env'
// variable.
//
void rundb(Db *db, int count, int has_env)
{
const char *name;
if (has_env)
name = CONSTRUCT01_DBNAME;
else
name = CONSTRUCT01_DBFULLPATH;
db->set_error_stream(&cerr);
// We don't really care about the pagesize, but we do want
// to make sure adjusting Db specific variables works before
// opening the db.
//
CHK(db->set_pagesize(1024));
CHK(db->open(NULL, name, NULL, DB_BTREE, count ? 0 : DB_CREATE, 0664));
// The bit map of keys we've seen
long bitmap = 0;
// The bit map of keys we expect to see
long expected = (1 << (count+1)) - 1;
char outbuf[10];
int i;
for (i=0; i<count; i++) {
outbuf[i] = '0' + i;
}
outbuf[i++] = '\0';
Dbt key(outbuf, i);
Dbt data(outbuf, i);
DEBUGOUT("Put: " << outbuf);
CHK(db->put(0, &key, &data, DB_NOOVERWRITE));
// Acquire a cursor for the table.
Dbc *dbcp;
CHK(db->cursor(NULL, &dbcp, 0));
// Walk through the table, checking
Dbt readkey;
Dbt readdata;
while (dbcp->get(&readkey, &readdata, DB_NEXT) == 0) {
char *key_string = (char *)readkey.get_data();
char *data_string = (char *)readdata.get_data();
DEBUGOUT("Got: " << key_string << ": " << data_string);
int len = strlen(key_string);
long bit = (1 << len);
if (len > count) {
ERR("reread length is bad");
}
else if (strcmp(data_string, key_string) != 0) {
ERR("key/data don't match");
}
else if ((bitmap & bit) != 0) {
ERR("key already seen");
}
else if ((expected & bit) == 0) {
ERR("key was not expected");
}
else {
bitmap |= bit;
expected &= ~(bit);
for (i=0; i<len; i++) {
if (key_string[i] != ('0' + i)) {
cout << " got " << key_string
<< " (" << (int)key_string[i] << ")"
<< ", wanted " << i
<< " (" << (int)('0' + i) << ")"
<< " at position " << i << "\n";
ERR("key is corrupt");
}
}
}
}
if (expected != 0) {
cout << " expected more keys, bitmap is: " << expected << "\n";
ERR("missing keys in database");
}
CHK(dbcp->close());
CHK(db->close(0));
}
示例9: modifyPart
// Modify or create a part based on the following information
MultiPartHeader::Add_Code MultiPartHeader::modifyPart(Db* pdb, quint16 partNo, RawHeader *rh, quint16 hostId)
{
//qDebug() << "Entering modifyPart";
Add_Code retCode = New_Part;
int ret = 0;
bool createPart = false;
Header* h = 0;
char *phead = 0;
Dbc *cursorp = 0;
Dbt key;
Dbt data;
//qDebug() << "Creating part for key " << multiPartKey;
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, DB_WRITECURSOR);
//qDebug("1");
// Position the cursor to the first record in the database whose
// key matches the search key and whose data begins with the search
// data.
ret = cursorp->get(&key, &data, DB_GET_BOTH);
if (ret == 0)
{
//qDebug("2");
// update the data
h = new Header((char*)data.get_data(), (char*)key.get_data());
//qDebug("3");
if (h->isHeldByServer(hostId))
{
retCode = Duplicate_Part;
}
else
retCode = Updated_Part;
// qDebug() << "Found an update for part " << partNo << ", existing part " << h->getPartNo() << ", existing msgid " << h->getMessageId() << ", new msgid " << rh->m_mid;
// Always update the article number
h->setServerPartNum(hostId, rh->m_num);
//qDebug("4");
phead=h->data();
data.set_data(phead);
data.set_size(h->getRecordSize());
//qDebug("5");
cursorp->put(&key, &data, DB_CURRENT);
//qDebug("6");
Q_DELETE_ARRAY(phead);
}
else if (ret == DB_NOTFOUND) // create the part
{
//qDebug("7");
createPart = true;
}
else // encountered an error
{
qDebug() << "Unable to find part " << partNo << " for key " << multiPartKey << ", result = " << ret;
retCode = Error_Part;
}
// Close the cursor
if (cursorp != NULL)
cursorp->close();
if (createPart == true)
{
//qDebug("8");
quint32 partSize = rh->m_bytes.toULong();
h = new Header(multiPartKey, partNo, partSize);
//qDebug("9");
h->setMessageId(rh->m_mid);
h->setServerPartNum(hostId, rh->m_num);
//save in the parts db
phead=h->data();
data.set_data(phead);
data.set_size(h->getRecordSize());
//qDebug("10");
ret = pdb->put(0, &key, &data, 0);
if (ret)
{
qDebug() << "Unable to create part " << partNo << " for key " << multiPartKey << ", result = " << ret;
retCode = Error_Part;
}
//.........这里部分代码省略.........
示例10: deactivateGuard
vector<Identity>::const_iterator
Freeze::EvictorIteratorI::nextBatch()
{
DeactivateController::Guard
deactivateGuard(_store->evictor()->deactivateController());
_batch.clear();
if(!_more)
{
return _batch.end();
}
vector<EvictorElementPtr> evictorElements;
evictorElements.reserve(_batchSize);
Key firstKey = _key;
CommunicatorPtr communicator = _store->communicator();
try
{
for(;;)
{
_batch.clear();
evictorElements.clear();
Dbt dbKey;
initializeOutDbt(_key, dbKey);
Dbt dbValue;
dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL);
Dbc* dbc = 0;
try
{
//
// Move to the first record
//
u_int32_t flags = DB_NEXT;
if(_initialized)
{
//
// _key represents the next element not yet returned
// if it has been deleted, we want the one after
//
flags = DB_SET_RANGE;
//
// Will be used as input as well
//
dbKey.set_size(static_cast<u_int32_t>(firstKey.size()));
}
_store->db()->cursor(0, &dbc, 0);
bool done = false;
do
{
for(;;)
{
try
{
//
// It is critical to set key size to key capacity before the
// get, as a resize that increases the size inserts 0
//
_key.resize(_key.capacity());
_more = (dbc->get(&dbKey, &dbValue, flags) == 0);
if(_more)
{
_key.resize(dbKey.get_size());
_initialized = true;
flags = DB_NEXT;
Ice::Identity ident;
ObjectStore::unmarshal(ident, _key, communicator);
if(_batch.size() < _batchSize)
{
_batch.push_back(ident);
}
else
{
//
// Keep the last element in _key
//
done = true;
}
}
break;
}
catch(const DbDeadlockException&)
{
throw;
}
catch(const DbException& dx)
{
//.........这里部分代码省略.........
示例11: scan
ScanResponse BDBBackend::scan (const string & bucket, const string & seed,
int32_t count)
{
ScanResponse scan_response;
Dbc * dbc;
try
{
Dbt db_key;
char key[BDB_BACKEND_MAX_KEY_SIZE + 1];
db_key.set_data (key);
db_key.set_ulen (BDB_BACKEND_MAX_KEY_SIZE + 1);
db_key.set_flags (DB_DBT_USERMEM);
Dbt db_value;
char value[BDB_BACKEND_MAX_VALUE_SIZE + 1];
db_value.set_data (value);
db_value.set_ulen (BDB_BACKEND_MAX_VALUE_SIZE + 1);
db_value.set_flags (DB_DBT_USERMEM);
get_db (bucket)->cursor (NULL, &dbc, 0);
// this get positions us at the last key we grabbed or the one
// imediately following it
// copy over the seed and it's size
strncpy (key, seed.c_str (), seed.length () + 1);
db_key.set_size (seed.length () + 1);
if (dbc->get (&db_key, &db_value, DB_SET_RANGE) == 0)
{
string key_tmp ((const char *)db_key.get_data (),
db_key.get_size ());
if (seed != key_tmp)
{
// we got the one after it, it must be gone now, so return
// this one
Element e;
e.key = key_tmp;
e.value = string ((const char *)db_value.get_data (),
db_value.get_size ());
scan_response.elements.push_back (e);
} // we'll skip it, it's the one we had last time
// now keep going until we run out of items or get our fill
while ((dbc->get (&db_key, &db_value, DB_NEXT) == 0) &&
(scan_response.elements.size () < (unsigned int)count))
{
Element e;
e.key = string ((const char *)db_key.get_data (),
db_key.get_size ());
e.value = string ((const char *)db_value.get_data (),
db_value.get_size ());
scan_response.elements.push_back (e);
}
}
}
catch (DbDeadlockException & e)
{
T_INFO ("scan: exception=%s", e.what ());
dbc->close ();
throw e;
}
catch (DbException & e)
{
T_ERROR ("scan: exception=%s", e.what ());
dbc->close ();
throw e;
}
dbc->close ();
scan_response.seed = scan_response.elements.size () > 0 ?
scan_response.elements.back ().key : "";
return scan_response;
}
示例12: GetCursor
/*
* Look at all the records and parse keys for addresses and private keys
*/
bool WalletUtilityDB::parseKeys(bool dumppriv, std::string masterPass)
{
DBErrors result = DB_LOAD_OK;
std::string strType;
bool first = true;
try {
Dbc* pcursor = GetCursor();
if (!pcursor)
{
LogPrintf("Error getting wallet database cursor\n");
result = DB_CORRUPT;
}
if (dumppriv)
{
while (result == DB_LOAD_OK && true)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
int result = ReadAtCursor(pcursor, ssKey, ssValue);
if (result == DB_NOTFOUND) {
break;
}
else if (result != 0)
{
LogPrintf("Error reading next record from wallet database\n");
result = DB_CORRUPT;
break;
}
ssKey >> strType;
if (strType == "mkey")
{
updateMasterKeys(ssKey, ssValue);
}
}
pcursor->close();
pcursor = GetCursor();
}
while (result == DB_LOAD_OK && true)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
int ret = ReadAtCursor(pcursor, ssKey, ssValue);
if (ret == DB_NOTFOUND)
{
std::cout << " ]" << std::endl;
first = true;
break;
}
else if (ret != DB_LOAD_OK)
{
LogPrintf("Error reading next record from wallet database\n");
result = DB_CORRUPT;
break;
}
ssKey >> strType;
if (strType == "key" || strType == "ckey")
{
std::string strAddr = getAddress(ssKey);
std::string strKey = "";
if (dumppriv && strType == "key")
strKey = getKey(ssKey, ssValue);
if (dumppriv && strType == "ckey")
{
if (masterPass == "")
{
std::cout << "Encrypted wallet, please provide a password. See help below" << std::endl;
show_help();
result = DB_LOAD_FAIL;
break;
}
strKey = getCryptedKey(ssKey, ssValue, masterPass);
}
if (strAddr != "")
{
if (first)
std::cout << "[ ";
else
std::cout << ", ";
}
if (dumppriv)
{
std::cout << "{\"addr\" : \"" + strAddr + "\", "
<< "\"pkey\" : \"" + strKey + "\"}"
<< std::flush;
}
//.........这里部分代码省略.........
示例13: DatabaseException
//.........这里部分代码省略.........
//
string oldDbName = dbName + ".old-" + generateUUID();
if(connectionI->trace() >= 2)
{
Trace out(connectionI->communicator()->getLogger(), "Freeze.Map");
out << "Renaming \"" << dbName << "\" to \"" << oldDbName << "\"";
}
connectionI->dbEnv()->getEnv()->dbrename(txn, dbName.c_str(), 0, oldDbName.c_str(), 0);
//
// Fortunately, DB closes oldDb automatically when it goes out of scope
//
Db oldDb(connectionI->dbEnv()->getEnv(), 0);
//
// Berkeley DB expects file paths to be UTF8 encoded.
//
oldDb.open(txn, nativeToUTF8(oldDbName, getProcessStringConverter()).c_str(),
0, DB_BTREE, DB_THREAD, FREEZE_DB_MODE);
IceInternal::UniquePtr<MapDb> newDb(new MapDb(connectionI, dbName, key, value, keyCompare, indices, true));
if(connectionI->trace() >= 2)
{
Trace out(connectionI->communicator()->getLogger(), "Freeze.Map");
out << "Writing contents of \"" << oldDbName << "\" to fresh \"" << dbName << "\"";
}
//
// Now simply write all of oldDb into newDb
//
Dbc* dbc = 0;
oldDb.cursor(txn, &dbc, 0);
try
{
while(dbc->get(&keyDbt, &valueDbt, DB_NEXT) == 0)
{
newDb->put(txn, &keyDbt, &valueDbt, 0);
}
}
catch(...)
{
dbc->close();
throw;
}
dbc->close();
if(connectionI->trace() >= 2)
{
Trace out(connectionI->communicator()->getLogger(), "Freeze.Map");
out << "Transfer complete; removing \"" << oldDbName << "\"";
}
connectionI->dbEnv()->getEnv()->dbremove(txn, oldDbName.c_str(), 0, 0);
if(ownTx)
{
tx->commit();
}
break; // for (;;)
}
catch(const DbDeadlockException& dx)
{
示例14: run_bdb_btree
void run_bdb_btree(stxxl::int64 ops)
{
const char * filename = BDB_FILE;
my_key key1_storage;
my_data data1_storage;
unlink(filename);
memset(key1_storage.keybuf, 'a', KEY_SIZE);
memset(data1_storage.databuf, 'b', DATA_SIZE);
Db db(NULL, 0); // Instantiate the Db object
try {
db.set_errfile(stderr);
db.set_pagesize(pagesize);
db.set_cachesize(0, cachesize, 1);
// Open the database
db.open(NULL, // Transaction pointer
filename, // Database file name
NULL, // Optional logical database name
DB_BTREE, // Database access method
DB_CREATE, // Open flags
0); // File mode (using defaults)
// here we start with the tests
Dbt key1(key1_storage.keybuf, KEY_SIZE);
Dbt data1(data1_storage.databuf, DATA_SIZE);
stxxl::timer Timer;
stxxl::int64 n_inserts = ops, n_locates = ops, n_range_queries = ops, n_deletes = ops;
stxxl::int64 i;
//comp_type cmp_;
ran32State = 0xdeadbeef;
DB_BTREE_STAT * dbstat;
db.stat(NULL, &dbstat, 0);
STXXL_MSG("Records in map: " << dbstat->bt_ndata);
db.get_env()->memp_stat(NULL, NULL, DB_STAT_CLEAR);
Timer.start();
for (i = 0; i < n_inserts; ++i)
{
//key1_storage.keybuf[KEYPOS] = letters[VALUE];
rand_key(i, key1_storage);
db.put(NULL, &key1, &data1, DB_NOOVERWRITE);
}
Timer.stop();
db.stat(NULL, &dbstat, 0);
STXXL_MSG("Records in map: " << dbstat->bt_ndata);
STXXL_MSG("Insertions elapsed time: " << (Timer.mseconds() / 1000.) <<
" seconds : " << (double(n_inserts) / (Timer.mseconds() / 1000.)) << " key/data pairs per sec");
db.get_env()->memp_stat_print(DB_STAT_CLEAR);
/////////////////////////////////////////
Timer.reset();
Timer.start();
Dbc * cursorp;
db.cursor(NULL, &cursorp, 0);
for (i = 0; i < n_locates; ++i)
{
//key1_storage.keybuf[KEYPOS] = letters[VALUE];
rand_key(i, key1_storage);
Dbt keyx(key1_storage.keybuf, KEY_SIZE);
Dbt datax(data1_storage.databuf, DATA_SIZE);
cursorp->get(&keyx, &datax, DB_SET_RANGE);
}
Timer.stop();
STXXL_MSG("Locates elapsed time: " << (Timer.mseconds() / 1000.) <<
" seconds : " << (double(ops) / (Timer.mseconds() / 1000.)) << " key/data pairs per sec");
db.get_env()->memp_stat_print(DB_STAT_CLEAR);
////////////////////////////////////
Timer.reset();
Timer.start();
stxxl::int64 n_scanned = 0;
for (i = 0; i < n_range_queries; ++i)
{
//key1_storage.keybuf[KEYPOS] = letters[VALUE];
rand_key(i, key1_storage);
//.........这里部分代码省略.........
示例15: closeAllIterators
void
Freeze::MapHelperI::clear()
{
DbTxn* txn = _connection->dbTxn();
if(txn == 0)
{
closeAllIterators();
}
Dbt dbKey;
dbKey.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL);
Dbt dbValue;
dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL);
try
{
for(;;)
{
Dbc* dbc = 0;
try
{
IteratorHelperI::TxPtr tx;
if(txn == 0)
{
#ifdef ICE_CPP11_COMPILER
tx.reset(new IteratorHelperI::Tx(*this));
#else
tx = new IteratorHelperI::Tx(*this);
#endif
txn = tx->getTxn();
}
_db->cursor(txn, &dbc, 0);
while(dbc->get(&dbKey, &dbValue, DB_NEXT | DB_RMW) == 0)
{
dbc->del(0);
}
Dbc* toClose = dbc;
dbc = 0;
toClose->close();
break; // for (;;)
}
catch(const DbDeadlockException&)
{
if(dbc != 0)
{
try
{
dbc->close();
}
catch(const DbDeadlockException&)
{
if(txn != 0)
{
throw;
}
else
{
//
// Ignored
//
}
}
}
if(_connection->deadlockWarning())
{
Warning out(_connection->communicator()->getLogger());
out << "Deadlock in Freeze::MapHelperI::clear on Map \""
<< _dbName << "\"; retrying ...";
}
if(txn != 0)
{
throw;
}
//
// Otherwise retry
//
}
catch(...)
{
if(dbc != 0)
{
try
{
dbc->close();
}
catch(const DbDeadlockException&)
{
if(txn != 0)
{
throw;
}
else
{
//
//.........这里部分代码省略.........