Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GitHub

This documentation is part of the "Projects with Books" initiative at zenOSmosis.

The source code for this project is available on GitHub.

Python Direct Binding (Legacy)

Loading…

Python Direct Binding (Legacy)

Relevant source files

The simd-r-drive-py package represents the legacy approach to providing Python access to the DataStore engine. Unlike the modern WebSocket-based client, this binding uses PyO3 and maturin to expose the Rust DataStore struct directly as a native Python extension module. It is currently considered experimental/alpha and has been largely superseded by the network-based client due to threading and platform compatibility constraints experiments/bindings/python_(old_client)/README.md3-5

Purpose and Scope

The primary goal of the direct binding is to provide the absolute lowest latency possible by eliminating network overhead and using memory-mapped files directly within the Python process. It allows Python users to interact with the append-only storage engine as if it were a native Python object, supporting zero-copy reads via Python’s memoryview interface experiments/bindings/python_(old_client)/README.md7

Status and Limitations

Sources: experiments/bindings/python_(old_client)/README.md3-39 experiments/bindings/python_(old_client)/pyproject.toml3-11

System Architecture

The direct binding bridges the Python interpreter to the Rust DataStore using a thin wrapper layer defined in the module entry point. The Rust simd-r-drive dependency is compiled with the expose-internal-api feature to allow the binding access to internal structures like EntryHandle experiments/bindings/python_(old_client)/Cargo.toml15-16

Data Flow: Python to Rust Engine

The following diagram illustrates how a Python call traverses the binding layer to reach the core storage engine.

Binding Execution Path

graph TD
    subgraph "Python_Space"
        ["Python_Application"] --> ["DataStore_Python_Class"]
end

    subgraph "PyO3_Native_Extension"
        ["DataStore_Python_Class"] -- "PyBytes_to_u8_slice" --> ["Rust_Wrapper_impl_DataStore"]
        ["EntryHandle_Python"] -- "as_memoryview" --> ["memoryview_Object"]
end

    subgraph "Core_Engine_simd_r_drive"
        ["Rust_Wrapper_impl_DataStore"] -- "DataStore::write" --> ["DataStore_Rust_Struct"]
        ["DataStore_Rust_Struct"] -- "Index_Lookup" --> ["KeyIndexer"]
        ["DataStore_Rust_Struct"] -- "Mmap_Access" --> ["Mmap_Shared_Memory"]
end

    ["Mmap_Shared_Memory"] -- "Direct_Slice" --> ["memoryview_Object"]

Sources: experiments/bindings/python_(old_client)/README.md164-187 experiments/bindings/python_(old_client)/Cargo.toml15-16

Implementation Details

Build System and Metadata

The project uses maturin as the build backend, targeting the pyo3 binding type experiments/bindings/python_(old_client)/pyproject.toml28-33 It specifies a minimum Python version of 3.8 but officially targets 3.10 through 3.13 for CPython experiments/bindings/python_(old_client)/pyproject.toml20-34 The crate type is set to cdylib to produce a shared library compatible with Python’s import system experiments/bindings/python_(old_client)/Cargo.toml8-10

Key Classes and Functions

Python EntityRust EquivalentDescription
DataStore(path)DataStore::open(path)Opens or creates the binary storage file experiments/bindings/python_(old_client)/README.md160-162
.write(key, value)DataStore::write()Appends a key-value pair to the store experiments/bindings/python_(old_client)/README.md164-166
.batch_write(items)DataStore::batch_write()Writes a list of tuples in a single operation experiments/bindings/python_(old_client)/README.md168-170
.read(key)DataStore::read()Returns a bytes object or None experiments/bindings/python_(old_client)/README.md176-178
.read_entry(key)EntryHandleReturns a handle for zero-copy access via .as_memoryview() experiments/bindings/python_(old_client)/README.md180-182
.write_stream(k, r)DataStore::write_stream()Streams data from a Python file-like object experiments/bindings/python_(old_client)/README.md172-174
.exists(key)DataStore::exists()Checks if a key is present in the index experiments/bindings/python_(old_client)/README.md192-194

Sources: experiments/bindings/python_(old_client)/README.md160-194 experiments/bindings/python_(old_client)/Cargo.toml8-16 experiments/bindings/python_(old_client)/pyproject.toml28-34

Zero-Copy Integration

One of the core features of the direct binding is the EntryHandle.as_memoryview() method. This allows Python libraries to consume data directly from the Rust-managed mmap without copying bytes into the Python heap experiments/bindings/python_(old_client)/README.md7-182

Code Entity Association: Zero-Copy Read

Sources: experiments/bindings/python_(old_client)/README.md180-182 experiments/bindings/python_(old_client)/README.md7

Usage and Lifecycle Management

Resource Cleanup

Because PyO3 does not guarantee deterministic destruction of Rust-backed objects, the underlying Rust Drop implementation handles the cleanup of file handles and memory maps. However, the Python garbage collector may delay this, potentially causing issues when trying to delete or move the underlying file on disk while the process is running, especially on Windows experiments/bindings/python_(old_client)/README.md38-39

Example: Streaming Large Payloads

The legacy binding supports streaming from Python’s io.BytesIO or other file-like objects directly into the store using the write_stream method experiments/bindings/python_(old_client)/README.md172-174

Sources: experiments/bindings/python_(old_client)/README.md136-156 experiments/bindings/python_(old_client)/README.md184-186

Supported Platforms Table

Operating SystemArchitectureSupport Level
Linuxx86_64, aarch64✅ Supported experiments/bindings/python_(old_client)/README.md25
macOSx86_64, arm64 (M1/M2)✅ Supported experiments/bindings/python_(old_client)/README.md26
Windowsx86_64, ARM64❌ Not Supported (Experimental) experiments/bindings/python_(old_client)/README.md38-39
musl Linux-❌ Not Supported experiments/bindings/python_(old_client)/README.md42

Sources: experiments/bindings/python_(old_client)/README.md23-45