diff options
author | Andrew Chow <achow101-github@achow101.com> | 2020-06-15 15:42:53 -0400 |
---|---|---|
committer | Andrew Chow <achow101-github@achow101.com> | 2020-06-17 10:29:27 -0400 |
commit | a389ed52e8f4939ab5b4adcf93dcb7783d9006f1 (patch) | |
tree | 1ef234d91e7b2d9da3cfaa3862c1b4d07b188431 /src/wallet/bdb.cpp | |
parent | 39bd9ddb8783807b9cde6288233e86ad7c85d61f (diff) | |
download | bitcoin-a389ed52e8f4939ab5b4adcf93dcb7783d9006f1.tar.xz |
walletdb: refactor Read, Write, Erase, and Exists into non-template func
In order to override these later, the specific details of how the Read,
Write, Erase, and Exists functions interact with the actual database
file need to go into functions that are not templated.
Diffstat (limited to 'src/wallet/bdb.cpp')
-rw-r--r-- | src/wallet/bdb.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index 7ed9c88122..125bf004e4 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -803,3 +803,67 @@ std::string BerkeleyDatabaseVersion() { return DbEnv::version(nullptr, nullptr, nullptr); } + +bool BerkeleyBatch::ReadKey(CDataStream& key, CDataStream& value) +{ + if (!pdb) + return false; + + // Key + SafeDbt datKey(key.data(), key.size()); + + // Read + SafeDbt datValue; + int ret = pdb->get(activeTxn, datKey, datValue, 0); + if (ret == 0 && datValue.get_data() != nullptr) { + value.write((char*)datValue.get_data(), datValue.get_size()); + return true; + } + return false; +} + +bool BerkeleyBatch::WriteKey(CDataStream& key, CDataStream& value, bool overwrite) +{ + if (!pdb) + return true; + if (fReadOnly) + assert(!"Write called on database in read-only mode"); + + // Key + SafeDbt datKey(key.data(), key.size()); + + // Value + SafeDbt datValue(value.data(), value.size()); + + // Write + int ret = pdb->put(activeTxn, datKey, datValue, (overwrite ? 0 : DB_NOOVERWRITE)); + return (ret == 0); +} + +bool BerkeleyBatch::EraseKey(CDataStream& key) +{ + if (!pdb) + return false; + if (fReadOnly) + assert(!"Erase called on database in read-only mode"); + + // Key + SafeDbt datKey(key.data(), key.size()); + + // Erase + int ret = pdb->del(activeTxn, datKey, 0); + return (ret == 0 || ret == DB_NOTFOUND); +} + +bool BerkeleyBatch::HasKey(CDataStream& key) +{ + if (!pdb) + return false; + + // Key + SafeDbt datKey(key.data(), key.size()); + + // Exists + int ret = pdb->exists(activeTxn, datKey, 0); + return ret == 0; +} |