本文整理汇总了C++中Db类的典型用法代码示例。如果您正苦于以下问题:C++ Db类的具体用法?C++ Db怎么用?C++ Db使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Db类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Db
Db * BDBBackend::get_db (const string & bucket)
{
Db * db = dbs[bucket];
if (!db)
{
u_int32_t db_flags = DB_AUTO_COMMIT; // allow auto-commit
db = new Db (this->db_env, 0);
try
{
db->open (NULL, // Txn pointer
bucket.c_str (), // file name
NULL, // logical db name
DB_BTREE, // database type
db_flags, // open flags
0); // file mode, defaults
dbs[bucket] = db;
}
catch (DbException & e)
{
delete db;
T_ERROR("get_db: exception=%s", e.what ());
ThrudocException de;
de.what = "BDBBackend error";
throw de;
}
}
return db;
}
示例2: throw
Table Database::getTable(const std::string &tableName) throw (DbException)
{
uint32_t dbFlags = DB_CREATE | DB_AUTO_COMMIT;
Db *db = new Db(&m_env, 0);
db->open(NULL, tableName.c_str(), NULL, DB_BTREE, dbFlags, 0);
return Table(db);
}
示例3: operator
void process_results_callable::operator ()(Nids *nids, const char *db_filename)
{
Db db;
if (db_filename && db_filename[0])
db.open(db_filename);
while(true) {
nids->process_result_sem.wait();
if (nids->threads_exit)
break;
if (db.is_opened())
nids->process_result(&db);
else
nids->process_result(NULL);
}
if (db_filename && db_filename[0])
db.close();
nids->threads_finished_sem.post();
BOOST_LOG_TRIVIAL(trace) << "process_result thread finished successfully" << endl;
cout << "process result thread finished" << endl;
}
示例4: memset
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;
}
示例5: t6
void t6(int except_flag)
{
cout << " Running test 6:\n";
/* From user [#2939] */
int err;
DbEnv* penv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
penv->set_cachesize(0, 32 * 1024, 0);
penv->open(CONSTRUCT01_DBDIR, DB_CREATE | DB_PRIVATE | DB_INIT_MPOOL, 0);
//LEAK: remove this block and leak disappears
Db* pdb = new Db(penv,0);
if ((err = pdb->close(0)) != 0) {
fprintf(stderr, "Error closing Db: %s\n", db_strerror(err));
}
delete pdb;
//LEAK: remove this block and leak disappears
if ((err = penv->close(0)) != 0) {
fprintf(stderr, "Error closing DbEnv: %s\n", db_strerror(err));
}
delete penv;
cout << " finished.\n";
}
示例6: main
int main(int argc, char *argv[])
{
try {
Db *db = new Db(NULL, 0);
db->open(NULL, "my.db", NULL, DB_BTREE, DB_CREATE, 0);
// populate our massive database.
// all our strings include null for convenience.
// Note we have to cast for idiomatic
// usage, since newer gcc requires it.
set<string> hash;
rand_init();
FILE*file=fopen("mydb","w");
for(int i=0;i<num_record;i++){
cout<<i<<endl;
string str_key=rand_str(5,15);
while(hash.count(str_key)!=0)
str_key=rand_str(5,15);
hash.insert(str_key);
Dbt *keydbt = new Dbt((char *)str_key.c_str(), str_key.size()+1);
string str_data=rand_str(150,250);
Dbt *datadbt = new Dbt((char *)str_data.c_str(), str_data.size()+1);
db->put(NULL, keydbt, datadbt, 0);
fprintf(file,"%d\n%s\n%s\n",i,str_key.c_str(),str_data.c_str());
}
fclose(file);
db->close(0);
}catch (DbException &dbe) {
cerr << "Db Exception: " << dbe.what();
}
return 0;
}
示例7: DB_ERROR
//static
int Db::_append_recno_intercept(DB *db, DBT *data, db_recno_t recno)
{
int err;
if (db == 0) {
DB_ERROR("Db::append_recno_callback", EINVAL, ON_ERROR_UNKNOWN);
return (EINVAL);
}
Db *cxxdb = (Db *)db->cj_internal;
if (cxxdb == 0) {
DB_ERROR("Db::append_recno_callback", EINVAL, ON_ERROR_UNKNOWN);
return (EINVAL);
}
if (cxxdb->append_recno_callback_ == 0) {
DB_ERROR("Db::append_recno_callback", EINVAL, cxxdb->error_policy());
return (EINVAL);
}
// making these copies is slow but portable.
// Another alternative is to cast the DBT* manufactured
// by the C layer to a Dbt*. It 'should be' safe since
// Dbt is a thin shell over DBT, adding no extra data,
// but is nonportable, and could lead to errors if anything
// were added to the Dbt class.
//
Dbt cxxdbt;
memcpy((DBT *)&cxxdbt, data, sizeof(DBT));
err = (*cxxdb->append_recno_callback_)(cxxdb, &cxxdbt, recno);
memcpy(data, (DBT *)&cxxdbt, sizeof(DBT));
return (err);
}
示例8: __db_close_int
extern "C" int
__db_close_int(long id, u_int32_t flags)
{
Db *dbp;
int ret;
ct_entry *ctp;
ret = 0;
ctp = get_tableent(id);
if (ctp == NULL)
return (DB_NOSERVER_ID);
DB_ASSERT(ctp->ct_type == CT_DB);
if (__dbsrv_verbose && ctp->ct_refcount != 1)
printf("Deref'ing dbp id %ld, refcount %d\n",
id, ctp->ct_refcount);
if (--ctp->ct_refcount != 0)
return (ret);
dbp = ctp->ct_dbp;
if (__dbsrv_verbose)
printf("Closing dbp id %ld\n", id);
ret = dbp->close(flags);
__dbdel_ctp(ctp);
return (ret);
}
示例9: strlen
int DiskBDB::Truncate ( const Vdt *dzname )
{
int ret = -1;
int numPartition = 0;
DbTxn* txn = NULL;
Db *DbHandle;
numPartition = m_dzManager->GetNumPartition( *dzname );
string s_dzname = "";
uint num_deleted = 0;
const char **pathArray;
m_DiskEnv->get_data_dirs(&pathArray);
string s_datadir_init = pathArray[0];
string s_datadir, s_pathFile;
TCUtility::fixDataDirectoryPath( s_datadir_init, s_datadir );
for( int i = 0; i < numPartition; i++ )
{
//txn = NULL;
//m_DiskEnv->txn_begin(NULL, &txn, 0);
s_pathFile.clear();
s_dzname.clear();
//s_dzname = "";
m_dzManager->AppendPartitionID(*dzname, i, s_dzname);
s_dzname += TC_TRUCATE_DZONE_MASK;
s_pathFile = s_datadir + s_dzname;
//ret = m_dzManager->GetHandleForPartition( s_dzname.c_str(), strlen(s_dzname.c_str()), DbHandle );
//ret = m_dzManager->OpenHandleNoEnv( s_dzname.c_str(), strlen(s_dzname.c_str()), DbHandle );
ret = m_dzManager->OpenHandleNoEnv( s_pathFile.c_str(), strlen(s_pathFile.c_str())+1, DbHandle );
//handle ret!=0
//blablabla...
try
{
ret = DbHandle->truncate(NULL, &num_deleted, 0);
//ret = DbHandle->truncate( txn, &num_deleted, 0 );
//handle ret!=0
//blablabla...
//txn->commit(0);
}
catch(DbException ex)
{
ret = ex.get_errno();
cout << ex.what();
cout << ex.get_errno();
cout<<"Error. DiskBDB::Truncate-> Exception encountered while truncating DiskBDB for DZName: " << s_dzname << endl;
//txn->abort();
}
DbHandle->close(0);
}
if(ret!=0)
{
cout<<"The Handle was not retrieved from datazone manager"<<endl;
return ret;
}
return ret;
}
示例10: openDb
// Open a Berkeley DB database
int
openDb(Db **dbpp, const char *progname, const char *fileName,
DbEnv *envp, u_int32_t extraFlags)
{
int ret;
u_int32_t openFlags;
try {
Db *dbp = new Db(envp, 0);
// Point to the new'd Db
*dbpp = dbp;
if (extraFlags != 0)
ret = dbp->set_flags(extraFlags);
// Now open the database */
openFlags = DB_CREATE | // Allow database creation
DB_THREAD |
DB_AUTO_COMMIT; // Allow autocommit
dbp->open(NULL, // Txn pointer
fileName, // File name
NULL, // Logical db name
DB_BTREE, // Database type (using btree)
openFlags, // Open flags
0); // File mode. Using defaults
} catch (DbException &e) {
std::cerr << progname << ": openDb: db open failed:" << std::endl;
std::cerr << e.what() << std::endl;
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
示例11: testDb
void testDb(Db dbEngLab, Table *ptblTeacher)
{
DB_RET Ret;
Ret = dbEngLab.open("EngLab", "D:\\Dropbox\\develop");
Ret = dbEngLab.addTable(ptblTeacher);
Ret = dbEngLab.save();
Ret = dbEngLab.commit();
}
示例12: assert
int Account::DoUserLogin(Login& login,Db& db,UserRecord& userRecord){
char sql[256];
const int maxArray = 256;
if ( login.phone_len > maxArray )
{
assert(false);
return kError;
}
string phone = "";
phone.assign(login.phone,login.phone_len);
sprintf_s(sql,"select * from playerInfo where phone = \"%s\"",phone.c_str());
db.ReadDb(sql);
if (db.nRow > 0)
{
time_t now_time;
now_time = time(NULL);
userRecord.login_time = (uint32_t)now_time;
string str_player_id = db.pazResult[db.nCol];
userRecord.user_id = atoi(str_player_id.c_str());
string nick = db.pazResult[db.nCol+1];
userRecord.nick.len = strlen(nick.c_str());
memcpy(userRecord.nick.name,nick.c_str(),userRecord.nick.len);
string str_age = db.pazResult[db.nCol+2];
userRecord.age = atoi(str_age.c_str());
string str_forbid_login_time = db.pazResult[db.nCol + 3];
userRecord.forbid_login_time = atoi(str_forbid_login_time.c_str());
string str_phone = db.pazResult[db.nCol + 5];
memcpy(userRecord.phone,str_phone.c_str(),strlen(str_phone.c_str()));
string str_address_1 = db.pazResult[db.nCol + 6];
userRecord.address1_len = str_address_1.length();
memcpy(userRecord.address1,str_address_1.c_str(),userRecord.address1_len);
string str_address_2 = db.pazResult[db.nCol + 7];
userRecord.address2_len = str_address_2.length();
memcpy(userRecord.address2,str_address_2.c_str(),userRecord.address2_len);
string str_address_3 = db.pazResult[db.nCol + 8];
userRecord.address3_len = str_address_3.length();
memcpy(userRecord.address3,str_address_3.c_str(),userRecord.address3_len);
db.freeTableData();
}else{
return kUserOrPasswdError;
}
return kSucess;
}
示例13: test_new_open_delete
void test_new_open_delete() {
system("rm -rf " DIR);
toku_os_mkdir(DIR, 0777);
DbEnv env(0);
{ int r = env.set_redzone(0); assert(r==0); }
{ int r = env.open(DIR, DB_INIT_MPOOL + DB_CREATE + DB_PRIVATE, 0777); assert(r == 0); }
Db *db = new Db(&env, 0); assert(db != 0);
{ int r = db->open(NULL, FNAME, 0, DB_BTREE, DB_CREATE, 0777); assert(r == 0); }
{ int r = db->close(0); assert(r == 0); }
delete db;
}
示例14: key
int DiskBDB::Get (const Vdt& dzname, const Vdt& Key, Vdt *Value,stats *stats_update)
{
Db *DbHandle;
Dbt dbt_value;
int ret=0;
//ret=m_dzManager->GetHandleForPartition("B0",2,DbHandle);
ret=m_dzManager->Get(dzname,Key,DbHandle);
if(ret!=0)
{
cout<<"Datazone handle not retrieved from datazone manager"<<endl;
return ret;
}
Dbt key(Key.get_data(),Key.get_size());
dbt_value.set_data((*Value).get_data());
dbt_value.set_size((*Value).get_size());
dbt_value.set_ulen((*Value).get_size() );
dbt_value.set_flags( DB_DBT_USERMEM );
bool keepTrying=true;
int numTries=0;
while(keepTrying && numTries < m_maxDeadlockRetries)
{
numTries++;
try {
ret=DbHandle->get(NULL,&key,&dbt_value,0);
keepTrying=false;
if( ret == 0 )
Value->set_size( dbt_value.get_size() );
}
catch(DbException &e) {
if(numTries==1)
printf("DiskBDB Get::%s\n",e.what());
ret=e.get_errno();
if(ret==DB_LOCK_DEADLOCK)
{
if(stats_update)
stats_update->NumDeadlocks++;
}
else
keepTrying=false;
if( ret==DB_BUFFER_SMALL )
(*Value).set_size(dbt_value.get_size());
} catch(exception &e) {
cout << e.what() << endl;
return (-1);
}
}
return ret;
}
示例15: Db
/* Initialize the database. */
void BulkExample::initDb(int dups, int sflag, int pagesize) {
DbTxn *txnp;
int ret;
txnp = NULL;
ret = 0;
dbp = new Db(dbenv, 0);
dbp->set_error_stream(&cerr);
dbp->set_errpfx(progname);
try{
if ((ret = dbp->set_bt_compare(compare_int)) != 0)
throwException(dbenv, NULL, ret, "DB->set_bt_compare");
if ((ret = dbp->set_pagesize(pagesize)) != 0)
throwException(dbenv, NULL, ret, "DB->set_pagesize");
if (dups && (ret = dbp->set_flags(DB_DUP)) != 0)
throwException(dbenv, NULL, ret, "DB->set_flags");
if ((ret = dbenv->txn_begin(NULL, &txnp, 0)) != 0)
throwException(dbenv, NULL, ret, "DB_ENV->txn_begin");
if ((ret = dbp->open(txnp, DATABASE, "primary", DB_BTREE,
DB_CREATE, 0664)) != 0)
throwException(dbenv, txnp, ret, "DB->open");
if (sflag) {
sdbp = new Db(dbenv, 0);
if ((ret = sdbp->set_flags(DB_DUPSORT)) != 0)
throwException(dbenv, txnp,
ret, "DB->set_flags");
if ((ret = sdbp->open(txnp, DATABASE, "secondary",
DB_BTREE, DB_CREATE, 0664)) != 0)
throwException(dbenv, txnp, ret, "DB->open");
if ((ret = dbp->associate(
txnp, sdbp, get_first_str, 0)) != 0)
throwException(dbenv, txnp,
ret, "DB->associate");
}
ret = txnp->commit(0);
txnp = NULL;
if (ret != 0)
throwException(dbenv, NULL, ret, "DB_TXN->commit");
} catch(DbException &dbe) {
cerr << "initDb " << dbe.what() << endl;
if (txnp != NULL)
(void)txnp->abort();
throw dbe;
}
}