A lightweight Python wrapper for RocksDB using CFFI
Project description
RockStore
A lightweight Python wrapper for RocksDB using CFFI.
Overview
RockStore provides a simple, Pythonic interface to RocksDB, Facebook's persistent key-value store. It uses CFFI for efficient native library bindings and supports both binary and string data operations.
Features
- Simple API: Easy-to-use Python interface for RocksDB operations
- Binary & String Support: Work with both raw bytes and UTF-8 strings
- Context Manager: Automatic resource management with
withstatements - Configurable Options: Customize compression, buffer sizes, and more
- Read-Only Mode: Open databases in read-only mode for safe concurrent access
- Cross-Platform: Works on macOS, Linux, and Windows
Installation
Prerequisites
First, install RocksDB on your system:
macOS (using Homebrew):
brew install rocksdb
Ubuntu/Debian:
sudo apt-get install librocksdb-dev
CentOS/RHEL/Fedora:
sudo yum install rocksdb-devel
# or for newer versions:
sudo dnf install rocksdb-devel
Windows:
- Download pre-built RocksDB binaries or build from source
- Ensure
rocksdb.dllis in your PATH
Install RockStore
pip install rockstore
Quick Start
Basic Usage
from rockstore import RockStore
# Open a database
db = RockStore('/path/to/database')
# Store and retrieve binary data
db.put(b'key1', b'value1')
value = db.get(b'key1')
print(value) # b'value1'
# Store and retrieve string data
db.put_string('name', 'Alice')
name = db.get_string('name')
print(name) # 'Alice'
# Delete data
db.delete_string('name')
# Clean up
db.close()
Using Context Manager (Recommended)
from rockstore import open_database
with open_database('/path/to/database') as db:
db.put_string('hello', 'world')
value = db.get_string('hello')
print(value) # 'world'
# Database is automatically closed
Getting All Data
with open_database('/path/to/database') as db:
db.put(b'key1', b'value1')
db.put(b'key2', b'value2')
# Get all key-value pairs
all_data = db.get_all()
for key, value in all_data.items():
print(f"{key} -> {value}")
Configuration Options
from rockstore import RockStore
# Create database with custom options
options = {
'create_if_missing': True,
'compression_type': 'lz4_compression',
'write_buffer_size': 64 * 1024 * 1024, # 64MB
'max_open_files': 1000
}
db = RockStore('/path/to/database', options=options)
Available Options
create_if_missing(bool): Create database if it doesn't exist (default: True)read_only(bool): Open database in read-only mode (default: False)compression_type(str): Compression algorithm - 'no_compression', 'snappy_compression', 'zlib_compression', 'bz2_compression', 'lz4_compression', 'lz4hc_compression', 'xpress_compression', 'zstd_compression' (default: 'snappy_compression')write_buffer_size(int): Write buffer size in bytes (default: 64MB)max_open_files(int): Maximum number of open files (default: 1000)
Per-Operation Options
# Synchronous write (forces immediate disk write)
db.put(b'key', b'value', sync=True)
# Read without caching
value = db.get(b'key', fill_cache=False)
# Synchronous delete
db.delete(b'key', sync=True)
API Reference
RockStore Class
Constructor
RockStore(path, options=None)
Methods
Binary Operations:
put(key: bytes, value: bytes, sync: bool = False)- Store binary dataget(key: bytes, fill_cache: bool = True) -> bytes | None- Retrieve binary datadelete(key: bytes, sync: bool = False)- Delete binary data
String Operations:
put_string(key: str, value: str, sync: bool = False)- Store string dataget_string(key: str, fill_cache: bool = True) -> str | None- Retrieve string datadelete_string(key: str, sync: bool = False)- Delete string data
Bulk Operations:
get_all(fill_cache: bool = True) -> dict[bytes, bytes]- Get all key-value pairs
Resource Management:
close()- Close the database- Context manager support (
withstatement)
Context Manager
open_database(path, options=None) -> RockStore
Requirements
- Python 3.8+
- CFFI >= 1.15.0
- RocksDB library installed on system
Development
Running Tests
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=rockstore
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rockstore-0.1.0.tar.gz.
File metadata
- Download URL: rockstore-0.1.0.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43e6800613becd8840bc0ebd95b39bec40a803c6534775d734c3aa01118d9f75
|
|
| MD5 |
1564d7db142e61eae7c9db64fe99a766
|
|
| BLAKE2b-256 |
663b9da01f3bab346fcb9e5e7e4997b15f39b7ce0197b5f29657a713947f09a4
|
File details
Details for the file rockstore-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rockstore-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72e4bc41c21e958b7426b82537eb22ba7513713adaff3ceb865f226789c0a5e1
|
|
| MD5 |
52a6e0af3f9f7b0e8b49cd723b48b1a9
|
|
| BLAKE2b-256 |
0445fc2d3f4e61675d676d0ba36655e8c409e9f233be2425c87a6d6996c84ffd
|