Skip to content

Commit d176964

Browse files
Improve python wrapper
1 parent 54b946c commit d176964

2 files changed

Lines changed: 51 additions & 35 deletions

File tree

src/amulet/rocksdb/__init__.pyi

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
import types
54
import typing
65

76
from . import _rocksdb, _version
@@ -63,8 +62,16 @@ class RocksDB:
6362
A RocksDB database
6463
"""
6564

66-
def __delitem__(self, key: bytes) -> None: ...
67-
def __getitem__(self, key: bytes) -> bytes: ...
65+
def __delitem__(self, key: bytes) -> None:
66+
"""
67+
del db[b"key"]
68+
"""
69+
70+
def __getitem__(self, key: bytes) -> bytes:
71+
"""
72+
db[b"key"]
73+
"""
74+
6875
@typing.overload
6976
def __init__(
7077
self,
@@ -105,7 +112,11 @@ class RocksDB:
105112
:raises: RocksDBException if an error occured.
106113
"""
107114

108-
def __setitem__(self, key: bytes, value: bytes) -> None: ...
115+
def __setitem__(self, key: bytes, value: bytes) -> None:
116+
"""
117+
db[b"key"] = b"value"
118+
"""
119+
109120
def close(self) -> None:
110121
"""
111122
Close the rocksdb database.
@@ -118,7 +129,7 @@ class RocksDB:
118129
Remove deleted entries from the database to reduce its size.
119130
"""
120131

121-
def compact_range(self, arg0: str | None, arg1: str | None) -> None:
132+
def compact_range(self, begin: bytes | None, end: bytes | None) -> None:
122133
"""
123134
Remove deleted entries from the database to reduce its size.
124135
"""

src/amulet/rocksdb/_rocksdb.py.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,14 @@
33
#include <pybind11/stl/filesystem.h>
44
#include <pybind11/typing.h>
55

6-
// #include <filesystem>
7-
// #include <optional>
8-
// #include <string>
6+
#include <filesystem>
7+
#include <optional>
8+
#include <string>
99
#include <string_view>
10-
// #include <variant>
11-
//
12-
// #include <rocksdb/cache.h>
13-
// #include <rocksdb/db.h>
14-
////#include <rocksdb/decompress_allocator.h>
15-
// #include <rocksdb/env.h>
16-
// #include <rocksdb/filter_policy.h>
17-
// #include <rocksdb/options.h>
18-
// #include <rocksdb/write_batch.h>
1910

2011
#include <amulet/pybind11_extensions/compatibility.hpp>
2112
#include <amulet/pybind11_extensions/iterator.hpp>
2213

23-
// #include <amulet/rocksdb.hpp>
2414
#include <amulet/rocksdb/compact_range_options.hpp>
2515
#include <amulet/rocksdb/db.hpp>
2616
#include <amulet/rocksdb/options.hpp>
@@ -70,7 +60,6 @@ namespace detail {
7060

7161
namespace {
7262

73-
7463
// class RocksDBKeysIterator {
7564
// private:
7665
// std::unique_ptr<Amulet::RocksDBIterator> iterator_ptr;
@@ -370,19 +359,17 @@ void init_module(py::module m)
370359
":raises: RocksDBException if an error occured."));
371360
RocksDB.def(
372361
py::init([](
373-
std::filesystem::path path,
374-
Amulet::RocksDB::Options& options,
375-
Amulet::RocksDB::ReadOptions& read_options,
376-
Amulet::RocksDB::WriteOptions& write_options,
377-
Amulet::RocksDB::CompactRangeOptions& compact_range_options
378-
) {
362+
std::filesystem::path path,
363+
Amulet::RocksDB::Options& options,
364+
Amulet::RocksDB::ReadOptions& read_options,
365+
Amulet::RocksDB::WriteOptions& write_options,
366+
Amulet::RocksDB::CompactRangeOptions& compact_range_options) {
379367
return std::make_unique<Amulet::RocksDB::RocksDB>(
380368
std::move(path),
381369
std::move(options),
382370
std::move(read_options),
383371
std::move(write_options),
384-
std::move(compact_range_options)
385-
);
372+
std::move(compact_range_options));
386373
}),
387374
py::arg("path"),
388375
py::arg("options"),
@@ -412,9 +399,23 @@ void init_module(py::module m)
412399

413400
RocksDB.def(
414401
"compact_range",
415-
&Amulet::RocksDB::RocksDB::compact_range,
416-
py::doc("Remove deleted entries from the database to reduce its size."),
417-
py::call_guard<py::gil_scoped_release>());
402+
[](Amulet::RocksDB::RocksDB& self, std::optional<py::bytes> begin, std::optional<py::bytes> end) {
403+
std::optional<std::string_view> begin_view;
404+
std::optional<std::string_view> end_view;
405+
if (begin) {
406+
begin_view = begin;
407+
}
408+
if (end) {
409+
end_view = end;
410+
}
411+
{
412+
py::gil_scoped_release gil;
413+
self.compact_range(begin_view, end_view);
414+
}
415+
},
416+
py::arg("begin"),
417+
py::arg("end"),
418+
py::doc("Remove deleted entries from the database to reduce its size."));
418419

419420
RocksDB.def(
420421
"compact",
@@ -440,7 +441,8 @@ void init_module(py::module m)
440441
"__setitem__",
441442
put,
442443
py::arg("key"),
443-
py::arg("value"));
444+
py::arg("value"),
445+
py::doc("db[b\"key\"] = b\"value\""));
444446

445447
// RocksDB.def(
446448
// "put_batch",
@@ -491,7 +493,11 @@ void init_module(py::module m)
491493
":return: The data stored behind the given key.\n"
492494
":raises: KeyError if the requested key is not present.\n"
493495
":raises: RocksDBException on other error."));
494-
RocksDB.def("__getitem__", get, py::arg("key"));
496+
RocksDB.def(
497+
"__getitem__",
498+
get,
499+
py::arg("key"),
500+
py::doc("db[b\"key\"]"));
495501

496502
auto del = [](Amulet::RocksDB::RocksDB& self, py::bytes key) {
497503
std::string_view key_view = key;
@@ -507,13 +513,12 @@ void init_module(py::module m)
507513
py::doc(
508514
"Delete a key from the database.\n"
509515
"\n"
510-
":param key: The key to delete from the database."),
511-
py::call_guard<py::gil_scoped_release>());
516+
":param key: The key to delete from the database."));
512517
RocksDB.def(
513518
"__delitem__",
514519
del,
515520
py::arg("key"),
516-
py::call_guard<py::gil_scoped_release>());
521+
py::doc("del db[b\"key\"]"));
517522

518523
// RocksDB.def(
519524
// "create_iterator",

0 commit comments

Comments
 (0)