Browse Source

minor changes

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

67
libwhisper/WhisperDB.cpp

@ -19,16 +19,9 @@
* @date July 2015
*/
#include <string>
#include "WhisperDB.h"
#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 "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()));
}

13
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<ldb::DB> m_db;
};
}

Loading…
Cancel
Save