Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/bdb.cpp
Show First 20 Lines • Show All 339 Lines • ▼ Show 20 Lines | if (env) { | ||||
size_t erased = env->m_databases.erase(strFile); | size_t erased = env->m_databases.erase(strFile); | ||||
assert(erased == 1); | assert(erased == 1); | ||||
env->m_fileids.erase(strFile); | env->m_fileids.erase(strFile); | ||||
} | } | ||||
} | } | ||||
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase &database, const char *pszMode, | BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase &database, const char *pszMode, | ||||
bool fFlushOnCloseIn) | bool fFlushOnCloseIn) | ||||
: pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr) { | : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), | ||||
m_database(database) { | |||||
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w')); | fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w')); | ||||
fFlushOnClose = fFlushOnCloseIn; | fFlushOnClose = fFlushOnCloseIn; | ||||
env = database.env.get(); | env = database.env.get(); | ||||
if (database.IsDummy()) { | if (database.IsDummy()) { | ||||
return; | return; | ||||
} | } | ||||
const std::string &strFilename = database.strFile; | const std::string &strFilename = database.strFile; | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | if (fCreate) { | ||||
if (fCreate && !Exists(std::string("version"))) { | if (fCreate && !Exists(std::string("version"))) { | ||||
bool fTmp = fReadOnly; | bool fTmp = fReadOnly; | ||||
fReadOnly = false; | fReadOnly = false; | ||||
Write(std::string("version"), CLIENT_VERSION); | Write(std::string("version"), CLIENT_VERSION); | ||||
fReadOnly = fTmp; | fReadOnly = fTmp; | ||||
} | } | ||||
} | } | ||||
++env->mapFileUseCount[strFilename]; | database.AddRef(); | ||||
strFile = strFilename; | strFile = strFilename; | ||||
} | } | ||||
} | } | ||||
void BerkeleyBatch::Flush() { | void BerkeleyBatch::Flush() { | ||||
if (activeTxn) { | if (activeTxn) { | ||||
return; | return; | ||||
} | } | ||||
Show All 29 Lines | void BerkeleyBatch::Close() { | ||||
activeTxn = nullptr; | activeTxn = nullptr; | ||||
pdb = nullptr; | pdb = nullptr; | ||||
CloseCursor(); | CloseCursor(); | ||||
if (fFlushOnClose) { | if (fFlushOnClose) { | ||||
Flush(); | Flush(); | ||||
} | } | ||||
{ | m_database.RemoveRef(); | ||||
LOCK(cs_db); | |||||
--env->mapFileUseCount[strFile]; | |||||
} | |||||
env->m_db_in_use.notify_all(); | |||||
} | } | ||||
void BerkeleyEnvironment::CloseDb(const std::string &strFile) { | void BerkeleyEnvironment::CloseDb(const std::string &strFile) { | ||||
LOCK(cs_db); | LOCK(cs_db); | ||||
auto it = m_databases.find(strFile); | auto it = m_databases.find(strFile); | ||||
assert(it != m_databases.end()); | assert(it != m_databases.end()); | ||||
BerkeleyDatabase &database = it->second.get(); | BerkeleyDatabase &database = it->second.get(); | ||||
if (database.m_db) { | if (database.m_db) { | ||||
▲ Show 20 Lines • Show All 423 Lines • ▼ Show 20 Lines | bool BerkeleyBatch::HasKey(CDataStream &&key) { | ||||
} | } | ||||
SafeDbt datKey(key.data(), key.size()); | SafeDbt datKey(key.data(), key.size()); | ||||
int ret = pdb->exists(activeTxn, datKey, 0); | int ret = pdb->exists(activeTxn, datKey, 0); | ||||
return ret == 0; | return ret == 0; | ||||
} | } | ||||
void BerkeleyDatabase::AddRef() { | |||||
LOCK(cs_db); | |||||
++env->mapFileUseCount[strFile]; | |||||
} | |||||
void BerkeleyDatabase::RemoveRef() { | |||||
{ | |||||
LOCK(cs_db); | |||||
--env->mapFileUseCount[strFile]; | |||||
} | |||||
env->m_db_in_use.notify_all(); | |||||
} | |||||
std::unique_ptr<BerkeleyBatch> | std::unique_ptr<BerkeleyBatch> | ||||
BerkeleyDatabase::MakeBatch(const char *mode, bool flush_on_close) { | BerkeleyDatabase::MakeBatch(const char *mode, bool flush_on_close) { | ||||
return std::make_unique<BerkeleyBatch>(*this, mode, flush_on_close); | return std::make_unique<BerkeleyBatch>(*this, mode, flush_on_close); | ||||
} | } |