2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-10-31 01:43:19 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2015-03-21 18:15:31 +01:00
|
|
|
#ifndef BITCOIN_WALLET_DB_H
|
|
|
|
#define BITCOIN_WALLET_DB_H
|
2011-05-15 09:11:04 +02:00
|
|
|
|
2020-06-18 22:15:33 +02:00
|
|
|
#include <clientversion.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <fs.h>
|
2020-06-18 22:15:33 +02:00
|
|
|
#include <streams.h>
|
2011-05-15 09:11:04 +02:00
|
|
|
|
|
|
|
#include <string>
|
2018-11-20 15:15:51 +01:00
|
|
|
|
2019-03-18 08:34:23 +01:00
|
|
|
/** Given a wallet directory path or legacy file path, return path to main data file in the wallet database. */
|
|
|
|
fs::path WalletDataFilePath(const fs::path& wallet_path);
|
2022-03-03 19:37:45 +01:00
|
|
|
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
|
2020-03-14 07:51:27 +01:00
|
|
|
|
2020-06-18 22:15:33 +02:00
|
|
|
/** 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;
|
|
|
|
};
|
|
|
|
|
2015-03-21 18:15:31 +01:00
|
|
|
#endif // BITCOIN_WALLET_DB_H
|