aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/db.h
diff options
context:
space:
mode:
authorRussell Yanofsky <russ@yanofsky.org>2018-10-24 16:08:54 -0400
committerChun Kuan Lee <ken2812221@gmail.com>2018-11-06 08:28:02 +0800
commitc456fbd8dfcc748e5ec9feaa57ec0f2900f99cde (patch)
tree4f360323dc4b35b70df471d3c72903750c9379ad /src/wallet/db.h
parent76ae7a1ac9b18437e89b54fa23bc22cff507ad42 (diff)
downloadbitcoin-c456fbd8dfcc748e5ec9feaa57ec0f2900f99cde.tar.xz
Refactor: Move m_db pointers into BerkeleyDatabase
This is a refactoring change that doesn't affect behavior. The motivation behind the change is give BerkeleyEnvironment objects access to BerkeleyDatabase objects so it will be possible to simplify the duplicate wallet check and more reliably avoid opening the same databases twice.
Diffstat (limited to 'src/wallet/db.h')
-rw-r--r--src/wallet/db.h16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/wallet/db.h b/src/wallet/db.h
index 8f96483a18..e9f89ac6cb 100644
--- a/src/wallet/db.h
+++ b/src/wallet/db.h
@@ -31,6 +31,8 @@ struct WalletDatabaseFileId {
bool operator==(const WalletDatabaseFileId& rhs) const;
};
+class BerkeleyDatabase;
+
class BerkeleyEnvironment
{
private:
@@ -43,7 +45,7 @@ private:
public:
std::unique_ptr<DbEnv> dbenv;
std::map<std::string, int> mapFileUseCount;
- std::map<std::string, Db*> mapDb;
+ std::map<std::string, std::reference_wrapper<BerkeleyDatabase>> m_databases;
std::unordered_map<std::string, WalletDatabaseFileId> m_fileids;
std::condition_variable_any m_db_in_use;
@@ -115,6 +117,8 @@ public:
nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0)
{
env = GetWalletEnv(wallet_path, strFile);
+ auto inserted = env->m_databases.emplace(strFile, std::ref(*this));
+ assert(inserted.second);
if (mock) {
env->Close();
env->Reset();
@@ -122,6 +126,13 @@ public:
}
}
+ ~BerkeleyDatabase() {
+ if (env) {
+ size_t erased = env->m_databases.erase(strFile);
+ assert(erased == 1);
+ }
+ }
+
/** Return object for accessing database at specified path. */
static std::unique_ptr<BerkeleyDatabase> Create(const fs::path& path)
{
@@ -161,6 +172,9 @@ public:
unsigned int nLastFlushed;
int64_t nLastWalletUpdate;
+ /** Database pointer. This is initialized lazily and reset during flushes, so it can be null. */
+ std::unique_ptr<Db> m_db;
+
private:
/** BerkeleyDB specific */
BerkeleyEnvironment *env;