mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-19 21:45:11 +02:00
walletdb: Refactor DatabaseBatch abstract class from BerkeleyBatch
This commit is contained in:
@ -172,7 +172,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
/** RAII class that provides access to a Berkeley database */
|
/** RAII class that provides access to a Berkeley database */
|
||||||
class BerkeleyBatch
|
class BerkeleyBatch : public DatabaseBatch
|
||||||
{
|
{
|
||||||
/** RAII class that automatically cleanses its data on destruction */
|
/** RAII class that automatically cleanses its data on destruction */
|
||||||
class SafeDbt final
|
class SafeDbt final
|
||||||
@ -195,10 +195,10 @@ class BerkeleyBatch
|
|||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool ReadKey(CDataStream&& key, CDataStream& value);
|
bool ReadKey(CDataStream&& key, CDataStream& value) override;
|
||||||
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true);
|
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
|
||||||
bool EraseKey(CDataStream&& key);
|
bool EraseKey(CDataStream&& key) override;
|
||||||
bool HasKey(CDataStream&& key);
|
bool HasKey(CDataStream&& key) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Db* pdb;
|
Db* pdb;
|
||||||
@ -211,71 +211,20 @@ protected:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
|
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
|
||||||
~BerkeleyBatch() { Close(); }
|
~BerkeleyBatch() override { Close(); }
|
||||||
|
|
||||||
BerkeleyBatch(const BerkeleyBatch&) = delete;
|
BerkeleyBatch(const BerkeleyBatch&) = delete;
|
||||||
BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;
|
BerkeleyBatch& operator=(const BerkeleyBatch&) = delete;
|
||||||
|
|
||||||
void Flush();
|
void Flush() override;
|
||||||
void Close();
|
void Close() override;
|
||||||
|
|
||||||
template <typename K, typename T>
|
bool StartCursor() override;
|
||||||
bool Read(const K& key, T& value)
|
bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override;
|
||||||
{
|
void CloseCursor() override;
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
bool TxnBegin() override;
|
||||||
ssKey.reserve(1000);
|
bool TxnCommit() override;
|
||||||
ssKey << key;
|
bool TxnAbort() override;
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool StartCursor();
|
|
||||||
bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete);
|
|
||||||
void CloseCursor();
|
|
||||||
bool TxnBegin();
|
|
||||||
bool TxnCommit();
|
|
||||||
bool TxnAbort();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string BerkeleyDatabaseVersion();
|
std::string BerkeleyDatabaseVersion();
|
||||||
|
@ -6,7 +6,9 @@
|
|||||||
#ifndef BITCOIN_WALLET_DB_H
|
#ifndef BITCOIN_WALLET_DB_H
|
||||||
#define BITCOIN_WALLET_DB_H
|
#define BITCOIN_WALLET_DB_H
|
||||||
|
|
||||||
|
#include <clientversion.h>
|
||||||
#include <fs.h>
|
#include <fs.h>
|
||||||
|
#include <streams.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -14,4 +16,82 @@
|
|||||||
fs::path WalletDataFilePath(const fs::path& wallet_path);
|
fs::path WalletDataFilePath(const fs::path& wallet_path);
|
||||||
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
|
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
|
||||||
|
|
||||||
|
/** RAII class that provides access to a WalletDatabase */
|
||||||
|
class DatabaseBatch
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
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;
|
||||||
|
virtual bool HasKey(CDataStream&& key) = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit DatabaseBatch() {}
|
||||||
|
virtual ~DatabaseBatch() {}
|
||||||
|
|
||||||
|
DatabaseBatch(const DatabaseBatch&) = delete;
|
||||||
|
DatabaseBatch& operator=(const DatabaseBatch&) = delete;
|
||||||
|
|
||||||
|
virtual void Flush() = 0;
|
||||||
|
virtual void Close() = 0;
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool StartCursor() = 0;
|
||||||
|
virtual bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) = 0;
|
||||||
|
virtual void CloseCursor() = 0;
|
||||||
|
virtual bool TxnBegin() = 0;
|
||||||
|
virtual bool TxnCommit() = 0;
|
||||||
|
virtual bool TxnAbort() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // BITCOIN_WALLET_DB_H
|
#endif // BITCOIN_WALLET_DB_H
|
||||||
|
Reference in New Issue
Block a user