-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_rocksdb.py.cpp
More file actions
409 lines (378 loc) · 15 KB
/
Copy path_rocksdb.py.cpp
File metadata and controls
409 lines (378 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl/filesystem.h>
#include <pybind11/typing.h>
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <amulet/pybind11_extensions/compatibility.hpp>
#include <amulet/pybind11_extensions/iterable.hpp>
#include <amulet/pybind11_extensions/iterator.hpp>
#include <amulet/rocksdb/compact_range_options.hpp>
#include <amulet/rocksdb/db.hpp>
#include <amulet/rocksdb/iterator.hpp>
#include <amulet/rocksdb/options.hpp>
#include <amulet/rocksdb/read_options.hpp>
#include <amulet/rocksdb/write_options.hpp>
namespace py = pybind11;
namespace pyext = Amulet::pybind11_extensions;
namespace {
class RocksDBKeysIterator : public Amulet::RocksDB::Iterator { };
class RocksDBValuesIterator : public Amulet::RocksDB::Iterator { };
class RocksDBItemsIterator : public Amulet::RocksDB::Iterator { };
} // namespace
void init_module(py::module m)
{
pyext::init_compiler_config(m);
std::string module_name = m.attr("__name__").cast<std::string>();
py::register_local_exception<Amulet::RocksDB::RocksDBException>(m, "RocksDBException");
py::enum_<Amulet::RocksDB::CompressionType> CompressionType(m, "CompressionType");
CompressionType.value(
"NoCompression",
Amulet::RocksDB::CompressionType::NoCompression,
"No compression.");
CompressionType.value(
"ZstdCompression",
Amulet::RocksDB::CompressionType::ZStandardCompression,
"Zstd compression.");
CompressionType.attr("__repr__") = py::cpp_function(
[module_name, CompressionType](const py::object& arg) -> py::str {
return py::str("{}.{}").format(module_name, CompressionType.attr("__str__")(arg));
},
py::name("__repr__"),
py::is_method(CompressionType));
py::classh<Amulet::RocksDB::Options> Options(m, "Options");
Options.def(
py::init());
Options.def_property(
"create_if_missing",
&Amulet::RocksDB::Options::get_create_if_missing,
&Amulet::RocksDB::Options::set_create_if_missing);
Options.def_property(
"compression_type",
&Amulet::RocksDB::Options::get_compression_type,
&Amulet::RocksDB::Options::set_compression_type);
py::classh<Amulet::RocksDB::ReadOptions> ReadOptions(m, "ReadOptions");
ReadOptions.def(
py::init());
py::classh<Amulet::RocksDB::WriteOptions> WriteOptions(m, "WriteOptions");
WriteOptions.def(
py::init());
WriteOptions.def_property(
"sync",
&Amulet::RocksDB::WriteOptions::get_sync,
&Amulet::RocksDB::WriteOptions::set_sync);
WriteOptions.def_property(
"disable_wal",
&Amulet::RocksDB::WriteOptions::get_disable_wal,
&Amulet::RocksDB::WriteOptions::set_disable_wal);
py::classh<Amulet::RocksDB::CompactRangeOptions> CompactRangeOptions(m, "CompactRangeOptions");
CompactRangeOptions.def(
py::init());
py::classh<Amulet::RocksDB::Iterator> BaseIterator(m, "BaseIterator", py::release_gil_before_calling_cpp_dtor());
BaseIterator.def(
"valid",
&Amulet::RocksDB::Iterator::is_valid,
py::doc(
"Is the iterator at a valid entry.\n"
"If False, calls to other methods may error."));
BaseIterator.def(
"seek_to_first",
&Amulet::RocksDB::Iterator::seek_to_first,
py::doc("Seek to the first entry in the database."));
BaseIterator.def(
"seek_to_last",
&Amulet::RocksDB::Iterator::seek_to_last,
py::doc("Seek to the last entry in the database."));
BaseIterator.def(
"seek",
[](Amulet::RocksDB::Iterator& self, py::bytes target) {
self.seek(target);
},
py::arg("target"),
py::doc(
"Seek to the given entry in the database.\n"
"If the entry does not exist it will seek to the location after."));
BaseIterator.def(
"next",
&Amulet::RocksDB::Iterator::next,
py::doc(
"Seek to the next entry in the database."));
BaseIterator.def(
"prev",
&Amulet::RocksDB::Iterator::prev,
py::doc(
"Seek to the previous entry in the database."));
BaseIterator.def(
"key",
[](Amulet::RocksDB::Iterator& self) {
if (!self.is_valid()) {
throw std::runtime_error("Iterator is not valid");
}
return py::bytes(self.key());
},
py::doc(
"Get the key of the current entry in the database.\n"
":raises: runtime_error if iterator is not valid."));
BaseIterator.def(
"value",
[](Amulet::RocksDB::Iterator& self) {
if (!self.is_valid()) {
throw std::runtime_error("Iterator is not valid");
}
return py::bytes(self.value());
},
py::doc(
"Get the value of the current entry in the database.\n"
":raises: runtime_error if iterator is not valid."));
BaseIterator.def(
"__iter__",
[](py::object self) { return self; });
py::classh<RocksDBKeysIterator, Amulet::RocksDB::Iterator> KeysIterator(m, "KeysIterator", py::release_gil_before_calling_cpp_dtor());
KeysIterator.def(
"__next__",
[](RocksDBKeysIterator& self) {
if (self.is_valid()) {
auto obj = py::bytes(self.key());
self.next();
return obj;
}
throw py::stop_iteration();
});
py::classh<RocksDBValuesIterator, Amulet::RocksDB::Iterator> ValuesIterator(m, "ValuesIterator", py::release_gil_before_calling_cpp_dtor());
ValuesIterator.def(
"__next__",
[](RocksDBValuesIterator& self) {
if (self.is_valid()) {
auto obj = py::bytes(self.key());
self.next();
return obj;
}
throw py::stop_iteration();
});
py::classh<RocksDBItemsIterator, Amulet::RocksDB::Iterator> ItemsIterator(m, "ItemsIterator", py::release_gil_before_calling_cpp_dtor());
ItemsIterator.def(
"__next__",
[](RocksDBItemsIterator& self) -> py::typing::Tuple<py::bytes, py::bytes> {
if (self.is_valid()) {
auto obj = py::make_tuple(py::bytes(self.key()), py::bytes(self.value()));
self.next();
return obj;
}
throw py::stop_iteration();
});
py::classh<Amulet::RocksDB::RocksDB> RocksDB(m, "RocksDB", py::release_gil_before_calling_cpp_dtor(),
"A RocksDB database");
RocksDB.def(
py::init<std::filesystem::path, bool, Amulet::RocksDB::CompressionType>(),
py::arg("path"),
py::arg("create_if_missing") = false,
py::arg("compression_type") = Amulet::RocksDB::CompressionType::ZStandardCompression,
py::doc(
"Construct a new :class:`RocksDB` instance from the database at the given path.\n"
"\n"
"A rocksdb database is like a dictionary that only contains bytes as the keys and values and exists entirely on the disk.\n"
"\n"
":param path: The path to the database directory.\n"
":param create_if_missing: If True a new database will be created if one does not exist at the given path.\n"
":param compression_type: The compression type to use. (Default ZStandardCompression)\n"
":raises: RocksDBException if an error occured."));
RocksDB.def(
py::init([](
std::filesystem::path path,
std::optional<std::reference_wrapper<Amulet::RocksDB::Options>> options,
std::optional<std::reference_wrapper<Amulet::RocksDB::ReadOptions>> read_options,
std::optional<std::reference_wrapper<Amulet::RocksDB::WriteOptions>> write_options,
std::optional<std::reference_wrapper<Amulet::RocksDB::CompactRangeOptions>> compact_range_options) {
return std::make_unique<Amulet::RocksDB::RocksDB>(
std::move(path),
options ? std::move(options->get()) : Amulet::RocksDB::Options(),
read_options ? std::move(read_options->get()) : Amulet::RocksDB::ReadOptions(),
write_options ? std::move(write_options->get()) : Amulet::RocksDB::WriteOptions(),
compact_range_options ? std::move(compact_range_options->get()) : Amulet::RocksDB::CompactRangeOptions());
}),
py::arg("path"),
py::kw_only(),
py::arg("options") = py::none(),
py::arg("read_options") = py::none(),
py::arg("write_options") = py::none(),
py::arg("compact_range_options") = py::none(),
py::doc(
"Construct a new :class:`RocksDB` instance from the database at the given path.\n"
"\n"
"A rocksdb database is like a dictionary that only contains bytes as the keys and values and exists entirely on the disk.\n"
"\n"
":param path: The path to the database directory.\n"
":param options: The RocksDB Options object.\n"
":param read_options: The RocksDB ReadOptions object.\n"
":param write_options: The RocksDB WriteOptions object.\n"
":param compact_range_options: The RocksDB CompactRangeOptions object.\n"
":raises: RocksDBException if an error occured."));
RocksDB.def(
"close",
&Amulet::RocksDB::RocksDB::close,
py::doc(
"Close the rocksdb database.\n"
"Only the owner of the database may close it.\n"
"If needed, an external lock must be used to ensure that no other threads are accessing the database."),
py::call_guard<py::gil_scoped_release>());
RocksDB.def(
"compact_range",
[](Amulet::RocksDB::RocksDB& self, std::optional<py::bytes> begin, std::optional<py::bytes> end) {
std::optional<std::string_view> begin_view;
std::optional<std::string_view> end_view;
if (begin) {
begin_view = begin;
}
if (end) {
end_view = end;
}
{
py::gil_scoped_release nogil;
self.compact_range(begin_view, end_view);
}
},
py::arg("begin"),
py::arg("end"),
py::doc("Remove deleted entries from the database to reduce its size."));
RocksDB.def(
"compact",
&Amulet::RocksDB::RocksDB::compact,
py::doc("Remove deleted entries from the database to reduce its size."),
py::call_guard<py::gil_scoped_release>());
auto put = [](Amulet::RocksDB::RocksDB& self, py::bytes key, py::bytes value) {
std::string_view key_view = key;
std::string_view value_view = value;
{
py::gil_scoped_release nogil;
self.put(key_view, value_view);
}
};
RocksDB.def(
"put",
put,
py::arg("key"),
py::arg("value"),
py::doc("Set a value in the database."));
RocksDB.def(
"__setitem__",
put,
py::arg("key"),
py::arg("value"),
py::doc("db[b\"key\"] = b\"value\""));
RocksDB.def(
"put_batch",
[](Amulet::RocksDB::RocksDB& self, pyext::collections::Iterable<std::pair<py::bytes, std::optional<py::bytes>>> py_batch) {
std::list<std::pair<std::string_view, std::optional<std::string_view>>> batch;
for (const auto& [key, value] : py_batch) {
if (value) {
batch.emplace_back(key, *value);
} else {
batch.emplace_back(key, std::nullopt);
}
}
{
py::gil_scoped_release nogil;
self.put_batch(batch);
}
},
py::arg("batch"),
py::doc("Set a group of values in the database."));
RocksDB.def(
"__contains__",
[](Amulet::RocksDB::RocksDB& self, py::bytes key) {
std::string_view key_view = key;
{
py::gil_scoped_release nogil;
return self.contains(key_view);
}
},
py::arg("key"));
auto get = [](Amulet::RocksDB::RocksDB& self, py::bytes key) {
std::string_view key_view = key;
std::string value;
try {
py::gil_scoped_release nogil;
value = self.get(key_view);
} catch (const Amulet::RocksDB::KeyError& e) {
throw py::key_error(e.what());
}
return py::bytes(value);
};
RocksDB.def(
"get",
get,
py::arg("key"),
py::doc(
"Get a key from the database.\n"
"\n"
":param key: The key to get from the database.\n"
":return: The data stored behind the given key.\n"
":raises: KeyError if the requested key is not present.\n"
":raises: RocksDBException on other error."));
RocksDB.def(
"__getitem__",
get,
py::arg("key"),
py::doc("db[b\"key\"]"));
auto del = [](Amulet::RocksDB::RocksDB& self, py::bytes key) {
std::string_view key_view = key;
{
py::gil_scoped_release nogil;
self.del(key_view);
}
};
RocksDB.def(
"delete",
del,
py::arg("key"),
py::doc(
"Delete a key from the database.\n"
"\n"
":param key: The key to delete from the database."));
RocksDB.def(
"__delitem__",
del,
py::arg("key"),
py::doc("del db[b\"key\"]"));
RocksDB.def(
"__iter__",
[](Amulet::RocksDB::RocksDB& self) {
auto* it = self.create_iterator().release();
it->seek_to_first();
return reinterpret_cast<RocksDBKeysIterator*>(it);
},
py::call_guard<py::gil_scoped_release>());
RocksDB.def(
"keys",
[](Amulet::RocksDB::RocksDB& self) {
auto* it = self.create_iterator().release();
it->seek_to_first();
return reinterpret_cast<RocksDBKeysIterator*>(it);
},
py::doc("An iterable of all keys in the database."),
py::call_guard<py::gil_scoped_release>());
RocksDB.def(
"values",
[](Amulet::RocksDB::RocksDB& self) {
auto* it = self.create_iterator().release();
it->seek_to_first();
return reinterpret_cast<RocksDBValuesIterator*>(it);
},
py::doc("An iterable of all values in the database."),
py::call_guard<py::gil_scoped_release>());
RocksDB.def(
"items",
[](Amulet::RocksDB::RocksDB& self) {
auto* it = self.create_iterator().release();
it->seek_to_first();
return reinterpret_cast<RocksDBItemsIterator*>(it);
},
py::doc("An iterable of all items in the database."),
py::call_guard<py::gil_scoped_release>());
}
PYBIND11_MODULE(_rocksdb, m)
{
m.def("init", &init_module, py::arg("m"));
}