Skip to content

Commit be9e1a9

Browse files
committed
wallet: Reduce references to global bitdb environment
1 parent 071c955 commit be9e1a9

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

src/wallet/db.cpp

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
364364
int ret;
365365
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
366366
fFlushOnClose = fFlushOnCloseIn;
367+
env = dbw.env;
367368
if (dbw.IsDummy()) {
368369
return;
369370
}
@@ -375,17 +376,17 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
375376
nFlags |= DB_CREATE;
376377

377378
{
378-
LOCK(bitdb.cs_db);
379-
if (!bitdb.Open(GetDataDir()))
379+
LOCK(env->cs_db);
380+
if (!env->Open(GetDataDir()))
380381
throw std::runtime_error("CDB: Failed to open database environment.");
381382

382383
strFile = strFilename;
383-
++bitdb.mapFileUseCount[strFile];
384-
pdb = bitdb.mapDb[strFile];
384+
++env->mapFileUseCount[strFile];
385+
pdb = env->mapDb[strFile];
385386
if (pdb == NULL) {
386-
pdb = new Db(bitdb.dbenv, 0);
387+
pdb = new Db(env->dbenv, 0);
387388

388-
bool fMockDb = bitdb.IsMock();
389+
bool fMockDb = env->IsMock();
389390
if (fMockDb) {
390391
DbMpoolFile* mpf = pdb->get_mpf();
391392
ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
@@ -403,7 +404,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
403404
if (ret != 0) {
404405
delete pdb;
405406
pdb = NULL;
406-
--bitdb.mapFileUseCount[strFile];
407+
--env->mapFileUseCount[strFile];
407408
strFile = "";
408409
throw std::runtime_error(strprintf("CDB: Error %d, can't open database %s", ret, strFilename));
409410
}
@@ -415,7 +416,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
415416
fReadOnly = fTmp;
416417
}
417418

418-
bitdb.mapDb[strFile] = pdb;
419+
env->mapDb[strFile] = pdb;
419420
}
420421
}
421422
}
@@ -430,7 +431,7 @@ void CDB::Flush()
430431
if (fReadOnly)
431432
nMinutes = 1;
432433

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);
434435
}
435436

436437
void CDB::Close()
@@ -446,8 +447,8 @@ void CDB::Close()
446447
Flush();
447448

448449
{
449-
LOCK(bitdb.cs_db);
450-
--bitdb.mapFileUseCount[strFile];
450+
LOCK(env->cs_db);
451+
--env->mapFileUseCount[strFile];
451452
}
452453
}
453454

@@ -479,22 +480,23 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
479480
if (dbw.IsDummy()) {
480481
return true;
481482
}
483+
CDBEnv *env = dbw.env;
482484
const std::string& strFile = dbw.strFile;
483485
while (true) {
484486
{
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) {
487489
// 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);
491493

492494
bool fSuccess = true;
493495
LogPrintf("CDB::Rewrite: Rewriting %s...\n", strFile);
494496
std::string strFileRes = strFile + ".rewrite";
495497
{ // surround usage of db with extra {}
496498
CDB db(dbw, "r");
497-
Db* pdbCopy = new Db(bitdb.dbenv, 0);
499+
Db* pdbCopy = new Db(env->dbenv, 0);
498500

499501
int ret = pdbCopy->open(NULL, // Txn pointer
500502
strFileRes.c_str(), // Filename
@@ -537,17 +539,17 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
537539
}
538540
if (fSuccess) {
539541
db.Close();
540-
bitdb.CloseDb(strFile);
542+
env->CloseDb(strFile);
541543
if (pdbCopy->close(0))
542544
fSuccess = false;
543545
delete pdbCopy;
544546
}
545547
}
546548
if (fSuccess) {
547-
Db dbA(bitdb.dbenv, 0);
549+
Db dbA(env->dbenv, 0);
548550
if (dbA.remove(strFile.c_str(), NULL, 0))
549551
fSuccess = false;
550-
Db dbB(bitdb.dbenv, 0);
552+
Db dbB(env->dbenv, 0);
551553
if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
552554
fSuccess = false;
553555
}
@@ -608,14 +610,15 @@ bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
608610
return true;
609611
}
610612
bool ret = false;
613+
CDBEnv *env = dbw.env;
611614
const std::string& strFile = dbw.strFile;
612615
TRY_LOCK(bitdb.cs_db,lockDb);
613616
if (lockDb)
614617
{
615618
// Don't do this if any databases are in use
616619
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())
619622
{
620623
nRefCount += (*mit).second;
621624
mit++;
@@ -624,17 +627,17 @@ bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
624627
if (nRefCount == 0)
625628
{
626629
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())
629632
{
630633
LogPrint(BCLog::DB, "Flushing %s\n", strFile);
631634
int64_t nStart = GetTimeMillis();
632635

633636
// Flush wallet file so it's self contained
634-
bitdb.CloseDb(strFile);
635-
bitdb.CheckpointLSN(strFile);
637+
env->CloseDb(strFile);
638+
env->CheckpointLSN(strFile);
636639

637-
bitdb.mapFileUseCount.erase(mi++);
640+
env->mapFileUseCount.erase(mi++);
638641
LogPrint(BCLog::DB, "Flushed %s %dms\n", strFile, GetTimeMillis() - nStart);
639642
ret = true;
640643
}
@@ -657,13 +660,13 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
657660
while (true)
658661
{
659662
{
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)
662665
{
663666
// 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);
667670

668671
// Copy wallet file
669672
fs::path pathSrc = GetDataDir() / strFile;
@@ -685,3 +688,10 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
685688
}
686689
return false;
687690
}
691+
692+
void CWalletDBWrapper::Flush(bool shutdown)
693+
{
694+
if (!IsDummy()) {
695+
env->Flush(shutdown);
696+
}
697+
}

src/wallet/db.h

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

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

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

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ bool CWallet::HasWalletSpend(const uint256& txid) const
431431

432432
void CWallet::Flush(bool shutdown)
433433
{
434-
bitdb.Flush(shutdown);
434+
dbw->Flush(shutdown);
435435
}
436436

437437
bool CWallet::Verify()

0 commit comments

Comments
 (0)