@@ -97,9 +97,8 @@ void BerkeleyEnvironment::Close()
97
97
fDbEnvInit = false ;
98
98
99
99
for (auto & db : m_databases) {
100
- auto count = mapFileUseCount.find (db.first );
101
- assert (count == mapFileUseCount.end () || count->second == 0 );
102
100
BerkeleyDatabase& database = db.second .get ();
101
+ assert (database.m_refcount <= 0 );
103
102
if (database.m_db ) {
104
103
database.m_db ->close (0 );
105
104
database.m_db .reset ();
@@ -285,8 +284,7 @@ bool BerkeleyDatabase::Verify(bilingual_str& errorStr)
285
284
286
285
if (fs::exists (file_path))
287
286
{
288
- LOCK (cs_db);
289
- assert (env->mapFileUseCount .count (strFile) == 0 );
287
+ assert (m_refcount == 0 );
290
288
291
289
Db db (env->dbenv .get (), 0 );
292
290
int result = db.verify (strFile.c_str (), nullptr , nullptr , 0 );
@@ -459,8 +457,8 @@ void BerkeleyEnvironment::ReloadDbEnv()
459
457
AssertLockNotHeld (cs_db);
460
458
std::unique_lock<RecursiveMutex> lock (cs_db);
461
459
m_db_in_use.wait (lock, [this ](){
462
- for (auto & count : mapFileUseCount ) {
463
- if (count .second > 0 ) return false ;
460
+ for (auto & db : m_databases ) {
461
+ if (db .second . get (). m_refcount > 0 ) return false ;
464
462
}
465
463
return true ;
466
464
});
@@ -488,11 +486,11 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip)
488
486
while (true ) {
489
487
{
490
488
LOCK (cs_db);
491
- if (!env-> mapFileUseCount . count (strFile) || env-> mapFileUseCount [strFile] = = 0 ) {
489
+ if (m_refcount < = 0 ) {
492
490
// Flush log data to the dat file
493
491
env->CloseDb (strFile);
494
492
env->CheckpointLSN (strFile);
495
- env-> mapFileUseCount . erase (strFile) ;
493
+ m_refcount = - 1 ;
496
494
497
495
bool fSuccess = true ;
498
496
LogPrintf (" BerkeleyBatch::Rewrite: Rewriting %s...\n " , strFile);
@@ -576,10 +574,11 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
576
574
return ;
577
575
{
578
576
LOCK (cs_db);
579
- std::map<std::string, int >::iterator mi = mapFileUseCount.begin ();
580
- while (mi != mapFileUseCount.end ()) {
581
- std::string strFile = (*mi).first ;
582
- int nRefCount = (*mi).second ;
577
+ bool no_dbs_accessed = true ;
578
+ for (auto & db_it : m_databases) {
579
+ std::string strFile = db_it.first ;
580
+ int nRefCount = db_it.second .get ().m_refcount ;
581
+ if (nRefCount < 0 ) continue ;
583
582
LogPrint (BCLog::WALLETDB, " BerkeleyEnvironment::Flush: Flushing %s (refcount = %d)...\n " , strFile, nRefCount);
584
583
if (nRefCount == 0 ) {
585
584
// Move log data to the dat file
@@ -590,14 +589,15 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
590
589
if (!fMockDb )
591
590
dbenv->lsn_reset (strFile.c_str (), 0 );
592
591
LogPrint (BCLog::WALLETDB, " BerkeleyEnvironment::Flush: %s closed\n " , strFile);
593
- mapFileUseCount.erase (mi++);
594
- } else
595
- mi++;
592
+ nRefCount = -1 ;
593
+ } else {
594
+ no_dbs_accessed = false ;
595
+ }
596
596
}
597
597
LogPrint (BCLog::WALLETDB, " BerkeleyEnvironment::Flush: Flush(%s)%s took %15dms\n " , fShutdown ? " true" : " false" , fDbEnvInit ? " " : " database not started" , GetTimeMillis () - nStart);
598
598
if (fShutdown ) {
599
599
char ** listp;
600
- if (mapFileUseCount. empty () ) {
600
+ if (no_dbs_accessed ) {
601
601
dbenv->log_archive (&listp, DB_ARCH_REMOVE);
602
602
Close ();
603
603
if (!fMockDb ) {
@@ -618,21 +618,20 @@ bool BerkeleyDatabase::PeriodicFlush()
618
618
if (!lockDb) return false ;
619
619
620
620
// Don't flush if any databases are in use
621
- for (const auto & use_count : env->mapFileUseCount ) {
622
- if (use_count .second > 0 ) return false ;
621
+ for (auto & it : env->m_databases ) {
622
+ if (it .second . get (). m_refcount > 0 ) return false ;
623
623
}
624
624
625
625
// Don't flush if there haven't been any batch writes for this database.
626
- auto it = env->mapFileUseCount .find (strFile);
627
- if (it == env->mapFileUseCount .end ()) return false ;
626
+ if (m_refcount < 0 ) return false ;
628
627
629
628
LogPrint (BCLog::WALLETDB, " Flushing %s\n " , strFile);
630
629
int64_t nStart = GetTimeMillis ();
631
630
632
631
// Flush wallet file so it's self contained
633
632
env->CloseDb (strFile);
634
633
env->CheckpointLSN (strFile);
635
- env-> mapFileUseCount . erase (it) ;
634
+ m_refcount = - 1 ;
636
635
637
636
LogPrint (BCLog::WALLETDB, " Flushed %s %dms\n " , strFile, GetTimeMillis () - nStart);
638
637
@@ -648,12 +647,11 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) const
648
647
{
649
648
{
650
649
LOCK (cs_db);
651
- if (!env-> mapFileUseCount . count (strFile) || env-> mapFileUseCount [strFile] = = 0 )
650
+ if (m_refcount < = 0 )
652
651
{
653
652
// Flush log data to the dat file
654
653
env->CloseDb (strFile);
655
654
env->CheckpointLSN (strFile);
656
- env->mapFileUseCount .erase (strFile);
657
655
658
656
// Copy wallet file
659
657
fs::path pathSrc = env->Directory () / strFile;
@@ -835,15 +833,17 @@ bool BerkeleyBatch::HasKey(CDataStream&& key)
835
833
void BerkeleyDatabase::AddRef ()
836
834
{
837
835
LOCK (cs_db);
838
- ++env->mapFileUseCount [strFile];
836
+ if (m_refcount < 0 ) {
837
+ m_refcount = 1 ;
838
+ } else {
839
+ m_refcount++;
840
+ }
839
841
}
840
842
841
843
void BerkeleyDatabase::RemoveRef ()
842
844
{
843
- {
844
- LOCK (cs_db);
845
- --env->mapFileUseCount [strFile];
846
- }
845
+ LOCK (cs_db);
846
+ m_refcount--;
847
847
env->m_db_in_use .notify_all ();
848
848
}
849
849
0 commit comments