本文整理汇总了C++中Db::open方法的典型用法代码示例。如果您正苦于以下问题:C++ Db::open方法的具体用法?C++ Db::open怎么用?C++ Db::open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Db
的用法示例。
在下文中一共展示了Db::open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: initDb
/* 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;
}
}
示例2: getTable
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: Db
// 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);
}
示例4: 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;
}
示例5: 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;
}
示例6: 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;
}
示例7: Berkley
Berkley(const string& dbname)
:_env(0), _dbname(dbname)
{
_env.set_error_stream(&std::cerr);
_env.open("/tmp/", DB_CREATE|DB_INIT_MPOOL,0);
_db = new Db(&_env, 0);
_db->open(nullptr, _dbname.c_str(), nullptr, DB_BTREE, DB_CREATE|DB_TRUNCATE, 0);
};
示例8: 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();
}
示例9: 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;
}
示例10: main
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;
}
示例11: main
int main(int argc, const char* argv[]) {
if (argc !=3) {
cout << "usage: "<< argv[0] << " <filename> <db filename>" << endl;
return 1;
}
const char* kDatabaseName = argv[2];
const char* filename = argv[1];
DbEnv env(0);
Db* pdb;
try {
env.set_error_stream(&cerr);
env.open("./", DB_CREATE | DB_INIT_MPOOL, 0);
pdb = new Db(&env, 0);
pdb->set_flags(DB_DUP);
// Create (or clear its content if it exists) the database
pdb->open(NULL, kDatabaseName, NULL, DB_BTREE, DB_CREATE | DB_TRUNCATE, 0);
readfile(pdb, filename);
readfile(pdb, "tables/jp.txt");
// Clean up
if (pdb != NULL) {
pdb->close(0);
delete pdb;
}
env.close(0);
} catch (DbException& e) {
cerr << "DbException: " << e.what() << endl;
return -1;
} catch (std::exception& e) {
cerr << e.what() << endl;
return -1;
}
return 0;
}
示例12: DbEnv
// Note that any of the db calls can throw DbException
void
db_setup(const char *home, const char *data_dir, ostream& err_stream)
{
//
// Create an environment object and initialize it for error
// reporting.
//
DbEnv *dbenv = new DbEnv(0);
dbenv->set_error_stream(&err_stream);
dbenv->set_errpfx(progname);
//
// We want to specify the shared memory buffer pool cachesize,
// but everything else is the default.
//
dbenv->set_cachesize(0, 64 * 1024, 0);
// Databases are in a subdirectory.
(void)dbenv->set_data_dir(data_dir);
// Open the environment with full transactional support.
dbenv->open(home,
DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
DB_INIT_TXN, 0);
// Open a database in the environment to verify the data_dir
// has been set correctly.
// Create a database handle, using the environment.
Db *db = new Db(dbenv, 0) ;
// Open the database.
db->open(NULL, "EvnExample_db1.db", NULL, DB_BTREE, DB_CREATE, 0644);
// Close the database handle.
db->close(0) ;
delete db;
// Close the handle.
dbenv->close(0);
delete dbenv;
}
示例13: admin
string BDBBackend::admin (const string & op, const string & data)
{
string ret = ThrudocBackend::admin (op, data);
if (!ret.empty ())
{
return ret;
}
else if (op == "create_bucket")
{
Db * db = NULL;
try
{
db = get_db (data);
// this will log an error message if db doesn't exist, ignore it
}
catch (ThrudocException e) {}
if (!db)
{
T_INFO ("admin: creating db=%s", data.c_str());
u_int32_t db_flags =
DB_CREATE | // allow creating db
DB_AUTO_COMMIT; // allow auto-commit
db = new Db (this->db_env, 0);
db->open (NULL, // Txn pointer
data.c_str (), // file name
NULL, // logical db name
DB_BTREE, // database type
db_flags, // open flags
0); // file mode, defaults
db->close (0);
delete db;
}
return "done";
}
// TODO delete_bucket, but have to figure out how to close the db
// handles across all of the threads first...
return "";
}
示例14: Rewrite
bool CDB::Rewrite(const string& strFile, const char* pszSkip)
{
while (!fShutdown)
{
{
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;
}
示例15: main
int main(int argc, const char* argv[]) {
if (argc !=3) {
cout << "usage: "<< argv[0] << " <filename> <db filename>" << endl;
return 1;
}
const char* kDatabaseName = argv[2];
const char* filename = argv[1];
DbEnv env(0);
Db* pdb;
string line;
try {
env.set_error_stream(&cerr);
env.open("./", DB_CREATE | DB_INIT_MPOOL, 0);
pdb = new Db(&env, 0);
// If you want to support duplicated records and make duplicated
// records sorted by data, you need to call:
// pdb->set_flags(DB_DUPSORT);
// Note that only Btree-typed database supports sorted duplicated
// records
// If the database does not exist, create it. If it exists, clear
// its content after openning.
//pdb->set_flags( DB_DUP );// | DB_DUPSORT);
pdb->open(NULL, kDatabaseName, NULL, DB_BTREE, DB_CREATE | DB_TRUNCATE, 0);
ifstream myfile (filename);
if (myfile.is_open()) {
while ( myfile.good() ) {
getline (myfile,line);
// Ignore empty and commented lines
if ((line.length() == 0) || (startswith(line, std::string("#")))) {
continue;
}
std::vector<std::string> cj = split(line, ' ');
int freq = atoi(cj[1].c_str());
Dbt key(const_cast<char*>(cj[0].data()), cj[0].size());
Dbt value(&freq, sizeof(int));
pdb->put(NULL, &key, &value, 0);
}
myfile.close();
} else {
cout << "Unable to open file";
}
// You need to set ulen and flags=DB_DBT_USERMEM to prevent Dbt
// from allocate its own memory but use the memory provided by you.
if (pdb != NULL) {
pdb->close(0);
delete pdb;
}
env.close(0);
} catch (DbException& e) {
cerr << "DbException: " << e.what() << endl;
return -1;
} catch (std::exception& e) {
cerr << e.what() << endl;
return -1;
}
return 0;
}