mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 12:02:48 +01:00
merge bitcoin#19334: Introduce WalletDatabase abstract class
This commit is contained in:
parent
9879322b99
commit
044f3ab201
@ -26,7 +26,6 @@
|
||||
#include <univalue.h>
|
||||
|
||||
#ifdef ENABLE_WALLET
|
||||
#include <wallet/bdb.h>
|
||||
#include <wallet/db.h>
|
||||
#endif
|
||||
|
||||
|
@ -323,8 +323,17 @@ void BerkeleyEnvironment::CheckpointLSN(const std::string& strFile)
|
||||
dbenv->lsn_reset(strFile.c_str(), 0);
|
||||
}
|
||||
|
||||
BerkeleyDatabase::~BerkeleyDatabase()
|
||||
{
|
||||
if (env) {
|
||||
LOCK(cs_db);
|
||||
size_t erased = env->m_databases.erase(strFile);
|
||||
assert(erased == 1);
|
||||
env->m_fileids.erase(strFile);
|
||||
}
|
||||
}
|
||||
|
||||
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr)
|
||||
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database)
|
||||
{
|
||||
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
|
||||
fFlushOnClose = fFlushOnCloseIn;
|
||||
@ -398,11 +407,16 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
|
||||
fReadOnly = fTmp;
|
||||
}
|
||||
}
|
||||
++env->mapFileUseCount[strFilename];
|
||||
database.AddRef();
|
||||
strFile = strFilename;
|
||||
}
|
||||
}
|
||||
|
||||
void BerkeleyDatabase::Open(const char* mode)
|
||||
{
|
||||
throw std::logic_error("BerkeleyDatabase does not implement Open. This function should not be called.");
|
||||
}
|
||||
|
||||
void BerkeleyBatch::Flush()
|
||||
{
|
||||
if (activeTxn)
|
||||
@ -436,11 +450,7 @@ void BerkeleyBatch::Close()
|
||||
if (fFlushOnClose)
|
||||
Flush();
|
||||
|
||||
{
|
||||
LOCK(cs_db);
|
||||
--env->mapFileUseCount[strFile];
|
||||
}
|
||||
env->m_db_in_use.notify_all();
|
||||
m_database.RemoveRef();
|
||||
}
|
||||
|
||||
void BerkeleyEnvironment::CloseDb(const std::string& strFile)
|
||||
@ -652,7 +662,7 @@ bool BerkeleyDatabase::PeriodicFlush()
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BerkeleyDatabase::Backup(const std::string& strDest)
|
||||
bool BerkeleyDatabase::Backup(const std::string& strDest) const
|
||||
{
|
||||
if (IsDummy()) {
|
||||
return false;
|
||||
@ -693,22 +703,17 @@ bool BerkeleyDatabase::Backup(const std::string& strDest)
|
||||
}
|
||||
}
|
||||
|
||||
void BerkeleyDatabase::Flush(bool shutdown)
|
||||
void BerkeleyDatabase::Flush()
|
||||
{
|
||||
if (!IsDummy()) {
|
||||
env->Flush(shutdown);
|
||||
if (shutdown) {
|
||||
LOCK(cs_db);
|
||||
g_dbenvs.erase(env->Directory().string());
|
||||
env = nullptr;
|
||||
} else {
|
||||
// TODO: To avoid g_dbenvs.erase erasing the environment prematurely after the
|
||||
// first database shutdown when multiple databases are open in the same
|
||||
// environment, should replace raw database `env` pointers with shared or weak
|
||||
// pointers, or else separate the database and environment shutdowns so
|
||||
// environments can be shut down after databases.
|
||||
env->m_fileids.erase(strFile);
|
||||
}
|
||||
env->Flush(false);
|
||||
}
|
||||
}
|
||||
|
||||
void BerkeleyDatabase::Close()
|
||||
{
|
||||
if (!IsDummy()) {
|
||||
env->Flush(true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -850,7 +855,22 @@ bool BerkeleyBatch::HasKey(CDataStream&& key)
|
||||
return ret == 0;
|
||||
}
|
||||
|
||||
std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
|
||||
void BerkeleyDatabase::AddRef()
|
||||
{
|
||||
LOCK(cs_db);
|
||||
++env->mapFileUseCount[strFile];
|
||||
}
|
||||
|
||||
void BerkeleyDatabase::RemoveRef()
|
||||
{
|
||||
{
|
||||
LOCK(cs_db);
|
||||
--env->mapFileUseCount[strFile];
|
||||
}
|
||||
env->m_db_in_use.notify_all();
|
||||
}
|
||||
|
||||
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
|
||||
{
|
||||
return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);
|
||||
}
|
||||
|
@ -96,56 +96,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);
|
||||
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(bool shutdown);
|
||||
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() override;
|
||||
/* flush the wallet passively (TRY_LOCK)
|
||||
ideal to be called periodically */
|
||||
bool PeriodicFlush();
|
||||
bool PeriodicFlush() override;
|
||||
|
||||
void IncrementUpdateCounter();
|
||||
void IncrementUpdateCounter() override;
|
||||
|
||||
void ReloadDbEnv();
|
||||
|
||||
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(std::string& error);
|
||||
bool Verify(std::string& error) override;
|
||||
|
||||
/**
|
||||
* Pointer to shared database environment.
|
||||
@ -162,7 +165,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;
|
||||
@ -211,6 +214,7 @@ protected:
|
||||
bool fReadOnly;
|
||||
bool fFlushOnClose;
|
||||
BerkeleyEnvironment *env;
|
||||
BerkeleyDatabase& m_database;
|
||||
|
||||
public:
|
||||
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
|
||||
|
@ -10,6 +10,8 @@
|
||||
#include <fs.h>
|
||||
#include <streams.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
/** Given a wallet directory path or legacy file path, return path to main data file in the wallet database. */
|
||||
@ -94,4 +96,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(std::string& 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
|
||||
|
@ -96,14 +96,14 @@ void FlushWallets()
|
||||
it->second->ResetPool();
|
||||
it->second->StopMixing();
|
||||
}
|
||||
pwallet->Flush(false);
|
||||
pwallet->Flush();
|
||||
}
|
||||
}
|
||||
|
||||
void StopWallets()
|
||||
{
|
||||
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
|
||||
pwallet->Flush(true);
|
||||
pwallet->Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -811,9 +811,14 @@ std::set<uint256> CWallet::GetConflicts(const uint256& txid) const
|
||||
return result;
|
||||
}
|
||||
|
||||
void CWallet::Flush(bool shutdown)
|
||||
void CWallet::Flush()
|
||||
{
|
||||
database->Flush(shutdown);
|
||||
database->Flush();
|
||||
}
|
||||
|
||||
void CWallet::Close()
|
||||
{
|
||||
database->Close();
|
||||
}
|
||||
|
||||
void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)
|
||||
|
@ -1113,7 +1113,10 @@ public:
|
||||
std::set<uint256> GetConflicts(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
|
||||
//! Flush wallet (bitdb flush)
|
||||
void Flush(bool shutdown=false);
|
||||
void Flush();
|
||||
|
||||
//! Close wallet database
|
||||
void Close();
|
||||
|
||||
/** Wallet is about to be unloaded */
|
||||
boost::signals2::signal<void ()> NotifyUnload;
|
||||
|
@ -820,20 +820,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>(), "");
|
||||
}
|
||||
|
@ -42,9 +42,6 @@ class CWalletTx;
|
||||
class uint160;
|
||||
class uint256;
|
||||
|
||||
/** Backend-agnostic database type. */
|
||||
using WalletDatabase = BerkeleyDatabase;
|
||||
|
||||
/** Error statuses for the wallet database */
|
||||
enum class DBErrors
|
||||
{
|
||||
@ -230,7 +227,7 @@ public:
|
||||
//! Abort current transaction
|
||||
bool TxnAbort();
|
||||
private:
|
||||
std::unique_ptr<BerkeleyBatch> m_batch;
|
||||
std::unique_ptr<DatabaseBatch> m_batch;
|
||||
WalletDatabase& m_database;
|
||||
};
|
||||
|
||||
@ -244,12 +241,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
|
||||
|
@ -17,7 +17,7 @@ namespace WalletTool {
|
||||
static void WalletToolReleaseWallet(CWallet* wallet)
|
||||
{
|
||||
wallet->WalletLogPrintf("Releasing wallet\n");
|
||||
wallet->Flush(true);
|
||||
wallet->Close();
|
||||
delete wallet;
|
||||
}
|
||||
|
||||
@ -135,7 +135,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
|
||||
std::shared_ptr<CWallet> wallet_instance = CreateWallet(name, path);
|
||||
if (wallet_instance) {
|
||||
WalletShowInfo(wallet_instance.get());
|
||||
wallet_instance->Flush(true);
|
||||
wallet_instance->Close();
|
||||
}
|
||||
} else if (command == "info" || command == "salvage") {
|
||||
if (!fs::exists(path)) {
|
||||
@ -147,7 +147,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
|
||||
std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);
|
||||
if (!wallet_instance) return false;
|
||||
WalletShowInfo(wallet_instance.get());
|
||||
wallet_instance->Flush(true);
|
||||
wallet_instance->Close();
|
||||
} else if (command == "salvage") {
|
||||
return SalvageWallet(path);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user