Skip to content

Commit 58504c2

Browse files
committed
[Wallet] refactor CWallet/CWalletDB/CDB (Round 2)
Focused on the ThreadFlushWalletDB/PeriodicFlush area. An adapted version of btc@7184e25c80aa8b1629a700bb7a7e290ad0bb2792
1 parent c16390d commit 58504c2

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

src/wallet/db.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,38 @@ void CDBEnv::Flush(bool fShutdown)
587587
}
588588
}
589589
}
590+
591+
bool CDB::PeriodicFlush(const std::string& strFile)
592+
{
593+
bool ret = false;
594+
TRY_LOCK(bitdb.cs_db, lockDb);
595+
if (lockDb) {
596+
// Don't do this if any databases are in use
597+
int nRefCount = 0;
598+
std::map<std::string, int>::iterator mi = bitdb.mapFileUseCount.begin();
599+
while (mi != bitdb.mapFileUseCount.end()) {
600+
nRefCount += (*mi).second;
601+
mi++;
602+
}
603+
604+
if (nRefCount == 0) {
605+
boost::this_thread::interruption_point();
606+
std::map<std::string, int>::iterator _mi = bitdb.mapFileUseCount.find(strFile);
607+
if (_mi != bitdb.mapFileUseCount.end()) {
608+
LogPrint(BCLog::DB, "Flushing %s\n", strFile);
609+
int64_t nStart = GetTimeMillis();
610+
611+
// Flush wallet file so it's self contained
612+
bitdb.CloseDb(strFile);
613+
bitdb.CheckpointLSN(strFile);
614+
615+
bitdb.mapFileUseCount.erase(_mi++);
616+
LogPrint(BCLog::DB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
617+
ret = true;
618+
}
619+
}
620+
}
621+
622+
return ret;
623+
}
624+

src/wallet/db.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ class CDB
110110

111111
/* flush the wallet passively (TRY_LOCK)
112112
ideal to be called periodically */
113-
// TODO: Back port Periodic flush..
114-
//static bool PeriodicFlush(std::string strFile);
113+
static bool PeriodicFlush(const std::string& strFile);
115114
/* verifies the database environment */
116115
static bool VerifyEnvironment(const std::string& walletFile, const fs::path& dataDir, std::string& errorStr);
117116
/* verifies the database file */

src/wallet/walletdb.cpp

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -903,34 +903,9 @@ void ThreadFlushWalletDB()
903903
}
904904

905905
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) {
906-
TRY_LOCK(bitdb.cs_db, lockDb);
907-
if (lockDb) {
908-
// Don't do this if any databases are in use
909-
int nRefCount = 0;
910-
std::map<std::string, int>::iterator mi = bitdb.mapFileUseCount.begin();
911-
while (mi != bitdb.mapFileUseCount.end()) {
912-
nRefCount += (*mi).second;
913-
mi++;
914-
}
915-
916-
if (nRefCount == 0) {
917-
boost::this_thread::interruption_point();
918-
const std::string& strFile = pwalletMain->strWalletFile;
919-
std::map<std::string, int>::iterator _mi = bitdb.mapFileUseCount.find(strFile);
920-
if (_mi != bitdb.mapFileUseCount.end()) {
921-
LogPrint(BCLog::DB, "Flushing %s\n", strFile);
922-
nLastFlushed = CWalletDB::GetUpdateCounter();
923-
int64_t nStart = GetTimeMillis();
924-
925-
// Flush wallet file so it's self contained
926-
bitdb.CloseDb(strFile);
927-
bitdb.CheckpointLSN(strFile);
928-
929-
bitdb.mapFileUseCount.erase(_mi++);
930-
LogPrint(BCLog::DB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
931-
}
932-
}
933-
}
906+
const std::string& strFile = pwalletMain->strWalletFile;
907+
if (CDB::PeriodicFlush(strFile))
908+
nLastFlushed = CWalletDB::GetUpdateCounter();
934909
}
935910
}
936911
}

0 commit comments

Comments
 (0)