36 else if (_status.IsIOError())
38 else if (_status.IsCorruption())
40 else if (_status.IsNotFound())
46 void checkStatus(leveldb::Status
const& _status, boost::filesystem::path
const& _path = {})
57 BOOST_THROW_EXCEPTION(ex);
60 class LevelDBWriteBatch :
public WriteBatchFace
63 void insert(
Slice _key,
Slice _value)
override;
64 void kill(
Slice _key)
override;
66 leveldb::WriteBatch
const& writeBatch()
const {
return m_writeBatch; }
67 leveldb::WriteBatch& writeBatch() {
return m_writeBatch; }
70 leveldb::WriteBatch m_writeBatch;
73 void LevelDBWriteBatch::insert(
Slice _key,
Slice _value)
75 m_writeBatch.Put(toLDBSlice(_key), toLDBSlice(_value));
78 void LevelDBWriteBatch::kill(
Slice _key)
80 m_writeBatch.Delete(toLDBSlice(_key));
87 return leveldb::ReadOptions();
92 return leveldb::WriteOptions();
97 leveldb::Options options;
98 options.create_if_missing =
true;
99 options.max_open_files = 256;
104 leveldb::WriteOptions _writeOptions, leveldb::Options _dbOptions)
105 : m_db(nullptr), m_readOptions(
std::move(_readOptions)), m_writeOptions(
std::move(_writeOptions))
107 auto db =
static_cast<leveldb::DB*
>(
nullptr);
108 auto const status = leveldb::DB::Open(_dbOptions, _path.string(), &db);
109 checkStatus(status, _path);
119 auto const status = m_db->Get(m_readOptions, key, &value);
120 if (status.IsNotFound())
121 return std::string();
131 auto const status = m_db->Get(m_readOptions, key, &value);
132 if (status.IsNotFound())
143 auto const status = m_db->Put(m_writeOptions, key, value);
150 auto const status = m_db->Delete(m_writeOptions, key);
156 return std::unique_ptr<WriteBatchFace>(
new LevelDBWriteBatch());
163 BOOST_THROW_EXCEPTION(DatabaseError() <<
errinfo_comment(
"Cannot commit null batch"));
165 auto* batchPtr =
dynamic_cast<LevelDBWriteBatch*
>(_batch.get());
168 BOOST_THROW_EXCEPTION(
169 DatabaseError() <<
errinfo_comment(
"Invalid batch type passed to LevelDB::commit"));
171 auto const status = m_db->Write(m_writeOptions, &batchPtr->writeBatch());
177 std::unique_ptr<leveldb::Iterator> itr(m_db->NewIterator(m_readOptions));
180 BOOST_THROW_EXCEPTION(DatabaseError() <<
errinfo_comment(
"null iterator"));
182 auto keepIterating =
true;
183 for (itr->SeekToFirst(); keepIterating && itr->Valid(); itr->Next())
185 auto const dbKey = itr->key();
186 auto const dbValue = itr->value();
187 Slice const key(dbKey.data(), dbKey.size());
188 Slice const value(dbValue.data(), dbValue.size());
189 keepIterating = _f(key, value);