Skip to content

Commit d416ae5

Browse files
committed
walletdb: Introduce WalletDatabase abstract class
Make WalletDatabase actually an abstract class and not just a typedef for BerkeleyDatabase. Have BerkeleyDatabase inherit this class.
1 parent 2179dbc commit d416ae5

File tree

6 files changed

+84
-33
lines changed

6 files changed

+84
-33
lines changed

src/qt/rpcconsole.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <univalue.h>
2525

2626
#ifdef ENABLE_WALLET
27-
#include <wallet/bdb.h>
2827
#include <wallet/db.h>
2928
#include <wallet/wallet.h>
3029
#endif

src/wallet/bdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ void BerkeleyDatabase::RemoveRef()
862862
env->m_db_in_use.notify_all();
863863
}
864864

865-
std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
865+
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
866866
{
867867
return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);
868868
}

src/wallet/bdb.h

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,64 +98,59 @@ class BerkeleyBatch;
9898
/** An instance of this class represents one database.
9999
* For BerkeleyDB this is just a (env, strFile) tuple.
100100
**/
101-
class BerkeleyDatabase
101+
class BerkeleyDatabase : public WalletDatabase
102102
{
103103
friend class BerkeleyBatch;
104104
public:
105105
/** Create dummy DB handle */
106-
BerkeleyDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
106+
BerkeleyDatabase() : WalletDatabase(), env(nullptr)
107107
{
108108
}
109109

110110
/** Create DB handle to real database */
111111
BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, std::string filename) :
112-
nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(std::move(env)), strFile(std::move(filename))
112+
WalletDatabase(), env(std::move(env)), strFile(std::move(filename))
113113
{
114114
auto inserted = this->env->m_databases.emplace(strFile, std::ref(*this));
115115
assert(inserted.second);
116116
}
117117

118-
~BerkeleyDatabase();
118+
~BerkeleyDatabase() override;
119119

120120
/** Open the database if it is not already opened.
121121
* Dummy function, doesn't do anything right now, but is needed for class abstraction */
122-
void Open(const char* mode);
122+
void Open(const char* mode) override;
123123

124124
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
125125
*/
126-
bool Rewrite(const char* pszSkip=nullptr);
126+
bool Rewrite(const char* pszSkip=nullptr) override;
127127

128128
/** Indicate the a new database user has began using the database. */
129-
void AddRef();
129+
void AddRef() override;
130130
/** Indicate that database user has stopped using the database and that it could be flushed or closed. */
131-
void RemoveRef();
131+
void RemoveRef() override;
132132

133133
/** Back up the entire database to a file.
134134
*/
135-
bool Backup(const std::string& strDest) const;
135+
bool Backup(const std::string& strDest) const override;
136136

137137
/** Make sure all changes are flushed to database file.
138138
*/
139-
void Flush();
139+
void Flush() override;
140140
/** Flush to the database file and close the database.
141141
* Also close the environment if no other databases are open in it.
142142
*/
143-
void Close();
143+
void Close() override;
144144
/* flush the wallet passively (TRY_LOCK)
145145
ideal to be called periodically */
146-
bool PeriodicFlush();
146+
bool PeriodicFlush() override;
147147

148-
void IncrementUpdateCounter();
149-
150-
void ReloadDbEnv();
148+
void IncrementUpdateCounter() override;
151149

152-
std::atomic<unsigned int> nUpdateCounter;
153-
unsigned int nLastSeen;
154-
unsigned int nLastFlushed;
155-
int64_t nLastWalletUpdate;
150+
void ReloadDbEnv() override;
156151

157152
/** Verifies the environment and database file */
158-
bool Verify(bilingual_str& error);
153+
bool Verify(bilingual_str& error) override;
159154

160155
/**
161156
* Pointer to shared database environment.
@@ -172,7 +167,7 @@ class BerkeleyDatabase
172167
std::unique_ptr<Db> m_db;
173168

174169
/** Make a BerkeleyBatch connected to this database */
175-
std::unique_ptr<BerkeleyBatch> MakeBatch(const char* mode, bool flush_on_close);
170+
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override;
176171

177172
private:
178173
std::string strFile;

src/wallet/db.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
#include <fs.h>
1111
#include <streams.h>
1212

13+
#include <atomic>
14+
#include <memory>
1315
#include <string>
1416

17+
struct bilingual_str;
18+
1519
/** Given a wallet directory path or legacy file path, return path to main data file in the wallet database. */
1620
fs::path WalletDataFilePath(const fs::path& wallet_path);
1721
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
@@ -94,4 +98,60 @@ class DatabaseBatch
9498
virtual bool TxnAbort() = 0;
9599
};
96100

101+
/** An instance of this class represents one database.
102+
**/
103+
class WalletDatabase
104+
{
105+
public:
106+
/** Create dummy DB handle */
107+
WalletDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0) {}
108+
virtual ~WalletDatabase() {};
109+
110+
/** Open the database if it is not already opened. */
111+
virtual void Open(const char* mode) = 0;
112+
113+
//! Counts the number of active database users to be sure that the database is not closed while someone is using it
114+
std::atomic<int> m_refcount{0};
115+
/** Indicate the a new database user has began using the database. Increments m_refcount */
116+
virtual void AddRef() = 0;
117+
/** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */
118+
virtual void RemoveRef() = 0;
119+
120+
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
121+
*/
122+
virtual bool Rewrite(const char* pszSkip=nullptr) = 0;
123+
124+
/** Back up the entire database to a file.
125+
*/
126+
virtual bool Backup(const std::string& strDest) const = 0;
127+
128+
/** Make sure all changes are flushed to database file.
129+
*/
130+
virtual void Flush() = 0;
131+
/** Flush to the database file and close the database.
132+
* Also close the environment if no other databases are open in it.
133+
*/
134+
virtual void Close() = 0;
135+
/* flush the wallet passively (TRY_LOCK)
136+
ideal to be called periodically */
137+
virtual bool PeriodicFlush() = 0;
138+
139+
virtual void IncrementUpdateCounter() = 0;
140+
141+
virtual void ReloadDbEnv() = 0;
142+
143+
std::atomic<unsigned int> nUpdateCounter;
144+
unsigned int nLastSeen;
145+
unsigned int nLastFlushed;
146+
int64_t nLastWalletUpdate;
147+
148+
/** Verifies the environment and database file */
149+
virtual bool Verify(bilingual_str& error) = 0;
150+
151+
std::string m_file_path;
152+
153+
/** Make a DatabaseBatch connected to this database */
154+
virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0;
155+
};
156+
97157
#endif // BITCOIN_WALLET_DB_H

src/wallet/walletdb.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,20 +1012,20 @@ bool IsWalletLoaded(const fs::path& wallet_path)
10121012
}
10131013

10141014
/** Return object for accessing database at specified path. */
1015-
std::unique_ptr<BerkeleyDatabase> CreateWalletDatabase(const fs::path& path)
1015+
std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path)
10161016
{
10171017
std::string filename;
10181018
return MakeUnique<BerkeleyDatabase>(GetWalletEnv(path, filename), std::move(filename));
10191019
}
10201020

10211021
/** Return object for accessing dummy database with no read/write capabilities. */
1022-
std::unique_ptr<BerkeleyDatabase> CreateDummyWalletDatabase()
1022+
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase()
10231023
{
10241024
return MakeUnique<BerkeleyDatabase>();
10251025
}
10261026

10271027
/** Return object for accessing temporary in-memory database. */
1028-
std::unique_ptr<BerkeleyDatabase> CreateMockWalletDatabase()
1028+
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase()
10291029
{
10301030
return MakeUnique<BerkeleyDatabase>(std::make_shared<BerkeleyEnvironment>(), "");
10311031
}

src/wallet/walletdb.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ class CWalletTx;
4040
class uint160;
4141
class uint256;
4242

43-
/** Backend-agnostic database type. */
44-
using WalletDatabase = BerkeleyDatabase;
45-
4643
/** Error statuses for the wallet database */
4744
enum class DBErrors
4845
{
@@ -280,7 +277,7 @@ class WalletBatch
280277
//! Abort current transaction
281278
bool TxnAbort();
282279
private:
283-
std::unique_ptr<BerkeleyBatch> m_batch;
280+
std::unique_ptr<DatabaseBatch> m_batch;
284281
WalletDatabase& m_database;
285282
};
286283

@@ -294,12 +291,12 @@ bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, st
294291
bool IsWalletLoaded(const fs::path& wallet_path);
295292

296293
/** Return object for accessing database at specified path. */
297-
std::unique_ptr<BerkeleyDatabase> CreateWalletDatabase(const fs::path& path);
294+
std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path);
298295

299296
/** Return object for accessing dummy database with no read/write capabilities. */
300-
std::unique_ptr<BerkeleyDatabase> CreateDummyWalletDatabase();
297+
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
301298

302299
/** Return object for accessing temporary in-memory database. */
303-
std::unique_ptr<BerkeleyDatabase> CreateMockWalletDatabase();
300+
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
304301

305302
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)