|
@ -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())); |
|
|
} |
|
|
} |
|
|