Skip to content

Commit d86efab

Browse files
committed
walletdb: Move Db->open to BerkeleyDatabase::Open
Instead of opening the Db handle in BerkeleyBatch, make BerkeleyDatabase do that.
1 parent 4fe4b3b commit d86efab

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

src/wallet/bdb.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,27 @@ BerkeleyDatabase::~BerkeleyDatabase()
317317

318318
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database)
319319
{
320+
database.AddRef();
321+
database.Open(pszMode);
320322
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
321323
fFlushOnClose = fFlushOnCloseIn;
322324
env = database.env.get();
323-
if (database.IsDummy()) {
325+
pdb = database.m_db.get();
326+
strFile = database.strFile;
327+
bool fCreate = strchr(pszMode, 'c') != nullptr;
328+
if (fCreate && !Exists(std::string("version"))) {
329+
bool fTmp = fReadOnly;
330+
fReadOnly = false;
331+
Write(std::string("version"), CLIENT_VERSION);
332+
fReadOnly = fTmp;
333+
}
334+
}
335+
336+
void BerkeleyDatabase::Open(const char* pszMode)
337+
{
338+
if (IsDummy()){
324339
return;
325340
}
326-
const std::string &strFilename = database.strFile;
327341

328342
bool fCreate = strchr(pszMode, 'c') != nullptr;
329343
unsigned int nFlags = DB_THREAD;
@@ -334,10 +348,9 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
334348
LOCK(cs_db);
335349
bilingual_str open_err;
336350
if (!env->Open(open_err))
337-
throw std::runtime_error("BerkeleyBatch: Failed to open database environment.");
351+
throw std::runtime_error("BerkeleyDatabase: Failed to open database environment.");
338352

339-
pdb = database.m_db.get();
340-
if (pdb == nullptr) {
353+
if (m_db == nullptr) {
341354
int ret;
342355
std::unique_ptr<Db> pdb_temp = MakeUnique<Db>(env->dbenv.get(), 0);
343356

@@ -346,19 +359,19 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
346359
DbMpoolFile* mpf = pdb_temp->get_mpf();
347360
ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
348361
if (ret != 0) {
349-
throw std::runtime_error(strprintf("BerkeleyBatch: Failed to configure for no temp file backing for database %s", strFilename));
362+
throw std::runtime_error(strprintf("BerkeleyDatabase: Failed to configure for no temp file backing for database %s", strFile));
350363
}
351364
}
352365

353366
ret = pdb_temp->open(nullptr, // Txn pointer
354-
fMockDb ? nullptr : strFilename.c_str(), // Filename
355-
fMockDb ? strFilename.c_str() : "main", // Logical db name
367+
fMockDb ? nullptr : strFile.c_str(), // Filename
368+
fMockDb ? strFile.c_str() : "main", // Logical db name
356369
DB_BTREE, // Database type
357370
nFlags, // Flags
358371
0);
359372

360373
if (ret != 0) {
361-
throw std::runtime_error(strprintf("BerkeleyBatch: Error %d, can't open database %s", ret, strFilename));
374+
throw std::runtime_error(strprintf("BerkeleyDatabase: Error %d, can't open database %s", ret, strFile));
362375
}
363376

364377
// Call CheckUniqueFileid on the containing BDB environment to
@@ -377,29 +390,15 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
377390
// versions of BDB have an set_lk_exclusive method for this
378391
// purpose, but the older version we use does not.)
379392
for (const auto& env : g_dbenvs) {
380-
CheckUniqueFileid(*env.second.lock().get(), strFilename, *pdb_temp, this->env->m_fileids[strFilename]);
393+
CheckUniqueFileid(*env.second.lock().get(), strFile, *pdb_temp, this->env->m_fileids[strFile]);
381394
}
382395

383-
pdb = pdb_temp.release();
384-
database.m_db.reset(pdb);
396+
m_db.reset(pdb_temp.release());
385397

386-
if (fCreate && !Exists(std::string("version"))) {
387-
bool fTmp = fReadOnly;
388-
fReadOnly = false;
389-
Write(std::string("version"), CLIENT_VERSION);
390-
fReadOnly = fTmp;
391-
}
392398
}
393-
database.AddRef();
394-
strFile = strFilename;
395399
}
396400
}
397401

398-
void BerkeleyDatabase::Open(const char* mode)
399-
{
400-
throw std::logic_error("BerkeleyDatabase does not implement Open. This function should not be called.");
401-
}
402-
403402
void BerkeleyBatch::Flush()
404403
{
405404
if (activeTxn)
@@ -420,6 +419,12 @@ void BerkeleyDatabase::IncrementUpdateCounter()
420419
++nUpdateCounter;
421420
}
422421

422+
BerkeleyBatch::~BerkeleyBatch()
423+
{
424+
Close();
425+
m_database.RemoveRef();
426+
}
427+
423428
void BerkeleyBatch::Close()
424429
{
425430
if (!pdb)
@@ -432,8 +437,6 @@ void BerkeleyBatch::Close()
432437

433438
if (fFlushOnClose)
434439
Flush();
435-
436-
m_database.RemoveRef();
437440
}
438441

439442
void BerkeleyEnvironment::CloseDb(const std::string& strFile)
@@ -844,7 +847,7 @@ void BerkeleyDatabase::RemoveRef()
844847
{
845848
LOCK(cs_db);
846849
m_refcount--;
847-
env->m_db_in_use.notify_all();
850+
if (env) env->m_db_in_use.notify_all();
848851
}
849852

850853
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)

src/wallet/bdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class BerkeleyBatch : public DatabaseBatch
217217

218218
public:
219219
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
220-
~BerkeleyBatch() override { Close(); }
220+
~BerkeleyBatch() override;
221221

222222
BerkeleyBatch(const BerkeleyBatch&) = delete;
223223
BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;

0 commit comments

Comments
 (0)