5
5
*/
6
6
7
7
#include < morum/db.hpp>
8
-
8
+ #include < qtils/assert.hpp>
9
+ #include < qtils/bytestr.hpp>
10
+ #include < qtils/macro/unwrap.hpp>
9
11
#include < rocksdb/db.h>
10
12
#include < rocksdb/options.h>
11
13
#include < rocksdb/slice.h>
12
14
#include < rocksdb/status.h>
13
15
#include < rocksdb/write_batch.h>
14
- #include < qtils/assert.hpp>
15
- #include < qtils/byte_utils.hpp>
16
- #include < qtils/macro/unwrap.hpp>
17
16
18
17
namespace morum {
19
18
20
19
template <typename T>
21
- std::expected<T, StorageError> wrap_status (
22
- T &&t, const rocksdb::Status &status) {
20
+ std::expected<T, StorageError> wrap_status (T &&t,
21
+ const rocksdb::Status &status) {
23
22
if (!status.ok ()) {
24
23
return std::unexpected (StorageError{status.ToString ()});
25
24
}
@@ -83,7 +82,7 @@ namespace morum {
83
82
// a vector of column family handles
84
83
QTILS_ASSERT_EQ (family_idx++, static_cast <int >(family));
85
84
desc.emplace_back (std::string{to_string_nocheck (family)},
86
- rocksdb::ColumnFamilyOptions{});
85
+ rocksdb::ColumnFamilyOptions{});
87
86
}
88
87
89
88
QTILS_UNWRAP_void (wrap_status (
@@ -102,17 +101,17 @@ namespace morum {
102
101
class RocksDbBatch final : public RocksDb::Batch,
103
102
public RocksDbColumnFamily::Batch {
104
103
public:
105
- RocksDbBatch (
106
- std::shared_ptr<RocksDb> db, rocksdb::ColumnFamilyHandle *default_cf)
104
+ RocksDbBatch (std::shared_ptr<RocksDb> db,
105
+ rocksdb::ColumnFamilyHandle *default_cf)
107
106
: db{db}, default_cf{default_cf} {
108
107
QTILS_ASSERT (db != nullptr );
109
108
QTILS_ASSERT (default_cf != nullptr );
110
109
}
111
110
112
111
virtual ~RocksDbBatch () = default ;
113
112
114
- std::expected<void , StorageError> write (
115
- qtils::ByteSpan key, qtils::ByteSpan value) override {
113
+ std::expected<void , StorageError> write (qtils::ByteSpan key,
114
+ qtils::ByteSpan value) override {
116
115
return wrap_status (
117
116
batch.Put (default_cf, qtils::byte2str (key), qtils::byte2str (value)));
118
117
}
@@ -122,26 +121,26 @@ namespace morum {
122
121
}
123
122
124
123
std::expected<void , StorageError> write (ColumnFamilyId cf,
125
- qtils::ByteSpan key,
126
- qtils::ByteSpan value) override {
124
+ qtils::ByteSpan key,
125
+ qtils::ByteSpan value) override {
127
126
return wrap_status (batch.Put (db->handles [static_cast <size_t >(cf)],
128
- qtils::byte2str (key),
129
- qtils::byte2str (value)));
127
+ qtils::byte2str (key),
128
+ qtils::byte2str (value)));
130
129
}
131
130
132
- std::expected<void , StorageError> remove (
133
- ColumnFamilyId cf, qtils::ByteSpan key) override {
134
- return wrap_status (batch.Delete (
135
- db-> handles [ static_cast < size_t >(cf)], qtils::byte2str (key)));
131
+ std::expected<void , StorageError> remove (ColumnFamilyId cf,
132
+ qtils::ByteSpan key) override {
133
+ return wrap_status (batch.Delete (db-> handles [ static_cast < size_t >(cf)],
134
+ qtils::byte2str (key)));
136
135
}
137
136
138
137
std::shared_ptr<RocksDb> db;
139
138
rocksdb::ColumnFamilyHandle *default_cf;
140
139
rocksdb::WriteBatch batch;
141
140
};
142
141
143
- RocksDb::RocksDb (
144
- rocksdb::DB *db, std::vector<rocksdb::ColumnFamilyHandle *> &&handles)
142
+ RocksDb::RocksDb (rocksdb::DB *db,
143
+ std::vector<rocksdb::ColumnFamilyHandle *> &&handles)
145
144
: db{db}, handles{std::move (handles)} {
146
145
QTILS_ASSERT (db != nullptr );
147
146
}
@@ -162,7 +161,8 @@ namespace morum {
162
161
}
163
162
164
163
std::unique_ptr<RocksDb::Batch> RocksDb::start_batch () {
165
- return std::make_unique<RocksDbBatch>(shared_from_this (),
164
+ return std::make_unique<RocksDbBatch>(
165
+ shared_from_this (),
166
166
handles.at (std::to_underlying (ColumnFamilyId::DEFAULT)));
167
167
}
168
168
@@ -174,8 +174,8 @@ namespace morum {
174
174
return wrap_status (db->Write (rocksdb::WriteOptions{}, &rocks_batch->batch ));
175
175
}
176
176
177
- RocksDbColumnFamily::RocksDbColumnFamily (
178
- std::shared_ptr<RocksDb> db, ColumnFamilyId family)
177
+ RocksDbColumnFamily::RocksDbColumnFamily (std::shared_ptr<RocksDb> db,
178
+ ColumnFamilyId family)
179
179
: db{db}, handle{db->handles .at (std::to_underlying (family))} {}
180
180
181
181
std::expected<void , StorageError> RocksDbColumnFamily::write (
@@ -193,9 +193,9 @@ namespace morum {
193
193
RocksDbColumnFamily::read (qtils::ByteSpan key) const {
194
194
std::string value;
195
195
auto status = db->db ->Get (rocksdb::ReadOptions{},
196
- handle,
197
- rocksdb::Slice{qtils::byte2str (key)},
198
- &value);
196
+ handle,
197
+ rocksdb::Slice{qtils::byte2str (key)},
198
+ &value);
199
199
if (status.IsNotFound ()) {
200
200
return std::nullopt;
201
201
}
@@ -204,13 +204,13 @@ namespace morum {
204
204
}
205
205
206
206
std::expected<std::optional<size_t >, StorageError>
207
- RocksDbColumnFamily::read_to (
208
- qtils::ByteSpan key, qtils::ByteSpanMut value) const {
207
+ RocksDbColumnFamily::read_to (qtils::ByteSpan key,
208
+ qtils::ByteSpanMut value) const {
209
209
std::string res;
210
210
auto status = db->db ->Get (rocksdb::ReadOptions{},
211
- handle,
212
- rocksdb::Slice{qtils::byte2str (key)},
213
- &res);
211
+ handle,
212
+ rocksdb::Slice{qtils::byte2str (key)},
213
+ &res);
214
214
if (status.IsNotFound ()) {
215
215
return std::nullopt;
216
216
}
@@ -240,4 +240,4 @@ namespace morum {
240
240
db->db ->Write (rocksdb::WriteOptions{}, &rocks_batch->batch ));
241
241
}
242
242
243
- } // namespace morum
243
+ } // namespace morum
0 commit comments