Appearance
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
- You need selections, numeric conversion or a .cpk file.Read or write a matrixUse store. It owns file I/O and combines the stream and chunk offsets for you.
- You are writing a container, file inspector or reader.Inspect bytes or embed a streamUse format parsers and their typed descriptors. Keep stream, chunk and record coordinates distinct.
- You already manage the buffers and container.Encode or decode rows in memoryUse scans with matching metadata, records and coding tables.
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.
Illustrative packed stream embedded at outer-file byte 8192. Every number below is in bytes.
- Stream → chunk4096
entry.offsetMeasured from the CPK stream start.
- Chunk → records4096
meta.records_start()Includes the metadata and alignment padding.
- Records → row128..192
meta.record_range(row)?A half-open range in the records region.
Outer-file record range
8192 + 4096 + 4096 + (128..192) = 16512..16576For 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.
01
Superblock
4096 bytesStream shape, dtype, page size and directory location.
02
Chunks
CPAKPage-aligned chunks containing metadata and row records.
03
Directory
40 bytes / entryOne 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.
| Offset | Field | Width |
|---|---|---|
| 0 | Magic CPK\0 | 4 bytes |
| 4 | Version, currently 1 | u16 |
| 6 | Flags, currently zero | u16 |
| 8 | Page size | u32 |
| 12 | Dtype code | u8 |
| 13 | Source layout code | u8 |
| 14 | Reserved, zero | u16 |
| 16 | Matrix row count | u64 |
| 24 | Column count | u32 |
| 28 | Chunk count | u32 |
| 32 | Target rows per chunk | u32 |
| 36 | Reserved, zero | u32 |
| 40 | Directory offset | u64 |
| 48 | Stream length | u64 |
| 56 | Non-cryptographic hash of bytes 0..56 and the directory | u64 |
| 64–4095 | Reserved, zero | 4032 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.
| Offset | Field | Width |
|---|---|---|
| 0 | First matrix row | u64 |
| 8 | Chunk offset, relative to the stream | u64 |
| 16 | Total chunk length, including metadata and padding | u64 |
| 24 | Metadata-prefix length | u64 |
| 32 | Chunk row count | u32 |
| 36 | Reserved, zero | u32 |
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:
- Three 256-entry
u16rANS frequency tables: gaps, ranks and dictionaries. Each used model sums to2^11; an unused model sums to zero. - An optional coded-to-source column permutation, then optional per-column mode values in coded order.
- Per-record offsets (
u64), lengths (u32) and kind flags (u8). - A cumulative
u64nnz table withn_cells + 1entries.
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.
01
Flags
u8Index mode + value mode
02
NNZ
varintStored elements for sparse records; nonzero values for dense records.
03
Index section
variableAbsent in dense records
04
Value section
variableRaw or coded values
Fields appear in byte order; widths are not drawn to scale. Empty rows may have zero record bytes.
| Count | Sparse record | Dense record |
|---|---|---|
nnz | Stored entry count, including explicit zeros | Nonzero value count |
| Stored elements | nnz | n_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 bits | Encoding |
|---|---|
| 0–1 | Index mode: raw (0), gap-coded (1), dense (2) |
| 2–3 | Value mode: raw (0), dictionary-coded (1), column-mode-coded (2) |
| 4–7 | Reserved, 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.