https://github.com/trk54ylmz/rocksdb-py
Python bindings for RocksDB written in Rust.
https://github.com/trk54ylmz/rocksdb-py
pyo3 python python3 rocksdb rocksdb-client rust
Last synced: 26 days ago
JSON representation
Python bindings for RocksDB written in Rust.
- Host: GitHub
- URL: https://github.com/trk54ylmz/rocksdb-py
- Owner: trK54Ylmz
- License: mit
- Created: 2022-02-03T10:10:18.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-03-23T10:07:49.000Z (3 months ago)
- Last Synced: 2025-05-06T20:12:34.961Z (about 1 month ago)
- Topics: pyo3, python, python3, rocksdb, rocksdb-client, rust
- Language: Rust
- Homepage:
- Size: 175 KB
- Stars: 21
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
## rocksdb-py
Python bindings for RocksDB written in Rust.
### Features
* Get, set, delete, multi get
* Destroy
* Batch write
* Database iterator
* Read options### Install
To install a wheel from PyPI,
```bash
pip install --upgrade rocksdb-py
```or if you want to build a wheel, see [build](https://github.com/trK54Ylmz/rocksdb-py#Build).
### Usage
#### Open database
Open a database with default options.
```python
import rocksdbpydb = rocksdbpy.open_default('/tmp/rocksdb')
```Open a database with the specified options.
```python
opts = Option()
opts.create_if_missing(True)db = rocksdbpy.open('/tmp/rocksdb', opts)
```Open a database with TTL compaction filter.
```python
opts = Option()
opts.create_if_missing(True)db = rocksdbpy.open_with_ttl('/tmp/rocksdb', 5, opts)
```Destroy the database and it's files.
```python
rocksdbpy.destroy('/tmp/rocksdb')
```Close active database and release lock.
```python
db.close()
```#### Simple read, set and delete
Set records by key and value.
```python
db.set(b'key', b'value')
```Get a value associated with a key.
```python
value = db.get(b'key')
```Remove existing records by key.
```python
db.delete(b'key')
```#### Batch write, database iterator and flush
Set database entries for list of key and values as a batch.
```python
from rocksdbpy import WriteBatchbatch = WriteBatch()
batch.add(b'first', b'1')
batch.add(b'second', b'2')db.write(batch)
```Extra operations for the batch.
```python
batch.delete(b'first')batch.clear()
size = batch.len()
```Return a heap-allocated iterator over the contents of the database.
```python
iterator = db.iterator()iterator = db.iterator(mode='end')
iterator = db.iterator(mode='from', key=b'test')
iterator = db.iterator(mode='from', key=b'test', direction=-1)
for key, value in iterator:
print(key, value)
```Flush database memtables to SST files on the disk using default options.
```python
db.flush()
```#### Read options
Set database read options.
```python
opts = Option()opts.create_if_missing(True)
opts.set_max_open_files(10)
opts.set_use_fsync(True)
opts.set_bytes_per_sync(1024 * 1024)
# and more
```### Build
You can build PIP package by using `maturin`. The example below is created for MacOS,
```bash
$ git clone https://github.com/trk54ylmz/rocksdb-py.git
$ cd rocksdb-py
$ maturin build
$ pip3 install ./target/wheels/rocksdb_py-0.0.1-cp39-cp39-macosx_11_0_arm64.whl
```