Skip to main content

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 focuses on clean binary data operations.

Features

  • Simple API: Easy-to-use Python interface for RocksDB operations
  • Binary Operations: Direct work with bytes for maximum performance
  • Context Manager: Automatic resource management with with statements
  • 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.dll is 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 (encode/decode manually)
db.put('name'.encode(), 'Alice'.encode())
name = db.get('name'.encode()).decode()
print(name)  # 'Alice'

# Delete data
db.delete(b'key1')

# Clean up
db.close()

Using Context Manager (Recommended)

from rockstore import open_database

with open_database('/path/to/database') as db:
    db.put(b'hello', b'world')
    value = db.get(b'hello')
    print(value)  # b'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}")

Working with Strings

# Helper functions for string encoding/decoding
def encode_string(s):
    return s.encode('utf-8')

def decode_bytes(b):
    return b.decode('utf-8')

with open_database('/path/to/database') as db:
    # Store string data
    db.put(encode_string('user:123'), encode_string('John Doe'))
    
    # Retrieve and decode
    user_data = db.get(encode_string('user:123'))
    if user_data:
        print(decode_bytes(user_data))  # 'John Doe'

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 data
  • get(key: bytes, fill_cache: bool = True) -> bytes | None - Retrieve binary data
  • delete(key: bytes, sync: bool = False) - Delete binary 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 (with statement)

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

rockstore-0.1.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rockstore-0.1.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file rockstore-0.1.1.tar.gz.

File metadata

  • Download URL: rockstore-0.1.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rockstore-0.1.1.tar.gz
Algorithm Hash digest
SHA256 69e365390fbb5e4b29c3c1e780befd91eb952f0948bd45b4b071b62138b44929
MD5 966981ace3623a72015fabb0a8748564
BLAKE2b-256 ee1b1fefe9651226951de73112d7b831642cf6e93c9006514c1077745e1ee647

See more details on using hashes here.

File details

Details for the file rockstore-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rockstore-0.1.1-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

Hashes for rockstore-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 34500d8d2cd4e3a4162c094af1eff2ed2aaf82b9d9c2fb7079f207a6e13f173c
MD5 bfc151fd629a315991acc756fbe29ca9
BLAKE2b-256 9a3c89a3187868c5c02fa56333379e87db6ef58744b2348fc44073f4bd0285b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page