diff options
author | Andrew Chow <achow101-github@achow101.com> | 2020-06-15 16:24:00 -0400 |
---|---|---|
committer | Andrew Chow <achow101-github@achow101.com> | 2020-07-14 11:07:16 -0400 |
commit | d416ae560e46a4846a3fd5990b7d390d2ef30ec8 (patch) | |
tree | 3e859a51aba093050caabfc31ab873a27c16f39b /src/wallet | |
parent | 2179dbcbcd0b9bef7ad9c907b85294b9a1bccf0f (diff) |
walletdb: Introduce WalletDatabase abstract class
Make WalletDatabase actually an abstract class and not just a typedef
for BerkeleyDatabase. Have BerkeleyDatabase inherit this class.
Diffstat (limited to 'src/wallet')
-rw-r--r-- | src/wallet/bdb.cpp | 2 | ||||
-rw-r--r-- | src/wallet/bdb.h | 37 | ||||
-rw-r--r-- | src/wallet/db.h | 60 | ||||
-rw-r--r-- | src/wallet/walletdb.cpp | 6 | ||||
-rw-r--r-- | src/wallet/walletdb.h | 11 |
5 files changed, 84 insertions, 32 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index 06ee9ae5ee..7fed9601b0 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -862,7 +862,7 @@ void BerkeleyDatabase::RemoveRef() env->m_db_in_use.notify_all(); } -std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close) +std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close) { return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close); } diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h index bbaeefa414..b8b585631a 100644 --- a/src/wallet/bdb.h +++ b/src/wallet/bdb.h @@ -98,64 +98,59 @@ class BerkeleyBatch; /** An instance of this class represents one database. * For BerkeleyDB this is just a (env, strFile) tuple. **/ -class BerkeleyDatabase +class BerkeleyDatabase : public WalletDatabase { friend class BerkeleyBatch; public: /** Create dummy DB handle */ - BerkeleyDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr) + BerkeleyDatabase() : WalletDatabase(), env(nullptr) { } /** Create DB handle to real database */ BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, std::string filename) : - nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(std::move(env)), strFile(std::move(filename)) + WalletDatabase(), env(std::move(env)), strFile(std::move(filename)) { auto inserted = this->env->m_databases.emplace(strFile, std::ref(*this)); assert(inserted.second); } - ~BerkeleyDatabase(); + ~BerkeleyDatabase() override; /** Open the database if it is not already opened. * Dummy function, doesn't do anything right now, but is needed for class abstraction */ - void Open(const char* mode); + void Open(const char* mode) override; /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero */ - bool Rewrite(const char* pszSkip=nullptr); + bool Rewrite(const char* pszSkip=nullptr) override; /** Indicate the a new database user has began using the database. */ - void AddRef(); + void AddRef() override; /** Indicate that database user has stopped using the database and that it could be flushed or closed. */ - void RemoveRef(); + void RemoveRef() override; /** Back up the entire database to a file. */ - bool Backup(const std::string& strDest) const; + bool Backup(const std::string& strDest) const override; /** Make sure all changes are flushed to database file. */ - void Flush(); + void Flush() override; /** Flush to the database file and close the database. * Also close the environment if no other databases are open in it. */ - void Close(); + void Close() override; /* flush the wallet passively (TRY_LOCK) ideal to be called periodically */ - bool PeriodicFlush(); + bool PeriodicFlush() override; - void IncrementUpdateCounter(); - - void ReloadDbEnv(); + void IncrementUpdateCounter() override; - std::atomic<unsigned int> nUpdateCounter; - unsigned int nLastSeen; - unsigned int nLastFlushed; - int64_t nLastWalletUpdate; + void ReloadDbEnv() override; /** Verifies the environment and database file */ - bool Verify(bilingual_str& error); + bool Verify(bilingual_str& error) override; /** * Pointer to shared database environment. @@ -172,7 +167,7 @@ public: std::unique_ptr<Db> m_db; /** Make a BerkeleyBatch connected to this database */ - std::unique_ptr<BerkeleyBatch> MakeBatch(const char* mode, bool flush_on_close); + std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override; private: std::string strFile; diff --git a/src/wallet/db.h b/src/wallet/db.h index 76668f8dc2..12dc1cc96b 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -10,8 +10,12 @@ #include <fs.h> #include <streams.h> +#include <atomic> +#include <memory> #include <string> +struct bilingual_str; + /** Given a wallet directory path or legacy file path, return path to main data file in the wallet database. */ fs::path WalletDataFilePath(const fs::path& wallet_path); void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename); @@ -94,4 +98,60 @@ public: virtual bool TxnAbort() = 0; }; +/** An instance of this class represents one database. + **/ +class WalletDatabase +{ +public: + /** Create dummy DB handle */ + WalletDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0) {} + virtual ~WalletDatabase() {}; + + /** Open the database if it is not already opened. */ + virtual void Open(const char* mode) = 0; + + //! Counts the number of active database users to be sure that the database is not closed while someone is using it + std::atomic<int> m_refcount{0}; + /** Indicate the a new database user has began using the database. Increments m_refcount */ + virtual void AddRef() = 0; + /** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */ + virtual void RemoveRef() = 0; + + /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero + */ + virtual bool Rewrite(const char* pszSkip=nullptr) = 0; + + /** Back up the entire database to a file. + */ + virtual bool Backup(const std::string& strDest) const = 0; + + /** Make sure all changes are flushed to database file. + */ + virtual void Flush() = 0; + /** Flush to the database file and close the database. + * Also close the environment if no other databases are open in it. + */ + virtual void Close() = 0; + /* flush the wallet passively (TRY_LOCK) + ideal to be called periodically */ + virtual bool PeriodicFlush() = 0; + + virtual void IncrementUpdateCounter() = 0; + + virtual void ReloadDbEnv() = 0; + + std::atomic<unsigned int> nUpdateCounter; + unsigned int nLastSeen; + unsigned int nLastFlushed; + int64_t nLastWalletUpdate; + + /** Verifies the environment and database file */ + virtual bool Verify(bilingual_str& error) = 0; + + std::string m_file_path; + + /** Make a DatabaseBatch connected to this database */ + virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0; +}; + #endif // BITCOIN_WALLET_DB_H diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 1478687bf9..8c409b40cd 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -1012,20 +1012,20 @@ bool IsWalletLoaded(const fs::path& wallet_path) } /** Return object for accessing database at specified path. */ -std::unique_ptr<BerkeleyDatabase> CreateWalletDatabase(const fs::path& path) +std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path) { std::string filename; return MakeUnique<BerkeleyDatabase>(GetWalletEnv(path, filename), std::move(filename)); } /** Return object for accessing dummy database with no read/write capabilities. */ -std::unique_ptr<BerkeleyDatabase> CreateDummyWalletDatabase() +std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase() { return MakeUnique<BerkeleyDatabase>(); } /** Return object for accessing temporary in-memory database. */ -std::unique_ptr<BerkeleyDatabase> CreateMockWalletDatabase() +std::unique_ptr<WalletDatabase> CreateMockWalletDatabase() { return MakeUnique<BerkeleyDatabase>(std::make_shared<BerkeleyEnvironment>(), ""); } diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 6b55361c07..7c5bf7652b 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -40,9 +40,6 @@ class CWalletTx; class uint160; class uint256; -/** Backend-agnostic database type. */ -using WalletDatabase = BerkeleyDatabase; - /** Error statuses for the wallet database */ enum class DBErrors { @@ -280,7 +277,7 @@ public: //! Abort current transaction bool TxnAbort(); private: - std::unique_ptr<BerkeleyBatch> m_batch; + std::unique_ptr<DatabaseBatch> m_batch; WalletDatabase& m_database; }; @@ -294,12 +291,12 @@ bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, st bool IsWalletLoaded(const fs::path& wallet_path); /** Return object for accessing database at specified path. */ -std::unique_ptr<BerkeleyDatabase> CreateWalletDatabase(const fs::path& path); +std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path); /** Return object for accessing dummy database with no read/write capabilities. */ -std::unique_ptr<BerkeleyDatabase> CreateDummyWalletDatabase(); +std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase(); /** Return object for accessing temporary in-memory database. */ -std::unique_ptr<BerkeleyDatabase> CreateMockWalletDatabase(); +std::unique_ptr<WalletDatabase> CreateMockWalletDatabase(); #endif // BITCOIN_WALLET_WALLETDB_H |