aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/bdb.h
diff options
context:
space:
mode:
authorSamuel Dobson <dobsonsa68@gmail.com>2020-07-23 14:49:17 +1200
committerSamuel Dobson <dobsonsa68@gmail.com>2020-07-23 15:22:25 +1200
commit9d4b3d86b694ac6e56495e1955f6bf5ff584cbb9 (patch)
tree49cc2ecdb359158e25b0bb56982a0eb97e62b8f0 /src/wallet/bdb.h
parentccef10261efc235c8fcc8aad54556615b0cc23be (diff)
parentd416ae560e46a4846a3fd5990b7d390d2ef30ec8 (diff)
Merge #19334: wallet: Introduce WalletDatabase abstract class
d416ae560e46a4846a3fd5990b7d390d2ef30ec8 walletdb: Introduce WalletDatabase abstract class (Andrew Chow) 2179dbcbcd0b9bef7ad9c907b85294b9a1bccf0f walletdb: Add BerkeleyDatabase::Open dummy function (Andrew Chow) 71d28e7cdca1c8553531bb3a4725d7916363ec5c walletdb: Introduce AddRef and RemoveRef functions (Andrew Chow) 27b27663849932971eb5deadb1f19234b9cd97ea walletdb: Move BerkeleyDatabase::Flush(true) to Close() (Andrew Chow) Pull request description: A `WalletDatabase` abstract class is created from `BerkeleyDatabase` and is implemented by `BerkeleyDatabase`. First, to get to the point that this is possible, 4 functions need to be added to `BerkeleyDatabase`: `AddRef`, `RemoveRef`, `Open`, and `Close`. First the increment and decrement of `mapFileUseCount` is refactored into separate functions `AddRef` and `RemoveRef`. `Open` is introduced as a dummy function. This will raise an exception so that it always fails. `Close` is refactored from `Flush`. The `shutdown` argument in `Flush` is removed and instead `Flush(true)` is now the `Close` function. Split from #18971 Requires #19325 ACKs for top commit: ryanofsky: Code review ACK d416ae560e46a4846a3fd5990b7d390d2ef30ec8. Only changes since last review were rebasing after base PR #19334 merge, and adding cs_db lock in BerkeleyDatabase destructor, which should avoid races accessing env->m_databases and env->m_fileids fjahr: Code review ACK d416ae560e46a4846a3fd5990b7d390d2ef30ec8 meshcollider: Code review & test run ACK d416ae560e46a4846a3fd5990b7d390d2ef30ec8 Tree-SHA512: 98d05ec093d7446c4488e2b0914584222a331e9a2f4d5be6af98e3f6d78fdd8e75526c12f91a8a52d4820c25bce02aa02aabe92d38bee7eb2fce07d0691b7b0d
Diffstat (limited to 'src/wallet/bdb.h')
-rw-r--r--src/wallet/bdb.h50
1 files changed, 27 insertions, 23 deletions
diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h
index 1b9b5de9f9..ef3b81d4d6 100644
--- a/src/wallet/bdb.h
+++ b/src/wallet/bdb.h
@@ -98,56 +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() {
- if (env) {
- size_t erased = env->m_databases.erase(strFile);
- assert(erased == 1);
- }
- }
+ ~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) 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() override;
+ /** Indicate that database user has stopped using the database and that it could be flushed or closed. */
+ 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 disk.
+ /** Make sure all changes are flushed to database file.
+ */
+ 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 Flush(bool shutdown);
+ 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.
@@ -164,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;
@@ -213,6 +216,7 @@ protected:
bool fReadOnly;
bool fFlushOnClose;
BerkeleyEnvironment *env;
+ BerkeleyDatabase& m_database;
public:
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);