Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/db.cpp
Show First 20 Lines • Show All 392 Lines • ▼ Show 20 Lines | CDB::CDB(CWalletDBWrapper &dbw, const char *pszMode, bool fFlushOnCloseIn) | ||||
{ | { | ||||
LOCK(env->cs_db); | LOCK(env->cs_db); | ||||
if (!env->Open(GetDataDir())) { | if (!env->Open(GetDataDir())) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"CDB: Failed to open database environment."); | "CDB: Failed to open database environment."); | ||||
} | } | ||||
strFile = strFilename; | pdb = env->mapDb[strFilename]; | ||||
++env->mapFileUseCount[strFile]; | |||||
pdb = env->mapDb[strFile]; | |||||
if (pdb == nullptr) { | if (pdb == nullptr) { | ||||
pdb = new Db(env->dbenv, 0); | std::unique_ptr<Db> pdb_temp(new Db(env->dbenv, 0)); | ||||
bool fMockDb = env->IsMock(); | bool fMockDb = env->IsMock(); | ||||
if (fMockDb) { | if (fMockDb) { | ||||
DbMpoolFile *mpf = pdb->get_mpf(); | DbMpoolFile *mpf = pdb_temp->get_mpf(); | ||||
ret = mpf->set_flags(DB_MPOOL_NOFILE, 1); | ret = mpf->set_flags(DB_MPOOL_NOFILE, 1); | ||||
if (ret != 0) { | if (ret != 0) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
strprintf("CDB: Failed to configure for no temp file " | strprintf("CDB: Failed to configure for no temp file " | ||||
"backing for database %s", | "backing for database %s", | ||||
strFile)); | strFilename)); | ||||
} | } | ||||
} | } | ||||
ret = | ret = pdb_temp->open( | ||||
pdb->open(nullptr, // Txn pointer | nullptr, // Txn pointer | ||||
fMockDb ? nullptr : strFile.c_str(), // Filename | fMockDb ? nullptr : strFilename.c_str(), // Filename | ||||
fMockDb ? strFile.c_str() : "main", // Logical db name | fMockDb ? strFilename.c_str() : "main", // Logical db name | ||||
DB_BTREE, // Database type | DB_BTREE, // Database type | ||||
nFlags, // Flags | nFlags, // Flags | ||||
0); | 0); | ||||
if (ret != 0) { | if (ret != 0) { | ||||
delete pdb; | |||||
pdb = nullptr; | |||||
--env->mapFileUseCount[strFile]; | |||||
strFile = ""; | |||||
throw std::runtime_error(strprintf( | throw std::runtime_error(strprintf( | ||||
"CDB: Error %d, can't open database %s", ret, strFilename)); | "CDB: Error %d, can't open database %s", ret, strFilename)); | ||||
} | } | ||||
pdb = pdb_temp.release(); | |||||
env->mapDb[strFilename] = pdb; | |||||
if (fCreate && !Exists(std::string("version"))) { | if (fCreate && !Exists(std::string("version"))) { | ||||
bool fTmp = fReadOnly; | bool fTmp = fReadOnly; | ||||
fReadOnly = false; | fReadOnly = false; | ||||
WriteVersion(CLIENT_VERSION); | WriteVersion(CLIENT_VERSION); | ||||
fReadOnly = fTmp; | fReadOnly = fTmp; | ||||
} | } | ||||
env->mapDb[strFile] = pdb; | |||||
} | } | ||||
++env->mapFileUseCount[strFilename]; | |||||
strFile = strFilename; | |||||
} | } | ||||
} | } | ||||
void CDB::Flush() { | void CDB::Flush() { | ||||
if (activeTxn) { | if (activeTxn) { | ||||
return; | return; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 292 Lines • Show Last 20 Lines |