Appearance
cellpack::store::CpkFile
Module contract · All interfaces
At a glance
pub struct CpkFile { /* private fields */ }An opened .cpk file.
Use for file inspection and prepared reads. Prefer prepare_read or prepare_selection when you need typed output or reusable buffers.
Try it
FIXED EXAMPLE RUST / CPU
Write and reopen a matrix
Write a small matrix and inspect the decoded dense rows and CSR structure. The file becomes readable after finish succeeds.
mod common;
use std::error::Error;
use cellpack::format::{Dtype, SourceLayout};
use cellpack::scans::DenseRef;
use cellpack::store::{CpkFile, CpkWriter, ReadWorkspace};
fn main() -> Result<(), Box<dyn Error>> {
let path = common::directory()?.join("example.cpk");
let values = [0.0f32, 2.0, 0.0, 1.0, 0.0, 3.0];
let bytes: Vec<u8> = values.iter().flat_map(|v| v.to_le_bytes()).collect();
let mut writer = CpkWriter::create(
&path, 3, Dtype::F32, SourceLayout::Dense, common::options(),
)?;
writer.append(DenseRef { n_genes: 3, value_width: 4, data: &bytes })?;
writer.finish()?;
let file = CpkFile::open(&path)?;
let selection = file.prepare_read(0..2)?;
let mut out = vec![0.0f32; selection.spec().dense_len()?];
selection.read_dense_into(&mut out, &mut ReadWorkspace::default(), 1)?;
let csr = file.read_csr(0..2)?;
println!("shape: {} × {}", file.n_rows(), file.n_cols());
println!("dense: {out:?}");
println!("CSR offsets: {:?}", csr.indptr);
println!("CSR columns: {:?}", csr.indices);
Ok(())
}
shape: 2 × 3
dense: [0.0, 2.0, 0.0, 1.0, 0.0, 3.0]
CSR offsets: [0, 1, 3]
CSR columns: [1, 0, 2]Member index
16 of 16 members
openOpens a complete .cpk stream and reads its superblock and directory. Record payloads remain on disk until a read is requested.open_regionOpens a CellPack stream embedded in a bounded region of another file. offset and length are outer-file bytes; None permits the region through the end of the file. The region must cover the declared stream. The caller locates the region; this method does not parse its container.superblockBorrows the parsed matrix-stream descriptor without additional I/O.identityDevice and inode of the opened descriptor, independent of path replacement.byte_offsetStart of this stream in its outer file, in bytes.n_rowsNumber of rows in the matrix, independent of any prepared selection.n_colsNumber of source columns in every matrix row.dtypeStored numeric type used to interpret allocated read value bytes.chunksBorrows the matrix directory in row order; offsets are stream-relative.read_metaA chunk’s metadata bytes.read_chunkA whole chunk’s bytes (metadata and records).read_chunk_intoReads a chunk into an exact-size caller byte buffer. metadata = true requires that directory entry’s meta_len bytes; false requires its full len . A failed read can leave partial output.read_csrRows as CSR in source column order, using the borrowed-output reader.read_denseRows as dense n_cols values each, row-major.prepare_readPrepares a row range of every column in the stored dtype, with the default read policy.prepare_selectionPrepares rows from metadata only; record bytes are read by each *_into call. gene_map sends each source column to an output column below columns (distinct targets) or drops it with UNMAPPED ; without it, columns is the file’s column count.
Methods and associated constants
open
Opens a complete .cpk stream and reads its superblock and directory. Record payloads remain on disk until a read is requested.
open_region
Opens a CellPack stream embedded in a bounded region of another file. offset and length are outer-file bytes; None permits the region through the end of the file. The region must cover the declared stream. The caller locates the region; this method does not parse its container.
superblock
pub fn superblock(&self) -> &SuperblockBorrows the parsed matrix-stream descriptor without additional I/O.
identity
Device and inode of the opened descriptor, independent of path replacement.
byte_offset
pub fn byte_offset(&self) -> u64Start of this stream in its outer file, in bytes.
n_rows
Number of rows in the matrix, independent of any prepared selection.
n_cols
Number of source columns in every matrix row.
dtype
Stored numeric type used to interpret allocated read value bytes.
chunks
pub fn chunks(&self) -> &[ChunkEntry]Borrows the matrix directory in row order; offsets are stream-relative.
read_meta
A chunk’s metadata bytes.
read_chunk
pub fn read_chunk(&self, chunk: usize) -> Result<Vec<u8>>A whole chunk’s bytes (metadata and records).
read_chunk_into
Reads a chunk into an exact-size caller byte buffer. metadata = true requires that directory entry’s meta_len bytes; false requires its full len. A failed read can leave partial output.
read_csr
Rows as CSR in source column order, using the borrowed-output reader.
read_dense
Rows as dense n_cols values each, row-major.
Result
Stored-dtype bytes (Vec<u8>), not a Vec<f32>. For typed values use a prepared selection.
prepare_read
pub fn prepare_read(&self, rows: Range<u64>) -> Result<ReadSelection>Prepares a row range of every column in the stored dtype, with the default read policy.
Parameters
| Parameter | Meaning and constraints |
|---|---|
rows | Half-open file-row interval in u64 row IDs, such as 0..2. |
Result
Prepared metadata and output sizing. Record I/O happens when the selection is read.
prepare_selection
pub fn prepare_selection(
&self,
rows: RowSelection<'_>,
gene_map: Option<&[u32]>,
columns: usize,
options: ReadOptions,
) -> Result<ReadSelection>Prepares rows from metadata only; record bytes are read by each *_into call. gene_map sends each source column to an output column below columns (distinct targets) or drops it with UNMAPPED; without it, columns is the file’s column count.
Parameters
| Parameter | Meaning and constraints |
|---|---|
rows | File rows. A List preserves order and repeated IDs. |
gene_map | Optional source-column → output-column map; one entry per source column. Use UNMAPPED to drop a column. |
columns | Output width in elements, including zero-filled missing output positions. |
options | Read policy and merge settings. These describe application reads, not physical device traffic. |
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.