moczarr.coverage
Coverage-envelope decoding (morton-moc/1), read side — pure functions.
The tiered coverage convention (zagg sparse_coverage.md §4): a leaf's
commit stamp carries a coverage envelope — the tier-0 morton box (<= 4
decimal-string members, null-padded) plus an encoding discriminator:
"full"— coverage is the whole shard subtree; no sidecar exists."bitmap"— exact cell-order occupancy lives in the in-leafcoverage.mocsidecar: a zstd-compressed bit field, biti= the i-th subtree cell in ascending packed-word order (base-4 digit tail, digits1..4->0..3), MSB-first per byte.- absent — box-only (phase-1 stamps, depth-0 configs).
The store root's coverage.moc is a "ranges" envelope: inclusive
[first, last] runs of same-order shard ids within one base cell,
consecutive in digit-tail rank, endpoints as decimal STRINGS (packed words
exceed 2^53 — raw JSON numbers would be float-mangled).
Postures, inherited from the design's D9 discipline: envelopes above the
leaf are caches — an unusable one reads as absent (None) and the caller
degrades to the walk, never to a wrong answer. A PRESENT-but-corrupt bitmap
sidecar raises instead: silently zero-padding would fabricate false
negatives, indistinguishable from healthy sparse coverage.
aoi_mask(cells, aoi)
Boolean mask over cells: which intersect the AOI morton cover.
The §5 nesting predicate (prefix = ancestor), applied in BOTH
directions: a cell is kept when it sits inside an AOI member (member
coarser-or-equal) or contains one (member finer). This is containment,
NOT np.isin against moc_and's output — MOC intersection returns
a compacted cover (a fully-occupied subtree compacts to its parent
word), so identity tests against it silently drop exactly the dense
regions. cells must share one order (a store's cell coordinate does,
by convention); aoi members may be mixed-order.
Single order is enforced, not merely assumed: a mixed-order cells
array raises ValueError. infer_order_from_morton returns the
minimum order over an array, so a silent inference would clip finer
cells to the wrong (coarser) order and drop their rows — the module's
raise-on-ambiguity discipline forbids that. The check is one vectorized
clip2order compare (O(n), no per-cell Python loop): clipping to the
common order is identity iff every cell already sits at that order.
POINT-kind words (spec §1 suffix 48..=63) are first-class members on
BOTH sides — the v1 posture: for containment a point counts as an
order-29 member like any other, normalized to its order-29 area twin
(same path; §4 — membership at a coarser level is ordinary truncation).
A pure-point cells array is therefore uniform order 29 (the
single-order guard accepts it), and mixed order-29 area + point arrays
are well-formed per §4.
Source code in src/moczarr/coverage.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
box_and(coverage, aoi)
Intersection of a leaf envelope's tier-0 box with an AOI morton cover.
One in-memory op on <= 4 members — the cheap AOI reject a reader runs on the stamp it already fetched, before paying for the bitmap sidecar. An empty result rejects the leaf outright (the box is a conservative superset: false positives possible, false negatives impossible).
Source code in src/moczarr/coverage.py
151 152 153 154 155 156 157 158 159 160 161 | |
box_words(coverage)
The tier-0 box members as packed uint64 words (nulls dropped).
Feed to mortie.moc_and against an AOI cover for the cheap leaf
reject: the box is a conservative superset (false positives possible,
false negatives impossible).
Source code in src/moczarr/coverage.py
61 62 63 64 65 66 67 68 69 | |
decode_bitmap(payload, shard, cell_order)
Occupied cell words from a bitmap-sidecar payload — exact, or raise.
Returns the sorted packed uint64 words at cell_order whose bits
are set. A corrupt payload — zstd garbage, or a decompressed size other
than the deterministic ceil(4^depth / 8) bytes — raises rather than
zero-padding to a plausible partial cell set (a false negative; the
exact truth is intact in the leaf, so surfacing beats under-reporting).
Source code in src/moczarr/coverage.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
parse_leaf_coverage(stamp)
The coverage envelope from a commit stamp, or None when absent.
Tolerant by design: debris (None stamp), pre-coverage stamps, a
malformed payload, or an unknown/future spec all read as absent — the
box tiers are indexes, never truth, so a reader without them degrades to
opening the leaf. Strict on the spec gate: a future envelope version
must be adopted deliberately, not half-parsed.
Source code in src/moczarr/coverage.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
parse_root_coverage(payload)
A usable store-root coverage envelope, or None.
The root MOC is a regenerable cache: a non-mapping payload, an unknown
spec, or a non-"ranges" encoding reads as absent and the caller
falls back to the discovery walk (D9 — degrade, never wrong answers).
Source code in src/moczarr/coverage.py
104 105 106 107 108 109 110 111 112 113 114 | |
ranges_contain(envelope, shard)
Whether the envelope's ranges list one shard id — O(ranges), no expansion.
Source code in src/moczarr/coverage.py
220 221 222 223 224 225 226 227 228 229 230 231 232 | |
ranges_words(envelope)
Shard words from a root envelope's ranges — exact expansion, or raise.
Malformed ranges (base-crossing, wrong order, reversed endpoints) raise:
a corrupt cache must never yield a plausible partial answer. Expansion
is O(covered shards); containment checks on the hot path should use
:func:ranges_contain instead (rank space, no materialization).
Source code in src/moczarr/coverage.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
root_coverage_and(envelope, aoi)
Intersection of the root ranges MOC with an AOI morton cover.
aoi is any morton cover (mixed order allowed — mortie's moc_and
resolves containment across orders). Returns the covered shards the AOI
touches; empty means no covered shard intersects. Expansion is
O(covered shards) — see :func:ranges_words.
Source code in src/moczarr/coverage.py
138 139 140 141 142 143 144 145 146 147 148 | |