Skip to content

Commit 5d296ac

Browse files
committed
Add function to close all Db's and reload the databae environment
Adds a ReloadDbEnv function to BerkeleyEnvironment in order to close all Db instances, closes the environment, resets it, and then reopens the BerkeleyEnvironment. Also adds a ReloadDbEnv function to BerkeleyDatabase that calls BerkeleyEnvironment's ReloadDbEnv.
1 parent a769461 commit 5d296ac

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/wallet/db.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ void BerkeleyBatch::Close()
556556
LOCK(cs_db);
557557
--env->mapFileUseCount[strFile];
558558
}
559+
env->m_db_in_use.notify_all();
559560
}
560561

561562
void BerkeleyEnvironment::CloseDb(const std::string& strFile)
@@ -572,6 +573,32 @@ void BerkeleyEnvironment::CloseDb(const std::string& strFile)
572573
}
573574
}
574575

576+
void BerkeleyEnvironment::ReloadDbEnv()
577+
{
578+
// Make sure that no Db's are in use
579+
AssertLockNotHeld(cs_db);
580+
std::unique_lock<CCriticalSection> lock(cs_db);
581+
m_db_in_use.wait(lock, [this](){
582+
for (auto& count : mapFileUseCount) {
583+
if (count.second > 0) return false;
584+
}
585+
return true;
586+
});
587+
588+
std::vector<std::string> filenames;
589+
for (auto it : mapDb) {
590+
filenames.push_back(it.first);
591+
}
592+
// Close the individual Db's
593+
for (const std::string& filename : filenames) {
594+
CloseDb(filename);
595+
}
596+
// Reset the environment
597+
Flush(true); // This will flush and close the environment
598+
Reset();
599+
Open(true);
600+
}
601+
575602
bool BerkeleyBatch::Rewrite(BerkeleyDatabase& database, const char* pszSkip)
576603
{
577604
if (database.IsDummy()) {
@@ -802,3 +829,10 @@ void BerkeleyDatabase::Flush(bool shutdown)
802829
}
803830
}
804831
}
832+
833+
void BerkeleyDatabase::ReloadDbEnv()
834+
{
835+
if (!IsDummy()) {
836+
env->ReloadDbEnv();
837+
}
838+
}

src/wallet/db.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class BerkeleyEnvironment
3838
std::unique_ptr<DbEnv> dbenv;
3939
std::map<std::string, int> mapFileUseCount;
4040
std::map<std::string, Db*> mapDb;
41+
std::condition_variable_any m_db_in_use;
4142

4243
BerkeleyEnvironment(const fs::path& env_directory);
4344
~BerkeleyEnvironment();
@@ -75,6 +76,7 @@ class BerkeleyEnvironment
7576
void CheckpointLSN(const std::string& strFile);
7677

7778
void CloseDb(const std::string& strFile);
79+
void ReloadDbEnv();
7880

7981
DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
8082
{
@@ -145,6 +147,8 @@ class BerkeleyDatabase
145147

146148
void IncrementUpdateCounter();
147149

150+
void ReloadDbEnv();
151+
148152
std::atomic<unsigned int> nUpdateCounter;
149153
unsigned int nLastSeen;
150154
unsigned int nLastFlushed;

0 commit comments

Comments
 (0)