void DBFlush(bool fShutdown) { // Flush log data to the actual data file // on all files that are not in use printf("DBFlush(%s)\n", fShutdown ? "true" : "false"); CRITICAL_BLOCK(cs_db) { dbenv.txn_checkpoint(0, 0, 0); map<string, int>::iterator mi = mapFileUseCount.begin(); while (mi != mapFileUseCount.end()) { string strFile = (*mi).first; int nRefCount = (*mi).second; if (nRefCount == 0) { dbenv.lsn_reset(strFile.c_str(), 0); mapFileUseCount.erase(mi++); } else mi++; } if (fShutdown) { char** listp; if (mapFileUseCount.empty()) dbenv.log_archive(&listp, DB_ARCH_REMOVE); dbenv.close(0); fDbEnvInit = false; } } }
/* Initialize the environment. */ void BulkExample::initDbenv(const char *home, u_int32_t cachesize) { int ret; ret = 0; dbenv = new DbEnv(0); dbenv->set_error_stream(&cerr); dbenv->set_errpfx(progname); try { if ((ret = dbenv->set_cachesize(0, cachesize, 0)) != 0) throwException(dbenv, NULL, ret, "DB_ENV->set_cachesize"); /* Open the environment with full transactional support. */ if ((ret = dbenv->open(home, DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0)) != 0) throwException(dbenv, NULL, ret, "DB_ENV->open"); } catch (DbException &dbe) { cerr << "initDbenv " << dbe.what() << endl; throw dbe; } }
int RepMgr::init(SimpleConfigInfo *config) { int ret = 0; app_config = config; dbenv.set_errfile(stderr); dbenv.set_errpfx(progname); // We can now open our environment. dbenv.set_cachesize(0, CACHESIZE, 0); dbenv.set_flags(DB_TXN_NOSYNC, 1); try { dbenv.open(app_config->home, DB_CREATE | DB_RECOVER | DB_THREAD | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0); } catch(DbException dbe) { cerr << "Caught an exception during DB environment open." << endl << "Ensure that the home directory is created prior to starting" << " the application." << endl; ret = ENOENT; goto err; } err: return ret; }
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"; }
extern "C" int __dbenv_close_int(long id, u_int32_t flags, int force) { DbEnv *dbenv; int ret; ct_entry *ctp; ret = 0; ctp = get_tableent(id); if (ctp == NULL) return (DB_NOSERVER_ID); DB_ASSERT(ctp->ct_type == CT_ENV); if (__dbsrv_verbose && ctp->ct_refcount != 1) printf("Deref'ing env id %ld, refcount %d\n", id, ctp->ct_refcount); /* * If we are timing out, we need to force the close, no matter * what the refcount. */ if (--ctp->ct_refcount != 0 && !force) return (ret); dbenv = ctp->ct_envp; if (__dbsrv_verbose) printf("Closing env id %ld\n", id); ret = dbenv->close(flags); __dbdel_ctp(ctp); return (ret); }
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); };
bool BerkeleyQ::connect() { if (NULL != db) return true; try { int ret = 0; uint env_flags = DB_CREATE | /* Create the environment if it does not exist */ DB_INIT_LOCK | /* Initialize the locking subsystem */ DB_INIT_MPOOL | /* Initialize the memory pool (in-memory cache) */ DB_PRIVATE | /* Region files are not backed by the * filesystem. Instead, they are backed by * heap memory. */ DB_THREAD; DbEnv* env = new DbEnv(0); ret = env->set_cachesize(0, /* 0 gigabytes */ 50 * 1024 * 1024, /* 50 megabytes */ 1); /* Create 1 cache. All memory will * be allocated contiguously. */ env->open(NULL, env_flags, 0); db = new Db(env, 0); // Instantiate the Db object db->open(NULL, // Transaction pointer NULL, // Database file name NULL, // Optional logical database name DB_BTREE, // Database access method DB_CREATE | DB_THREAD, // Open flags 0); // File mode (using defaults) DbMpoolFile* mp = db->get_mpf(); ret = mp->set_flags(DB_MPOOL_NOFILE, 1); db->get_env()->lock_id(&locker_id); start(); } catch(DbException &e) { db = NULL; q_error("failed connecting to berkeley. %s", e.what()); } catch(std::exception &e) { db = NULL; q_error("failed connecting to berkeley. %s", e.what()); } catch (...) { db = NULL; q_error("failed connecting to berkeley. unknown error"); } return (NULL != db); }
void DbEnv::_thread_id_intercept(DB_ENV *env, pid_t *pidp, db_threadid_t *thridp) { DbEnv *cxxenv = DbEnv::get_DbEnv(env); if (cxxenv == 0) { DB_ERROR(DbEnv::get_DbEnv(env), "DbEnv::thread_id_callback", EINVAL, ON_ERROR_UNKNOWN); } else cxxenv->thread_id_callback_(cxxenv, pidp, thridp); }
char *DbEnv::_thread_id_string_intercept(DB_ENV *env, pid_t pid, db_threadid_t thrid, char *buf) { DbEnv *cxxenv = DbEnv::get_DbEnv(env); if (cxxenv == 0) { DB_ERROR(DbEnv::get_DbEnv(env), "DbEnv::thread_id_string_callback", EINVAL, ON_ERROR_UNKNOWN); return (NULL); } return (cxxenv->thread_id_string_callback_(cxxenv, pid, thrid, buf)); }
void db_teardown(const char *home, const char *data_dir, ostream& err_stream) { // Remove the shared database regions. DbEnv *dbenv = new DbEnv(0); dbenv->set_error_stream(&err_stream); dbenv->set_errpfx(progname); (void)dbenv->set_data_dir(data_dir); dbenv->remove(home, 0); delete dbenv; }
void DbEnv::_paniccall_intercept(DB_ENV *env, int errval) { if (env == 0) { DB_ERROR("DbEnv::paniccall_callback", EINVAL, ON_ERROR_UNKNOWN); } DbEnv *cxxenv = (DbEnv *)env->cj_internal; if (cxxenv == 0) { DB_ERROR("DbEnv::paniccall_callback", EINVAL, ON_ERROR_UNKNOWN); } if (cxxenv->paniccall_callback_ == 0) { DB_ERROR("DbEnv::paniccall_callback", EINVAL, cxxenv->error_policy()); } (*cxxenv->paniccall_callback_)(cxxenv, errval); }
void DbEnv::_feedback_intercept(DB_ENV *env, int opcode, int pct) { DbEnv *cxxenv = DbEnv::get_DbEnv(env); if (cxxenv == 0) { DB_ERROR(0, "DbEnv::feedback_callback", EINVAL, ON_ERROR_UNKNOWN); return; } if (cxxenv->feedback_callback_ == 0) { DB_ERROR(DbEnv::get_DbEnv(env), "DbEnv::feedback_callback", EINVAL, cxxenv->error_policy()); return; } (*cxxenv->feedback_callback_)(cxxenv, opcode, pct); }
void DbEnv::_paniccall_intercept(DB_ENV *env, int errval) { DbEnv *cxxenv = DbEnv::get_DbEnv(env); if (cxxenv == 0) { DB_ERROR(0, "DbEnv::paniccall_callback", EINVAL, ON_ERROR_UNKNOWN); return; } if (cxxenv->paniccall_callback_ == 0) { DB_ERROR(cxxenv, "DbEnv::paniccall_callback", EINVAL, cxxenv->error_policy()); return; } (*cxxenv->paniccall_callback_)(cxxenv, errval); }
int DbEnv::_backup_open_intercept(DB_ENV *dbenv, const char *dbname, const char *target, void **handle) { DbEnv *cxxenv = DbEnv::get_DbEnv(dbenv); if (cxxenv == 0) { DB_ERROR(DbEnv::get_DbEnv(dbenv), "DbEnv::backup_open_callback", EINVAL, ON_ERROR_UNKNOWN); return (EINVAL); } if (cxxenv->backup_open_callback_ == 0) { DB_ERROR(DbEnv::get_DbEnv(dbenv), "DbEnv::backup_open_callback", EINVAL, cxxenv->error_policy()); return (EINVAL); } return (*cxxenv->backup_open_callback_)(cxxenv, dbname, target, handle); }
void CDB::Close () { if (!pdb) return; if (!vTxn.empty () && ownTxn.front ()) vTxn.front ()->abort (); vTxn.clear (); ownTxn.clear (); pdb = NULL; if (!fReadOnly) { /* Flush database activity from memory pool to disk log. wallet file is always flushed, the other files only every couple of minutes. Note: Loopcoin has more .dat files than Bitcoin. */ unsigned int nMinutes = 2; if (strFile == walletPath) nMinutes = 0; else if ((strFile == "blkindex.dat" || strFile == "nameindex.dat") && IsInitialBlockDownload ()) nMinutes = 5; dbenv.txn_checkpoint (0, nMinutes, 0); } CRITICAL_BLOCK(cs_db) --mapFileUseCount[strFile]; }
void DbEnv::_event_func_intercept( DB_ENV *env, u_int32_t event, void *event_info) { DbEnv *cxxenv = DbEnv::get_DbEnv(env); if (cxxenv == 0) { DB_ERROR(0, "DbEnv::event_func_callback", EINVAL, ON_ERROR_UNKNOWN); return; } if (cxxenv->event_func_callback_ == 0) { DB_ERROR(cxxenv, "DbEnv::event_func_callback", EINVAL, cxxenv->error_policy()); return; } (*cxxenv->event_func_callback_)(cxxenv, event, event_info); }
void CDB::Close() { if (!pdb) return; if (!vTxn.empty()) vTxn.front()->abort(); vTxn.clear(); pdb = NULL; // Flush database activity from memory pool to disk log unsigned int nMinutes = 0; if (fReadOnly) nMinutes = 1; if (strFile == "addr.dat") nMinutes = 2; if (strFile == "blkindex.dat") nMinutes = 2; if (strFile == "blkindex.dat" && IsInitialBlockDownload()) nMinutes = 5; dbenv.txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100)*1024 : 0, nMinutes, 0); { LOCK(cs_db); --mapFileUseCount[strFile]; } }
int DbEnv::_backup_write_intercept(DB_ENV *dbenv, u_int32_t off_gbytes, u_int32_t off_bytes, u_int32_t size, u_int8_t *buf, void *handle) { DbEnv *cxxenv = DbEnv::get_DbEnv(dbenv); if (cxxenv == 0) { DB_ERROR(DbEnv::get_DbEnv(dbenv), "DbEnv::backup_write_callback", EINVAL, ON_ERROR_UNKNOWN); return (EINVAL); } if (cxxenv->backup_write_callback_ == 0) { DB_ERROR(DbEnv::get_DbEnv(dbenv), "DbEnv::backup_write_callback", EINVAL, cxxenv->error_policy()); return (EINVAL); } return (*cxxenv->backup_write_callback_)( cxxenv, off_gbytes, off_bytes, size, buf, handle); }
int DbEnv::_recovery_init_intercept(DB_ENV *env) { if (env == 0) { DB_ERROR("DbEnv::recovery_init_callback", EINVAL, ON_ERROR_UNKNOWN); } DbEnv *cxxenv = (DbEnv *)env->cj_internal; if (cxxenv == 0) { DB_ERROR("DbEnv::recovery_init_callback", EINVAL, ON_ERROR_UNKNOWN); } if (cxxenv->recovery_init_callback_ == 0) { DB_ERROR("DbEnv::recovery_init_callback", EINVAL, cxxenv->error_policy()); } return ((*cxxenv->recovery_init_callback_)(cxxenv)); }
~CDBInit() { if (fDbEnvInit) { dbenv.close(0); fDbEnvInit = false; } }
~CDBInit() { if (dbLock) { dbenv.close(0); delete dbLock; dbLock = NULL; } }
/* 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; } }
// 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); // Do something interesting... // Close the handle. dbenv->close(0); }
void DbEnv::_feedback_intercept(DB_ENV *env, int opcode, int pct) { if (env == 0) { DB_ERROR("DbEnv::feedback_callback", EINVAL, ON_ERROR_UNKNOWN); return; } DbEnv *cxxenv = (DbEnv *)env->cj_internal; if (cxxenv == 0) { DB_ERROR("DbEnv::feedback_callback", EINVAL, ON_ERROR_UNKNOWN); return; } if (cxxenv->feedback_callback_ == 0) { DB_ERROR("DbEnv::feedback_callback", EINVAL, cxxenv->error_policy()); return; } (*cxxenv->feedback_callback_)(cxxenv, opcode, pct); }
int RepMgr::terminate() { try { dbenv.close(0); } catch (DbException dbe) { cerr << "error closing environment: " << dbe.what() << endl; } return 0; }
int DbEnv::_app_dispatch_intercept(DB_ENV *env, DBT *dbt, DB_LSN *lsn, db_recops op) { DbEnv *cxxenv = DbEnv::get_DbEnv(env); if (cxxenv == 0) { DB_ERROR(DbEnv::get_DbEnv(env), "DbEnv::app_dispatch_callback", EINVAL, ON_ERROR_UNKNOWN); return (EINVAL); } if (cxxenv->app_dispatch_callback_ == 0) { DB_ERROR(DbEnv::get_DbEnv(env), "DbEnv::app_dispatch_callback", EINVAL, cxxenv->error_policy()); return (EINVAL); } Dbt *cxxdbt = (Dbt *)dbt; DbLsn *cxxlsn = (DbLsn *)lsn; return ((*cxxenv->app_dispatch_callback_)(cxxenv, cxxdbt, cxxlsn, op)); }
void DBFlush(bool fShutdown) { // Flush log data to the actual data file // on all files that are not in use printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started"); if (!fDbEnvInit) return; { LOCK(cs_db); map<string, int>::iterator mi = mapFileUseCount.begin(); while (mi != mapFileUseCount.end()) { string strFile = (*mi).first; int nRefCount = (*mi).second; printf("%s refcount=%d\n", strFile.c_str(), nRefCount); if (nRefCount == 0) { // Move log data to the dat file CloseDb(strFile); printf("%s checkpoint\n", strFile.c_str()); dbenv.txn_checkpoint(0, 0, 0); if ((strFile != "blkindex.dat" && strFile != "addr.dat") || fDetachDB) { printf("%s detach\n", strFile.c_str()); dbenv.lsn_reset(strFile.c_str(), 0); } printf("%s closed\n", strFile.c_str()); mapFileUseCount.erase(mi++); } else mi++; } if (fShutdown) { char** listp; if (mapFileUseCount.empty()) { dbenv.log_archive(&listp, DB_ARCH_REMOVE); EnvShutdown(); } } } }
int DbEnv::_tx_recover_intercept(DB_ENV *env, DBT *dbt, DB_LSN *lsn, db_recops op) { if (env == 0) { DB_ERROR("DbEnv::tx_recover_callback", EINVAL, ON_ERROR_UNKNOWN); return (EINVAL); } DbEnv *cxxenv = (DbEnv *)env->cj_internal; if (cxxenv == 0) { DB_ERROR("DbEnv::tx_recover_callback", EINVAL, ON_ERROR_UNKNOWN); return (EINVAL); } if (cxxenv->tx_recover_callback_ == 0) { DB_ERROR("DbEnv::tx_recover_callback", EINVAL, cxxenv->error_policy()); return (EINVAL); } Dbt *cxxdbt = (Dbt *)dbt; DbLsn *cxxlsn = (DbLsn *)lsn; return ((*cxxenv->tx_recover_callback_)(cxxenv, cxxdbt, cxxlsn, op)); }
static int env_recover(char *progname) { DbEnv *dbenv; home_entry *hp; u_int32_t flags; int exitval, ret; for (hp = LIST_FIRST(&__dbsrv_home); hp != NULL; hp = LIST_NEXT(hp, entries)) { exitval = 0; dbenv = new DbEnv(DB_CXX_NO_EXCEPTIONS); if (__dbsrv_verbose == 1) { (void)dbenv->set_verbose(DB_VERB_RECOVERY, 1); (void)dbenv->set_verbose(DB_VERB_CHKPOINT, 1); } dbenv->set_errfile(stderr); dbenv->set_errpfx(progname); if (hp->passwd != NULL) (void)dbenv->set_encrypt(hp->passwd, DB_ENCRYPT_AES); /* * Initialize the env with DB_RECOVER. That is all we * have to do to run recovery. */ if (__dbsrv_verbose) printf("Running recovery on %s\n", hp->home); flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_USE_ENVIRON | DB_RECOVER; if ((ret = dbenv->open(hp->home, flags, 0)) != 0) { dbenv->err(ret, "DbEnv->open"); goto error; } if (0) { error: exitval = 1; } if ((ret = dbenv->close(0)) != 0) { exitval = 1; fprintf(stderr, "%s: dbenv->close: %s\n", progname, db_strerror(ret)); } if (exitval) return (exitval); } return (0); }
// Handle replication events of interest to this application. void RepMgrGSG::event_callback(DbEnv* dbenv, u_int32_t which, void *info) { APP_DATA *app = (APP_DATA*)dbenv->get_app_private(); info = 0; // Currently unused. switch (which) { case DB_EVENT_REP_MASTER: app->is_master = 1; break; case DB_EVENT_REP_CLIENT: app->is_master = 0; break; case DB_EVENT_REP_STARTUPDONE: // FALLTHROUGH case DB_EVENT_REP_NEWMASTER: // Ignore. break; default: dbenv->errx("ignoring event %d", which); } }