@@ -317,13 +317,27 @@ BerkeleyDatabase::~BerkeleyDatabase()
317
317
318
318
BerkeleyBatch::BerkeleyBatch (BerkeleyDatabase& database, const char * pszMode, bool fFlushOnCloseIn ) : pdb(nullptr ), activeTxn(nullptr ), m_cursor(nullptr ), m_database(database)
319
319
{
320
+ database.AddRef ();
321
+ database.Open (pszMode);
320
322
fReadOnly = (!strchr (pszMode, ' +' ) && !strchr (pszMode, ' w' ));
321
323
fFlushOnClose = fFlushOnCloseIn ;
322
324
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 ()){
324
339
return ;
325
340
}
326
- const std::string &strFilename = database.strFile ;
327
341
328
342
bool fCreate = strchr (pszMode, ' c' ) != nullptr ;
329
343
unsigned int nFlags = DB_THREAD;
@@ -334,10 +348,9 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
334
348
LOCK (cs_db);
335
349
bilingual_str open_err;
336
350
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." );
338
352
339
- pdb = database.m_db .get ();
340
- if (pdb == nullptr ) {
353
+ if (m_db == nullptr ) {
341
354
int ret;
342
355
std::unique_ptr<Db> pdb_temp = MakeUnique<Db>(env->dbenv .get (), 0 );
343
356
@@ -346,19 +359,19 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
346
359
DbMpoolFile* mpf = pdb_temp->get_mpf ();
347
360
ret = mpf->set_flags (DB_MPOOL_NOFILE, 1 );
348
361
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 ));
350
363
}
351
364
}
352
365
353
366
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
356
369
DB_BTREE, // Database type
357
370
nFlags, // Flags
358
371
0 );
359
372
360
373
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 ));
362
375
}
363
376
364
377
// Call CheckUniqueFileid on the containing BDB environment to
@@ -377,29 +390,15 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
377
390
// versions of BDB have an set_lk_exclusive method for this
378
391
// purpose, but the older version we use does not.)
379
392
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 ]);
381
394
}
382
395
383
- pdb = pdb_temp.release ();
384
- database.m_db .reset (pdb);
396
+ m_db.reset (pdb_temp.release ());
385
397
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
- }
392
398
}
393
- database.AddRef ();
394
- strFile = strFilename;
395
399
}
396
400
}
397
401
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
-
403
402
void BerkeleyBatch::Flush ()
404
403
{
405
404
if (activeTxn)
@@ -420,6 +419,12 @@ void BerkeleyDatabase::IncrementUpdateCounter()
420
419
++nUpdateCounter;
421
420
}
422
421
422
+ BerkeleyBatch::~BerkeleyBatch ()
423
+ {
424
+ Close ();
425
+ m_database.RemoveRef ();
426
+ }
427
+
423
428
void BerkeleyBatch::Close ()
424
429
{
425
430
if (!pdb)
@@ -432,8 +437,6 @@ void BerkeleyBatch::Close()
432
437
433
438
if (fFlushOnClose )
434
439
Flush ();
435
-
436
- m_database.RemoveRef ();
437
440
}
438
441
439
442
void BerkeleyEnvironment::CloseDb (const std::string& strFile)
@@ -844,7 +847,7 @@ void BerkeleyDatabase::RemoveRef()
844
847
{
845
848
LOCK (cs_db);
846
849
m_refcount--;
847
- env->m_db_in_use .notify_all ();
850
+ if (env) env->m_db_in_use .notify_all ();
848
851
}
849
852
850
853
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch (const char * mode, bool flush_on_close)
0 commit comments