Fragment-index arrays¶
This page specifies the fragment-index blob stored in each cell of
vertex_fragments/ and link_fragments/. What fragments are and how
readers use them is on Fragments.
Terms¶
- Fragment index
The blob in one cell of
vertex_fragments/(orlink_fragments/) that splits the rows of the matchingvertices/cell (or intra-chunk link cell) intoFfragments. A range fragment is a contiguous run of rows[start, start + count). An explicit fragment is a list of row indices.
Introduction¶
A reader that wants some of a chunk’s rows reads the fragment index first. One bit test says whether a fragment is a range, so the common case needs no per-fragment decoding. Explicit fragments let two fragments name the same row; shared fragments at coarser levels use this.
The implementation is zarr_vectors.encoding.fragments (encode_fragments,
decode_fragments, ChunkFragmentIndex), which is internal.
Technical reference¶
Byte layout (version 1)¶
All integers are little-endian.
HEADER (16 bytes, struct "<IHHII")
uint32 magic = 0x5A564647 ('ZVFG' as bytes 47 46 56 5A)
uint16 version = 1
uint16 flags = 0
uint32 num_fragments F
uint32 num_range_fragments R
RANGE BITMAP
ceil(F/8) bytes, zero-padded to the next multiple of 8 bytes
bit f (least significant bit first within byte f >> 3) = 1 if fragment f is a range
RANGE TABLE (R × 16 bytes)
int64 start, int64 count one pair per range fragment, in fragment order
EXPLICIT CSR (E = F − R; compressed sparse rows)
uint32 explicit_offsets[E + 1]
int64 explicit_indices[T] T = explicit_offsets[E]
The header size and bitmap padding keep the range table 8-byte aligned.
Rule |
Detail |
|---|---|
Empty cell |
The 16-byte header alone, with |
|
Always written when |
Range table row |
Range fragment |
Explicit indices |
Explicit fragment |
Encoder |
|
Worked example¶
Three fragments: rows 0 to 3, the rows [12, 7, 19], and rows 20 to 27.
import numpy as np
from zarr_vectors.encoding.fragments import encode_fragments, decode_fragments
blob = encode_fragments([(0, 4), np.array([12, 7, 19]), (20, 8)])
len(blob) # 88
fidx = decode_fragments(blob)
[fidx.is_range(f) for f in range(3)] # [True, False, True]
fidx.range(2) # (20, 8)
fidx.indices(1) # array([12, 7, 19])
F = 3, R = 2, E = 1, T = 3:
offset bytes meaning
0x00 47 46 56 5A magic 'ZVFG'
0x04 01 00 version = 1
0x06 00 00 flags = 0
0x08 03 00 00 00 F = 3
0x0C 02 00 00 00 R = 2
0x10 05 00 00 00 00 00 00 00 bitmap: bits 0 and 2 set, padded to 8 bytes
0x18 00 00 00 00 00 00 00 00 range[0].start = 0
0x20 04 00 00 00 00 00 00 00 range[0].count = 4
0x28 14 00 00 00 00 00 00 00 range[1].start = 20
0x30 08 00 00 00 00 00 00 00 range[1].count = 8
0x38 00 00 00 00 explicit_offsets[0] = 0
0x3C 03 00 00 00 explicit_offsets[1] = 3
0x40 0C 00 00 00 00 00 00 00 explicit_indices[0] = 12
0x48 07 00 00 00 00 00 00 00 explicit_indices[1] = 7
0x50 13 00 00 00 00 00 00 00 explicit_indices[2] = 19
Fragment 1: bit 1 of 0x05 is 0, so it is explicit. One set bit precedes
it, so it is explicit fragment 1 - 1 = 0, with indices
explicit_indices[0:3] = [12, 7, 19].
vertex_fragments/ and link_fragments/ arrays¶
Both are per-chunk arrays with one fragment-index blob
per cell. Their zarr_vectors object has kind ("vertex_fragments" or
"link_fragments") and encoding "fragment_index_v1".
vertex_fragments/splits the rows ofvertices/. Every writer creates it.link_fragments/splits the rows oflinks/0/<all-zero offsets>/only. Graphs, skeletons and meshes write it. Its cells are keyed by chunk alone, so only one link array per chunk can own them; see Links.
A cell that was never written reads as b"", which decode_fragments
rejects as too short; read_vertex_fragment_index does not check for
it. List a level’s chunks from nonempty_chunks (list_chunk_keys)
rather than probing the grid.
Validation¶
A blob is well formed when:
it starts with the magic, version
1and flags0;R ≤ F, andRequals the number of set bits among the firstFbits of the bitmap;it is long enough for the sections it declares;
explicit_offsetsstarts at 0 and never decreases;every range and every explicit index lies in
[0, rows in the payload cell).
decode_fragments raises ArrayError for a bad magic, version or flags,
for R > F, and for a blob too short for its sections. It does not recount
the bitmap against R. read_cells(fragments=True) also checks rules 2
and 4 (check_fragment_index), on the host and on the device alike, and
records a cell that fails any of these as an error with no fragments.
L1 warns when a level has no
vertex_fragments/. L3 reports a
vertices/ cell that fails to decode through its fragment index. When a
level sets fragments_tile, L3 also checks that each cell’s fragments are
ranges covering its rows in order.