Chapters · Chapter 4

Anatomy of a BundleStore

Part II — The Engine Room · seven builds, seven receipts

These exercises wind your own odometers. Every build here lands a thirteen-row insert path on a real bundle, watches a Welford accumulator update against a value Bee can hand-check, or perturbs the storage-detect heuristic until the ground flips under your feet. The receipt class is verbatim-numeric: a log line, an X-Bundle-Mutation-Counter value, a storage_mode string, an integer comparison that decides whether a cache hits.

Run it live — GQL console

Point at the public read-only instance (no key needed) or your own engine (start it with GIGI_CORS_ORIGIN=* for browser access — dev only).

E4.1Reproduce the worked hero insert

Build

Drive BundleStore through 50 well-behaved temperature readings, then insert temp=35.5; capture mutation_counter, the pre-insert FieldStats, the per-record \(K\) from compute_record_k, and the post-insert (count, mean, m2) for both field_stats and curvature_stats.

Receipt

A harness whose verbatim block matches mine to the printed decimal — mean=20.5922, m2=228.7369, k_mean=0.004752, k_m2=0.022189 — as archived at harvest/book_ch4_worked_example.rs.

Bonus

Permute the order of the 50 seed inserts and confirm the final (mean, m2) is byte-identical: Welford is commutative as a monoid, and your test is the receipt.

E4.2Refute the naive variance formula at a 1e8 offset

Build

Run two accumulators side-by-side on the offset block from page 13 — \(\texttt{offset}=10^8\), values \(\{0.1, 0.2, 0.3, 0.5\}\) — where one uses my repaired Welford from src/bundle.rs:747-771 and the other the pre-b30cc99 naive sum_sq/n - (sum/n)\^{}2 form.

Receipt

A log showing the Welford branch within one part per billion of the exact \(0.021875\) (BLD-ANATOMY-WORKED-WELFORD), while the naive branch returns a number dominated by f64 round-off at \(4\times 10^{16}\).

Bonus

Sweep the offset by powers of ten and plot the absolute error of each branch — find the offset at which the naive form's variance estimate first crosses the true variance.

E4.3Stress the storage auto-detect at the 32nd insert

Build

Construct three single-integer-keyed bundles — one with keys 0..32, one with keys i*i+7, one with 31 consecutive integers plus a single stray at 999 — and assert store.storage_mode() after the 32nd insert.

Receipt

The three strings sequential, hashed, hybrid matching BLD-ANATOMY-WORKED-STORAGE; the hybrid bundle's flatness ratio computed by hand to \(30/31 = 0.968\), inside the \((0.95, 1.0)\) window from detect_base_geometry at src/bundle.rs:526.

Bonus

Keep feeding the hybrid bundle strays until the overflow ratio crosses \(0.05\) and watch row 13's promotion fire; record the exact insert at which storage_mode flips from hybrid to hashed.

E4.4Benchmark the O(1) curvature accessor ceiling

Build

Load a bundle with \(10\,000\) records, then time thirty thousand reads across curvature_stats' mean, standard deviation, and threshold accessors. Repeat at \(10^5\) and \(10^6\) records.

Receipt

A wall-clock total at or under my \(12\,\mu s\) on the \(10^4\) run (BLD-ANATOMY-WORKED-BUDGET), and a per-call cost that does not scale with record count — proving the accessor depends on field count, not \(n\).

Bonus

Add a fourth size at \(10^7\) records and confirm ad_7_2_stats_access_is_o1's five-millisecond budget for ten thousand calls still clears on your machine.

E4.5Wire a counter-compare cache against X-Bundle-Mutation-Counter

Build

Stand up a small cache in front of any derived bundle quantity (a SEMANTIC call, a vector-cache lookup, anything you choose); store counter_at_build alongside the value; on read, scrape X-Bundle-Mutation-Counter from the brain endpoint response header and compare.

Receipt

A test that (a) hits cold, (b) hits warm on a second call with no inserts, (c) misses after one POST insert because the header value advanced by exactly one — matching the contract in mutation_counter_contract at src/bundle.rs:4974.

Bonus

Add a 100-record batch_insert and verify the header advances by one, not one hundred — the batch-bump guard at src/bundle.rs:1176 from mutation_counter_bumps_on_batch_insert.

E4.6Forge the EXPLAIN-vs-executor divergence

Build

Find the IsNull-on-indexed-field corner the chapter calls out: write a query whose EXPLAIN output classes a predicate as filter while filtered_query_ex actually serves it from the bitmap.

Receipt

An EXPLAIN response whose plan line reads filter on that predicate, paired with a timing measurement showing the execution time matches index_scan, not a residual filter — demonstrating the QueryPlan struct (engine.rs:4372-4394) is descriptive, not operational.

Bonus

File the divergence as a third entry in Chapter 7's status-table corrections list — a book that checks its claims against the engine occasionally repairs the engine.

E4.7Instrument the trigger delivery gap

Build

Issue CREATE TRIGGER via GQL, perform a mutation that fires it, then inspect engine.pending_notifications directly through an in-process handle.

Receipt

A test showing the notification accumulates in pending_notifications but never reaches any HTTP route, WebSocket, or external consumer — the "last meter unwired" finding at engine.rs:1160, with the WAL persistence of the trigger definition itself verified by restart.

Bonus

Sketch a 50-line consumer that drains pending_notifications to stdout and propose it as the missing meter — prompt to spec, not yet code.

← Chapter 3 exercisesChapter 5 exercises →