API Reference

Class Docs

class pyrex.PyRocksDB

Bases: pybind11_object

A Python wrapper for RocksDB providing simple key-value storage.

This class interacts exclusively with the ‘default’ column family. For multi-column-family support, use PyRocksDBExtended.

close(self: pyrex._pyrex.PyRocksDB) None

Closes the database, releasing resources and the lock.

delete(self: pyrex._pyrex.PyRocksDB, key: bytes) None

Deletes a key.

get(self: pyrex._pyrex.PyRocksDB, key: bytes) object

Retrieves the value for a key.

get_options(self: pyrex._pyrex.PyRocksDB) pyrex._pyrex.PyOptions

Returns the options the database was opened with.

new_iterator(self: pyrex._pyrex.PyRocksDB) pyrex._pyrex.PyRocksDBIterator

Creates a new iterator.

put(self: pyrex._pyrex.PyRocksDB, key: bytes, value: bytes) None

Inserts a key-value pair.

write(self: pyrex._pyrex.PyRocksDB, write_batch: pyrex._pyrex.PyWriteBatch) None

Applies a batch of operations atomically.

class pyrex.PyOptions

Bases: pybind11_object

Configuration options for opening and managing a RocksDB database.

This class wraps rocksdb::Options and rocksdb::ColumnFamilyOptions to provide a convenient way to configure database behavior from Python.

property cf_compression

Default compression type for newly created Column Families.

property cf_write_buffer_size

Default write_buffer_size for newly created Column Families.

property compression

The compression type to use for sst files. Defaults to Snappy.

property create_if_missing

If True, the database will be created if it is missing. Defaults to True.

property error_if_exists

If True, an error is raised if the database already exists. Defaults to False.

increase_parallelism(self: pyrex._pyrex.PyOptions, total_threads: SupportsInt) None

Increases RocksDB’s parallelism by tuning background threads.

Parameters:

total_threads (int) – The total number of background threads to use.

property max_background_jobs

Maximum number of concurrent background jobs (compactions and flushes).

property max_open_files

Number of open files that can be used by the DB. Defaults to -1 (unlimited).

optimize_for_small_db(self: pyrex._pyrex.PyOptions) None

Optimizes RocksDB for small databases by reducing memory and CPU consumption.

use_block_based_bloom_filter(self: pyrex._pyrex.PyOptions, bits_per_key: SupportsFloat = 10.0) None

Enables a Bloom filter for block-based tables to speed up ‘Get’ operations.

Parameters:

bits_per_key (float) – The number of bits per key for the Bloom filter. Higher values reduce false positives but increase memory usage.

property write_buffer_size

Amount of data to build up in a memory buffer (MemTable) before flushing. Defaults to 64MB.

class pyrex.PyWriteBatch

Bases: pybind11_object

A batch of write operations (Put, Delete) that can be applied atomically.

clear(self: pyrex._pyrex.PyWriteBatch) None

Clears all operations from the batch.

delete(self: pyrex._pyrex.PyWriteBatch, key: bytes) None

Adds a key deletion to the batch for the default column family.

delete_cf(self: pyrex._pyrex.PyWriteBatch, cf_handle: pyrex._pyrex.ColumnFamilyHandle, key: bytes) None

Adds a key deletion to the batch for a specific column family.

merge(self: pyrex._pyrex.PyWriteBatch, key: bytes, value: bytes) None

Adds a merge operation to the batch for the default column family.

merge_cf(self: pyrex._pyrex.PyWriteBatch, cf_handle: pyrex._pyrex.ColumnFamilyHandle, key: bytes, value: bytes) None

Adds a merge operation to the batch for a specific column family.

put(self: pyrex._pyrex.PyWriteBatch, key: bytes, value: bytes) None

Adds a key-value pair to the batch for the default column family.

put_cf(self: pyrex._pyrex.PyWriteBatch, cf_handle: pyrex._pyrex.ColumnFamilyHandle, key: bytes, value: bytes) None

Adds a key-value pair to the batch for a specific column family.

class pyrex.PyRocksDBIterator

Bases: pybind11_object

An iterator for traversing key-value pairs in a RocksDB database.

check_status(self: pyrex._pyrex.PyRocksDBIterator) None

Raises RocksDBException if an error occurred during iteration.

key(self: pyrex._pyrex.PyRocksDBIterator) object

Returns the current key as bytes, or None if invalid.

next(self: pyrex._pyrex.PyRocksDBIterator) None

Moves the iterator to the next entry.

prev(self: pyrex._pyrex.PyRocksDBIterator) None

Moves the iterator to the previous entry.

seek(self: pyrex._pyrex.PyRocksDBIterator, key: bytes) None

Positions the iterator at the first key >= the given key.

seek_to_first(self: pyrex._pyrex.PyRocksDBIterator) None

Positions the iterator at the first key.

seek_to_last(self: pyrex._pyrex.PyRocksDBIterator) None

Positions the iterator at the last key.

valid(self: pyrex._pyrex.PyRocksDBIterator) bool

Returns True if the iterator is currently positioned at a valid entry.

value(self: pyrex._pyrex.PyRocksDBIterator) object

Returns the current value as bytes, or None if invalid.