Skip to content
API REFERENCEUsage guides →

cellpack::store::CpkWriter ​

struct

Module contract · All interfaces

At a glance ​

pub struct CpkWriter { /* private fields */ }

Writes one matrix to a .cpk file. Rows are appended in order; the file is valid only after CpkWriter::finish.

Use for incremental writes. Declare the input layout and dtype once, append row batches, then consume the writer with finish.

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(())
}
OUTPUTExpected result
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 ​

6 of 6 members

  • createCreates (or truncates) path for a matrix of n_cols columns of dtype values arriving as source .
  • set_threadsEncodes up to threads whole chunks of one append concurrently. Chunks are independent, so the file is identical to a serial write.
  • appendAppends dense or CSR rows. Rows in the other form than the writer’s source layout are converted once: dense rows keep their non-zero values (a value is zero when all its bytes are), CSR rows fill zeroed dense rows.
  • end_chunkEnds the current chunk early, so the next row starts a new one (to keep chunks aligned with blocks of the source).
  • append_packedAppends one chunk already coded with encode_chunk and packed with pack_chunk at this writer’s page size, so callers can encode chunks in parallel and keep only the ordered writes here. Rows staged by CpkWriter::append must be ended with CpkWriter::end_chunk first.
  • finishEncodes the remaining rows, writes the directory, which ends the file, and finally the superblock.

Methods and associated constants ​

create ​

pub fn create(
    path: impl AsRef<Path>,
    n_cols: u32,
    dtype: Dtype,
    source: SourceLayout,
    options: CpkOptions,
) -> Result<Self>

Creates (or truncates) path for a matrix of n_cols columns of dtype values arriving as source.

Parameters ​

ParameterMeaning and constraints
pathDestination file. An existing file is truncated immediately.
n_colsNumber of source columns, in elements; every appended row must have this width.
dtypeStored numeric type. Its byte width must match the input values.
sourceLayout of incoming rows (Dense or Csr); this does not force the encoded record layout.
optionsEncoding choices, target rows per chunk, and page size in bytes.

Result ​

A writer owning staging buffers. The destination is not a valid stream until finish succeeds.

set_threads ​

pub fn set_threads(&mut self, threads: usize)

Encodes up to threads whole chunks of one append concurrently. Chunks are independent, so the file is identical to a serial write.

append ​

pub fn append<'a>(&mut self, rows: impl Into<MatrixRef<'a>>) -> Result<()>

Appends dense or CSR rows. Rows in the other form than the writer’s source layout are converted once: dense rows keep their non-zero values (a value is zero when all its bytes are), CSR rows fill zeroed dense rows.

Parameters ​

ParameterMeaning and constraints
rowsBorrowed row-major dense or canonical CSR input. Rows are appended in order; the writer does not retain the borrow after this call.

end_chunk ​

pub fn end_chunk(&mut self) -> Result<()>

Ends the current chunk early, so the next row starts a new one (to keep chunks aligned with blocks of the source).

append_packed ​

pub fn append_packed(&mut self, packed: &[u8]) -> Result<()>

Appends one chunk already coded with encode_chunk and packed with pack_chunk at this writer’s page size, so callers can encode chunks in parallel and keep only the ordered writes here. Rows staged by CpkWriter::append must be ended with CpkWriter::end_chunk first.

finish ​

pub fn finish(self) -> Result<()>

Encodes the remaining rows, writes the directory, which ends the file, and finally the superblock.

Result ​

Writes the remaining rows, directory, and superblock. Consumes the writer. Success does not imply fsync or atomic publication.

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.

Definition