Browse Source

minor changes

cl-refactor
Vlad Gluhovsky 10 years ago
parent
commit
581e4b2bd5
  1. 65
      libwhisper/WhisperDB.cpp
  2. 13
      libwhisper/WhisperDB.h

65
libwhisper/WhisperDB.cpp

@ -19,16 +19,9 @@
* @date July 2015 * @date July 2015
*/ */
#include <string> #include "WhisperDB.h"
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <libdevcore/Common.h>
#include <libdevcore/CommonData.h>
#include <libdevcore/Exceptions.h>
#include <libdevcore/Log.h>
#include <libdevcore/SHA3.h>
#include <libdevcore/FileSystem.h> #include <libdevcore/FileSystem.h>
#include "WhisperDB.h"
using namespace std; using namespace std;
using namespace dev; using namespace dev;
@ -41,50 +34,36 @@ WhisperDB::WhisperDB()
ldb::Options op; ldb::Options op;
op.create_if_missing = true; op.create_if_missing = true;
op.max_open_files = 256; op.max_open_files = 256;
ldb::DB::Open(op, path + "/whisper", &m_db); ldb::DB* p = nullptr;
} leveldb::Status status = ldb::DB::Open(op, path + "/whisper", &p);
m_db.reset(p);
WhisperDB::~WhisperDB() if (!status.ok())
{ BOOST_THROW_EXCEPTION(FailedToOpenLevelDB(status.ToString()));
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();
} }
string WhisperDB::lookup(dev::h256 const& _key) const string WhisperDB::lookup(dev::h256 const& _key) const
{ {
string ret; string ret;
string s = _key.hex(); leveldb::Slice slice((char const*)_key.data(), _key.size);
string cropped = s.substr(s.size() - 8); leveldb::Status status = m_db->Get(m_readOptions, slice, &ret);
leveldb::Status status = m_db->Get(m_readOptions, s, &ret); if (!status.ok() && !status.IsNotFound())
if (status.ok()) BOOST_THROW_EXCEPTION(FailedLookupInLevelDB(status.ToString()));
cdebug << "Whisper DB get:" << cropped << ret;
else
cdebug << "Whisper DB get failed:" << status.ToString() << "key:" << cropped;
return ret; return ret;
} }
bool WhisperDB::kill(dev::h256 const& _key) void WhisperDB::insert(dev::h256 const& _key, string const& _value)
{ {
string s = _key.hex(); leveldb::Slice slice((char const*)_key.data(), _key.size);
string cropped = s.substr(s.size() - 8); leveldb::Status status = m_db->Put(m_writeOptions, slice, _value);
leveldb::Status status = m_db->Delete(m_writeOptions, s); if (!status.ok())
if (status.ok()) BOOST_THROW_EXCEPTION(FailedInsertInLevelDB(status.ToString()));
cdebug << "Whisper DB erase:" << cropped; }
else
cdebug << "Whisper DB erase failed:" << status.ToString() << "key:" << cropped;
return status.ok(); void WhisperDB::kill(dev::h256 const& _key)
{
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()));
} }

13
libwhisper/WhisperDB.h

@ -30,20 +30,25 @@ namespace dev
namespace shh 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 class WhisperDB
{ {
public: public:
WhisperDB(); 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; 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: private:
ldb::ReadOptions m_readOptions; ldb::ReadOptions m_readOptions;
ldb::WriteOptions m_writeOptions; ldb::WriteOptions m_writeOptions;
ldb::DB* m_db = nullptr; std::unique_ptr<ldb::DB> m_db;
}; };
} }

Loading…
Cancel
Save