Appearance
cellpack::loader::DataSourceBuilder
Module contract · All interfaces
At a glance
pub struct DataSourceBuilder { /* private fields */ }Registers matrices one at a time, then composes them into a DataSource.
Registration accumulates each chunk’s column map and modes in plain vectors; Self::build moves the maps into a huge-page arena and the modes into words, and nothing is shared before then, so the builder is &mut self throughout. Files are opened once per canonical path, so registering one .cpk twice reuses its descriptor and page numbering.
Register immutable files in a known order and map each source column into one shared standard vocabulary. All registered streams must use the builder's page size.
Try it
FIXED EXAMPLE RUST / CPU
Deliver ordered batches
Two output schemas share each requested position. Missing dimensions use the schema default; present sparse zeros stay zero.
mod common;
use std::error::Error;
use std::sync::Arc;
use cellpack::loader::{
DataSourceBuilder, Dtype, IoBackend, Loader, LoaderConfig,
OutputSchema, Planning, Replicate, Ring,
};
fn main() -> Result<(), Box<dyn Error>> {
let mut builder = DataSourceBuilder::new(5, 4096)?.direct_io(false);
builder.add_cpk(common::fixture()?, &[0, 1, 2, 3])?;
let source = Arc::new(builder.build()?);
let schemas = vec![
OutputSchema::dense(1, vec![3, 1, 4], Dtype::F32)?.with_default(f64::NAN),
OutputSchema::dense(2, vec![0, 2], Dtype::F64)?,
];
let loader = Loader::new(source, schemas, LoaderConfig {
workers: 2, backend: IoBackend::Pread, slot_cells: 2,
..LoaderConfig::default()
})?;
let cells = [3u32, 0, 2, 3, 1];
let accesses = Replicate::new(cells, 2)?;
let ring = Arc::new(Ring::allocate(loader.ring_layout(2, 1))?);
let run = loader.prepare(&accesses, ring, Planning::None)?;
let stats = run.for_each_batch(|batch| {
let panel: Vec<f32> = batch.region(0).chunks_exact(4)
.map(|v| f32::from_ne_bytes(v.try_into().unwrap())).collect();
println!("positions {}..{}: panel {panel:?}",
batch.first_position(), batch.first_position() + batch.rows() as u64);
Ok(())
})?;
println!("delivered: {} positions, {} accesses", cells.len(), stats.counters.accesses);
Ok(())
}
positions 0..2: panel [0.0, 8.0, NaN, 1.0, 2.0, NaN]
positions 2..4: panel [6.0, 0.0, NaN, 0.0, 8.0, NaN]
positions 4..5: panel [0.0, 0.0, NaN]
delivered: 5 positions, 10 accessesMember index
5 of 5 members
newvocab : the number of standard ids the matrices map their columns to (ids are 0..vocab ), at most u32::MAX - 1 ; page_size : the page size of every file registered later, a power of two in [512, 65536] .direct_ioData reads use O_DIRECT; metadata reads remain buffered.add_cpkRegisters a .cpk file as one matrix. to_std is the matrix’s standard id of every stored column ( UNMAPPED : none), given by the caller for this matrix: the source neither infers nor aligns columns. The file’s page size must be the data source’s.add_cpk_regionRegisters an embedded CellPack stream without extracting its bytes: the file image lies at offset of path and is length bytes long ( None : to the end of the file). map is the matrix’s standard id per stored column, as for Self::add_cpk . The file’s superblock and every chunk must use the source’s page size; each chunk’s metadata is parsed and its per-cell page counts recorded for the sizing estimates.buildCopies the accumulated column maps into a huge-page arena and the column modes into 8-byte words, then returns the source. Metadata and decode tables stay owned by their chunks.
Methods and associated constants
new
vocab: the number of standard ids the matrices map their columns to (ids are 0..vocab), at most u32::MAX - 1; page_size: the page size of every file registered later, a power of two in [512, 65536].
Parameters
| Parameter | Meaning and constraints |
|---|---|
vocab | Number of standard-column IDs; positive and below u32::MAX. |
page_size | Power-of-two bytes per page, between 512 and 65536. Must match every stream. |
direct_io
Data reads use O_DIRECT; metadata reads remain buffered.
add_cpk
Registers a .cpk file as one matrix. to_std is the matrix’s standard id of every stored column (UNMAPPED: none), given by the caller for this matrix: the source neither infers nor aligns columns. The file’s page size must be the data source’s.
add_cpk_region
pub fn add_cpk_region(
&mut self,
path: impl AsRef<Path>,
offset: u64,
length: Option<u64>,
map: &[u32],
) -> Result<u32>Registers an embedded CellPack stream without extracting its bytes: the file image lies at offset of path and is length bytes long (None: to the end of the file). map is the matrix’s standard id per stored column, as for Self::add_cpk. The file’s superblock and every chunk must use the source’s page size; each chunk’s metadata is parsed and its per-cell page counts recorded for the sizing estimates.
build
pub fn build(self) -> Result<DataSource>Copies the accumulated column maps into a huge-page arena and the column modes into 8-byte words, then returns the source. Metadata and decode tables stay owned by their chunks.
Contract reference
See the module reference for semantic input/output rules, units, allocation, mutation, failure behavior, and lifetime requirements. Declarations and documented members are synchronized from the crate source.