From 581e4b2bd5e5042a0951785b45a9831a6155cbc8 Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky Date: Fri, 10 Jul 2015 17:03:04 +0200 Subject: [PATCH] minor changes --- libwhisper/WhisperDB.cpp | 67 ++++++++++++++-------------------------- libwhisper/WhisperDB.h | 13 +++++--- 2 files changed, 32 insertions(+), 48 deletions(-) diff --git a/libwhisper/WhisperDB.cpp b/libwhisper/WhisperDB.cpp index dcb3cf1e0..91c5f74f4 100644 --- a/libwhisper/WhisperDB.cpp +++ b/libwhisper/WhisperDB.cpp @@ -19,16 +19,9 @@ * @date July 2015 */ -#include +#include "WhisperDB.h" #include - -#include -#include -#include -#include -#include #include -#include "WhisperDB.h" using namespace std; using namespace dev; @@ -41,50 +34,36 @@ WhisperDB::WhisperDB() ldb::Options op; op.create_if_missing = true; op.max_open_files = 256; - ldb::DB::Open(op, path + "/whisper", &m_db); -} - -WhisperDB::~WhisperDB() -{ - delete m_db; -} - -bool WhisperDB::insert(dev::h256 const& _key, string const& _value) -{ - string s = _key.hex(); - string cropped = s.substr(s.size() - 8); - leveldb::Status status = m_db->Put(m_writeOptions, s, _value); - if (status.ok()) - cdebug << "Whisper DB put:" << cropped << _value; - else - cdebug << "Whisper DB put failed:" << status.ToString() << "key:" << cropped; - - return status.ok(); + ldb::DB* p = nullptr; + leveldb::Status status = ldb::DB::Open(op, path + "/whisper", &p); + m_db.reset(p); + if (!status.ok()) + BOOST_THROW_EXCEPTION(FailedToOpenLevelDB(status.ToString())); } string WhisperDB::lookup(dev::h256 const& _key) const { string ret; - string s = _key.hex(); - string cropped = s.substr(s.size() - 8); - leveldb::Status status = m_db->Get(m_readOptions, s, &ret); - if (status.ok()) - cdebug << "Whisper DB get:" << cropped << ret; - else - cdebug << "Whisper DB get failed:" << status.ToString() << "key:" << cropped; + leveldb::Slice slice((char const*)_key.data(), _key.size); + leveldb::Status status = m_db->Get(m_readOptions, slice, &ret); + if (!status.ok() && !status.IsNotFound()) + BOOST_THROW_EXCEPTION(FailedLookupInLevelDB(status.ToString())); return ret; } -bool WhisperDB::kill(dev::h256 const& _key) +void WhisperDB::insert(dev::h256 const& _key, string const& _value) +{ + leveldb::Slice slice((char const*)_key.data(), _key.size); + leveldb::Status status = m_db->Put(m_writeOptions, slice, _value); + if (!status.ok()) + BOOST_THROW_EXCEPTION(FailedInsertInLevelDB(status.ToString())); +} + +void WhisperDB::kill(dev::h256 const& _key) { - string s = _key.hex(); - string cropped = s.substr(s.size() - 8); - leveldb::Status status = m_db->Delete(m_writeOptions, s); - if (status.ok()) - cdebug << "Whisper DB erase:" << cropped; - else - cdebug << "Whisper DB erase failed:" << status.ToString() << "key:" << cropped; - - return status.ok(); + leveldb::Slice slice((char const*)_key.data(), _key.size); + leveldb::Status status = m_db->Delete(m_writeOptions, slice); + if (!status.ok()) + BOOST_THROW_EXCEPTION(FailedDeleteInLevelDB(status.ToString())); } diff --git a/libwhisper/WhisperDB.h b/libwhisper/WhisperDB.h index b2aa12df3..c20f52a75 100644 --- a/libwhisper/WhisperDB.h +++ b/libwhisper/WhisperDB.h @@ -30,20 +30,25 @@ namespace dev namespace shh { +struct FailedToOpenLevelDB: virtual Exception { FailedToOpenLevelDB(std::string _message = std::string()): Exception(_message) {} }; +struct FailedInsertInLevelDB: virtual Exception { FailedInsertInLevelDB(std::string _message = std::string()): Exception(_message) {} }; +struct FailedLookupInLevelDB: virtual Exception { FailedLookupInLevelDB(std::string _message = std::string()): Exception(_message) {} }; +struct FailedDeleteInLevelDB: virtual Exception { FailedDeleteInLevelDB(std::string _message = std::string()): Exception(_message) {} }; + class WhisperDB { public: WhisperDB(); - ~WhisperDB(); + ~WhisperDB() {} - bool insert(dev::h256 const& _key, std::string const& _value); - bool kill(dev::h256 const& _key); std::string lookup(dev::h256 const& _key) const; + void insert(dev::h256 const& _key, std::string const& _value); + void kill(dev::h256 const& _key); private: ldb::ReadOptions m_readOptions; ldb::WriteOptions m_writeOptions; - ldb::DB* m_db = nullptr; + std::unique_ptr m_db; }; }