|
|
@ -2,8 +2,8 @@ |
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef BITCOIN_LEVELDBWRAPPER_H |
|
|
|
#ifndef BITCOIN_DBWRAPPER_H |
|
|
|
#define BITCOIN_LEVELDBWRAPPER_H |
|
|
|
#define BITCOIN_DBWRAPPER_H |
|
|
|
|
|
|
|
|
|
|
|
#include "clientversion.h" |
|
|
|
#include "clientversion.h" |
|
|
|
#include "serialize.h" |
|
|
|
#include "serialize.h" |
|
|
@ -17,18 +17,18 @@ |
|
|
|
#include <leveldb/db.h> |
|
|
|
#include <leveldb/db.h> |
|
|
|
#include <leveldb/write_batch.h> |
|
|
|
#include <leveldb/write_batch.h> |
|
|
|
|
|
|
|
|
|
|
|
class leveldb_error : public std::runtime_error |
|
|
|
class dbwrapper_error : public std::runtime_error |
|
|
|
{ |
|
|
|
{ |
|
|
|
public: |
|
|
|
public: |
|
|
|
leveldb_error(const std::string& msg) : std::runtime_error(msg) {} |
|
|
|
dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
void HandleError(const leveldb::Status& status) throw(leveldb_error); |
|
|
|
void HandleError(const leveldb::Status& status) throw(dbwrapper_error); |
|
|
|
|
|
|
|
|
|
|
|
/** Batch of changes queued to be written to a CLevelDBWrapper */ |
|
|
|
/** Batch of changes queued to be written to a CDBWrapper */ |
|
|
|
class CLevelDBBatch |
|
|
|
class CDBBatch |
|
|
|
{ |
|
|
|
{ |
|
|
|
friend class CLevelDBWrapper; |
|
|
|
friend class CDBWrapper; |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
leveldb::WriteBatch batch; |
|
|
|
leveldb::WriteBatch batch; |
|
|
@ -38,7 +38,7 @@ public: |
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param[in] obfuscate_key If passed, XOR data with this key. |
|
|
|
* @param[in] obfuscate_key If passed, XOR data with this key. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
CLevelDBBatch(const std::vector<unsigned char> *obfuscate_key) : obfuscate_key(obfuscate_key) { }; |
|
|
|
CDBBatch(const std::vector<unsigned char> *obfuscate_key) : obfuscate_key(obfuscate_key) { }; |
|
|
|
|
|
|
|
|
|
|
|
template <typename K, typename V> |
|
|
|
template <typename K, typename V> |
|
|
|
void Write(const K& key, const V& value) |
|
|
|
void Write(const K& key, const V& value) |
|
|
@ -69,7 +69,7 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class CLevelDBIterator |
|
|
|
class CDBIterator |
|
|
|
{ |
|
|
|
{ |
|
|
|
private: |
|
|
|
private: |
|
|
|
leveldb::Iterator *piter; |
|
|
|
leveldb::Iterator *piter; |
|
|
@ -81,14 +81,13 @@ public: |
|
|
|
* @param[in] piterIn The original leveldb iterator. |
|
|
|
* @param[in] piterIn The original leveldb iterator. |
|
|
|
* @param[in] obfuscate_key If passed, XOR data with this key. |
|
|
|
* @param[in] obfuscate_key If passed, XOR data with this key. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
CLevelDBIterator(leveldb::Iterator *piterIn, const std::vector<unsigned char>* obfuscate_key) : |
|
|
|
CDBIterator(leveldb::Iterator *piterIn, const std::vector<unsigned char>* obfuscate_key) : |
|
|
|
piter(piterIn), obfuscate_key(obfuscate_key) { }; |
|
|
|
piter(piterIn), obfuscate_key(obfuscate_key) { }; |
|
|
|
~CLevelDBIterator(); |
|
|
|
~CDBIterator(); |
|
|
|
|
|
|
|
|
|
|
|
bool Valid(); |
|
|
|
bool Valid(); |
|
|
|
|
|
|
|
|
|
|
|
void SeekToFirst(); |
|
|
|
void SeekToFirst(); |
|
|
|
void SeekToLast(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename K> void Seek(const K& key) { |
|
|
|
template<typename K> void Seek(const K& key) { |
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
|
|
@ -99,7 +98,6 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Next(); |
|
|
|
void Next(); |
|
|
|
void Prev(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename K> bool GetKey(K& key) { |
|
|
|
template<typename K> bool GetKey(K& key) { |
|
|
|
leveldb::Slice slKey = piter->key(); |
|
|
|
leveldb::Slice slKey = piter->key(); |
|
|
@ -134,7 +132,7 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class CLevelDBWrapper |
|
|
|
class CDBWrapper |
|
|
|
{ |
|
|
|
{ |
|
|
|
private: |
|
|
|
private: |
|
|
|
//! custom environment this database is using (may be NULL in case of default environment)
|
|
|
|
//! custom environment this database is using (may be NULL in case of default environment)
|
|
|
@ -178,11 +176,11 @@ public: |
|
|
|
* @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR |
|
|
|
* @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR |
|
|
|
* with a zero'd byte array. |
|
|
|
* with a zero'd byte array. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false); |
|
|
|
CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false); |
|
|
|
~CLevelDBWrapper(); |
|
|
|
~CDBWrapper(); |
|
|
|
|
|
|
|
|
|
|
|
template <typename K, typename V> |
|
|
|
template <typename K, typename V> |
|
|
|
bool Read(const K& key, V& value) const throw(leveldb_error) |
|
|
|
bool Read(const K& key, V& value) const throw(dbwrapper_error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key)); |
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key)); |
|
|
@ -208,15 +206,15 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template <typename K, typename V> |
|
|
|
template <typename K, typename V> |
|
|
|
bool Write(const K& key, const V& value, bool fSync = false) throw(leveldb_error) |
|
|
|
bool Write(const K& key, const V& value, bool fSync = false) throw(dbwrapper_error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CLevelDBBatch batch(&obfuscate_key); |
|
|
|
CDBBatch batch(&obfuscate_key); |
|
|
|
batch.Write(key, value); |
|
|
|
batch.Write(key, value); |
|
|
|
return WriteBatch(batch, fSync); |
|
|
|
return WriteBatch(batch, fSync); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template <typename K> |
|
|
|
template <typename K> |
|
|
|
bool Exists(const K& key) const throw(leveldb_error) |
|
|
|
bool Exists(const K& key) const throw(dbwrapper_error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key)); |
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key)); |
|
|
@ -235,14 +233,14 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template <typename K> |
|
|
|
template <typename K> |
|
|
|
bool Erase(const K& key, bool fSync = false) throw(leveldb_error) |
|
|
|
bool Erase(const K& key, bool fSync = false) throw(dbwrapper_error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CLevelDBBatch batch(&obfuscate_key); |
|
|
|
CDBBatch batch(&obfuscate_key); |
|
|
|
batch.Erase(key); |
|
|
|
batch.Erase(key); |
|
|
|
return WriteBatch(batch, fSync); |
|
|
|
return WriteBatch(batch, fSync); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool WriteBatch(CLevelDBBatch& batch, bool fSync = false) throw(leveldb_error); |
|
|
|
bool WriteBatch(CDBBatch& batch, bool fSync = false) throw(dbwrapper_error); |
|
|
|
|
|
|
|
|
|
|
|
// not available for LevelDB; provide for compatibility with BDB
|
|
|
|
// not available for LevelDB; provide for compatibility with BDB
|
|
|
|
bool Flush() |
|
|
|
bool Flush() |
|
|
@ -250,15 +248,15 @@ public: |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool Sync() throw(leveldb_error) |
|
|
|
bool Sync() throw(dbwrapper_error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CLevelDBBatch batch(&obfuscate_key); |
|
|
|
CDBBatch batch(&obfuscate_key); |
|
|
|
return WriteBatch(batch, true); |
|
|
|
return WriteBatch(batch, true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CLevelDBIterator *NewIterator() |
|
|
|
CDBIterator *NewIterator() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return new CLevelDBIterator(pdb->NewIterator(iteroptions), &obfuscate_key); |
|
|
|
return new CDBIterator(pdb->NewIterator(iteroptions), &obfuscate_key); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -278,5 +276,5 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#endif // BITCOIN_LEVELDBWRAPPER_H
|
|
|
|
#endif // BITCOIN_DBWRAPPER_H
|
|
|
|
|
|
|
|
|