8 #include <boost/filesystem.hpp> 20 : prevent_destruction_(prevent_destruction) {
21 ro_.fill_cache =
false;
33 std::ignore =
db_.release();
38 const boost::filesystem::path &path,
39 rocksdb::Options options,
40 bool prevent_destruction) {
45 rocksdb::DB *db =
nullptr;
48 auto absolute_path = fs::absolute(path, fs::current_path());
50 boost::system::error_code ec;
51 if (not fs::create_directory(absolute_path.native(), ec) and ec.value()) {
52 log->error(
"Can't create directory {} for database: {}",
53 absolute_path.native(),
57 if (not fs::is_directory(absolute_path.native())) {
58 log->error(
"Can't open {} for database: is not a directory",
59 absolute_path.native());
63 auto status = rocksdb::DB::Open(options, path.native(), &db);
65 std::unique_ptr<RocksDB> l{
new RocksDB(prevent_destruction)};
66 l->db_ = std::unique_ptr<rocksdb::DB>(db);
67 l->logger_ = std::move(log);
72 "Can't open database in {}: {}",
73 absolute_path.native(),
80 return std::make_unique<Batch>(*this);
84 size_t usage_bytes = 0;
87 bool result =
db_->GetProperty(
"rocksdb.cur-size-all-mem-tables", &usage);
90 usage_bytes = std::stoul(usage);
92 logger_->error(
"Unable to parse memory usage value");
95 logger_->error(
"Unable to retrieve memory usage value");
102 auto it = std::unique_ptr<rocksdb::Iterator>(
db_->NewIterator(
ro_));
103 return std::make_unique<RocksDBCursor>(std::move(it));
113 if (status.IsNotFound()) {
121 auto it = std::unique_ptr<rocksdb::Iterator>(
db_->NewIterator(
ro_));
132 reinterpret_cast<uint8_t *>(value.data()),
133 reinterpret_cast<uint8_t *>(value.data()) + value.size());
143 return std::make_optional(
Buffer(
144 reinterpret_cast<uint8_t *>(value.data()),
145 reinterpret_cast<uint8_t *>(value.data()) + value.size()));
148 if (status.IsNotFound()) {
159 return outcome::success();
166 Buffer copy(std::move(value));
167 return put(key, copy);
173 return outcome::success();
181 std::unique_ptr<rocksdb::Iterator> begin(
db_->NewIterator(
ro_));
182 first.empty() ? begin->SeekToFirst() : begin->Seek(
make_slice(first));
183 auto bk = begin->key();
184 std::unique_ptr<rocksdb::Iterator> end(
db_->NewIterator(
ro_));
185 last.empty() ? end->SeekToLast() : end->Seek(
make_slice(last));
186 auto ek = end->key();
187 rocksdb::CompactRangeOptions options;
188 db_->CompactRange(options, &bk, &ek);
void compact(const Buffer &first, const Buffer &last)
Class represents arbitrary (including empty) byte buffer.
rocksdb::WriteOptions wo_
outcome::result< void > remove(const BufferView &key) override
outcome::result< kagome::storage::Buffer > load(const Key &key) const override
Load value by key.
DatabaseError status_as_error(const rocksdb::Status &s)
outcome::result< bool > contains(const Key &key) const override
Checks if given key-value binding exists in the storage.
std::unique_ptr< Cursor > cursor() override
Returns new key-value iterator.
outcome::result< std::optional< Buffer > > tryLoad(const Key &key) const override
Load value by key.
outcome::result< void > put(const BufferView &key, const Buffer &value) override
size_t size() const override
rocksdb::Slice make_slice(const common::BufferView &buf)
bool createDirectoryRecursive(const path &path)
std::unique_ptr< rocksdb::DB > db_
bool prevent_destruction_
common::SLBuffer< std::numeric_limits< size_t >::max()> Buffer
std::unique_ptr< BufferBatch > batch() override
Creates new Write Batch - an object, which can be used to efficiently write bulk data.
bool empty() const override
Returns true if the storage is empty.
RocksDB(bool prevent_destruction)
static outcome::result< std::unique_ptr< RocksDB > > create(const boost::filesystem::path &path, rocksdb::Options options=rocksdb::Options(), bool prevent_destruction=false)
Factory method to create an instance of RocksDB class.
Logger createLogger(const std::string &tag)