23 #include <boost/filesystem.hpp> 30 namespace fs = boost::filesystem;
32 WhisperDB::WhisperDB(
string const& _type)
34 m_readOptions.verify_checksums =
true;
36 fs::create_directories(path);
40 op.create_if_missing =
true;
41 op.max_open_files = 256;
43 leveldb::Status status = leveldb::DB::Open(op, path, &p);
49 string WhisperDB::lookup(
dev::h256 const& _key)
const 52 leveldb::Slice slice((
char const*)_key.
data(), _key.
size);
53 leveldb::Status status = m_db->Get(m_readOptions, slice, &ret);
54 if (!status.ok() && !status.IsNotFound())
60 void WhisperDB::insert(
dev::h256 const& _key,
string const& _value)
62 leveldb::Slice slice((
char const*)_key.
data(), _key.
size);
63 leveldb::Status status = m_db->Put(m_writeOptions, slice, _value);
70 leveldb::Slice k((
char const*)_key.
data(), _key.
size);
71 leveldb::Slice v((
char const*)_value.data(), _value.size());
72 leveldb::Status status = m_db->Put(m_writeOptions, k, v);
79 leveldb::Slice slice((
char const*)_key.
data(), _key.
size);
80 leveldb::Status status = m_db->Delete(m_writeOptions, slice);
85 void WhisperMessagesDB::loadAllMessages(std::map<h256, Envelope>& o_dst)
87 leveldb::ReadOptions op;
88 op.fill_cache =
false;
89 op.verify_checksums =
true;
90 vector<string> wasted;
91 unique_ptr<leveldb::Iterator> it(m_db->NewIterator(op));
94 for (it->SeekToFirst(); it->Valid(); it->Next())
96 leveldb::Slice
const k = it->key();
97 leveldb::Slice
const v = it->value();
108 h1 =
h256((
byte const*)k.data(), h256::ConstructFromPointer);
111 cwarn <<
"Corrupted data in Level DB:" << h1.
hex() <<
"versus" << h2.
hex();
112 else if (e.expiry() > now)
120 cwarn <<
"RLPException in WhisperDB::loadAll():" << ex.
what();
124 cwarn <<
"Exception in WhisperDB::loadAll():" << ex.
what();
128 wasted.push_back(k.ToString());
131 cdebug <<
"WhisperDB::loadAll(): loaded " << o_dst.size() <<
", deleted " << wasted.size() <<
"messages";
133 for (
auto const& k: wasted)
135 leveldb::Status status = m_db->Delete(m_writeOptions, k);
137 cwarn <<
"Failed to delete an entry from Level DB:" << k;
141 void WhisperMessagesDB::saveSingleMessage(
h256 const& _key,
Envelope const& _e)
153 cwarn << boost::diagnostic_information(ex);
157 cwarn << boost::diagnostic_information(ex);
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
void streamRLP(RLPStream &_s, IncludeNonce _withNonce=WithNonce) const
uint64_t utcTime()
Get the current time in seconds since the epoch in UTC.
const char * what() const noexceptoverride
std::hash for asio::adress
std::string getDataDir(std::string _prefix="ethereum")
Base class for all exceptions.
std::vector< byte > bytes
uint8_t const size_t const size
Base class for all RLP exceptions.
void swapOut(bytes &_dest)
Swap the contents of the output stream out for some other byte array.
Class for writing to an RLP bytestream.
Class for interpreting Recursive Linear-Prefix Data.
#define DEV_IGNORE_EXCEPTIONS(X)