diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2015-10-07 17:12:24 -0700 |
---|---|---|
committer | James O'Beirne <james.obeirne@gmail.com> | 2015-10-08 09:32:27 -0700 |
commit | 3499ce1e1ad87a86598d00b7124072c91ddad833 (patch) | |
tree | a85dd4fc69ca25aad28bf5bead2097d9688abece /src/leveldbwrapper.h | |
parent | 4fac576c619f450da8ca09ac8790063b3dea4364 (diff) |
Encapsulate CLevelDB iterators cleanly
Conflicts:
src/leveldb.cpp
src/leveldb.h
src/txdb.cpp
Diffstat (limited to 'src/leveldbwrapper.h')
-rw-r--r-- | src/leveldbwrapper.h | 65 |
1 files changed, 61 insertions, 4 deletions
diff --git a/src/leveldbwrapper.h b/src/leveldbwrapper.h index f5c463830c..ece4e49694 100644 --- a/src/leveldbwrapper.h +++ b/src/leveldbwrapper.h @@ -68,7 +68,65 @@ public: batch.Delete(slKey); } }; + +class CLevelDBIterator +{ +private: + leveldb::Iterator *piter; + +public: + CLevelDBIterator(leveldb::Iterator *piterIn) : piter(piterIn) {} + ~CLevelDBIterator(); + + bool Valid(); + void SeekToFirst(); + void SeekToLast(); + + template<typename K> void Seek(const K& key) { + CDataStream ssKey(SER_DISK, CLIENT_VERSION); + ssKey.reserve(ssKey.GetSerializeSize(key)); + ssKey << key; + leveldb::Slice slKey(&ssKey[0], ssKey.size()); + piter->Seek(slKey); + } + + void Next(); + void Prev(); + + template<typename K> bool GetKey(K& key) { + leveldb::Slice slKey = piter->key(); + try { + CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION); + ssKey >> key; + } catch(std::exception &e) { + return false; + } + return true; + } + + unsigned int GetKeySize() { + return piter->key().size(); + } + + template<typename V> bool GetValue(V& value) { + leveldb::Slice slValue = piter->value(); + try { + CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION); + ssValue.Xor(db.GetObfuscateKey()); + ssValue >> value; + } catch(std::exception &e) { + return false; + } + return true; + } + + unsigned int GetValueSize() { + return piter->value().size(); + } + +}; + class CLevelDBWrapper { private: @@ -191,11 +249,10 @@ public: return WriteBatch(batch, true); } - // not exactly clean encapsulation, but it's easiest for now - leveldb::Iterator* NewIterator() + CLevelDBIterator *NewIterator() + { + return new CLevelDBIterator(pdb->NewIterator(iteroptions)); { - return pdb->NewIterator(iteroptions); - } /** * Return true if the database managed by this class contains no entries. |