mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-26 22:08:58 +01:00
Merge #19334: wallet: Introduce WalletDatabase abstract class
d416ae560ewalletdb: Introduce WalletDatabase abstract class (Andrew Chow)2179dbcbcdwalletdb: Add BerkeleyDatabase::Open dummy function (Andrew Chow)71d28e7cdcwalletdb: Introduce AddRef and RemoveRef functions (Andrew Chow)27b2766384walletdb: 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 ACKd416ae560e. 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 ACKd416ae560emeshcollider: Code review & test run ACKd416ae560eTree-SHA512: 98d05ec093d7446c4488e2b0914584222a331e9a2f4d5be6af98e3f6d78fdd8e75526c12f91a8a52d4820c25bce02aa02aabe92d38bee7eb2fce07d0691b7b0d
This commit is contained in:
@@ -312,8 +312,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;
|
||||
@@ -388,11 +397,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)
|
||||
@@ -426,11 +440,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)
|
||||
@@ -675,22 +685,17 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) const
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -832,7 +837,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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user