walletdb: Move BerkeleyDatabase::Flush(true) to Close()

Instead of having Flush optionally shutdown the database and
environment, add a Close() function that does that.
This commit is contained in:
Andrew Chow 2020-06-15 17:59:24 -04:00
parent 834ac4c0f5
commit 27b2766384
6 changed files with 41 additions and 30 deletions

View file

@ -324,6 +324,15 @@ void BerkeleyEnvironment::CheckpointLSN(const std::string& strFile)
dbenv->lsn_reset(strFile.c_str(), 0); 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)
{ {
@ -685,22 +694,17 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) const
} }
} }
void BerkeleyDatabase::Flush(bool shutdown) void BerkeleyDatabase::Flush()
{ {
if (!IsDummy()) { if (!IsDummy()) {
env->Flush(shutdown); env->Flush(false);
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);
} }
}
void BerkeleyDatabase::Close()
{
if (!IsDummy()) {
env->Flush(true);
} }
} }

View file

@ -115,12 +115,7 @@ public:
assert(inserted.second); assert(inserted.second);
} }
~BerkeleyDatabase() { ~BerkeleyDatabase();
if (env) {
size_t erased = env->m_databases.erase(strFile);
assert(erased == 1);
}
}
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
*/ */
@ -130,9 +125,13 @@ public:
*/ */
bool Backup(const std::string& strDest) const; bool Backup(const std::string& strDest) const;
/** Make sure all changes are flushed to disk. /** Make sure all changes are flushed to database file.
*/ */
void Flush(bool shutdown); void Flush();
/** Flush to the database file and close the database.
* Also close the environment if no other databases are open in it.
*/
void Close();
/* flush the wallet passively (TRY_LOCK) /* flush the wallet passively (TRY_LOCK)
ideal to be called periodically */ ideal to be called periodically */
bool PeriodicFlush(); bool PeriodicFlush();

View file

@ -99,14 +99,14 @@ void StartWallets(CScheduler& scheduler, const ArgsManager& args)
void FlushWallets() void FlushWallets()
{ {
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) { for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
pwallet->Flush(false); pwallet->Flush();
} }
} }
void StopWallets() void StopWallets()
{ {
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) { for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
pwallet->Flush(true); pwallet->Close();
} }
} }

View file

@ -439,9 +439,14 @@ bool CWallet::HasWalletSpend(const uint256& txid) const
return (iter != mapTxSpends.end() && iter->first.hash == txid); return (iter != mapTxSpends.end() && iter->first.hash == txid);
} }
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) void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)

View file

@ -1087,7 +1087,10 @@ public:
bool HasWalletSpend(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool HasWalletSpend(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
//! Flush wallet (bitdb flush) //! Flush wallet (bitdb flush)
void Flush(bool shutdown=false); void Flush();
//! Close wallet database
void Close();
/** Wallet is about to be unloaded */ /** Wallet is about to be unloaded */
boost::signals2::signal<void ()> NotifyUnload; boost::signals2::signal<void ()> NotifyUnload;

View file

@ -17,7 +17,7 @@ namespace WalletTool {
static void WalletToolReleaseWallet(CWallet* wallet) static void WalletToolReleaseWallet(CWallet* wallet)
{ {
wallet->WalletLogPrintf("Releasing wallet\n"); wallet->WalletLogPrintf("Releasing wallet\n");
wallet->Flush(true); wallet->Close();
delete wallet; delete wallet;
} }
@ -133,7 +133,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
std::shared_ptr<CWallet> wallet_instance = CreateWallet(name, path); std::shared_ptr<CWallet> wallet_instance = CreateWallet(name, path);
if (wallet_instance) { if (wallet_instance) {
WalletShowInfo(wallet_instance.get()); WalletShowInfo(wallet_instance.get());
wallet_instance->Flush(true); wallet_instance->Close();
} }
} else if (command == "info" || command == "salvage") { } else if (command == "info" || command == "salvage") {
if (!fs::exists(path)) { if (!fs::exists(path)) {
@ -145,7 +145,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path); std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);
if (!wallet_instance) return false; if (!wallet_instance) return false;
WalletShowInfo(wallet_instance.get()); WalletShowInfo(wallet_instance.get());
wallet_instance->Flush(true); wallet_instance->Close();
} else if (command == "salvage") { } else if (command == "salvage") {
return SalvageWallet(path); return SalvageWallet(path);
} }