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/ (or link_fragments/) that splits the rows of the matching vertices/ cell (or intra-chunk link cell) into F fragments. 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 F = 0 and R = 0

explicit_offsets[0]

Always written when F > 0, even when E = 0, so an all-range index ends with four zero bytes

Range table row

Range fragment f is row r, the number of set bits before bit f. ChunkFragmentIndex builds this prefix count once, then looks up in constant time

Explicit indices

Explicit fragment e (counting explicit fragments only) owns explicit_indices[explicit_offsets[e]:explicit_offsets[e+1]]: absolute, non-negative rows of the payload cell. Rows may repeat across fragments; an empty fragment is allowed

Encoder

encode_fragments writes a (start, count) tuple as a range, and an integer array as a range when it equals arange(arr[0], arr[0] + len(arr)). force_explicit=True keeps arrays explicit. The bulk writers write only ranges

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].

Validation

A blob is well formed when:

  1. it starts with the magic, version 1 and flags 0;

  2. R ≤ F, and R equals the number of set bits among the first F bits of the bitmap;

  3. it is long enough for the sections it declares;

  4. explicit_offsets starts at 0 and never decreases;

  5. 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.