refactor: clean up PeriodicFlush()

pull/764/head
John Newbery 5 years ago
parent f7c19e829e
commit e846a2a1d9

@ -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 // Don't flush if any databases are in use
int nRefCount = 0; for (auto it = env->mapFileUseCount.begin() ; it != env->mapFileUseCount.end(); it++) {
std::map<std::string, int>::iterator mit = env->mapFileUseCount.begin(); if ((*it).second > 0) return false;
while (mit != env->mapFileUseCount.end())
{
nRefCount += (*mit).second;
mit++;
} }
if (nRefCount == 0) // Don't flush if there haven't been any batch writes for this database.
{ auto it = env->mapFileUseCount.find(strFile);
std::map<std::string, int>::iterator mi = env->mapFileUseCount.find(strFile); if (it == env->mapFileUseCount.end()) return false;
if (mi != env->mapFileUseCount.end())
{
LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile); LogPrint(BCLog::WALLETDB, "Flushing %s\n", strFile);
int64_t nStart = GetTimeMillis(); int64_t nStart = GetTimeMillis();
// Flush wallet file so it's self contained // Flush wallet file so it's self contained
env->CloseDb(strFile); env->CloseDb(strFile);
env->CheckpointLSN(strFile); env->CheckpointLSN(strFile);
env->mapFileUseCount.erase(it);
env->mapFileUseCount.erase(mi++);
LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart); LogPrint(BCLog::WALLETDB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
ret = true;
}
}
}
return ret; return true;
} }
bool BerkeleyDatabase::Backup(const std::string& strDest) const bool BerkeleyDatabase::Backup(const std::string& strDest) const

Loading…
Cancel
Save