A Rust-backed Python library for caching Arrow record-batch streams so they can be replayed multiple times
Project description
batchcorder
A Rust-backed Python library for caching Arrow record-batch streams so they can be replayed multiple times from a source that can only be read once.
The problem
Arrow RecordBatchReader is a single-use stream — once consumed, it is gone.
Training loops and multi-pass data pipelines need to iterate the same stream
repeatedly without re-reading from disk or the network each time.
What batchcorder does
StreamCache wraps any Arrow stream source (anything that implements
__arrow_c_stream__) and stores each RecordBatch in a cache. Two storage
modes are supported:
- Memory-only (default): batches are stored as reference-counted pointers in RAM; reads are zero-copy and no IPC serialisation happens.
- Hybrid memory+disk: batches are serialised to an append-only IPC file on disk; a configurable hot layer keeps recently ingested batches in RAM to reduce disk I/O.
Multiple independent readers can replay the stream concurrently, each maintaining their own position in the batch sequence.
flowchart LR
U["upstream source<br/>(read once)"] --> D["StreamCache<br/>[mem cache / mem+disk cache]"]
D --> R0["StreamCacheReader 0<br/>(from batch 0)"]
D --> R1["StreamCacheReader 1<br/>(from batch 0)"]
D --> R2["StreamCacheReader 2<br/>(from batch 3)"]
Installation
pip install batchcorder
Usage
import tempfile
import pyarrow as pa
from batchcorder import StreamCache
table = pa.table({"x": [1, 2, 3], "y": [4, 5, 6]})
# Memory-only (default capacity = total physical RAM)
ds = StreamCache(table.to_reader(max_chunksize=1))
# Memory-only with explicit capacity
ds = StreamCache(
table.to_reader(max_chunksize=1),
memory_capacity=64 * 1024 * 1024, # 64 MB
)
# Hybrid memory+disk
tmp = tempfile.mkdtemp()
ds = StreamCache(
table.to_reader(max_chunksize=1),
memory_capacity=64 * 1024 * 1024, # 64 MB in RAM
disk_path=tmp,
disk_capacity=512 * 1024 * 1024, # 512 MB on disk
)
# Replay as many times as needed
for batch in ds:
print(batch)
# Or get an independent reader handle
reader = ds.reader()
result = pa.RecordBatchReader.from_stream(reader).read_all()
# Pre-ingest everything upfront
ds.ingest_all()
Compatibility
StreamCache and StreamCacheReader implement both __arrow_c_stream__
and __arrow_c_schema__, so they work with any Arrow-compatible library:
import pyarrow as pa
import duckdb
pa.table(ds) # PyArrow
pa.table(ds.reader()) # via StreamCacheReader
duckdb.table("ds") # DuckDB
Key properties
- Single-read source: the upstream stream is consumed exactly once; all subsequent reads come from the cache.
- Concurrent readers: multiple
StreamCacheReaderinstances from the same stream cache are fully independent and thread-safe. - Lazy ingestion: batches are fetched from the upstream source on demand as readers advance, not upfront.
- Replay from any position:
ds.reader(from_start=True)(default) replays from batch 0;ds.reader(from_start=False)starts from the current ingestion frontier (next batch not yet ingested).
Development
# Install dependencies and build the extension
uv sync --no-install-project --dev
uv run maturin develop --uv
# Run tests
uv run pytest
# Run all pre-commit checks
uv run pre-commit run --all-files
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 Distributions
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 batchcorder-0.1.2.tar.gz.
File metadata
- Download URL: batchcorder-0.1.2.tar.gz
- Upload date:
- Size: 221.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae89c23b1f4fbfa751493514212f1e1803688b644f166b1c906ac02311e2567e
|
|
| MD5 |
a7b8846d066ebfa61a78128e2f7620d9
|
|
| BLAKE2b-256 |
68c90d47bd2abe4efbf780279d55be86ed5ff02b8a318af8f625293e5b53788b
|
File details
Details for the file batchcorder-0.1.2-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: batchcorder-0.1.2-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 989.5 kB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20a4a12a58103e7ab33eae6735acaed623b7e25b63ba8288e3624cbf3d911fb2
|
|
| MD5 |
0d3eb433b61504d62022fb971deee34f
|
|
| BLAKE2b-256 |
7d3e52ee76d0f208dec3988e81c719f389880e986bbb09d4590db2b10358ca55
|
File details
Details for the file batchcorder-0.1.2-cp310-abi3-win32.whl.
File metadata
- Download URL: batchcorder-0.1.2-cp310-abi3-win32.whl
- Upload date:
- Size: 873.6 kB
- Tags: CPython 3.10+, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c50ec323a719b8599c0a26553193ddfff1cbfae1f987247164bf76d2cb648ac1
|
|
| MD5 |
b841c9d383930f7f34e0cccbd2075235
|
|
| BLAKE2b-256 |
33f0452327795a11b5a5188d5d63e60ce2b41d81144e7e088b7b5e321e423410
|
File details
Details for the file batchcorder-0.1.2-cp310-abi3-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: batchcorder-0.1.2-cp310-abi3-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.10+, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf875cfe78c0a22b3a2a3b9797660e7e9b1fa6c7ff55f593677d7abbba032cdc
|
|
| MD5 |
b6bb19b4ba0eb82bdce89790398cc926
|
|
| BLAKE2b-256 |
904f6fba60b87327b83760e27e7f71c81748ac3ba8265a22c9347be7a4211cf3
|
File details
Details for the file batchcorder-0.1.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: batchcorder-0.1.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34e56c937555f13bb15e5fcedc0203167bad6fe269a807dc6e6531b4569ffe21
|
|
| MD5 |
39c9809c55457beec968756e00d92086
|
|
| BLAKE2b-256 |
f3319216687533c97cbcda2e30194446983d846bdb15c144bcef7ec90a9ff608
|
File details
Details for the file batchcorder-0.1.2-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: batchcorder-0.1.2-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5346893185b9b4d396b05f1787b3d4bd98c0d95e1dcca10aa73f9e409e8d657
|
|
| MD5 |
e9bb8f49ad78c96930a2b79c831f4092
|
|
| BLAKE2b-256 |
7c3036bed1e8f49d6880318b69eb9f1ffad5ff34a3ea18a62212416b67c2fe3b
|
File details
Details for the file batchcorder-0.1.2-cp310-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: batchcorder-0.1.2-cp310-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.10+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d1cc120667b97089449df86ab8cf13eb35d6c6f36b5e87429896b2bee6d9769
|
|
| MD5 |
40f7e2e90e6fe01eb2402abf7cfb9608
|
|
| BLAKE2b-256 |
f5e40c312eb1658e5c5bef100a35b1cf8f3ffb7467c23d8b45aa3bb1a809f808
|