5 #ifndef BITCOIN_DBWRAPPER_H
6 #define BITCOIN_DBWRAPPER_H
15 #include <boost/filesystem/path.hpp>
17 #include <leveldb/db.h>
18 #include <leveldb/write_batch.h>
20 static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
21 static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
65 template <
typename K,
typename V>
66 void Write(
const K& key,
const V& value)
77 batch.Put(slKey, slValue);
114 template<
typename K>
void Seek(
const K& key) {
116 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
118 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
124 template<
typename K>
bool GetKey(K& key) {
125 leveldb::Slice slKey =
piter->key();
129 }
catch (
const std::exception&) {
136 return piter->key().size();
140 leveldb::Slice slValue =
piter->value();
142 CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(),
SER_DISK, CLIENT_VERSION);
145 }
catch (
const std::exception&) {
152 return piter->value().size();
202 CDBWrapper(
const boost::filesystem::path& path,
size_t nCacheSize,
bool fMemory =
false,
bool fWipe =
false,
bool obfuscate =
false);
205 template <
typename K,
typename V>
206 bool Read(
const K& key, V& value)
const
209 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
211 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
213 std::string strValue;
216 if (status.IsNotFound())
218 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
222 CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
SER_DISK, CLIENT_VERSION);
225 }
catch (
const std::exception&) {
231 template <
typename K,
typename V>
232 bool Write(
const K& key,
const V& value,
bool fSync =
false)
235 batch.
Write(key, value);
239 template <
typename K>
243 ssKey.
reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
245 leveldb::Slice slKey(ssKey.
data(), ssKey.
size());
247 std::string strValue;
250 if (status.IsNotFound())
252 LogPrintf(
"LevelDB read failure: %s\n", status.ToString());
258 template <
typename K>
259 bool Erase(
const K& key,
bool fSync =
false)
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
CDBBatch(const CDBWrapper &_parent)
leveldb::WriteBatch batch
const CDBWrapper & parent
unsigned int GetKeySize()
unsigned int GetValueSize()
leveldb::Iterator * piter
const CDBWrapper & parent
CDBIterator(const CDBWrapper &_parent, leveldb::Iterator *_piter)
CDBIterator * NewIterator()
CDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
leveldb::Env * penv
custom environment this database is using (may be NULL in case of default environment)
bool WriteBatch(CDBBatch &batch, bool fSync=false)
bool Read(const K &key, V &value) const
bool Erase(const K &key, bool fSync=false)
bool Write(const K &key, const V &value, bool fSync=false)
bool Exists(const K &key) const
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
leveldb::Options options
database options used
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
leveldb::WriteOptions writeoptions
options used when writing to the database
leveldb::WriteOptions syncoptions
options used when sync writing to the database
leveldb::DB * pdb
the database itself
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
bool IsEmpty()
Return true if the database managed by this class contains no entries.
leveldb::ReadOptions readoptions
options used when reading from the database
Double ended buffer combining vector and stream-like interfaces.
void reserve(size_type n)
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
dbwrapper_error(const std::string &msg)
These should be considered an implementation detail of the specific database.
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.