Skip to content

Commit 71d28e7

Browse files
committed
walletdb: Introduce AddRef and RemoveRef functions
Refactor mapFileUseCount increment and decrement to separate functions AddRef and RemoveRef
1 parent 27b2766 commit 71d28e7

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/wallet/bdb.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ BerkeleyDatabase::~BerkeleyDatabase()
334334
}
335335
}
336336

337-
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr)
337+
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database)
338338
{
339339
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
340340
fFlushOnClose = fFlushOnCloseIn;
@@ -408,7 +408,7 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
408408
fReadOnly = fTmp;
409409
}
410410
}
411-
++env->mapFileUseCount[strFilename];
411+
database.AddRef();
412412
strFile = strFilename;
413413
}
414414
}
@@ -446,11 +446,7 @@ void BerkeleyBatch::Close()
446446
if (fFlushOnClose)
447447
Flush();
448448

449-
{
450-
LOCK(cs_db);
451-
--env->mapFileUseCount[strFile];
452-
}
453-
env->m_db_in_use.notify_all();
449+
m_database.RemoveRef();
454450
}
455451

456452
void BerkeleyEnvironment::CloseDb(const std::string& strFile)
@@ -846,6 +842,21 @@ bool BerkeleyBatch::HasKey(CDataStream&& key)
846842
return ret == 0;
847843
}
848844

845+
void BerkeleyDatabase::AddRef()
846+
{
847+
LOCK(cs_db);
848+
++env->mapFileUseCount[strFile];
849+
}
850+
851+
void BerkeleyDatabase::RemoveRef()
852+
{
853+
{
854+
LOCK(cs_db);
855+
--env->mapFileUseCount[strFile];
856+
}
857+
env->m_db_in_use.notify_all();
858+
}
859+
849860
std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
850861
{
851862
return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);

src/wallet/bdb.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ class BerkeleyDatabase
121121
*/
122122
bool Rewrite(const char* pszSkip=nullptr);
123123

124+
/** Indicate the a new database user has began using the database. */
125+
void AddRef();
126+
/** Indicate that database user has stopped using the database and that it could be flushed or closed. */
127+
void RemoveRef();
128+
124129
/** Back up the entire database to a file.
125130
*/
126131
bool Backup(const std::string& strDest) const;
@@ -212,6 +217,7 @@ class BerkeleyBatch : public DatabaseBatch
212217
bool fReadOnly;
213218
bool fFlushOnClose;
214219
BerkeleyEnvironment *env;
220+
BerkeleyDatabase& m_database;
215221

216222
public:
217223
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);

0 commit comments

Comments
 (0)