@@ -364,6 +364,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
364
364
int ret;
365
365
fReadOnly = (!strchr (pszMode, ' +' ) && !strchr (pszMode, ' w' ));
366
366
fFlushOnClose = fFlushOnCloseIn ;
367
+ env = dbw.env ;
367
368
if (dbw.IsDummy ()) {
368
369
return ;
369
370
}
@@ -375,17 +376,17 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
375
376
nFlags |= DB_CREATE;
376
377
377
378
{
378
- LOCK (bitdb. cs_db );
379
- if (!bitdb. Open (GetDataDir ()))
379
+ LOCK (env-> cs_db );
380
+ if (!env-> Open (GetDataDir ()))
380
381
throw std::runtime_error (" CDB: Failed to open database environment." );
381
382
382
383
strFile = strFilename;
383
- ++bitdb. mapFileUseCount [strFile];
384
- pdb = bitdb. mapDb [strFile];
384
+ ++env-> mapFileUseCount [strFile];
385
+ pdb = env-> mapDb [strFile];
385
386
if (pdb == NULL ) {
386
- pdb = new Db (bitdb. dbenv , 0 );
387
+ pdb = new Db (env-> dbenv , 0 );
387
388
388
- bool fMockDb = bitdb. IsMock ();
389
+ bool fMockDb = env-> IsMock ();
389
390
if (fMockDb ) {
390
391
DbMpoolFile* mpf = pdb->get_mpf ();
391
392
ret = mpf->set_flags (DB_MPOOL_NOFILE, 1 );
@@ -403,7 +404,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
403
404
if (ret != 0 ) {
404
405
delete pdb;
405
406
pdb = NULL ;
406
- --bitdb. mapFileUseCount [strFile];
407
+ --env-> mapFileUseCount [strFile];
407
408
strFile = " " ;
408
409
throw std::runtime_error (strprintf (" CDB: Error %d, can't open database %s" , ret, strFilename));
409
410
}
@@ -415,7 +416,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
415
416
fReadOnly = fTmp ;
416
417
}
417
418
418
- bitdb. mapDb [strFile] = pdb;
419
+ env-> mapDb [strFile] = pdb;
419
420
}
420
421
}
421
422
}
@@ -430,7 +431,7 @@ void CDB::Flush()
430
431
if (fReadOnly )
431
432
nMinutes = 1 ;
432
433
433
- bitdb. dbenv ->txn_checkpoint (nMinutes ? GetArg (" -dblogsize" , DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0 , nMinutes, 0 );
434
+ env-> dbenv ->txn_checkpoint (nMinutes ? GetArg (" -dblogsize" , DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0 , nMinutes, 0 );
434
435
}
435
436
436
437
void CDB::Close ()
@@ -446,8 +447,8 @@ void CDB::Close()
446
447
Flush ();
447
448
448
449
{
449
- LOCK (bitdb. cs_db );
450
- --bitdb. mapFileUseCount [strFile];
450
+ LOCK (env-> cs_db );
451
+ --env-> mapFileUseCount [strFile];
451
452
}
452
453
}
453
454
@@ -479,22 +480,23 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
479
480
if (dbw.IsDummy ()) {
480
481
return true ;
481
482
}
483
+ CDBEnv *env = dbw.env ;
482
484
const std::string& strFile = dbw.strFile ;
483
485
while (true ) {
484
486
{
485
- LOCK (bitdb. cs_db );
486
- if (!bitdb. mapFileUseCount .count (strFile) || bitdb. mapFileUseCount [strFile] == 0 ) {
487
+ LOCK (env-> cs_db );
488
+ if (!env-> mapFileUseCount .count (strFile) || env-> mapFileUseCount [strFile] == 0 ) {
487
489
// Flush log data to the dat file
488
- bitdb. CloseDb (strFile);
489
- bitdb. CheckpointLSN (strFile);
490
- bitdb. mapFileUseCount .erase (strFile);
490
+ env-> CloseDb (strFile);
491
+ env-> CheckpointLSN (strFile);
492
+ env-> mapFileUseCount .erase (strFile);
491
493
492
494
bool fSuccess = true ;
493
495
LogPrintf (" CDB::Rewrite: Rewriting %s...\n " , strFile);
494
496
std::string strFileRes = strFile + " .rewrite" ;
495
497
{ // surround usage of db with extra {}
496
498
CDB db (dbw, " r" );
497
- Db* pdbCopy = new Db (bitdb. dbenv , 0 );
499
+ Db* pdbCopy = new Db (env-> dbenv , 0 );
498
500
499
501
int ret = pdbCopy->open (NULL , // Txn pointer
500
502
strFileRes.c_str (), // Filename
@@ -537,17 +539,17 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
537
539
}
538
540
if (fSuccess ) {
539
541
db.Close ();
540
- bitdb. CloseDb (strFile);
542
+ env-> CloseDb (strFile);
541
543
if (pdbCopy->close (0 ))
542
544
fSuccess = false ;
543
545
delete pdbCopy;
544
546
}
545
547
}
546
548
if (fSuccess ) {
547
- Db dbA (bitdb. dbenv , 0 );
549
+ Db dbA (env-> dbenv , 0 );
548
550
if (dbA.remove (strFile.c_str (), NULL , 0 ))
549
551
fSuccess = false ;
550
- Db dbB (bitdb. dbenv , 0 );
552
+ Db dbB (env-> dbenv , 0 );
551
553
if (dbB.rename (strFileRes.c_str (), NULL , strFile.c_str (), 0 ))
552
554
fSuccess = false ;
553
555
}
@@ -608,14 +610,15 @@ bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
608
610
return true ;
609
611
}
610
612
bool ret = false ;
613
+ CDBEnv *env = dbw.env ;
611
614
const std::string& strFile = dbw.strFile ;
612
615
TRY_LOCK (bitdb.cs_db ,lockDb);
613
616
if (lockDb)
614
617
{
615
618
// Don't do this if any databases are in use
616
619
int nRefCount = 0 ;
617
- std::map<std::string, int >::iterator mit = bitdb. mapFileUseCount .begin ();
618
- while (mit != bitdb. mapFileUseCount .end ())
620
+ std::map<std::string, int >::iterator mit = env-> mapFileUseCount .begin ();
621
+ while (mit != env-> mapFileUseCount .end ())
619
622
{
620
623
nRefCount += (*mit).second ;
621
624
mit++;
@@ -624,17 +627,17 @@ bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
624
627
if (nRefCount == 0 )
625
628
{
626
629
boost::this_thread::interruption_point ();
627
- std::map<std::string, int >::iterator mi = bitdb. mapFileUseCount .find (strFile);
628
- if (mi != bitdb. mapFileUseCount .end ())
630
+ std::map<std::string, int >::iterator mi = env-> mapFileUseCount .find (strFile);
631
+ if (mi != env-> mapFileUseCount .end ())
629
632
{
630
633
LogPrint (BCLog::DB, " Flushing %s\n " , strFile);
631
634
int64_t nStart = GetTimeMillis ();
632
635
633
636
// Flush wallet file so it's self contained
634
- bitdb. CloseDb (strFile);
635
- bitdb. CheckpointLSN (strFile);
637
+ env-> CloseDb (strFile);
638
+ env-> CheckpointLSN (strFile);
636
639
637
- bitdb. mapFileUseCount .erase (mi++);
640
+ env-> mapFileUseCount .erase (mi++);
638
641
LogPrint (BCLog::DB, " Flushed %s %dms\n " , strFile, GetTimeMillis () - nStart);
639
642
ret = true ;
640
643
}
@@ -657,13 +660,13 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
657
660
while (true )
658
661
{
659
662
{
660
- LOCK (bitdb. cs_db );
661
- if (!bitdb. mapFileUseCount .count (strFile) || bitdb. mapFileUseCount [strFile] == 0 )
663
+ LOCK (env-> cs_db );
664
+ if (!env-> mapFileUseCount .count (strFile) || env-> mapFileUseCount [strFile] == 0 )
662
665
{
663
666
// Flush log data to the dat file
664
- bitdb. CloseDb (strFile);
665
- bitdb. CheckpointLSN (strFile);
666
- bitdb. mapFileUseCount .erase (strFile);
667
+ env-> CloseDb (strFile);
668
+ env-> CheckpointLSN (strFile);
669
+ env-> mapFileUseCount .erase (strFile);
667
670
668
671
// Copy wallet file
669
672
fs::path pathSrc = GetDataDir () / strFile;
@@ -685,3 +688,10 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
685
688
}
686
689
return false ;
687
690
}
691
+
692
+ void CWalletDBWrapper::Flush (bool shutdown)
693
+ {
694
+ if (!IsDummy ()) {
695
+ env->Flush (shutdown);
696
+ }
697
+ }
0 commit comments