From cd9696fc97fc06831c1edede62a063028f2afe75 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Sun, 13 May 2012 21:37:39 -0400 Subject: Encapsulate BDB environment inside new CDBEnv class Cleans up and organizes several scattered functions and variables related to the BDB env. Class CDBInit() existed to provide a guaranteed-via-C++-destructor cleanup of the db environment. A formal CDBEnv class provides all of this inside a single wrapper. --- src/db.h | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/db.h') diff --git a/src/db.h b/src/db.h index 0ff06e40a8..e2983e0787 100644 --- a/src/db.h +++ b/src/db.h @@ -25,14 +25,36 @@ class CWallet; class CWalletTx; extern unsigned int nWalletDBUpdated; -extern bool fDetachDB; -extern DbEnv dbenv; -extern void DBFlush(bool fShutdown); void ThreadFlushWalletDB(void* parg); bool BackupWallet(const CWallet& wallet, const std::string& strDest); +class CDBEnv +{ +private: + bool fDetachDB; + bool fDbEnvInit; + boost::filesystem::path pathEnv; + + void EnvShutdown(); + +public: + mutable CCriticalSection cs_db; + DbEnv dbenv; + + CDBEnv(); + ~CDBEnv(); + bool Open(boost::filesystem::path pathEnv_); + void Close(); + void Flush(bool fShutdown); + void CheckpointLSN(std::string strFile); + void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; } +}; + +extern CDBEnv bitdb; + + /** RAII class that provides access to a Berkeley database */ class CDB { @@ -216,7 +238,7 @@ public: if (!pdb) return false; DbTxn* ptxn = NULL; - int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_WRITE_NOSYNC); + int ret = bitdb.dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_WRITE_NOSYNC); if (!ptxn || ret != 0) return false; vTxn.push_back(ptxn); -- cgit v1.2.3 From 24b57e3c6a1e5c00e10ee19803dd86d821ffebb2 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Mon, 14 May 2012 12:33:34 -0400 Subject: Create CDBEnv::TxnBegin(), and use it in CDB::TxnBegin() --- src/db.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/db.h') diff --git a/src/db.h b/src/db.h index e2983e0787..aff5e367b4 100644 --- a/src/db.h +++ b/src/db.h @@ -50,6 +50,15 @@ public: void Flush(bool fShutdown); void CheckpointLSN(std::string strFile); void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; } + + DbTxn *TxnBegin(DbTxn *baseTxn, int flags=DB_TXN_WRITE_NOSYNC) + { + DbTxn* ptxn = NULL; + int ret = dbenv.txn_begin(baseTxn, &ptxn, flags); + if (!ptxn || ret != 0) + return NULL; + return ptxn; + } }; extern CDBEnv bitdb; @@ -237,9 +246,8 @@ public: { if (!pdb) return false; - DbTxn* ptxn = NULL; - int ret = bitdb.dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_WRITE_NOSYNC); - if (!ptxn || ret != 0) + DbTxn* ptxn = bitdb.TxnBegin(GetTxn()); + if (!ptxn) return false; vTxn.push_back(ptxn); return true; -- cgit v1.2.3 From 8b1202c52c4d8f42c23b02a4cfdb097663e6e7b0 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Mon, 14 May 2012 12:39:29 -0400 Subject: Remove unused nested BDB transaction support --- src/db.h | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) (limited to 'src/db.h') diff --git a/src/db.h b/src/db.h index aff5e367b4..7af4d5e1cd 100644 --- a/src/db.h +++ b/src/db.h @@ -51,10 +51,10 @@ public: void CheckpointLSN(std::string strFile); void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; } - DbTxn *TxnBegin(DbTxn *baseTxn, int flags=DB_TXN_WRITE_NOSYNC) + DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC) { DbTxn* ptxn = NULL; - int ret = dbenv.txn_begin(baseTxn, &ptxn, flags); + int ret = dbenv.txn_begin(NULL, &ptxn, flags); if (!ptxn || ret != 0) return NULL; return ptxn; @@ -70,7 +70,7 @@ class CDB protected: Db* pdb; std::string strFile; - std::vector vTxn; + DbTxn *activeTxn; bool fReadOnly; explicit CDB(const char* pszFile, const char* pszMode="r+"); @@ -97,7 +97,7 @@ protected: // Read Dbt datValue; datValue.set_flags(DB_DBT_MALLOC); - int ret = pdb->get(GetTxn(), &datKey, &datValue, 0); + int ret = pdb->get(activeTxn, &datKey, &datValue, 0); memset(datKey.get_data(), 0, datKey.get_size()); if (datValue.get_data() == NULL) return false; @@ -133,7 +133,7 @@ protected: Dbt datValue(&ssValue[0], ssValue.size()); // Write - int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE)); + int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE)); // Clear memory in case it was a private key memset(datKey.get_data(), 0, datKey.get_size()); @@ -156,7 +156,7 @@ protected: Dbt datKey(&ssKey[0], ssKey.size()); // Erase - int ret = pdb->del(GetTxn(), &datKey, 0); + int ret = pdb->del(activeTxn, &datKey, 0); // Clear memory memset(datKey.get_data(), 0, datKey.get_size()); @@ -176,7 +176,7 @@ protected: Dbt datKey(&ssKey[0], ssKey.size()); // Exists - int ret = pdb->exists(GetTxn(), &datKey, 0); + int ret = pdb->exists(activeTxn, &datKey, 0); // Clear memory memset(datKey.get_data(), 0, datKey.get_size()); @@ -233,45 +233,33 @@ protected: return 0; } - DbTxn* GetTxn() - { - if (!vTxn.empty()) - return vTxn.back(); - else - return NULL; - } - public: bool TxnBegin() { - if (!pdb) + if (!pdb || activeTxn) return false; - DbTxn* ptxn = bitdb.TxnBegin(GetTxn()); + DbTxn* ptxn = bitdb.TxnBegin(); if (!ptxn) return false; - vTxn.push_back(ptxn); + activeTxn = ptxn; return true; } bool TxnCommit() { - if (!pdb) - return false; - if (vTxn.empty()) + if (!pdb || !activeTxn) return false; - int ret = vTxn.back()->commit(0); - vTxn.pop_back(); + int ret = activeTxn->commit(0); + activeTxn = NULL; return (ret == 0); } bool TxnAbort() { - if (!pdb) - return false; - if (vTxn.empty()) + if (!pdb || !activeTxn) return false; - int ret = vTxn.back()->abort(); - vTxn.pop_back(); + int ret = activeTxn->abort(); + activeTxn = NULL; return (ret == 0); } -- cgit v1.2.3 From ffe8b77a617efd802a9d4ba7e42b163fbd9a250b Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Fri, 18 May 2012 02:49:50 -0400 Subject: Further CDBEnv encapsulation work. --- src/db.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/db.h') diff --git a/src/db.h b/src/db.h index 7af4d5e1cd..abc58c4b1e 100644 --- a/src/db.h +++ b/src/db.h @@ -42,6 +42,8 @@ private: public: mutable CCriticalSection cs_db; DbEnv dbenv; + std::map mapFileUseCount; + std::map mapDb; CDBEnv(); ~CDBEnv(); @@ -51,6 +53,8 @@ public: void CheckpointLSN(std::string strFile); void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; } + void CloseDb(const std::string& strFile); + DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC) { DbTxn* ptxn = NULL; -- cgit v1.2.3