Skip to content

Commit c4a4418

Browse files
author
MarcoFalke
committed
Merge #19085: Refactor: clean up PeriodicFlush()
e846a2a refactor: clean up PeriodicFlush() (John Newbery) Pull request description: `PeriodicFlush()` is much more convoluted than it needs to be: it has triple nesting, local variables counting refs and return values, and increments the `mapFileUseCount` iterator unnecessarily. Removing all of that makes the function much easier to understand. ACKs for top commit: MarcoFalke: ACK e846a2a 🎁 jonatack: re-ACK e846a2a per `git range-diff f7c19e8 7c10020 e846a2a` promag: ACK e846a2a. Tree-SHA512: 22bc600a5268b139c0a2c16b5a9f14837b262670ec24aef00643fcedd1c3ebcbf46dea1633e76adc8acf78e8840b776e17127307c5ee95308caa94239dad5b88
2 parents f7c19e8 + e846a2a commit c4a4418

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/wallet/bdb.cpp

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -615,42 +615,33 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
615615

616616
bool BerkeleyDatabase::PeriodicFlush()
617617
{
618-
if (IsDummy()) {
619-
return true;
620-
}
621-
bool ret = false;
618+
// There's nothing to do for dummy databases. Return true.
619+
if (IsDummy()) return true;
620+
621+
// Don't flush if we can't acquire the lock.
622622
TRY_LOCK(cs_db, lockDb);
623-
if (lockDb)
624-
{
625-
// Don't do this if any databases are in use
626-
int nRefCount = 0;
627-
std::map<std::string, int>::iterator mit = env->mapFileUseCount.begin();
628-
while (mit != env->mapFileUseCount.end())
629-
{
630-
nRefCount += (*mit).second;
631-
mit++;
632-
}
623+
if (!lockDb) return false;
633624

634-
if (nRefCount == 0)
635-
{
636-
std::map<std::string, int>::iterator mi = env->mapFileUseCount.find(strFile);
637-
if (mi != env->mapFileUseCount.end())
638-
{
639-
LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile);
640-
int64_t nStart = GetTimeMillis();
625+
// Don't flush if any databases are in use
626+
for (auto it = env->mapFileUseCount.begin() ; it != env->mapFileUseCount.end(); it++) {
627+
if ((*it).second > 0) return false;
628+
}
641629

642-
// Flush wallet file so it's self contained
643-
env->CloseDb(strFile);
644-
env->CheckpointLSN(strFile);
630+
// Don't flush if there haven't been any batch writes for this database.
631+
auto it = env->mapFileUseCount.find(strFile);
632+
if (it == env->mapFileUseCount.end()) return false;
645633

646-
env->mapFileUseCount.erase(mi++);
647-
LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
648-
ret = true;
649-
}
650-
}
651-
}
634+
LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile);
635+
int64_t nStart = GetTimeMillis();
636+
637+
// Flush wallet file so it's self contained
638+
env->CloseDb(strFile);
639+
env->CheckpointLSN(strFile);
640+
env->mapFileUseCount.erase(it);
652641

653-
return ret;
642+
LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
643+
644+
return true;
654645
}
655646

656647
bool BerkeleyDatabase::Backup(const std::string& strDest) const

0 commit comments

Comments
 (0)