mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-10 10:52:31 -05:00
Merge #19085: Refactor: clean up PeriodicFlush()
e846a2a1d9
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: ACKe846a2a1d9
🎁 jonatack: re-ACKe846a2a
per `git range-difff7c19e8
7c10020 e846a2a` promag: ACKe846a2a1d9
. Tree-SHA512: 22bc600a5268b139c0a2c16b5a9f14837b262670ec24aef00643fcedd1c3ebcbf46dea1633e76adc8acf78e8840b776e17127307c5ee95308caa94239dad5b88
This commit is contained in:
commit
c4a44186d8
1 changed files with 23 additions and 32 deletions
|
@ -615,42 +615,33 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
|
||||||
|
|
||||||
bool BerkeleyDatabase::PeriodicFlush()
|
bool BerkeleyDatabase::PeriodicFlush()
|
||||||
{
|
{
|
||||||
if (IsDummy()) {
|
// There's nothing to do for dummy databases. Return true.
|
||||||
return true;
|
if (IsDummy()) return true;
|
||||||
}
|
|
||||||
bool ret = false;
|
// Don't flush if we can't acquire the lock.
|
||||||
TRY_LOCK(cs_db, lockDb);
|
TRY_LOCK(cs_db, lockDb);
|
||||||
if (lockDb)
|
if (!lockDb) return false;
|
||||||
{
|
|
||||||
// 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 (nRefCount == 0)
|
// Don't flush if any databases are in use
|
||||||
{
|
for (auto it = env->mapFileUseCount.begin() ; it != env->mapFileUseCount.end(); it++) {
|
||||||
std::map<std::string, int>::iterator mi = env->mapFileUseCount.find(strFile);
|
if ((*it).second > 0) return false;
|
||||||
if (mi != env->mapFileUseCount.end())
|
|
||||||
{
|
|
||||||
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(mi++);
|
|
||||||
LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
|
|
||||||
ret = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
// 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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BerkeleyDatabase::Backup(const std::string& strDest) const
|
bool BerkeleyDatabase::Backup(const std::string& strDest) const
|
||||||
|
|
Loading…
Add table
Reference in a new issue