Skip to content

Commit 27b2766

Browse files
committed
walletdb: Move BerkeleyDatabase::Flush(true) to Close()
Instead of having Flush optionally shutdown the database and environment, add a Close() function that does that.
1 parent 834ac4c commit 27b2766

File tree

6 files changed

+41
-30
lines changed

6 files changed

+41
-30
lines changed

src/wallet/bdb.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ void BerkeleyEnvironment::CheckpointLSN(const std::string& strFile)
324324
dbenv->lsn_reset(strFile.c_str(), 0);
325325
}
326326

327+
BerkeleyDatabase::~BerkeleyDatabase()
328+
{
329+
if (env) {
330+
LOCK(cs_db);
331+
size_t erased = env->m_databases.erase(strFile);
332+
assert(erased == 1);
333+
env->m_fileids.erase(strFile);
334+
}
335+
}
327336

328337
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr)
329338
{
@@ -685,22 +694,17 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) const
685694
}
686695
}
687696

688-
void BerkeleyDatabase::Flush(bool shutdown)
697+
void BerkeleyDatabase::Flush()
689698
{
690699
if (!IsDummy()) {
691-
env->Flush(shutdown);
692-
if (shutdown) {
693-
LOCK(cs_db);
694-
g_dbenvs.erase(env->Directory().string());
695-
env = nullptr;
696-
} else {
697-
// TODO: To avoid g_dbenvs.erase erasing the environment prematurely after the
698-
// first database shutdown when multiple databases are open in the same
699-
// environment, should replace raw database `env` pointers with shared or weak
700-
// pointers, or else separate the database and environment shutdowns so
701-
// environments can be shut down after databases.
702-
env->m_fileids.erase(strFile);
703-
}
700+
env->Flush(false);
701+
}
702+
}
703+
704+
void BerkeleyDatabase::Close()
705+
{
706+
if (!IsDummy()) {
707+
env->Flush(true);
704708
}
705709
}
706710

src/wallet/bdb.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,7 @@ class BerkeleyDatabase
115115
assert(inserted.second);
116116
}
117117

118-
~BerkeleyDatabase() {
119-
if (env) {
120-
size_t erased = env->m_databases.erase(strFile);
121-
assert(erased == 1);
122-
}
123-
}
118+
~BerkeleyDatabase();
124119

125120
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
126121
*/
@@ -130,9 +125,13 @@ class BerkeleyDatabase
130125
*/
131126
bool Backup(const std::string& strDest) const;
132127

133-
/** Make sure all changes are flushed to disk.
128+
/** Make sure all changes are flushed to database file.
129+
*/
130+
void Flush();
131+
/** Flush to the database file and close the database.
132+
* Also close the environment if no other databases are open in it.
134133
*/
135-
void Flush(bool shutdown);
134+
void Close();
136135
/* flush the wallet passively (TRY_LOCK)
137136
ideal to be called periodically */
138137
bool PeriodicFlush();

src/wallet/load.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ void StartWallets(CScheduler& scheduler, const ArgsManager& args)
9999
void FlushWallets()
100100
{
101101
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
102-
pwallet->Flush(false);
102+
pwallet->Flush();
103103
}
104104
}
105105

106106
void StopWallets()
107107
{
108108
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
109-
pwallet->Flush(true);
109+
pwallet->Close();
110110
}
111111
}
112112

src/wallet/wallet.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,14 @@ bool CWallet::HasWalletSpend(const uint256& txid) const
439439
return (iter != mapTxSpends.end() && iter->first.hash == txid);
440440
}
441441

442-
void CWallet::Flush(bool shutdown)
442+
void CWallet::Flush()
443443
{
444-
database->Flush(shutdown);
444+
database->Flush();
445+
}
446+
447+
void CWallet::Close()
448+
{
449+
database->Close();
445450
}
446451

447452
void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)

src/wallet/wallet.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10871087
bool HasWalletSpend(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10881088

10891089
//! Flush wallet (bitdb flush)
1090-
void Flush(bool shutdown=false);
1090+
void Flush();
1091+
1092+
//! Close wallet database
1093+
void Close();
10911094

10921095
/** Wallet is about to be unloaded */
10931096
boost::signals2::signal<void ()> NotifyUnload;

src/wallet/wallettool.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace WalletTool {
1717
static void WalletToolReleaseWallet(CWallet* wallet)
1818
{
1919
wallet->WalletLogPrintf("Releasing wallet\n");
20-
wallet->Flush(true);
20+
wallet->Close();
2121
delete wallet;
2222
}
2323

@@ -133,7 +133,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
133133
std::shared_ptr<CWallet> wallet_instance = CreateWallet(name, path);
134134
if (wallet_instance) {
135135
WalletShowInfo(wallet_instance.get());
136-
wallet_instance->Flush(true);
136+
wallet_instance->Close();
137137
}
138138
} else if (command == "info" || command == "salvage") {
139139
if (!fs::exists(path)) {
@@ -145,7 +145,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
145145
std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);
146146
if (!wallet_instance) return false;
147147
WalletShowInfo(wallet_instance.get());
148-
wallet_instance->Flush(true);
148+
wallet_instance->Close();
149149
} else if (command == "salvage") {
150150
return SalvageWallet(path);
151151
}

0 commit comments

Comments
 (0)