5 #ifndef BITCOIN_DBWRAPPER_H 6 #define BITCOIN_DBWRAPPER_H 18 #include <leveldb/db.h> 19 #include <leveldb/iterator.h> 20 #include <leveldb/options.h> 21 #include <leveldb/slice.h> 22 #include <leveldb/status.h> 23 #include <leveldb/write_batch.h> 84 template <
typename K,
typename V>
85 void Write(
const K& key,
const V& value)
96 batch.Put(slKey, slValue);
104 size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
109 template <
typename K>
149 template<
typename K>
void Seek(
const K& key) {
153 leveldb::Slice slKey((
const char*)ssKey.
data(), ssKey.
size());
160 leveldb::Slice slKey =
piter->key();
164 }
catch (
const std::exception&) {
171 leveldb::Slice slValue =
piter->value();
176 }
catch (
const std::exception&) {
231 CDBWrapper(
const fs::path& path,
size_t nCacheSize,
bool fMemory =
false,
bool fWipe =
false,
bool obfuscate =
false);
237 template <
typename K,
typename V>
238 bool Read(
const K& key, V& value)
const 243 leveldb::Slice slKey((
const char*)ssKey.
data(), ssKey.
size());
245 std::string strValue;
248 if (status.IsNotFound())
250 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
257 }
catch (
const std::exception&) {
263 template <
typename K,
typename V>
264 bool Write(
const K& key,
const V& value,
bool fSync =
false)
267 batch.
Write(key, value);
271 template <
typename K>
277 leveldb::Slice slKey((
const char*)ssKey.
data(), ssKey.
size());
279 std::string strValue;
282 if (status.IsNotFound())
284 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
290 template <
typename K>
291 bool Erase(
const K& key,
bool fSync =
false)
321 leveldb::Slice slKey1((
const char*)ssKey1.data(), ssKey1.size());
322 leveldb::Slice slKey2((
const char*)ssKey2.
data(), ssKey2.
size());
324 leveldb::Range range(slKey1, slKey2);
325 pdb->GetApproximateSizes(&range, 1, &size);
330 #endif // BITCOIN_DBWRAPPER_H bool Exists(const K &key) const
dbwrapper_error(const std::string &msg)
These should be considered an implementation detail of the specific database.
Batch of changes queued to be written to a CDBWrapper.
CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Double ended buffer combining vector and stream-like interfaces.
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
leveldb::WriteBatch batch
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key...
CDBIterator * NewIterator()
leveldb::ReadOptions readoptions
options used when reading from the database
size_t DynamicMemoryUsage() const
const CDBWrapper & parent
leveldb::WriteOptions syncoptions
options used when sync writing to the database
const CDBWrapper & parent
CDBWrapper & operator=(const CDBWrapper &)=delete
bool Erase(const K &key, bool fSync=false)
leveldb::DB * pdb
the database itself
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
void Write(const K &key, const V &value)
size_t SizeEstimate() const
leveldb::WriteOptions writeoptions
options used when writing to the database
bool IsEmpty()
Return true if the database managed by this class contains no entries.
bool Read(const K &key, V &value) const
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
leveldb::Iterator * piter
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
bool Write(const K &key, const V &value, bool fSync=false)
std::string m_name
the name of this database
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment) ...
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter)
void reserve(size_type n)
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
Span< const std::byte > MakeByteSpan(V &&v) noexcept
CDBBatch(const CDBWrapper &_parent)
bool WriteBatch(CDBBatch &batch, bool fSync=false)
leveldb::Options options
database options used
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
size_t EstimateSize(const K &key_begin, const K &key_end) const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.