Use unique_ptr for dbenv (DbEnv)

pull/476/merge
practicalswift 7 years ago
parent f72cbf9ba9
commit 29ab96dbd2

@ -75,13 +75,12 @@ void CDBEnv::EnvShutdown()
void CDBEnv::Reset() void CDBEnv::Reset()
{ {
delete dbenv; dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS));
dbenv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
fDbEnvInit = false; fDbEnvInit = false;
fMockDb = false; fMockDb = false;
} }
CDBEnv::CDBEnv() : dbenv(nullptr) CDBEnv::CDBEnv()
{ {
Reset(); Reset();
} }
@ -89,8 +88,6 @@ CDBEnv::CDBEnv() : dbenv(nullptr)
CDBEnv::~CDBEnv() CDBEnv::~CDBEnv()
{ {
EnvShutdown(); EnvShutdown();
delete dbenv;
dbenv = nullptr;
} }
void CDBEnv::Close() void CDBEnv::Close()
@ -182,7 +179,7 @@ CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type
LOCK(cs_db); LOCK(cs_db);
assert(mapFileUseCount.count(strFile) == 0); assert(mapFileUseCount.count(strFile) == 0);
Db db(dbenv, 0); Db db(dbenv.get(), 0);
int result = db.verify(strFile.c_str(), nullptr, nullptr, 0); int result = db.verify(strFile.c_str(), nullptr, nullptr, 0);
if (result == 0) if (result == 0)
return VERIFY_OK; return VERIFY_OK;
@ -225,7 +222,7 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco
} }
LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size()); LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size());
std::unique_ptr<Db> pdbCopy(new Db(bitdb.dbenv, 0)); std::unique_ptr<Db> pdbCopy(new Db(bitdb.dbenv.get(), 0));
int ret = pdbCopy->open(nullptr, // Txn pointer int ret = pdbCopy->open(nullptr, // Txn pointer
filename.c_str(), // Filename filename.c_str(), // Filename
"main", // Logical db name "main", // Logical db name
@ -334,7 +331,7 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<C
std::stringstream strDump; std::stringstream strDump;
Db db(dbenv, 0); Db db(dbenv.get(), 0);
int result = db.verify(strFile.c_str(), nullptr, &strDump, flags); int result = db.verify(strFile.c_str(), nullptr, &strDump, flags);
if (result == DB_VERIFY_BAD) { if (result == DB_VERIFY_BAD) {
LogPrintf("CDBEnv::Salvage: Database salvage found errors, all data may not be recoverable.\n"); LogPrintf("CDBEnv::Salvage: Database salvage found errors, all data may not be recoverable.\n");
@ -525,7 +522,7 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
std::string strFileRes = strFile + ".rewrite"; std::string strFileRes = strFile + ".rewrite";
{ // surround usage of db with extra {} { // surround usage of db with extra {}
CDB db(dbw, "r"); CDB db(dbw, "r");
Db* pdbCopy = new Db(env->dbenv, 0); Db* pdbCopy = new Db(env->dbenv.get(), 0);
int ret = pdbCopy->open(nullptr, // Txn pointer int ret = pdbCopy->open(nullptr, // Txn pointer
strFileRes.c_str(), // Filename strFileRes.c_str(), // Filename
@ -577,10 +574,10 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
delete pdbCopy; delete pdbCopy;
} }
if (fSuccess) { if (fSuccess) {
Db dbA(env->dbenv, 0); Db dbA(env->dbenv.get(), 0);
if (dbA.remove(strFile.c_str(), nullptr, 0)) if (dbA.remove(strFile.c_str(), nullptr, 0))
fSuccess = false; fSuccess = false;
Db dbB(env->dbenv, 0); Db dbB(env->dbenv.get(), 0);
if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0)) if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0))
fSuccess = false; fSuccess = false;
} }

@ -36,7 +36,7 @@ private:
public: public:
mutable CCriticalSection cs_db; mutable CCriticalSection cs_db;
DbEnv *dbenv; std::unique_ptr<DbEnv> dbenv;
std::map<std::string, int> mapFileUseCount; std::map<std::string, int> mapFileUseCount;
std::map<std::string, Db*> mapDb; std::map<std::string, Db*> mapDb;

Loading…
Cancel
Save