Skip to content

Commit eac9200

Browse files
committed
walletdb: Refactor DatabaseBatch abstract class from BerkeleyBatch
1 parent cc9d09e commit eac9200

File tree

2 files changed

+94
-65
lines changed

2 files changed

+94
-65
lines changed

src/wallet/bdb.h

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class BerkeleyDatabase
172172
};
173173

174174
/** RAII class that provides access to a Berkeley database */
175-
class BerkeleyBatch
175+
class BerkeleyBatch : public DatabaseBatch
176176
{
177177
/** RAII class that automatically cleanses its data on destruction */
178178
class SafeDbt final
@@ -195,10 +195,10 @@ class BerkeleyBatch
195195
};
196196

197197
private:
198-
bool ReadKey(CDataStream&& key, CDataStream& value);
199-
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true);
200-
bool EraseKey(CDataStream&& key);
201-
bool HasKey(CDataStream&& key);
198+
bool ReadKey(CDataStream&& key, CDataStream& value) override;
199+
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
200+
bool EraseKey(CDataStream&& key) override;
201+
bool HasKey(CDataStream&& key) override;
202202

203203
protected:
204204
Db* pdb;
@@ -211,71 +211,20 @@ class BerkeleyBatch
211211

212212
public:
213213
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
214-
~BerkeleyBatch() { Close(); }
214+
~BerkeleyBatch() override { Close(); }
215215

216216
BerkeleyBatch(const BerkeleyBatch&) = delete;
217217
BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;
218218

219-
void Flush();
220-
void Close();
221-
222-
template <typename K, typename T>
223-
bool Read(const K& key, T& value)
224-
{
225-
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
226-
ssKey.reserve(1000);
227-
ssKey << key;
228-
229-
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
230-
if (!ReadKey(std::move(ssKey), ssValue)) return false;
231-
try {
232-
ssValue >> value;
233-
return true;
234-
} catch (const std::exception&) {
235-
return false;
236-
}
237-
}
238-
239-
template <typename K, typename T>
240-
bool Write(const K& key, const T& value, bool fOverwrite = true)
241-
{
242-
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
243-
ssKey.reserve(1000);
244-
ssKey << key;
245-
246-
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
247-
ssValue.reserve(10000);
248-
ssValue << value;
249-
250-
return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
251-
}
252-
253-
template <typename K>
254-
bool Erase(const K& key)
255-
{
256-
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
257-
ssKey.reserve(1000);
258-
ssKey << key;
259-
260-
return EraseKey(std::move(ssKey));
261-
}
262-
263-
template <typename K>
264-
bool Exists(const K& key)
265-
{
266-
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
267-
ssKey.reserve(1000);
268-
ssKey << key;
269-
270-
return HasKey(std::move(ssKey));
271-
}
219+
void Flush() override;
220+
void Close() override;
272221

273-
bool StartCursor();
274-
bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete);
275-
void CloseCursor();
276-
bool TxnBegin();
277-
bool TxnCommit();
278-
bool TxnAbort();
222+
bool StartCursor() override;
223+
bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override;
224+
void CloseCursor() override;
225+
bool TxnBegin() override;
226+
bool TxnCommit() override;
227+
bool TxnAbort() override;
279228
};
280229

281230
std::string BerkeleyDatabaseVersion();

src/wallet/db.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,92 @@
66
#ifndef BITCOIN_WALLET_DB_H
77
#define BITCOIN_WALLET_DB_H
88

9+
#include <clientversion.h>
910
#include <fs.h>
11+
#include <streams.h>
1012

1113
#include <string>
1214

1315
/** Given a wallet directory path or legacy file path, return path to main data file in the wallet database. */
1416
fs::path WalletDataFilePath(const fs::path& wallet_path);
1517
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
1618

19+
/** RAII class that provides access to a WalletDatabase */
20+
class DatabaseBatch
21+
{
22+
private:
23+
virtual bool ReadKey(CDataStream&& key, CDataStream& value) = 0;
24+
virtual bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) = 0;
25+
virtual bool EraseKey(CDataStream&& key) = 0;
26+
virtual bool HasKey(CDataStream&& key) = 0;
27+
28+
public:
29+
explicit DatabaseBatch() {}
30+
virtual ~DatabaseBatch() {}
31+
32+
DatabaseBatch(const DatabaseBatch&) = delete;
33+
DatabaseBatch& operator=(const DatabaseBatch&) = delete;
34+
35+
virtual void Flush() = 0;
36+
virtual void Close() = 0;
37+
38+
template <typename K, typename T>
39+
bool Read(const K& key, T& value)
40+
{
41+
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
42+
ssKey.reserve(1000);
43+
ssKey << key;
44+
45+
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
46+
if (!ReadKey(std::move(ssKey), ssValue)) return false;
47+
try {
48+
ssValue >> value;
49+
return true;
50+
} catch (const std::exception&) {
51+
return false;
52+
}
53+
}
54+
55+
template <typename K, typename T>
56+
bool Write(const K& key, const T& value, bool fOverwrite = true)
57+
{
58+
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
59+
ssKey.reserve(1000);
60+
ssKey << key;
61+
62+
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
63+
ssValue.reserve(10000);
64+
ssValue << value;
65+
66+
return WriteKey(std::move(ssKey), std::move(ssValue), fOverwrite);
67+
}
68+
69+
template <typename K>
70+
bool Erase(const K& key)
71+
{
72+
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
73+
ssKey.reserve(1000);
74+
ssKey << key;
75+
76+
return EraseKey(std::move(ssKey));
77+
}
78+
79+
template <typename K>
80+
bool Exists(const K& key)
81+
{
82+
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
83+
ssKey.reserve(1000);
84+
ssKey << key;
85+
86+
return HasKey(std::move(ssKey));
87+
}
88+
89+
virtual bool StartCursor() = 0;
90+
virtual bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) = 0;
91+
virtual void CloseCursor() = 0;
92+
virtual bool TxnBegin() = 0;
93+
virtual bool TxnCommit() = 0;
94+
virtual bool TxnAbort() = 0;
95+
};
96+
1797
#endif // BITCOIN_WALLET_DB_H

0 commit comments

Comments
 (0)