Skip to content

Commit f40006a

Browse files
laanwjfurszy
authored andcommitted
wallet: Reduce references to global bitdb environment
1 parent e420d3d commit f40006a

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

src/wallet/db.cpp

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
354354
int ret;
355355
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
356356
fFlushOnClose = fFlushOnCloseIn;
357+
env = dbw.env;
357358
if (dbw.IsDummy()) {
358359
return;
359360
}
@@ -365,17 +366,17 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
365366
nFlags |= DB_CREATE;
366367

367368
{
368-
LOCK(bitdb.cs_db);
369-
if (!bitdb.Open(GetDataDir()))
370-
throw std::runtime_error("CDB : Failed to open database environment.");
369+
LOCK(env->cs_db);
370+
if (!env->Open(GetDataDir()))
371+
throw std::runtime_error("CDB: Failed to open database environment.");
371372

372373
strFile = strFilename;
373-
++bitdb.mapFileUseCount[strFile];
374-
pdb = bitdb.mapDb[strFile];
374+
++env->mapFileUseCount[strFile];
375+
pdb = env->mapDb[strFile];
375376
if (pdb == NULL) {
376-
pdb = new Db(bitdb.dbenv, 0);
377+
pdb = new Db(env->dbenv, 0);
377378

378-
bool fMockDb = bitdb.IsMock();
379+
bool fMockDb = env->IsMock();
379380
if (fMockDb) {
380381
DbMpoolFile* mpf = pdb->get_mpf();
381382
ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
@@ -393,7 +394,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
393394
if (ret != 0) {
394395
delete pdb;
395396
pdb = NULL;
396-
--bitdb.mapFileUseCount[strFile];
397+
--env->mapFileUseCount[strFile];
397398
std::string tempCopy(strFile);
398399
strFile = "";
399400
throw std::runtime_error(strprintf("CDB : Error %d, can't open database %s", ret, tempCopy));
@@ -406,7 +407,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
406407
fReadOnly = fTmp;
407408
}
408409

409-
bitdb.mapDb[strFile] = pdb;
410+
env->mapDb[strFile] = pdb;
410411
}
411412
}
412413
}
@@ -421,7 +422,7 @@ void CDB::Flush()
421422
if (fReadOnly)
422423
nMinutes = 1;
423424

424-
bitdb.dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0);
425+
env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0);
425426
}
426427

427428
void CDB::Close()
@@ -437,8 +438,8 @@ void CDB::Close()
437438
Flush();
438439

439440
{
440-
LOCK(bitdb.cs_db);
441-
--bitdb.mapFileUseCount[strFile];
441+
LOCK(env->cs_db);
442+
--env->mapFileUseCount[strFile];
442443
}
443444
}
444445

@@ -470,22 +471,23 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
470471
if (dbw.IsDummy()) {
471472
return true;
472473
}
474+
CDBEnv *env = dbw.env;
473475
const std::string& strFile = dbw.strFile;
474476
while (true) {
475477
{
476-
LOCK(bitdb.cs_db);
477-
if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0) {
478+
LOCK(env->cs_db);
479+
if (!env->mapFileUseCount.count(strFile) || env->mapFileUseCount[strFile] == 0) {
478480
// Flush log data to the dat file
479-
bitdb.CloseDb(strFile);
480-
bitdb.CheckpointLSN(strFile);
481-
bitdb.mapFileUseCount.erase(strFile);
481+
env->CloseDb(strFile);
482+
env->CheckpointLSN(strFile);
483+
env->mapFileUseCount.erase(strFile);
482484

483485
bool fSuccess = true;
484486
LogPrintf("CDB::Rewrite : Rewriting %s...\n", strFile);
485487
std::string strFileRes = strFile + ".rewrite";
486488
{ // surround usage of db with extra {}
487489
CDB db(dbw, "r");
488-
Db* pdbCopy = new Db(bitdb.dbenv, 0);
490+
Db* pdbCopy = new Db(env->dbenv, 0);
489491

490492
int ret = pdbCopy->open(NULL, // Txn pointer
491493
strFileRes.c_str(), // Filename
@@ -528,17 +530,17 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
528530
}
529531
if (fSuccess) {
530532
db.Close();
531-
bitdb.CloseDb(strFile);
533+
env->CloseDb(strFile);
532534
if (pdbCopy->close(0))
533535
fSuccess = false;
534536
delete pdbCopy;
535537
}
536538
}
537539
if (fSuccess) {
538-
Db dbA(bitdb.dbenv, 0);
540+
Db dbA(env->dbenv, 0);
539541
if (dbA.remove(strFile.c_str(), NULL, 0))
540542
fSuccess = false;
541-
Db dbB(bitdb.dbenv, 0);
543+
Db dbB(env->dbenv, 0);
542544
if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
543545
fSuccess = false;
544546
}
@@ -599,30 +601,31 @@ bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
599601
return true;
600602
}
601603
bool ret = false;
604+
CDBEnv *env = dbw.env;
602605
const std::string& strFile = dbw.strFile;
603606
TRY_LOCK(bitdb.cs_db,lockDb);
604607
if (lockDb)
605608
{
606609
// Don't do this if any databases are in use
607610
int nRefCount = 0;
608-
std::map<std::string, int>::iterator mi = bitdb.mapFileUseCount.begin();
609-
while (mi != bitdb.mapFileUseCount.end()) {
611+
std::map<std::string, int>::iterator mi = env->mapFileUseCount.begin();
612+
while (mi != env->mapFileUseCount.end()) {
610613
nRefCount += (*mi).second;
611614
mi++;
612615
}
613616

614617
if (nRefCount == 0) {
615618
boost::this_thread::interruption_point();
616-
std::map<std::string, int>::iterator _mi = bitdb.mapFileUseCount.find(strFile);
617-
if (_mi != bitdb.mapFileUseCount.end()) {
619+
std::map<std::string, int>::iterator _mi = env->mapFileUseCount.find(strFile);
620+
if (_mi != env->mapFileUseCount.end()) {
618621
LogPrint(BCLog::DB, "Flushing %s\n", strFile);
619622
int64_t nStart = GetTimeMillis();
620623

621624
// Flush wallet file so it's self contained
622-
bitdb.CloseDb(strFile);
623-
bitdb.CheckpointLSN(strFile);
625+
env->CloseDb(strFile);
626+
env->CheckpointLSN(strFile);
624627

625-
bitdb.mapFileUseCount.erase(_mi++);
628+
env->mapFileUseCount.erase(_mi++);
626629
LogPrint(BCLog::DB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
627630
ret = true;
628631
}
@@ -645,13 +648,13 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
645648
while (true)
646649
{
647650
{
648-
LOCK(bitdb.cs_db);
649-
if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0)
651+
LOCK(env->cs_db);
652+
if (!env->mapFileUseCount.count(strFile) || env->mapFileUseCount[strFile] == 0)
650653
{
651654
// Flush log data to the dat file
652-
bitdb.CloseDb(strFile);
653-
bitdb.CheckpointLSN(strFile);
654-
bitdb.mapFileUseCount.erase(strFile);
655+
env->CloseDb(strFile);
656+
env->CheckpointLSN(strFile);
657+
env->mapFileUseCount.erase(strFile);
655658

656659
// Copy wallet file
657660
fs::path pathSrc = GetDataDir() / strFile;
@@ -677,3 +680,10 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
677680
}
678681
return false;
679682
}
683+
684+
void CWalletDBWrapper::Flush(bool shutdown)
685+
{
686+
if (!IsDummy()) {
687+
env->Flush(shutdown);
688+
}
689+
}

src/wallet/db.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ class CWalletDBWrapper
117117
*/
118118
std::string GetName() const { return strFile; }
119119

120+
/** Make sure all changes are flushed to disk.
121+
*/
122+
void Flush(bool shutdown);
123+
120124
/** Return whether this database handle is a dummy for testing.
121125
* Only to be used at a low level, application should ideally not care
122126
* about this.
@@ -139,6 +143,7 @@ class CDB
139143
DbTxn* activeTxn;
140144
bool fReadOnly;
141145
bool fFlushOnClose;
146+
CDBEnv *env;
142147

143148
explicit CDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
144149
~CDB() { Close(); }

0 commit comments

Comments
 (0)