Skip to content

File and record format ​

CellPack has three nested byte layouts: a CPK stream describes one matrix, each CPAK chunk shares compression metadata across rows, and each record holds one independently decodable row. This page describes version 1; multi-byte integers and stored values are little-endian.

Choose the layer you need

Inspect a chunk without loading its records ​

Given an existing, finalized matrix.cpk, this program opens its directory and reads only the first chunk's metadata. An empty matrix has no first chunk.

rust
use cellpack::{CpkFile, Result};
use cellpack::format::parse_meta;

fn main() -> Result<()> {
    let file = CpkFile::open("matrix.cpk")?;
    println!("matrix: {} x {}, {:?}", file.n_rows(), file.n_cols(), file.dtype());

    if let Some(entry) = file.chunks().first() {
        let bytes = file.read_meta(0)?;
        let meta = parse_meta(&bytes)?;
        println!("chunk starts at matrix row {}", entry.first_row);
        println!("metadata: {} bytes; records start: {} bytes into chunk",
                 meta.meta_len(), meta.records_start());
        println!("chunk: {} rows x {} columns", meta.n_cells(), meta.n_dims());
    }
    Ok(())
}

meta borrows bytes; keep the buffer immutable and alive while using the view. Use OwnedMeta::from_vec(bytes) if the metadata must outlive that input buffer. Metadata-only input supports shape, row counts and record-range queries, but records_of needs a complete chunk buffer.

Keep byte coordinates separate ​

ChunkEntry::offset and Superblock::directory_offset are stream-relative. Meta::records_start() is chunk-relative. Meta::record_range(row) is records-region-relative; its row argument is local to the chunk.

One record, three relative offsets

Illustrative packed stream embedded at outer-file byte 8192. Every number below is in bytes.

  1. Stream → chunk4096entry.offset

    Measured from the CPK stream start.

  2. Chunk → records4096meta.records_start()

    Includes the metadata and alignment padding.

  3. Records → row128..192meta.record_range(row)?

    A half-open range in the records region.

Outer-file record range8192 + 4096 + 4096 + (128..192) = 16512..16576

For an embedded stream, add the outer-file start exactly once. CpkFile::open_region accepts that outer offset and handles the translation. To find the local row of a directory entry, subtract entry.first_row from the matrix row ID.

Matrix stream: CPK ​

A .cpk stream has a 4096-byte superblock, page-aligned chunks, and a final chunk directory. Each directory entry locates a consecutive range of matrix rows.

Inside a .cpk stream
01
Superblock4096 bytes

Stream shape, dtype, page size and directory location.

02
ChunksCPAK

Page-aligned chunks containing metadata and row records.

03
Directory40 bytes / entry

One entry per chunk; ends the stream.

Offsets are relative to the start of the stream. The valid superblock is written last.

The writer starts with an invalid superblock and writes the valid one last. Call CpkWriter::finish before opening the result. The header/directory hash detects damage or mismatches in those two regions; it is neither cryptographic nor a whole-payload integrity check.

Superblock byte map

Offsets below are from the stream start.

OffsetFieldWidth
0Magic CPK\04 bytes
4Version, currently 1u16
6Flags, currently zerou16
8Page sizeu32
12Dtype codeu8
13Source layout codeu8
14Reserved, zerou16
16Matrix row countu64
24Column countu32
28Chunk countu32
32Target rows per chunku32
36Reserved, zerou32
40Directory offsetu64
48Stream lengthu64
56Non-cryptographic hash of bytes 0..56 and the directoryu64
64–4095Reserved, zero4032 bytes

Dtype codes 0–9 are U8, U16, U32, U64, I8, I16, I32, I64, F32, F64. Source layout codes are CSR (0) and dense (1). Neither determines an individual record's sparse/dense coding choice. The chunk stores only a value width; the stream supplies the numeric interpretation.

Directory entry byte map

Each entry is 40 bytes. Offsets here are relative to the entry start.

OffsetFieldWidth
0First matrix rowu64
8Chunk offset, relative to the streamu64
16Total chunk length, including metadata and paddingu64
24Metadata-prefix lengthu64
32Chunk row countu32
36Reserved, zerou32

Entries tile all matrix rows in order; chunks are aligned, ascending and non-overlapping. The directory ends the declared stream exactly.

Chunk: CPAK ​

A chunk starts with a 64-byte header using magic CPAK and version 1. It declares the row/column counts, value width, rANS lane settings, packed page size, row bounds, total nnz, metadata length and records-region length. Two flags indicate an optional column permutation and per-column mode values.

The metadata sections follow in this order:

  1. Three 256-entry u16 rANS frequency tables: gaps, ranks and dictionaries. Each used model sums to 2^11; an unused model sums to zero.
  2. An optional coded-to-source column permutation, then optional per-column mode values in coded order.
  3. Per-record offsets (u64), lengths (u32) and kind flags (u8).
  4. A cumulative u64 nnz table with n_cells + 1 entries.

Raw column IDs and permutation entries use two bytes through 65536 columns and four bytes above that. Offset and cumulative nnz tables begin at 8-byte-aligned positions; their section locations follow from the declared chunk shape.

meta_len ends after the nnz table. With page_log2 = 0, records begin there and are contiguous in row order. Packed chunks insert padding until records_start is page-aligned; records may then appear in a different physical order with gaps. Use the per-row extent table in both cases. Packed page sizes are powers of two from 512 to 65536 bytes.

Record: one row ​

A nonempty record starts with flags and an nnz varint, followed by its index and value sections. A sparse record stores entries in coded column order; an explicit stored zero is still an entry. A dense record omits the index section and stores one value per column, including zeros.

Nonempty record layout
01
Flagsu8

Index mode + value mode

02
NNZvarint

Stored elements for sparse records; nonzero values for dense records.

03
Index sectionvariable

Absent in dense records

04
Value sectionvariable

Raw or coded values

Fields appear in byte order; widths are not drawn to scale. Empty rows may have zero record bytes.

CountSparse recordDense record
nnzStored entry count, including explicit zerosNonzero value count
Stored elementsnnzn_dims

Empty rows can have a zero-length record. This is why max_nnz alone is not a bound for every dense decode buffer: use max_elems or the codec sizing API.

Record flag encoding and value coding
Flag bitsEncoding
0–1Index mode: raw (0), gap-coded (1), dense (2)
2–3Value mode: raw (0), dictionary-coded (1), column-mode-coded (2)
4–7Reserved, zero

Dictionary coding stores distinct value bit patterns and per-element ranks. Column-mode coding uses rank zero for that column's mode and positive ranks for exceptions in a record-local dictionary. Leading escape or pool bytes can be carried in rANS states; use the decoder rather than treating the stored sections as independent byte streams.

What parsing guarantees ​

parse_superblock checks matrix-level fields. parse_directory then ties the directory to that header and checks row coverage and chunk extents. Neither reads chunk metadata or payload bytes.

parse_meta validates a chunk's metadata structure and returns borrowed views. Some per-row table checks happen only when queried; it does not validate every inner record. Decoding retains the trusted-data contract. The format API reference gives input lengths, lifetimes and error behavior for each parser.