28 using errinfo_rocksdbStatusCode = boost::error_info<struct tag_rocksdbStatusCode, rocksdb::Status::Code>;
29 using errinfo_rocksdbStatusSubCode = boost::error_info<struct tag_rocksdbStatusSubCode, rocksdb::Status::SubCode>;
33 switch (_status.code())
35 case rocksdb::Status::kOk:
37 case rocksdb::Status::kNotFound:
39 case rocksdb::Status::kCorruption:
41 case rocksdb::Status::kNotSupported:
43 case rocksdb::Status::kInvalidArgument:
45 case rocksdb::Status::kIOError:
52 void checkStatus(rocksdb::Status
const& _status, boost::filesystem::path
const& _path = {})
59 << errinfo_rocksdbStatusCode(_status.code())
60 << errinfo_rocksdbStatusSubCode(_status.subcode())
65 BOOST_THROW_EXCEPTION(ex);
68 class RocksDBWriteBatch :
public WriteBatchFace
71 void insert(
Slice _key,
Slice _value)
override;
72 void kill(
Slice _key)
override;
74 rocksdb::WriteBatch
const& writeBatch()
const {
return m_writeBatch; }
75 rocksdb::WriteBatch& writeBatch() {
return m_writeBatch; }
78 rocksdb::WriteBatch m_writeBatch;
81 void RocksDBWriteBatch::insert(
Slice _key,
Slice _value)
83 auto const status = m_writeBatch.Put(
90 void RocksDBWriteBatch::kill(
Slice _key)
92 auto const status = m_writeBatch.Delete(
rocksdb::Slice(_key.data(), _key.size()));
100 return rocksdb::ReadOptions();
105 return rocksdb::WriteOptions();
110 rocksdb::Options options;
111 options.create_if_missing =
true;
112 options.max_open_files = 256;
117 rocksdb::WriteOptions _writeOptions, rocksdb::Options _dbOptions)
118 : m_db(nullptr), m_readOptions(
std::move(_readOptions)), m_writeOptions(
std::move(_writeOptions))
120 auto db =
static_cast<rocksdb::DB*
>(
nullptr);
121 auto const status = rocksdb::DB::Open(_dbOptions, _path.string(), &db);
122 checkStatus(status, _path);
132 auto const status = m_db->Get(m_readOptions, key, &value);
133 if (status.IsNotFound())
134 return std::string();
144 if (!m_db->KeyMayExist(m_readOptions, key, &value,
nullptr))
147 auto const status = m_db->Get(m_readOptions, key, &value);
148 if (status.IsNotFound())
159 auto const status = m_db->Put(m_writeOptions, key, value);
166 auto const status = m_db->Delete(m_writeOptions, key);
172 return std::unique_ptr<WriteBatchFace>(
new RocksDBWriteBatch());
178 BOOST_THROW_EXCEPTION(DatabaseError() <<
errinfo_comment(
"Cannot commit null batch"));
180 auto* batchPtr =
dynamic_cast<RocksDBWriteBatch*
>(_batch.get());
182 BOOST_THROW_EXCEPTION(DatabaseError() <<
errinfo_comment(
"Invalid batch type passed to rocksdb::commit"));
184 auto const status = m_db->Write(m_writeOptions, &batchPtr->writeBatch());
190 std::unique_ptr<rocksdb::Iterator> itr(m_db->NewIterator(m_readOptions));
192 BOOST_THROW_EXCEPTION(DatabaseError() <<
errinfo_comment(
"null iterator"));
194 auto keepIterating =
true;
195 for (itr->SeekToFirst(); keepIterating && itr->Valid(); itr->Next())
197 auto const dbKey = itr->key();
198 auto const dbValue = itr->value();
199 Slice const key(dbKey.data(), dbKey.size());
200 Slice const value(dbValue.data(), dbValue.size());
201 keepIterating = f(key, value);