Skip to content

Commit

Permalink
Use unique_ptr for dbenv (DbEnv)
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalswift authored and furszy committed Jul 21, 2021
1 parent a1bef4f commit 5b31813
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
31 changes: 14 additions & 17 deletions src/wallet/db.cpp
Expand Up @@ -77,22 +77,19 @@ void CDBEnv::EnvShutdown()

void CDBEnv::Reset()
{
delete dbenv;
dbenv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS));
fDbEnvInit = false;
fMockDb = false;
}

CDBEnv::CDBEnv() : dbenv(NULL)
CDBEnv::CDBEnv()
{
Reset();
}

CDBEnv::~CDBEnv()
{
EnvShutdown();
delete dbenv;
dbenv = NULL;
}

void CDBEnv::Close()
Expand Down Expand Up @@ -184,8 +181,8 @@ CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type
LOCK(cs_db);
assert(mapFileUseCount.count(strFile) == 0);

Db db(dbenv, 0);
int result = db.verify(strFile.c_str(), NULL, NULL, 0);
Db db(dbenv.get(), 0);
int result = db.verify(strFile.c_str(), nullptr, nullptr, 0);
if (result == 0)
return VERIFY_OK;
else if (recoverFunc == NULL)
Expand Down Expand Up @@ -225,8 +222,8 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco
}
LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size());

std::unique_ptr<Db> pdbCopy(new Db(bitdb.dbenv, 0));
int ret = pdbCopy->open(NULL, // Txn pointer
std::unique_ptr<Db> pdbCopy(new Db(bitdb.dbenv.get(), 0));
int ret = pdbCopy->open(nullptr, // Txn pointer
filename.c_str(), // Filename
"main", // Logical db name
DB_BTREE, // Database type
Expand Down Expand Up @@ -326,8 +323,8 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<C

std::stringstream strDump;

Db db(dbenv, 0);
int result = db.verify(strFile.c_str(), NULL, &strDump, flags);
Db db(dbenv.get(), 0);
int result = db.verify(strFile.c_str(), nullptr, &strDump, flags);
if (result == DB_VERIFY_BAD) {
LogPrintf("CDBEnv::Salvage : Database salvage found errors, all data may not be recoverable.\n");
if (!fAggressive) {
Expand Down Expand Up @@ -408,7 +405,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
pdb = env->mapDb[strFilename];
if (pdb == nullptr) {
int ret;
std::unique_ptr<Db> pdb_temp(new Db(env->dbenv, 0));
std::unique_ptr<Db> pdb_temp(new Db(env->dbenv.get(), 0));

bool fMockDb = env->IsMock();
if (fMockDb) {
Expand Down Expand Up @@ -517,7 +514,7 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
std::string strFileRes = strFile + ".rewrite";
{ // surround usage of db with extra {}
CDB db(dbw, "r");
Db* pdbCopy = new Db(env->dbenv, 0);
Db* pdbCopy = new Db(env->dbenv.get(), 0);

int ret = pdbCopy->open(NULL, // Txn pointer
strFileRes.c_str(), // Filename
Expand Down Expand Up @@ -569,11 +566,11 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
delete pdbCopy;
}
if (fSuccess) {
Db dbA(env->dbenv, 0);
if (dbA.remove(strFile.c_str(), NULL, 0))
Db dbA(env->dbenv.get(), 0);
if (dbA.remove(strFile.c_str(), nullptr, 0))
fSuccess = false;
Db dbB(env->dbenv, 0);
if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
Db dbB(env->dbenv.get(), 0);
if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0))
fSuccess = false;
}
if (!fSuccess)
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/db.h
Expand Up @@ -37,7 +37,7 @@ class CDBEnv

public:
mutable RecursiveMutex cs_db;
DbEnv *dbenv;
std::unique_ptr<DbEnv> dbenv;
std::map<std::string, int> mapFileUseCount;
std::map<std::string, Db*> mapDb;

Expand Down

0 comments on commit 5b31813

Please sign in to comment.