5#ifndef BITCOIN_DBWRAPPER_H
6#define BITCOIN_DBWRAPPER_H
85bool DestroyDB(
const std::string& path_str);
102 void EraseImpl(std::span<const std::byte> key);
112 template <
typename K,
typename V>
121 template <
typename K>
142 void SeekImpl(std::span<const std::byte> key);
143 std::span<const std::byte>
GetKeyImpl()
const;
159 template<
typename K>
void Seek(
const K& key) {
171 }
catch (
const std::exception&) {
183 }
catch (
const std::exception&) {
208 std::optional<std::string>
ReadImpl(std::span<const std::byte> key)
const;
209 bool ExistsImpl(std::span<const std::byte> key)
const;
210 size_t EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2)
const;
222 DeserializationError,
248 template <
typename K,
typename V>
257 std::optional<std::string> strValue;
263 }
catch (
const std::exception& e) {
271 }
catch (
const std::exception& e) {
286 template <
typename K,
typename V>
287 bool Read(
const K& key, V& value)
const
291 switch (
const auto& [err_code, err_msg] = res.
error(); err_code) {
298 template <
typename K,
typename V>
299 void Write(
const K& key,
const V& value,
bool fSync =
false)
302 batch.
Write(key, value);
306 template <
typename K>
315 template <
typename K>
316 void Erase(
const K& key,
bool fSync =
false)
329 std::optional<std::string>
GetProperty(
const std::string& property)
const;
#define Assert(val)
Identity function.
Batch of changes queued to be written to a CDBWrapper.
const std::unique_ptr< WriteBatchImpl > m_impl_batch
void WriteImpl(std::span< const std::byte > key, DataStream &value)
void Write(const K &key, const V &value)
void EraseImpl(std::span< const std::byte > key)
CDBBatch(const CDBWrapper &_parent)
size_t ApproximateSize() const
const CDBWrapper & parent
DataStream m_value_scratch
CDBIterator(const CDBWrapper &_parent, std::unique_ptr< IteratorImpl > _piter)
const std::unique_ptr< IteratorImpl > m_impl_iter
void SeekImpl(std::span< const std::byte > key)
std::span< const std::byte > GetKeyImpl() const
const CDBWrapper & parent
std::span< const std::byte > GetValueImpl() const
CDBWrapper(const CDBWrapper &)=delete
std::optional< std::string > ReadImpl(std::span< const std::byte > key) const
size_t EstimateSizeImpl(std::span< const std::byte > key1, std::span< const std::byte > key2) const
void CompactFull()
Perform a blocking full compaction of the underlying LevelDB.
size_t DynamicMemoryUsage() const
bool Read(const K &key, V &value) const
Wrapper around TryRead() that preserves the original Read() semantics: returns true on success,...
CDBIterator * NewIterator()
std::string m_name
the name of this database
bool Exists(const K &key) const
CDBWrapper(const DBParams ¶ms)
void Erase(const K &key, bool fSync=false)
void WriteBatch(CDBBatch &batch, bool fSync=false)
bool ExistsImpl(std::span< const std::byte > key) const
void Write(const K &key, const V &value, bool fSync=false)
static bool HasKeyStartingWith(const fs::path &path, uint8_t prefix)
Probe an unopened database for a key prefix.
Obfuscation m_obfuscation
optional XOR-obfuscation of the database
std::unique_ptr< LevelDBContext > m_db_context
holds all leveldb-specific fields of this class
CDBWrapper & operator=(const CDBWrapper &)=delete
static const std::string OBFUSCATION_KEY
obfuscation key storage key, null-prefixed to avoid collisions
auto & DBContext() const LIFETIMEBOUND
bool IsEmpty()
Return true if the database managed by this class contains no entries.
ReadStatus TryRead(const K &key, V &value) const
Read and deserialize a value from the database, with explicit error discrimination.
std::optional< std::string > GetProperty(const std::string &property) const
Return a LevelDB property value, if available.
size_t EstimateSize(const K &key_begin, const K &key_end) const
Double ended buffer combining vector and stream-like interfaces.
void write(std::span< const value_type > src)
void reserve(size_type n)
Minimal stream for reading from an existing byte array by std::span.
dbwrapper_error(const std::string &msg)
The util::Expected class provides a standard way for low-level functions to return either error value...
constexpr bool has_value() const noexcept
constexpr const T & value() const &LIFETIMEBOUND
constexpr const E & error() const &noexcept LIFETIMEBOUND
The util::Unexpected class represents an unexpected value stored in util::Expected.
bool DestroyDB(const std::string &path_str)
constexpr size_t DBWRAPPER_PREALLOC_KEY_SIZE
constexpr size_t DBWRAPPER_MAX_FILE_SIZE
constexpr size_t DBWRAPPER_PREALLOC_VALUE_SIZE
These should be considered an implementation detail of the specific database.
const Obfuscation & GetObfuscation(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
auto MakeWritableByteSpan(V &&v) noexcept
@ DeserializationError
Key exists but value could not be deserialized.
@ DatabaseError
Unexpected internal DB error.
User-controlled performance and debug options.
bool force_compact
Compact database on startup.
Application-specific storage settings.
DBOptions options
Passed-through options.
bool obfuscate
If true, store data obfuscated via simple XOR.
size_t max_file_size
Maximum LevelDB SST file size.
bool wipe_data
If true, remove all existing data.
uint64_t cache_bytes
Configures various leveldb cache settings.
leveldb::Env * testing_env
If non-null, use this as the leveldb::Env instead of the default.
fs::path path
Location in the filesystem where leveldb data will be stored.
bool bloom_filter
If true, build a LevelDB bloom filter to accelerate point lookups.
bool memory_only
If true, use leveldb's memory environment.