aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2020-05-27 16:15:50 -0400
committerJohn Newbery <john@johnnewbery.com>2020-07-09 07:00:11 +0100
commite846a2a1d9e8aa37abfa55bd4d2a0a9f55ba6c8e (patch)
treef22758bce27765ee4e8daf0a45711de6d9369d76 /src
parentf7c19e829eca10ce8b4acafc61264f8bb9b922f3 (diff)
refactor: clean up PeriodicFlush()
Diffstat (limited to 'src')
-rw-r--r--src/wallet/bdb.cpp53
1 files changed, 22 insertions, 31 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp
index fa4a505982..5f823d5906 100644
--- a/src/wallet/bdb.cpp
+++ b/src/wallet/bdb.cpp
@@ -615,42 +615,33 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
bool BerkeleyDatabase::PeriodicFlush()
{
- if (IsDummy()) {
- return true;
- }
- bool ret = false;
+ // There's nothing to do for dummy databases. Return true.
+ if (IsDummy()) return true;
+
+ // Don't flush if we can't acquire the lock.
TRY_LOCK(cs_db, lockDb);
- if (lockDb)
- {
- // Don't do this if any databases are in use
- int nRefCount = 0;
- std::map<std::string, int>::iterator mit = env->mapFileUseCount.begin();
- while (mit != env->mapFileUseCount.end())
- {
- nRefCount += (*mit).second;
- mit++;
- }
+ if (!lockDb) return false;
- if (nRefCount == 0)
- {
- std::map<std::string, int>::iterator mi = env->mapFileUseCount.find(strFile);
- if (mi != env->mapFileUseCount.end())
- {
- LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile);
- int64_t nStart = GetTimeMillis();
+ // Don't flush if any databases are in use
+ for (auto it = env->mapFileUseCount.begin() ; it != env->mapFileUseCount.end(); it++) {
+ if ((*it).second > 0) return false;
+ }
- // Flush wallet file so it's self contained
- env->CloseDb(strFile);
- env->CheckpointLSN(strFile);
+ // Don't flush if there haven't been any batch writes for this database.
+ auto it = env->mapFileUseCount.find(strFile);
+ if (it == env->mapFileUseCount.end()) return false;
- env->mapFileUseCount.erase(mi++);
- LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
- ret = true;
- }
- }
- }
+ LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile);
+ int64_t nStart = GetTimeMillis();
+
+ // Flush wallet file so it's self contained
+ env->CloseDb(strFile);
+ env->CheckpointLSN(strFile);
+ env->mapFileUseCount.erase(it);
- return ret;
+ LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
+
+ return true;
}
bool BerkeleyDatabase::Backup(const std::string& strDest) const