@@ -615,42 +615,33 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
615
615
616
616
bool BerkeleyDatabase::PeriodicFlush ()
617
617
{
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.
622
622
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 ;
633
624
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
+ }
641
629
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 ;
645
633
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);
652
641
653
- return ret;
642
+ LogPrint (BCLog::WALLETDB, " Flushed %s %dms\n " , strFile, GetTimeMillis () - nStart);
643
+
644
+ return true ;
654
645
}
655
646
656
647
bool BerkeleyDatabase::Backup (const std::string& strDest) const
0 commit comments