aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/bdb.h
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2020-07-14 16:20:52 +0200
committerMarcoFalke <falke.marco@gmail.com>2020-07-14 16:21:01 +0200
commita924f61caea8833b2afd3d4105fc7cd6f424d6c6 (patch)
treec3b9170a05b1194aa103324d1f0f80a77d21336b /src/wallet/bdb.h
parentb26d62c49acb68d9fe2a2f09e803d05671d0422c (diff)
parentb82f0ca4d5465b36debb6c57f335bdccf4899c49 (diff)
downloadbitcoin-a924f61caea8833b2afd3d4105fc7cd6f424d6c6.tar.xz
Merge #19325: wallet: Refactor BerkeleyDatabase to introduce DatabaseBatch abstract class
b82f0ca4d5465b36debb6c57f335bdccf4899c49 walletdb: Add MakeBatch function to BerkeleyDatabase and use it (Andrew Chow) eac9200814fa01da6522625be01dded730b26751 walletdb: Refactor DatabaseBatch abstract class from BerkeleyBatch (Andrew Chow) Pull request description: In order to support alternative database systems, we need to have a generic `Batch` class. This PR adds a `DatabaseBatch` abstract class which is implemented by `BerkeleyBatch`. `DatabaseBatch` is now the class that is used by `WalletBatch` to interact with the database. To be able to get the correct type of `DatabaseBatch`, `BerkeleyDatabase` now has a `MakeBatch` function which returns a newly constructed `std::unique_ptr<DatabaseBatch>`. For `BerkeleyDatabase`, that will be `std::unique_ptr<BerkeleyBatch>`. The `Read`, `Write`, `Erase`, and `Exists` template functions are moved from `BerkeleyBatch`. Part of #18971 Requires #19308 and #19324 ACKs for top commit: Sjors: re-utACK b82f0ca4d5465b36debb6c57f335bdccf4899c49 MarcoFalke: ACK b82f0ca4d5465b36debb6c57f335bdccf4899c49 🌘 meshcollider: LGTM, utACK b82f0ca4d5465b36debb6c57f335bdccf4899c49 Tree-SHA512: 6d2d41631c0983391dbecd702e881c6775b155c90b275df97f7157e42608ed251744f9d7ce5173d02a6c5cc38d90b611880fac7fa635d3d8c4d590681f56ac6a
Diffstat (limited to 'src/wallet/bdb.h')
-rw-r--r--src/wallet/bdb.h84
1 files changed, 19 insertions, 65 deletions
diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h
index 9cc42ddc06..e54776fc0d 100644
--- a/src/wallet/bdb.h
+++ b/src/wallet/bdb.h
@@ -93,6 +93,8 @@ std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& wallet_path, s
/** Return whether a BDB wallet database is currently loaded. */
bool IsBDBWalletLoaded(const fs::path& wallet_path);
+class BerkeleyBatch;
+
/** An instance of this class represents one database.
* For BerkeleyDB this is just a (env, strFile) tuple.
**/
@@ -161,6 +163,9 @@ public:
/** Database pointer. This is initialized lazily and reset during flushes, so it can be null. */
std::unique_ptr<Db> m_db;
+ /** Make a BerkeleyBatch connected to this database */
+ std::unique_ptr<BerkeleyBatch> MakeBatch(const char* mode, bool flush_on_close);
+
private:
std::string strFile;
@@ -172,7 +177,7 @@ private:
};
/** RAII class that provides access to a Berkeley database */
-class BerkeleyBatch
+class BerkeleyBatch : public DatabaseBatch
{
/** RAII class that automatically cleanses its data on destruction */
class SafeDbt final
@@ -195,10 +200,10 @@ class BerkeleyBatch
};
private:
- bool ReadKey(CDataStream&& key, CDataStream& value);
- bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true);
- bool EraseKey(CDataStream&& key);
- bool HasKey(CDataStream&& key);
+ bool ReadKey(CDataStream&& key, CDataStream& value) override;
+ bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
+ bool EraseKey(CDataStream&& key) override;
+ bool HasKey(CDataStream&& key) override;
protected:
Db* pdb;
@@ -211,71 +216,20 @@ protected:
public:
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
- ~BerkeleyBatch() { Close(); }
+ ~BerkeleyBatch() override { Close(); }
BerkeleyBatch(const BerkeleyBatch&) = delete;
BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;
- void Flush();
- void Close();
-
- template <typename K, typename T>
- bool Read(const K& key, T& value)
- {
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
-
- CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- if (!ReadKey(std::move(ssKey), ssValue)) return false;
- try {
- ssValue >> value;
- return true;
- } catch (const std::exception&) {
- return false;
- }
- }
-
- template <typename K, typename T>
- bool Write(const K& key, const T& value, bool fOverwrite = true)
- {
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
-
- CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- ssValue.reserve(10000);
- ssValue << value;
-
- return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
- }
-
- template <typename K>
- bool Erase(const K& key)
- {
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
-
- return EraseKey(std::move(ssKey));
- }
-
- template <typename K>
- bool Exists(const K& key)
- {
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
-
- return HasKey(std::move(ssKey));
- }
+ void Flush() override;
+ void Close() override;
- bool StartCursor();
- bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete);
- void CloseCursor();
- bool TxnBegin();
- bool TxnCommit();
- bool TxnAbort();
+ bool StartCursor() override;
+ bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override;
+ void CloseCursor() override;
+ bool TxnBegin() override;
+ bool TxnCommit() override;
+ bool TxnAbort() override;
};
std::string BerkeleyDatabaseVersion();