@@ -615,42 +615,33 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
615615
616616bool 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
656647bool BerkeleyDatabase::Backup (const std::string& strDest) const
0 commit comments