pyrex-rocksdb 0.1.4__cp38-cp38-musllinux_1_2_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pyrex-rocksdb might be problematic. Click here for more details.
- pyrex/__init__.py +23 -0
- pyrex/_pyrex.cpp +752 -0
- pyrex/_pyrex.cpython-38-x86_64-linux-gnu.so +0 -0
- pyrex_rocksdb-0.1.4.dist-info/LICENSE +202 -0
- pyrex_rocksdb-0.1.4.dist-info/METADATA +122 -0
- pyrex_rocksdb-0.1.4.dist-info/RECORD +15 -0
- pyrex_rocksdb-0.1.4.dist-info/WHEEL +5 -0
- pyrex_rocksdb-0.1.4.dist-info/top_level.txt +2 -0
- pyrex_rocksdb.libs/libbz2-b428631a.so.1.0.8 +0 -0
- pyrex_rocksdb.libs/libgcc_s-0cd532bd.so.1 +0 -0
- pyrex_rocksdb.libs/liblz4-af355be2.so.1.10.0 +0 -0
- pyrex_rocksdb.libs/libsnappy-f36b9e28.so.1.2.2 +0 -0
- pyrex_rocksdb.libs/libstdc++-5d72f927.so.6.0.33 +0 -0
- pyrex_rocksdb.libs/liburing-20646edb.so.2.9 +0 -0
- pyrex_rocksdb.libs/libzstd-f3c21b15.so.1.5.7 +0 -0
pyrex/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# This makes functions and classes like `PyRocksDB` directly available as `pyrex.PyRocksDB`
|
|
2
|
+
from ._pyrex import *
|
|
3
|
+
|
|
4
|
+
# Version information (highly recommended)
|
|
5
|
+
try:
|
|
6
|
+
from importlib.metadata import version, PackageNotFoundError
|
|
7
|
+
except ImportError: # Python < 3.8
|
|
8
|
+
from importlib_metadata import version, PackageNotFoundError # pip install importlib_metadata for older Pythons
|
|
9
|
+
|
|
10
|
+
try:
|
|
11
|
+
__version__ = version("pyrex-rocksdb") # Use the 'name' from pyproject.toml
|
|
12
|
+
except PackageNotFoundError:
|
|
13
|
+
# Package is not installed (e.g., running tests in dev mode without editable install)
|
|
14
|
+
__version__ = "unknown"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# Package Docstring
|
|
18
|
+
"""
|
|
19
|
+
A fast RocksDB wrapper for Python using pybind11.
|
|
20
|
+
|
|
21
|
+
This package provides high-performance bindings to the RocksDB key-value store,
|
|
22
|
+
allowing seamless interaction from Python applications.
|
|
23
|
+
"""
|
pyrex/_pyrex.cpp
ADDED
|
@@ -0,0 +1,752 @@
|
|
|
1
|
+
#include <pybind11/pybind11.h>
|
|
2
|
+
#include <pybind11/stl.h>
|
|
3
|
+
|
|
4
|
+
#include <memory>
|
|
5
|
+
#include <vector>
|
|
6
|
+
#include <map>
|
|
7
|
+
#include <set>
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <mutex>
|
|
10
|
+
#include <atomic>
|
|
11
|
+
#include <iostream>
|
|
12
|
+
|
|
13
|
+
#include "rocksdb/db.h"
|
|
14
|
+
#include "rocksdb/options.h"
|
|
15
|
+
#include "rocksdb/status.h"
|
|
16
|
+
#include "rocksdb/slice.h"
|
|
17
|
+
#include "rocksdb/table.h"
|
|
18
|
+
#include "rocksdb/filter_policy.h"
|
|
19
|
+
#include "rocksdb/write_batch.h"
|
|
20
|
+
#include "rocksdb/iterator.h"
|
|
21
|
+
|
|
22
|
+
namespace py = pybind11;
|
|
23
|
+
|
|
24
|
+
// --- Custom Exception ---
|
|
25
|
+
class RocksDBException : public std::runtime_error {
|
|
26
|
+
public:
|
|
27
|
+
explicit RocksDBException(const std::string& msg) : std::runtime_error(msg) {}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// --- Forward Declarations ---
|
|
31
|
+
class PyRocksDB;
|
|
32
|
+
|
|
33
|
+
// --- PyReadOptions Wrapper ---
|
|
34
|
+
class PyReadOptions {
|
|
35
|
+
public:
|
|
36
|
+
rocksdb::ReadOptions options_;
|
|
37
|
+
|
|
38
|
+
PyReadOptions() = default;
|
|
39
|
+
|
|
40
|
+
// Expose properties to Python
|
|
41
|
+
bool get_fill_cache() const { return options_.fill_cache; }
|
|
42
|
+
void set_fill_cache(bool value) { options_.fill_cache = value; }
|
|
43
|
+
|
|
44
|
+
bool get_verify_checksums() const { return options_.verify_checksums; }
|
|
45
|
+
void set_verify_checksums(bool value) { options_.verify_checksums = value; }
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// --- PyWriteOptions Wrapper ---
|
|
49
|
+
class PyWriteOptions {
|
|
50
|
+
public:
|
|
51
|
+
rocksdb::WriteOptions options_;
|
|
52
|
+
|
|
53
|
+
PyWriteOptions() = default;
|
|
54
|
+
|
|
55
|
+
// Expose properties to Python
|
|
56
|
+
bool get_sync() const { return options_.sync; }
|
|
57
|
+
void set_sync(bool value) { options_.sync = value; }
|
|
58
|
+
|
|
59
|
+
bool get_disable_wal() const { return options_.disableWAL; }
|
|
60
|
+
void set_disable_wal(bool value) { options_.disableWAL = value; }
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
// --- PyOptions Wrapper ---
|
|
65
|
+
class PyOptions {
|
|
66
|
+
public:
|
|
67
|
+
rocksdb::Options options_;
|
|
68
|
+
rocksdb::ColumnFamilyOptions cf_options_;
|
|
69
|
+
|
|
70
|
+
PyOptions() {
|
|
71
|
+
options_.compression = rocksdb::kSnappyCompression;
|
|
72
|
+
cf_options_.compression = rocksdb::kSnappyCompression;
|
|
73
|
+
}
|
|
74
|
+
bool get_create_if_missing() const { return options_.create_if_missing; }
|
|
75
|
+
void set_create_if_missing(bool value) { options_.create_if_missing = value; }
|
|
76
|
+
bool get_error_if_exists() const { return options_.error_if_exists; }
|
|
77
|
+
void set_error_if_exists(bool value) { options_.error_if_exists = value; }
|
|
78
|
+
int get_max_open_files() const { return options_.max_open_files; }
|
|
79
|
+
void set_max_open_files(int value) { options_.max_open_files = value; }
|
|
80
|
+
size_t get_write_buffer_size() const { return options_.write_buffer_size; }
|
|
81
|
+
void set_write_buffer_size(size_t value) { options_.write_buffer_size = value; }
|
|
82
|
+
rocksdb::CompressionType get_compression() const { return options_.compression; }
|
|
83
|
+
void set_compression(rocksdb::CompressionType value) { options_.compression = value; }
|
|
84
|
+
int get_max_background_jobs() const { return options_.max_background_jobs; }
|
|
85
|
+
void set_max_background_jobs(int value) { options_.max_background_jobs = value; }
|
|
86
|
+
void increase_parallelism(int total_threads) { options_.IncreaseParallelism(total_threads); }
|
|
87
|
+
void optimize_for_small_db() { options_.OptimizeForSmallDb(); }
|
|
88
|
+
void use_block_based_bloom_filter(double bits_per_key = 10.0) {
|
|
89
|
+
rocksdb::BlockBasedTableOptions table_options;
|
|
90
|
+
table_options.filter_policy.reset(rocksdb::NewBloomFilterPolicy(bits_per_key));
|
|
91
|
+
options_.table_factory.reset(rocksdb::NewBlockBasedTableFactory(table_options));
|
|
92
|
+
cf_options_.table_factory.reset(rocksdb::NewBlockBasedTableFactory(table_options));
|
|
93
|
+
}
|
|
94
|
+
size_t get_cf_write_buffer_size() const { return cf_options_.write_buffer_size; }
|
|
95
|
+
void set_cf_write_buffer_size(size_t value) { cf_options_.write_buffer_size = value; }
|
|
96
|
+
rocksdb::CompressionType get_cf_compression() const { return cf_options_.compression; }
|
|
97
|
+
void set_cf_compression(rocksdb::CompressionType value) { cf_options_.compression = value; }
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// --- PyColumnFamilyHandle Wrapper ---
|
|
101
|
+
class PyColumnFamilyHandle {
|
|
102
|
+
public:
|
|
103
|
+
rocksdb::ColumnFamilyHandle* cf_handle_;
|
|
104
|
+
std::string name_;
|
|
105
|
+
|
|
106
|
+
PyColumnFamilyHandle(rocksdb::ColumnFamilyHandle* handle, const std::string& name)
|
|
107
|
+
: cf_handle_(handle), name_(name) {
|
|
108
|
+
if (!cf_handle_) {
|
|
109
|
+
throw RocksDBException("Invalid ColumnFamilyHandle received.");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const std::string& get_name() const { return name_; }
|
|
113
|
+
bool is_valid() const { return cf_handle_ != nullptr; }
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// --- PyWriteBatch Wrapper ---
|
|
117
|
+
class PyWriteBatch {
|
|
118
|
+
public:
|
|
119
|
+
rocksdb::WriteBatch wb_;
|
|
120
|
+
PyWriteBatch() = default;
|
|
121
|
+
void put(const py::bytes& key, const py::bytes& value) { wb_.Put(static_cast<std::string>(key), static_cast<std::string>(value)); }
|
|
122
|
+
void put_cf(PyColumnFamilyHandle& cf, const py::bytes& key, const py::bytes& value) {
|
|
123
|
+
if (!cf.is_valid()) throw RocksDBException("ColumnFamilyHandle is invalid.");
|
|
124
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
125
|
+
rocksdb::Slice value_slice(static_cast<std::string_view>(value));
|
|
126
|
+
wb_.Put(cf.cf_handle_, key_slice, value_slice);
|
|
127
|
+
}
|
|
128
|
+
void del(const py::bytes& key) {
|
|
129
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
130
|
+
wb_.Delete(key_slice);
|
|
131
|
+
}
|
|
132
|
+
void del_cf(PyColumnFamilyHandle& cf, const py::bytes& key) {
|
|
133
|
+
if (!cf.is_valid()) throw RocksDBException("ColumnFamilyHandle is invalid.");
|
|
134
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
135
|
+
wb_.Delete(cf.cf_handle_, key_slice);
|
|
136
|
+
}
|
|
137
|
+
void merge(const py::bytes& key, const py::bytes& value) {
|
|
138
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
139
|
+
rocksdb::Slice value_slice(static_cast<std::string_view>(value));
|
|
140
|
+
wb_.Merge(key_slice, value_slice);
|
|
141
|
+
}
|
|
142
|
+
void merge_cf(PyColumnFamilyHandle& cf, const py::bytes& key, const py::bytes& value) {
|
|
143
|
+
if (!cf.is_valid()) throw RocksDBException("ColumnFamilyHandle is invalid.");
|
|
144
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
145
|
+
rocksdb::Slice value_slice(static_cast<std::string_view>(value));
|
|
146
|
+
wb_.Merge(cf.cf_handle_, key_slice, value_slice);
|
|
147
|
+
}
|
|
148
|
+
void clear() { wb_.Clear(); }
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// --- PyRocksDBIterator Class Declaration ---
|
|
152
|
+
class PyRocksDBIterator {
|
|
153
|
+
private:
|
|
154
|
+
rocksdb::Iterator* it_raw_ptr_;
|
|
155
|
+
std::shared_ptr<PyRocksDB> parent_db_ptr_;
|
|
156
|
+
void check_parent_db_is_open() const;
|
|
157
|
+
|
|
158
|
+
public:
|
|
159
|
+
explicit PyRocksDBIterator(rocksdb::Iterator* it, std::shared_ptr<PyRocksDB> parent_db);
|
|
160
|
+
~PyRocksDBIterator();
|
|
161
|
+
bool valid();
|
|
162
|
+
void seek_to_first();
|
|
163
|
+
void seek_to_last();
|
|
164
|
+
void seek(const py::bytes& key);
|
|
165
|
+
void next();
|
|
166
|
+
void prev();
|
|
167
|
+
py::object key();
|
|
168
|
+
py::object value();
|
|
169
|
+
void check_status();
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// --- PyRocksDB Class (Base) ---
|
|
173
|
+
class PyRocksDB : public std::enable_shared_from_this<PyRocksDB> {
|
|
174
|
+
protected:
|
|
175
|
+
rocksdb::DB* db_ = nullptr;
|
|
176
|
+
rocksdb::ColumnFamilyHandle* default_cf_handle_ = nullptr;
|
|
177
|
+
PyOptions opened_options_;
|
|
178
|
+
std::string path_;
|
|
179
|
+
std::map<std::string, std::shared_ptr<PyColumnFamilyHandle>> cf_handles_;
|
|
180
|
+
std::atomic<bool> is_closed_{false};
|
|
181
|
+
std::atomic<bool> is_read_only_{false};
|
|
182
|
+
std::mutex active_iterators_mutex_;
|
|
183
|
+
std::set<rocksdb::Iterator*> active_rocksdb_iterators_;
|
|
184
|
+
|
|
185
|
+
// Default options for read/write operations
|
|
186
|
+
std::shared_ptr<PyReadOptions> default_read_options_;
|
|
187
|
+
std::shared_ptr<PyWriteOptions> default_write_options_;
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
friend class PyRocksDBIterator;
|
|
191
|
+
|
|
192
|
+
void check_db_open() const {
|
|
193
|
+
if (is_closed_ || db_ == nullptr) {
|
|
194
|
+
throw RocksDBException("Database is not open or has been closed.");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
void check_read_only() const {
|
|
199
|
+
if (is_read_only_.load()) {
|
|
200
|
+
throw RocksDBException("Cannot perform put/write/delete operation: Database opened in read-only mode.");
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
rocksdb::ColumnFamilyHandle* get_default_cf_handle() const {
|
|
205
|
+
auto it = cf_handles_.find(rocksdb::kDefaultColumnFamilyName);
|
|
206
|
+
if (it == cf_handles_.end() || !it->second->is_valid()) {
|
|
207
|
+
throw RocksDBException("Default column family handle is not available.");
|
|
208
|
+
}
|
|
209
|
+
return it->second->cf_handle_;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
public:
|
|
213
|
+
// Default constructor for inheritance
|
|
214
|
+
PyRocksDB()
|
|
215
|
+
: default_read_options_(std::make_shared<PyReadOptions>()),
|
|
216
|
+
default_write_options_(std::make_shared<PyWriteOptions>())
|
|
217
|
+
{}
|
|
218
|
+
|
|
219
|
+
// Public constructor for the simple interface
|
|
220
|
+
PyRocksDB(const std::string& path, PyOptions* py_options, bool read_only = false)
|
|
221
|
+
: default_read_options_(std::make_shared<PyReadOptions>()),
|
|
222
|
+
default_write_options_(std::make_shared<PyWriteOptions>())
|
|
223
|
+
{
|
|
224
|
+
this->path_ = path;
|
|
225
|
+
this->is_read_only_.store(read_only);
|
|
226
|
+
rocksdb::Options options;
|
|
227
|
+
if (py_options) {
|
|
228
|
+
options = py_options->options_;
|
|
229
|
+
this->opened_options_ = *py_options;
|
|
230
|
+
} else {
|
|
231
|
+
options.create_if_missing = true;
|
|
232
|
+
this->opened_options_.options_ = options;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
rocksdb::Status s;
|
|
236
|
+
if (read_only) {
|
|
237
|
+
s = rocksdb::DB::OpenForReadOnly(options, path, &this->db_);
|
|
238
|
+
} else {
|
|
239
|
+
s = rocksdb::DB::Open(options, path, &this->db_);
|
|
240
|
+
}
|
|
241
|
+
if (!s.ok()) {
|
|
242
|
+
throw RocksDBException("Failed to open RocksDB at " + path + ": " + s.ToString());
|
|
243
|
+
}
|
|
244
|
+
this->default_cf_handle_ = this->db_->DefaultColumnFamily();
|
|
245
|
+
this->cf_handles_[rocksdb::kDefaultColumnFamilyName] = std::make_shared<PyColumnFamilyHandle>(this->default_cf_handle_, rocksdb::kDefaultColumnFamilyName);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
virtual ~PyRocksDB() {
|
|
249
|
+
close();
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
void close() {
|
|
253
|
+
if (!is_closed_.exchange(true)) {
|
|
254
|
+
{
|
|
255
|
+
std::lock_guard<std::mutex> lock(active_iterators_mutex_);
|
|
256
|
+
for (rocksdb::Iterator* iter_raw_ptr : active_rocksdb_iterators_) {
|
|
257
|
+
delete iter_raw_ptr;
|
|
258
|
+
}
|
|
259
|
+
active_rocksdb_iterators_.clear();
|
|
260
|
+
}
|
|
261
|
+
for (auto const& [name, handle_ptr] : cf_handles_) {
|
|
262
|
+
handle_ptr->cf_handle_ = nullptr;
|
|
263
|
+
}
|
|
264
|
+
cf_handles_.clear();
|
|
265
|
+
if (db_) {
|
|
266
|
+
delete db_;
|
|
267
|
+
db_ = nullptr;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
void put(const py::bytes& key, const py::bytes& value, std::shared_ptr<PyWriteOptions> write_options = nullptr) {
|
|
273
|
+
check_db_open();
|
|
274
|
+
check_read_only();
|
|
275
|
+
|
|
276
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
277
|
+
rocksdb::Slice value_slice(static_cast<std::string_view>(value));
|
|
278
|
+
|
|
279
|
+
const auto& opts = write_options ? write_options->options_ : default_write_options_->options_;
|
|
280
|
+
rocksdb::Status s = db_->Put(opts, default_cf_handle_, key_slice, value_slice);
|
|
281
|
+
if (!s.ok()) throw RocksDBException("Put failed: " + s.ToString());
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
py::object get(const py::bytes& key, std::shared_ptr<PyReadOptions> read_options = nullptr) {
|
|
285
|
+
check_db_open();
|
|
286
|
+
std::string value_str;
|
|
287
|
+
rocksdb::Status s;
|
|
288
|
+
|
|
289
|
+
const auto& opts = read_options ? read_options->options_ : default_read_options_->options_;
|
|
290
|
+
|
|
291
|
+
{
|
|
292
|
+
py::gil_scoped_release release;
|
|
293
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
294
|
+
s = db_->Get(opts, default_cf_handle_, key_slice, &value_str);
|
|
295
|
+
}
|
|
296
|
+
if (s.ok()) return py::bytes(value_str);
|
|
297
|
+
if (s.IsNotFound()) return py::none();
|
|
298
|
+
throw RocksDBException("Get failed: " + s.ToString());
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
void del(const py::bytes& key, std::shared_ptr<PyWriteOptions> write_options = nullptr) {
|
|
302
|
+
check_db_open();
|
|
303
|
+
check_read_only();
|
|
304
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
305
|
+
const auto& opts = write_options ? write_options->options_ : default_write_options_->options_;
|
|
306
|
+
rocksdb::Status s = db_->Delete(opts, default_cf_handle_, key_slice);
|
|
307
|
+
if (!s.ok()) throw RocksDBException("Delete failed: " + s.ToString());
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
void write(PyWriteBatch& batch, std::shared_ptr<PyWriteOptions> write_options = nullptr) {
|
|
311
|
+
check_db_open();
|
|
312
|
+
check_read_only();
|
|
313
|
+
const auto& opts = write_options ? write_options->options_ : default_write_options_->options_;
|
|
314
|
+
rocksdb::Status s = db_->Write(opts, &batch.wb_);
|
|
315
|
+
if (!s.ok()) throw RocksDBException("Write failed: " + s.ToString());
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
std::shared_ptr<PyRocksDBIterator> new_iterator(std::shared_ptr<PyReadOptions> read_options = nullptr) {
|
|
319
|
+
check_db_open();
|
|
320
|
+
const auto& opts = read_options ? read_options->options_ : default_read_options_->options_;
|
|
321
|
+
rocksdb::Iterator* raw_iter = db_->NewIterator(opts, default_cf_handle_);
|
|
322
|
+
{
|
|
323
|
+
std::lock_guard<std::mutex> lock(active_iterators_mutex_);
|
|
324
|
+
active_rocksdb_iterators_.insert(raw_iter);
|
|
325
|
+
}
|
|
326
|
+
return std::make_shared<PyRocksDBIterator>(raw_iter, shared_from_this());
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
PyOptions get_options() const { return opened_options_; }
|
|
330
|
+
|
|
331
|
+
// Getters and setters for default options
|
|
332
|
+
std::shared_ptr<PyReadOptions> get_default_read_options() { return default_read_options_; }
|
|
333
|
+
void set_default_read_options(std::shared_ptr<PyReadOptions> opts) {
|
|
334
|
+
if (!opts) throw RocksDBException("ReadOptions cannot be None.");
|
|
335
|
+
default_read_options_ = opts;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
std::shared_ptr<PyWriteOptions> get_default_write_options() { return default_write_options_; }
|
|
339
|
+
void set_default_write_options(std::shared_ptr<PyWriteOptions> opts) {
|
|
340
|
+
if (!opts) throw RocksDBException("WriteOptions cannot be None.");
|
|
341
|
+
default_write_options_ = opts;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
// --- PyRocksDBExtended Class (Derived) ---
|
|
346
|
+
class PyRocksDBExtended : public PyRocksDB {
|
|
347
|
+
public:
|
|
348
|
+
// Constructor for the extended interface with CF support
|
|
349
|
+
PyRocksDBExtended(const std::string& path, PyOptions* py_options, bool read_only = false) {
|
|
350
|
+
this->path_ = path;
|
|
351
|
+
this->is_read_only_ = read_only;
|
|
352
|
+
rocksdb::Options options;
|
|
353
|
+
if (py_options) {
|
|
354
|
+
options = py_options->options_;
|
|
355
|
+
this->opened_options_ = *py_options;
|
|
356
|
+
} else {
|
|
357
|
+
options.create_if_missing = true;
|
|
358
|
+
this->opened_options_.options_ = options;
|
|
359
|
+
this->opened_options_.cf_options_.compression = rocksdb::kSnappyCompression;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
std::vector<std::string> cf_names;
|
|
363
|
+
rocksdb::Status s = rocksdb::DB::ListColumnFamilies(options, path, &cf_names);
|
|
364
|
+
|
|
365
|
+
std::vector<rocksdb::ColumnFamilyDescriptor> cf_descriptors;
|
|
366
|
+
if (s.IsNotFound() || s.IsIOError()) {
|
|
367
|
+
if (options.create_if_missing) {
|
|
368
|
+
cf_descriptors.push_back(rocksdb::ColumnFamilyDescriptor(rocksdb::kDefaultColumnFamilyName, this->opened_options_.cf_options_));
|
|
369
|
+
} else {
|
|
370
|
+
throw RocksDBException("Database not found at " + path + " and create_if_missing is false.");
|
|
371
|
+
}
|
|
372
|
+
} else if (s.ok()) {
|
|
373
|
+
for (const auto& name : cf_names) {
|
|
374
|
+
cf_descriptors.push_back(rocksdb::ColumnFamilyDescriptor(name, this->opened_options_.cf_options_));
|
|
375
|
+
}
|
|
376
|
+
} else {
|
|
377
|
+
throw RocksDBException("Failed to list column families at " + path + ": " + s.ToString());
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
std::vector<rocksdb::ColumnFamilyHandle*> handles;
|
|
381
|
+
rocksdb::Status s_open;
|
|
382
|
+
|
|
383
|
+
if (read_only) {
|
|
384
|
+
s_open = rocksdb::DB::OpenForReadOnly(options, path, cf_descriptors, &handles, &this->db_);
|
|
385
|
+
} else {
|
|
386
|
+
s_open = rocksdb::DB::Open(options, path, cf_descriptors, &handles, &this->db_);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (!s_open.ok()) {
|
|
390
|
+
throw RocksDBException("Failed to open RocksDB at " + path + ": " + s.ToString());
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
for (size_t i = 0; i < handles.size(); ++i) {
|
|
394
|
+
const std::string& cf_name = cf_descriptors[i].name;
|
|
395
|
+
this->cf_handles_[cf_name] = std::make_shared<PyColumnFamilyHandle>(handles[i], cf_name);
|
|
396
|
+
|
|
397
|
+
if (cf_name == rocksdb::kDefaultColumnFamilyName) {
|
|
398
|
+
this->default_cf_handle_ = handles[i];
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (!this->default_cf_handle_) {
|
|
403
|
+
throw RocksDBException("Default column family not found after opening.");
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
void put_cf(PyColumnFamilyHandle& cf, const py::bytes& key, const py::bytes& value, std::shared_ptr<PyWriteOptions> write_options = nullptr) {
|
|
408
|
+
check_db_open();
|
|
409
|
+
check_read_only();
|
|
410
|
+
if (!cf.is_valid()) throw RocksDBException("ColumnFamilyHandle is invalid.");
|
|
411
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
412
|
+
rocksdb::Slice value_slice(static_cast<std::string_view>(value));
|
|
413
|
+
const auto& opts = write_options ? write_options->options_ : default_write_options_->options_;
|
|
414
|
+
rocksdb::Status s = db_->Put(opts, cf.cf_handle_, key_slice, value_slice);
|
|
415
|
+
if (!s.ok()) throw RocksDBException("put_cf failed: " + s.ToString());
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
py::object get_cf(PyColumnFamilyHandle& cf, const py::bytes& key, std::shared_ptr<PyReadOptions> read_options = nullptr) {
|
|
419
|
+
check_db_open();
|
|
420
|
+
if (!cf.is_valid()) throw RocksDBException("ColumnFamilyHandle is invalid.");
|
|
421
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
422
|
+
std::string value_str;
|
|
423
|
+
const auto& opts = read_options ? read_options->options_ : default_read_options_->options_;
|
|
424
|
+
rocksdb::Status s = db_->Get(opts, cf.cf_handle_, key_slice, &value_str);
|
|
425
|
+
if (s.ok()) return py::bytes(value_str);
|
|
426
|
+
if (s.IsNotFound()) return py::none();
|
|
427
|
+
throw RocksDBException("get_cf failed: " + s.ToString());
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
void del_cf(PyColumnFamilyHandle& cf, const py::bytes& key, std::shared_ptr<PyWriteOptions> write_options = nullptr) {
|
|
431
|
+
check_db_open();
|
|
432
|
+
check_read_only();
|
|
433
|
+
if (!cf.is_valid()) throw RocksDBException("ColumnFamilyHandle is invalid.");
|
|
434
|
+
rocksdb::Slice key_slice(static_cast<std::string_view>(key));
|
|
435
|
+
const auto& opts = write_options ? write_options->options_ : default_write_options_->options_;
|
|
436
|
+
rocksdb::Status s = db_->Delete(opts, cf.cf_handle_, key_slice);
|
|
437
|
+
if (!s.ok()) throw RocksDBException("del_cf failed: " + s.ToString());
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
std::vector<std::string> list_column_families() {
|
|
441
|
+
check_db_open();
|
|
442
|
+
std::vector<std::string> names;
|
|
443
|
+
for (const auto& pair : cf_handles_) {
|
|
444
|
+
names.push_back(pair.first);
|
|
445
|
+
}
|
|
446
|
+
return names;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
std::shared_ptr<PyColumnFamilyHandle> create_column_family(const std::string& name, PyOptions* cf_py_options) {
|
|
450
|
+
check_db_open();
|
|
451
|
+
check_read_only();
|
|
452
|
+
if (cf_handles_.count(name)) {
|
|
453
|
+
throw RocksDBException("Column family '" + name + "' already exists.");
|
|
454
|
+
}
|
|
455
|
+
rocksdb::ColumnFamilyOptions cf_opts = cf_py_options ? cf_py_options->cf_options_ : opened_options_.cf_options_;
|
|
456
|
+
rocksdb::ColumnFamilyHandle* cf_handle;
|
|
457
|
+
rocksdb::Status s = db_->CreateColumnFamily(cf_opts, name, &cf_handle);
|
|
458
|
+
if (!s.ok()) throw RocksDBException("Failed to create column family '" + name + "': " + s.ToString());
|
|
459
|
+
|
|
460
|
+
auto new_handle = std::make_shared<PyColumnFamilyHandle>(cf_handle, name);
|
|
461
|
+
cf_handles_[name] = new_handle;
|
|
462
|
+
return new_handle;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
void drop_column_family(PyColumnFamilyHandle& cf_handle) {
|
|
466
|
+
check_db_open();
|
|
467
|
+
check_read_only();
|
|
468
|
+
if (!cf_handle.is_valid()) throw RocksDBException("ColumnFamilyHandle is invalid.");
|
|
469
|
+
if (cf_handle.get_name() == rocksdb::kDefaultColumnFamilyName) throw RocksDBException("Cannot drop the default column family.");
|
|
470
|
+
|
|
471
|
+
rocksdb::ColumnFamilyHandle* raw_handle = cf_handle.cf_handle_;
|
|
472
|
+
std::string cf_name = cf_handle.get_name();
|
|
473
|
+
|
|
474
|
+
rocksdb::Status s = db_->DropColumnFamily(raw_handle);
|
|
475
|
+
if (!s.ok()) throw RocksDBException("Failed to drop column family '" + cf_name + "': " + s.ToString());
|
|
476
|
+
|
|
477
|
+
s = db_->DestroyColumnFamilyHandle(raw_handle);
|
|
478
|
+
if (!s.ok()) throw RocksDBException("Dropped CF but failed to destroy handle: " + s.ToString());
|
|
479
|
+
|
|
480
|
+
cf_handles_.erase(cf_name);
|
|
481
|
+
cf_handle.cf_handle_ = nullptr;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
std::shared_ptr<PyColumnFamilyHandle> get_column_family(const std::string& name) {
|
|
485
|
+
check_db_open();
|
|
486
|
+
auto it = cf_handles_.find(name);
|
|
487
|
+
return (it == cf_handles_.end()) ? nullptr : it->second;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
std::shared_ptr<PyColumnFamilyHandle> get_default_cf() {
|
|
491
|
+
check_db_open();
|
|
492
|
+
return get_column_family(rocksdb::kDefaultColumnFamilyName);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
std::shared_ptr<PyRocksDBIterator> new_cf_iterator(PyColumnFamilyHandle& cf_handle, std::shared_ptr<PyReadOptions> read_options = nullptr) {
|
|
496
|
+
check_db_open();
|
|
497
|
+
if (!cf_handle.is_valid()) throw RocksDBException("ColumnFamilyHandle is invalid.");
|
|
498
|
+
|
|
499
|
+
const auto& opts = read_options ? read_options->options_ : default_read_options_->options_;
|
|
500
|
+
rocksdb::Iterator* raw_iter = db_->NewIterator(opts, cf_handle.cf_handle_);
|
|
501
|
+
{
|
|
502
|
+
std::lock_guard<std::mutex> lock(active_iterators_mutex_);
|
|
503
|
+
active_rocksdb_iterators_.insert(raw_iter);
|
|
504
|
+
}
|
|
505
|
+
return std::make_shared<PyRocksDBIterator>(raw_iter, shared_from_this());
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// --- PyRocksDBIterator Method Implementations ---
|
|
510
|
+
PyRocksDBIterator::PyRocksDBIterator(rocksdb::Iterator* it, std::shared_ptr<PyRocksDB> parent_db)
|
|
511
|
+
: it_raw_ptr_(it), parent_db_ptr_(std::move(parent_db)) {
|
|
512
|
+
if (!it_raw_ptr_) {
|
|
513
|
+
throw RocksDBException("Failed to create iterator: null pointer received.");
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
PyRocksDBIterator::~PyRocksDBIterator() {
|
|
518
|
+
if (parent_db_ptr_) {
|
|
519
|
+
std::lock_guard<std::mutex> lock(parent_db_ptr_->active_iterators_mutex_);
|
|
520
|
+
if (it_raw_ptr_ && parent_db_ptr_->active_rocksdb_iterators_.count(it_raw_ptr_)) {
|
|
521
|
+
parent_db_ptr_->active_rocksdb_iterators_.erase(it_raw_ptr_);
|
|
522
|
+
delete it_raw_ptr_;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
it_raw_ptr_ = nullptr;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
void PyRocksDBIterator::check_parent_db_is_open() const {
|
|
529
|
+
if (!parent_db_ptr_ || parent_db_ptr_->is_closed_.load()) {
|
|
530
|
+
throw RocksDBException("Database is closed.");
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
bool PyRocksDBIterator::valid() { check_parent_db_is_open(); return it_raw_ptr_->Valid(); }
|
|
535
|
+
void PyRocksDBIterator::seek_to_first() { check_parent_db_is_open(); it_raw_ptr_->SeekToFirst(); }
|
|
536
|
+
void PyRocksDBIterator::seek_to_last() { check_parent_db_is_open(); it_raw_ptr_->SeekToLast(); }
|
|
537
|
+
void PyRocksDBIterator::seek(const py::bytes& key) { check_parent_db_is_open(); it_raw_ptr_->Seek(static_cast<std::string>(key)); }
|
|
538
|
+
void PyRocksDBIterator::next() { check_parent_db_is_open(); it_raw_ptr_->Next(); }
|
|
539
|
+
void PyRocksDBIterator::prev() { check_parent_db_is_open(); it_raw_ptr_->Prev(); }
|
|
540
|
+
|
|
541
|
+
py::object PyRocksDBIterator::key() {
|
|
542
|
+
check_parent_db_is_open();
|
|
543
|
+
if (it_raw_ptr_ && it_raw_ptr_->Valid()) {
|
|
544
|
+
return py::bytes(it_raw_ptr_->key().ToString());
|
|
545
|
+
}
|
|
546
|
+
return py::none();
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
py::object PyRocksDBIterator::value() {
|
|
550
|
+
check_parent_db_is_open();
|
|
551
|
+
if (it_raw_ptr_ && it_raw_ptr_->Valid()) {
|
|
552
|
+
return py::bytes(it_raw_ptr_->value().ToString());
|
|
553
|
+
}
|
|
554
|
+
return py::none();
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
void PyRocksDBIterator::check_status() {
|
|
558
|
+
check_parent_db_is_open();
|
|
559
|
+
if (it_raw_ptr_) {
|
|
560
|
+
rocksdb::Status s = it_raw_ptr_->status();
|
|
561
|
+
if (!s.ok()) throw RocksDBException("Iterator error: " + s.ToString());
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// --- PYBIND11 MODULE DEFINITION ---
|
|
566
|
+
PYBIND11_MODULE(_pyrex, m) {
|
|
567
|
+
m.doc() = R"doc(
|
|
568
|
+
A robust, high-performance Python wrapper for the RocksDB key-value store.
|
|
569
|
+
|
|
570
|
+
This module provides two main classes for interacting with RocksDB:
|
|
571
|
+
1. PyRocksDB: A simple interface for standard key-value operations on a
|
|
572
|
+
database with a single (default) column family.
|
|
573
|
+
2. PyRocksDBExtended: An advanced interface that inherits from PyRocksDB and
|
|
574
|
+
adds full support for creating, managing, and using multiple Column Families.
|
|
575
|
+
)doc";
|
|
576
|
+
|
|
577
|
+
// 1. Create the Python exception type and give it a docstring.
|
|
578
|
+
static py::exception<RocksDBException> rocksdb_exception(m, "RocksDBException", PyExc_RuntimeError);
|
|
579
|
+
rocksdb_exception.doc() = R"doc(
|
|
580
|
+
Custom exception raised for RocksDB-specific operational errors.
|
|
581
|
+
|
|
582
|
+
This exception is raised when a RocksDB operation fails for reasons
|
|
583
|
+
such as I/O errors, corruption, invalid arguments, or when an operation
|
|
584
|
+
is attempted on a closed database.
|
|
585
|
+
)doc";
|
|
586
|
+
|
|
587
|
+
// 2. Register a translator that maps the C++ exception to the Python one.
|
|
588
|
+
py::register_exception_translator([](std::exception_ptr p) {
|
|
589
|
+
try {
|
|
590
|
+
if (p) {
|
|
591
|
+
std::rethrow_exception(p);
|
|
592
|
+
}
|
|
593
|
+
} catch (const RocksDBException &e) {
|
|
594
|
+
// Use PyErr_SetString to set the Python error object correctly.
|
|
595
|
+
// 'rocksdb_exception' is a static variable and accessible without capture.
|
|
596
|
+
PyErr_SetString(rocksdb_exception.ptr(), e.what());
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
py::enum_<rocksdb::CompressionType>(m, "CompressionType", R"doc(
|
|
602
|
+
Enum for different compression types supported by RocksDB.
|
|
603
|
+
)doc")
|
|
604
|
+
.value("kNoCompression", rocksdb::kNoCompression, "No compression.")
|
|
605
|
+
.value("kSnappyCompression", rocksdb::kSnappyCompression, "Snappy compression (default).")
|
|
606
|
+
.value("kBZip2Compression", rocksdb::kBZip2Compression, "BZip2 compression.")
|
|
607
|
+
.value("kLZ4Compression", rocksdb::kLZ4Compression, "LZ4 compression.")
|
|
608
|
+
.value("kLZ4HCCompression", rocksdb::kLZ4HCCompression, "LZ4HC (high compression) compression.")
|
|
609
|
+
.value("kXpressCompression", rocksdb::kXpressCompression, "Xpress compression.")
|
|
610
|
+
.value("kZSTD", rocksdb::kZSTD, "Zstandard compression.");
|
|
611
|
+
|
|
612
|
+
py::class_<PyReadOptions, std::shared_ptr<PyReadOptions>>(m, "ReadOptions", R"doc(
|
|
613
|
+
Configuration options for read operations (Get, Iterator).
|
|
614
|
+
)doc")
|
|
615
|
+
.def(py::init<>(), "Constructs a new ReadOptions object with default settings.")
|
|
616
|
+
.def_property("fill_cache", &PyReadOptions::get_fill_cache, &PyReadOptions::set_fill_cache, "If True, reads will fill the block cache. Defaults to True.")
|
|
617
|
+
.def_property("verify_checksums", &PyReadOptions::get_verify_checksums, &PyReadOptions::set_verify_checksums, "If True, all data read from underlying storage will be verified against its checksums. Defaults to True.");
|
|
618
|
+
|
|
619
|
+
py::class_<PyWriteOptions, std::shared_ptr<PyWriteOptions>>(m, "WriteOptions", R"doc(
|
|
620
|
+
Configuration options for write operations (Put, Delete, Write).
|
|
621
|
+
)doc")
|
|
622
|
+
.def(py::init<>(), "Constructs a new WriteOptions object with default settings.")
|
|
623
|
+
.def_property("sync", &PyWriteOptions::get_sync, &PyWriteOptions::set_sync, "If True, the write will be flushed from the OS buffer cache before the write is considered complete. Defaults to False.")
|
|
624
|
+
.def_property("disable_wal", &PyWriteOptions::get_disable_wal, &PyWriteOptions::set_disable_wal, "If True, writes will not be written to the Write Ahead Log. Defaults to False.");
|
|
625
|
+
|
|
626
|
+
py::class_<PyOptions>(m, "PyOptions", R"doc(
|
|
627
|
+
Configuration options for opening and managing a RocksDB database.
|
|
628
|
+
|
|
629
|
+
This class wraps `rocksdb::Options` and `rocksdb::ColumnFamilyOptions`
|
|
630
|
+
to provide a convenient way to configure database behavior from Python.
|
|
631
|
+
)doc")
|
|
632
|
+
.def(py::init<>(), "Constructs a new PyOptions object with default settings.")
|
|
633
|
+
.def_property("create_if_missing", &PyOptions::get_create_if_missing, &PyOptions::set_create_if_missing, "If True, the database will be created if it is missing. Defaults to True.")
|
|
634
|
+
.def_property("error_if_exists", &PyOptions::get_error_if_exists, &PyOptions::set_error_if_exists, "If True, an error is raised if the database already exists. Defaults to False.")
|
|
635
|
+
.def_property("max_open_files", &PyOptions::get_max_open_files, &PyOptions::set_max_open_files, "Number of open files that can be used by the DB. Defaults to -1 (unlimited).")
|
|
636
|
+
.def_property("write_buffer_size", &PyOptions::get_write_buffer_size, &PyOptions::set_write_buffer_size, "Amount of data to build up in a memory buffer (MemTable) before flushing. Defaults to 64MB.")
|
|
637
|
+
.def_property("compression", &PyOptions::get_compression, &PyOptions::set_compression, "The compression type to use for sst files. Defaults to Snappy.")
|
|
638
|
+
.def_property("max_background_jobs", &PyOptions::get_max_background_jobs, &PyOptions::set_max_background_jobs, "Maximum number of concurrent background jobs (compactions and flushes).")
|
|
639
|
+
.def("increase_parallelism", &PyOptions::increase_parallelism, py::arg("total_threads"), R"doc(
|
|
640
|
+
Increases RocksDB's parallelism by tuning background threads.
|
|
641
|
+
|
|
642
|
+
Args:
|
|
643
|
+
total_threads (int): The total number of background threads to use.
|
|
644
|
+
)doc", py::call_guard<py::gil_scoped_release>())
|
|
645
|
+
.def("optimize_for_small_db", &PyOptions::optimize_for_small_db, R"doc(
|
|
646
|
+
Optimizes RocksDB for small databases by reducing memory and CPU consumption.
|
|
647
|
+
)doc", py::call_guard<py::gil_scoped_release>())
|
|
648
|
+
.def("use_block_based_bloom_filter", &PyOptions::use_block_based_bloom_filter, py::arg("bits_per_key") = 10.0, R"doc(
|
|
649
|
+
Enables a Bloom filter for block-based tables to speed up 'Get' operations.
|
|
650
|
+
|
|
651
|
+
Args:
|
|
652
|
+
bits_per_key (float): The number of bits per key for the Bloom filter.
|
|
653
|
+
Higher values reduce false positives but increase memory usage.
|
|
654
|
+
)doc", py::call_guard<py::gil_scoped_release>())
|
|
655
|
+
.def_property("cf_write_buffer_size", &PyOptions::get_cf_write_buffer_size, &PyOptions::set_cf_write_buffer_size, "Default write_buffer_size for newly created Column Families.")
|
|
656
|
+
.def_property("cf_compression", &PyOptions::get_cf_compression, &PyOptions::set_cf_compression, "Default compression type for newly created Column Families.");
|
|
657
|
+
|
|
658
|
+
py::class_<PyColumnFamilyHandle, std::shared_ptr<PyColumnFamilyHandle>>(m, "ColumnFamilyHandle", R"doc(
|
|
659
|
+
Represents a handle to a RocksDB Column Family.
|
|
660
|
+
|
|
661
|
+
This object is used to perform operations on a specific data partition
|
|
662
|
+
within a `PyRocksDBExtended` instance.
|
|
663
|
+
)doc")
|
|
664
|
+
.def_property_readonly("name", &PyColumnFamilyHandle::get_name, "The name of this column family.")
|
|
665
|
+
.def("is_valid", &PyColumnFamilyHandle::is_valid, "Checks if the handle is still valid (i.e., has not been dropped).");
|
|
666
|
+
|
|
667
|
+
py::class_<PyWriteBatch>(m, "PyWriteBatch", R"doc(
|
|
668
|
+
A batch of write operations (Put, Delete) that can be applied atomically.
|
|
669
|
+
)doc")
|
|
670
|
+
.def(py::init<>(), "Constructs an empty write batch.")
|
|
671
|
+
.def("put", &PyWriteBatch::put, py::arg("key"), py::arg("value"), "Adds a key-value pair to the batch for the default column family.")
|
|
672
|
+
.def("put_cf", &PyWriteBatch::put_cf, py::arg("cf_handle"), py::arg("key"), py::arg("value"), "Adds a key-value pair to the batch for a specific column family.")
|
|
673
|
+
.def("delete", &PyWriteBatch::del, py::arg("key"), "Adds a key deletion to the batch for the default column family.")
|
|
674
|
+
.def("delete_cf", &PyWriteBatch::del_cf, py::arg("cf_handle"), py::arg("key"), "Adds a key deletion to the batch for a specific column family.")
|
|
675
|
+
.def("merge", &PyWriteBatch::merge, py::arg("key"), py::arg("value"), "Adds a merge operation to the batch for the default column family.")
|
|
676
|
+
.def("merge_cf", &PyWriteBatch::merge_cf, py::arg("cf_handle"), py::arg("key"), py::arg("value"), "Adds a merge operation to the batch for a specific column family.")
|
|
677
|
+
.def("clear", &PyWriteBatch::clear, "Clears all operations from the batch.");
|
|
678
|
+
|
|
679
|
+
py::class_<PyRocksDBIterator, std::shared_ptr<PyRocksDBIterator>>(m, "PyRocksDBIterator", R"doc(
|
|
680
|
+
An iterator for traversing key-value pairs in a RocksDB database.
|
|
681
|
+
)doc")
|
|
682
|
+
.def("valid", &PyRocksDBIterator::valid, "Returns True if the iterator is currently positioned at a valid entry.", py::call_guard<py::gil_scoped_release>())
|
|
683
|
+
.def("seek_to_first", &PyRocksDBIterator::seek_to_first, "Positions the iterator at the first key.", py::call_guard<py::gil_scoped_release>())
|
|
684
|
+
.def("seek_to_last", &PyRocksDBIterator::seek_to_last, "Positions the iterator at the last key.", py::call_guard<py::gil_scoped_release>())
|
|
685
|
+
.def("seek", &PyRocksDBIterator::seek, py::arg("key"), "Positions the iterator at the first key >= the given key.", py::call_guard<py::gil_scoped_release>())
|
|
686
|
+
.def("next", &PyRocksDBIterator::next, "Moves the iterator to the next entry.", py::call_guard<py::gil_scoped_release>())
|
|
687
|
+
.def("prev", &PyRocksDBIterator::prev, "Moves the iterator to the previous entry.", py::call_guard<py::gil_scoped_release>())
|
|
688
|
+
.def("key", &PyRocksDBIterator::key, "Returns the current key as bytes, or None if invalid.")
|
|
689
|
+
.def("value", &PyRocksDBIterator::value, "Returns the current value as bytes, or None if invalid.")
|
|
690
|
+
.def("check_status", &PyRocksDBIterator::check_status, "Raises RocksDBException if an error occurred during iteration.", py::call_guard<py::gil_scoped_release>());
|
|
691
|
+
|
|
692
|
+
py::class_<PyRocksDB, std::shared_ptr<PyRocksDB>>(m, "PyRocksDB", R"doc(
|
|
693
|
+
A Python wrapper for RocksDB providing simple key-value storage.
|
|
694
|
+
|
|
695
|
+
This class interacts exclusively with the 'default' column family.
|
|
696
|
+
For multi-column-family support, use `PyRocksDBExtended`.
|
|
697
|
+
)doc")
|
|
698
|
+
.def(py::init<const std::string&, PyOptions*, bool>(),
|
|
699
|
+
py::arg("path"),
|
|
700
|
+
py::arg("options") = nullptr,
|
|
701
|
+
py::arg("read_only") = false,
|
|
702
|
+
R"doc(
|
|
703
|
+
Opens a RocksDB database at the specified path.
|
|
704
|
+
|
|
705
|
+
Args:
|
|
706
|
+
path (str): The file system path to the database.
|
|
707
|
+
options (PyOptions, optional): Custom options for configuration.
|
|
708
|
+
read_only (bool, optional): If True, opens the database in read-only mode.
|
|
709
|
+
Defaults to False.
|
|
710
|
+
)doc", py::call_guard<py::gil_scoped_release>())
|
|
711
|
+
.def("put", &PyRocksDB::put, py::arg("key"), py::arg("value"), py::arg("write_options") = nullptr, "Inserts a key-value pair.", py::call_guard<py::gil_scoped_release>())
|
|
712
|
+
.def("get", &PyRocksDB::get, py::arg("key"), py::arg("read_options") = nullptr, "Retrieves the value for a key.")
|
|
713
|
+
.def("delete", &PyRocksDB::del, py::arg("key"), py::arg("write_options") = nullptr, "Deletes a key.", py::call_guard<py::gil_scoped_release>())
|
|
714
|
+
.def("write", &PyRocksDB::write, py::arg("write_batch"), py::arg("write_options") = nullptr, "Applies a batch of operations atomically.", py::call_guard<py::gil_scoped_release>())
|
|
715
|
+
.def("new_iterator", &PyRocksDB::new_iterator, py::arg("read_options") = nullptr, "Creates a new iterator.", py::keep_alive<0, 1>())
|
|
716
|
+
.def("get_options", &PyRocksDB::get_options, "Returns the options the database was opened with.")
|
|
717
|
+
.def_property("default_read_options", &PyRocksDB::get_default_read_options, &PyRocksDB::set_default_read_options, "The default ReadOptions used for get and iterator operations.")
|
|
718
|
+
.def_property("default_write_options", &PyRocksDB::get_default_write_options, &PyRocksDB::set_default_write_options, "The default WriteOptions used for put, delete, and write operations.")
|
|
719
|
+
.def("close", &PyRocksDB::close, "Closes the database, releasing resources and the lock.", py::call_guard<py::gil_scoped_release>())
|
|
720
|
+
.def("__enter__", [](PyRocksDB &db) -> PyRocksDB& { return db; })
|
|
721
|
+
.def("__exit__", [](PyRocksDB &db, py::object /* type */, py::object /* value */, py::object /* traceback */) {
|
|
722
|
+
db.close();
|
|
723
|
+
});
|
|
724
|
+
|
|
725
|
+
py::class_<PyRocksDBExtended, PyRocksDB, std::shared_ptr<PyRocksDBExtended>>(m, "PyRocksDBExtended", R"doc(
|
|
726
|
+
An advanced Python wrapper for RocksDB with full Column Family support.
|
|
727
|
+
)doc")
|
|
728
|
+
.def(py::init<const std::string&, PyOptions*, bool>(),
|
|
729
|
+
py::arg("path"),
|
|
730
|
+
py::arg("options") = nullptr,
|
|
731
|
+
py::arg("read_only") = false,
|
|
732
|
+
R"doc(
|
|
733
|
+
Opens or creates a RocksDB database with Column Family support.
|
|
734
|
+
|
|
735
|
+
Args:
|
|
736
|
+
path (str): The file system path to the database.
|
|
737
|
+
options (PyOptions, optional): Custom options for configuration.
|
|
738
|
+
read_only (bool, optional): If True, opens the database in read-only mode.
|
|
739
|
+
Defaults to False.
|
|
740
|
+
)doc", py::call_guard<py::gil_scoped_release>())
|
|
741
|
+
|
|
742
|
+
.def("put_cf", &PyRocksDBExtended::put_cf, py::arg("cf_handle"), py::arg("key"), py::arg("value"), py::arg("write_options") = nullptr, "Inserts a key-value pair into a specific column family.", py::call_guard<py::gil_scoped_release>())
|
|
743
|
+
.def("get_cf", &PyRocksDBExtended::get_cf, py::arg("cf_handle"), py::arg("key"), py::arg("read_options") = nullptr, "Retrieves the value for a key from a specific column family.")
|
|
744
|
+
.def("delete_cf", &PyRocksDBExtended::del_cf, py::arg("cf_handle"), py::arg("key"), py::arg("write_options") = nullptr, "Deletes a key from a specific column family.", py::call_guard<py::gil_scoped_release>())
|
|
745
|
+
.def("list_column_families", &PyRocksDBExtended::list_column_families, "Lists the names of all existing column families.")
|
|
746
|
+
.def("create_column_family", &PyRocksDBExtended::create_column_family, py::arg("name"), py::arg("cf_options") = nullptr, "Creates a new column family.", py::call_guard<py::gil_scoped_release>())
|
|
747
|
+
.def("drop_column_family", &PyRocksDBExtended::drop_column_family, py::arg("cf_handle"), "Drops a column family.", py::call_guard<py::gil_scoped_release>())
|
|
748
|
+
.def("new_cf_iterator", &PyRocksDBExtended::new_cf_iterator, py::arg("cf_handle"), py::arg("read_options") = nullptr, "Creates a new iterator for a specific column family.", py::keep_alive<0, 1>())
|
|
749
|
+
.def("get_column_family", &PyRocksDBExtended::get_column_family, py::arg("name"), "Retrieves a ColumnFamilyHandle by its name.")
|
|
750
|
+
.def_property_readonly("default_cf", &PyRocksDBExtended::get_default_cf, "Returns the handle for the default column family.");
|
|
751
|
+
}
|
|
752
|
+
|
|
Binary file
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyrex-rocksdb
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: A fast RocksDB wrapper for Python using pybind11.
|
|
5
|
+
Author-email: Charilaos Mylonas <mylonas.charilaos@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/mylonasc/pyrex
|
|
7
|
+
Project-URL: Repository, https://github.com/mylonasc/pyrex
|
|
8
|
+
Keywords: rocksdb,database,key-value,pybind11
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
17
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
18
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
19
|
+
Classifier: Development Status :: 3 - Alpha
|
|
20
|
+
Classifier: Intended Audience :: Developers
|
|
21
|
+
Classifier: Topic :: Database
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest; extra == "dev"
|
|
27
|
+
Requires-Dist: sphinx; extra == "dev"
|
|
28
|
+
Requires-Dist: sphinx-rtd-theme; extra == "dev"
|
|
29
|
+
Requires-Dist: cibuildwheel; extra == "dev"
|
|
30
|
+
Requires-Dist: pybind11-stubgen~=2.5.4; extra == "dev"
|
|
31
|
+
Requires-Dist: mypy~=1.10; extra == "dev"
|
|
32
|
+
Requires-Dist: twine; extra == "dev"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
[](https://pypi.org/project/pyrex-rocksdb/)
|
|
36
|
+
[](https://img.shields.io/pypi/pyversions/pyrex-rocksdb/)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+

|
|
40
|
+
|
|
41
|
+
# Installation
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# pyrex-rocksdb
|
|
45
|
+
A python wrapper for the original (C++) version of RocksDB.
|
|
46
|
+
|
|
47
|
+
Currently MacOS and Linux wheels are available.
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
For linux systems, wheels are provided and can be installed from pypi using:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install pyrex-rocksdb
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
For Windows and MacOS I have built an earlier version of the library.
|
|
58
|
+
I will re-build once I include certain other important features in the API that are not yet implemented.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## Motivation
|
|
63
|
+
|
|
64
|
+
This library is intended for providing a fast, write-optimized, in-process key value (KV) store in python. Therefore the "big brothers" of the database are the likes of MongoDB and Cassandra. The difference is that you don't need a separate server to run this (hence "in-process") and it is designed to be fairly portable.
|
|
65
|
+
|
|
66
|
+
RocksDB, which is the underlying storage engine of this database, is an LSM-tree engine. An LSM-tree is different from the ballanced tree index databases (e.g., [B-tree](https://en.wikipedia.org/wiki/B-tree)/ and [B+tree](https://en.wikipedia.org/wiki/B%2B_tree) databases). LSM-tree databases offer very high write throughputs and better space efficiency. See more about the motivation for LSM-tree databases (and RocksDB in particular) in [this talk](https://www.youtube.com/watch?v=V_C-T5S-w8g).
|
|
67
|
+
|
|
68
|
+
### LSM-tree + SSTable engine basics
|
|
69
|
+
To understand where `pyrex` provides efficiency gains, it is important to understand some basics about the underlying `RocksDB` engine.
|
|
70
|
+
|
|
71
|
+
RocksDB and LevelDB are **key-value stores** with a **Log-Structured Merge-tree (LSM-tree)** architecture.
|
|
72
|
+
|
|
73
|
+
The key components of LSM-tree architectures are
|
|
74
|
+
* A **MemTable** that stores in-memory sorted data
|
|
75
|
+
* A set of **Sorted-String tables (SSTables)** which are immutable sorted files on disk where data from the MemTable is flushed
|
|
76
|
+
* The process of **Compaction**, which is a background process that merges the SSTables to remove redundant data and keep read performance high.
|
|
77
|
+
|
|
78
|
+
In such databases, fast writes create many small, sorted data files called SSTables. To prevent reads from slowing down by checking too many files, a background process called compaction merges these SSTables together. This process organizes the data into levels, where newer, overlapping files sit in Level 0 and are progressively merged into higher levels (Level 1, Level 2, etc.). Each higher level contains larger, non-overlapping files, which ensures that finding a key remains efficient and old data is purged to save space. There are several optimizations and configurations possible for these processes (configurability and "pluggability" are commonly cited RocksDB advantages).
|
|
79
|
+
|
|
80
|
+
However the main big advantage of RocksDB over LevelDB is its **multi-threaded compaction support** (LevelDB supports only single threaded compaction, which comes with significant performance limitations).
|
|
81
|
+
There are several other configurability advantages RocksDB offers over LevelDB. For a more elaborate enumaration of RocksDB advantages please refer to the [RocksDB wiki](https://github.com/facebook/rocksdb/wiki/Features-Not-in-LevelDB).
|
|
82
|
+
|
|
83
|
+
Not all are currently supported by the `pyrex` API, but I'm working on supporting more of them. Feel free to open an issue if there is a feature you want to see (or open a pull request).
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
## Example usage:
|
|
87
|
+
|
|
88
|
+
Here is a simple example showing the usage of put/get in the DB:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
import pyrex
|
|
92
|
+
import os
|
|
93
|
+
import shutil
|
|
94
|
+
|
|
95
|
+
DB_PATH = "./test_rocksdb_minimal"
|
|
96
|
+
|
|
97
|
+
with pyrex.PyRocksDB(DB_PATH) as db:
|
|
98
|
+
db.put(b"my_key", b"my_value")
|
|
99
|
+
retrieved_value = db.get(b"my_key")
|
|
100
|
+
|
|
101
|
+
print(f"Retrieved: {retrieved_value.decode()}") # Output: Retrieved: my_value
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
for more examples check the relevant folder and the documentation.
|
|
106
|
+
|
|
107
|
+
## Installation
|
|
108
|
+
|
|
109
|
+
<details>
|
|
110
|
+
<summary>Note on CICD</summary>
|
|
111
|
+
The wheels provided are not completely platform-independent at the moment.
|
|
112
|
+
I heavily rely on github actions to develop since I don't own mac or windows machines.
|
|
113
|
+
The CICD workflow for package builds is under development A windows/macos/linux build was successful, but further development is needed.
|
|
114
|
+
</details>
|
|
115
|
+
|
|
116
|
+
## Benchmarks
|
|
117
|
+
|
|
118
|
+
`Pyrex` was benchmarked against [plyvel](https://github.com/wbolster/plyvel) and [lmdb](https://github.com/jnwatson/py-lmdb/) (which is based on a B+tree -- based architecture and relies on OS's block cache).
|
|
119
|
+
|
|
120
|
+
Initial benchmarks are promissing and to be reported soon.
|
|
121
|
+
|
|
122
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
pyrex/__init__.py,sha256=zshmllNLYDo3znwRaknHeAd4obEkjS3jK6UkcXAsvw4,831
|
|
2
|
+
pyrex/_pyrex.cpp,sha256=rTj0pKRt3M1ttRYOhN53kTCoRnetmnuS93c9MMdtGvQ,38172
|
|
3
|
+
pyrex/_pyrex.cpython-38-x86_64-linux-gnu.so,sha256=Rj_Xj81XrlXZoNGuvuM6aEM_NU0eWxXzff3cjOCqka0,15611121
|
|
4
|
+
pyrex_rocksdb.libs/libbz2-b428631a.so.1.0.8,sha256=yNT-YG4YnyKLDORihi0Csabys6xeMBmxfwShJYrJsQo,79185
|
|
5
|
+
pyrex_rocksdb.libs/libgcc_s-0cd532bd.so.1,sha256=yPk0-VjyKzucjnkP3mvC0vVaua6Ln17qZUJbICcXgtA,181737
|
|
6
|
+
pyrex_rocksdb.libs/liblz4-af355be2.so.1.10.0,sha256=1l7gNbbgRYTtETZg2ZQzjg-mutZ3wSlpm4wQP1By-3s,155537
|
|
7
|
+
pyrex_rocksdb.libs/libsnappy-f36b9e28.so.1.2.2,sha256=TqZ4Uf1Jn2ParASUMKhTY55L7owWh0xVh9Rhn3Kraj0,65233
|
|
8
|
+
pyrex_rocksdb.libs/libstdc++-5d72f927.so.6.0.33,sha256=fogxHsmB1_D6C-a_-uHh8Ei_6Qh52a8vLlicJRM3ehk,3562401
|
|
9
|
+
pyrex_rocksdb.libs/liburing-20646edb.so.2.9,sha256=inggmvA5uM-DUCVKf_oWwVjf8TOj_mvc6SzQvo2Od4c,27825
|
|
10
|
+
pyrex_rocksdb.libs/libzstd-f3c21b15.so.1.5.7,sha256=bKuFOLGu-ETlWtit2osjhuOZLQPPp-mW1vIcgxkSIrU,728201
|
|
11
|
+
pyrex_rocksdb-0.1.4.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
12
|
+
pyrex_rocksdb-0.1.4.dist-info/METADATA,sha256=xT3J-0H5XWUPpu2ZRZZgfLWfHUmN7eCVTJRo7AbBu9I,6077
|
|
13
|
+
pyrex_rocksdb-0.1.4.dist-info/WHEEL,sha256=AtKzrIIwO6LyEQPNa-CKogjoLSeXFnST8-hqmpwwZQA,110
|
|
14
|
+
pyrex_rocksdb-0.1.4.dist-info/top_level.txt,sha256=Hyct9jOureNtYbpQ0AUae8uzaC--sgtHGo5Kevyspgg,14
|
|
15
|
+
pyrex_rocksdb-0.1.4.dist-info/RECORD,,
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|