aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/db.h
diff options
context:
space:
mode:
authorAndrew Chow <achow101-github@achow101.com>2022-04-11 15:14:24 -0400
committerAndrew Chow <github@achow101.com>2022-12-14 12:41:41 -0500
commit7a198bba0a1d0a0f0fd4ca947955cb52b84bdd4b (patch)
treeac500d26ed18aeb2265c814aac88158c858a9e8f /src/wallet/db.h
parent69efbc011bb74fcd8dd9ed2a8a5d31bc9e323c10 (diff)
downloadbitcoin-7a198bba0a1d0a0f0fd4ca947955cb52b84bdd4b.tar.xz
wallet: Introduce DatabaseCursor RAII class for managing cursor
Instead of having DatabaseBatch deal with opening and closing database cursors, have a separate RAII class that deals with those. For now, DatabaseBatch manages DatabaseCursor, but this will change later.
Diffstat (limited to 'src/wallet/db.h')
-rw-r--r--src/wallet/db.h41
1 files changed, 35 insertions, 6 deletions
diff --git a/src/wallet/db.h b/src/wallet/db.h
index f09844c37e..aa1377ccef 100644
--- a/src/wallet/db.h
+++ b/src/wallet/db.h
@@ -22,10 +22,24 @@ struct bilingual_str;
namespace wallet {
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
+class DatabaseCursor
+{
+public:
+ explicit DatabaseCursor() {}
+ virtual ~DatabaseCursor() {}
+
+ DatabaseCursor(const DatabaseCursor&) = delete;
+ DatabaseCursor& operator=(const DatabaseCursor&) = delete;
+
+ virtual bool Next(CDataStream& key, CDataStream& value, bool& complete) { return false; }
+};
+
/** RAII class that provides access to a WalletDatabase */
class DatabaseBatch
{
private:
+ std::unique_ptr<DatabaseCursor> m_cursor;
+
virtual bool ReadKey(CDataStream&& key, CDataStream& value) = 0;
virtual bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) = 0;
virtual bool EraseKey(CDataStream&& key) = 0;
@@ -92,9 +106,21 @@ public:
return HasKey(std::move(ssKey));
}
- virtual bool StartCursor() = 0;
- virtual bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) = 0;
- virtual void CloseCursor() = 0;
+ virtual std::unique_ptr<DatabaseCursor> GetNewCursor() = 0;
+ bool StartCursor()
+ {
+ m_cursor = GetNewCursor();
+ return m_cursor != nullptr;
+ }
+ bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete)
+ {
+ if (!m_cursor) return false;
+ return m_cursor->Next(ssKey, ssValue, complete);
+ }
+ void CloseCursor()
+ {
+ m_cursor.reset();
+ }
virtual bool TxnBegin() = 0;
virtual bool TxnCommit() = 0;
virtual bool TxnAbort() = 0;
@@ -156,6 +182,11 @@ public:
virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
};
+class DummyCursor : public DatabaseCursor
+{
+ bool Next(CDataStream& key, CDataStream& value, bool& complete) override { return false; }
+};
+
/** RAII class that provides access to a DummyDatabase. Never fails. */
class DummyBatch : public DatabaseBatch
{
@@ -169,9 +200,7 @@ public:
void Flush() override {}
void Close() override {}
- bool StartCursor() override { return true; }
- bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override { return true; }
- void CloseCursor() override {}
+ std::unique_ptr<DatabaseCursor> GetNewCursor() override { return std::make_unique<DummyCursor>(); }
bool TxnBegin() override { return true; }
bool TxnCommit() override { return true; }
bool TxnAbort() override { return true; }