Skip to the workspace
Pebellslearn by building
Bits & Machine Numbers  ›  Capstone Project

Capstone — Quarry's Record Codec

45 minPacked header words, a shard bitset and wraparound-safe epochs, working as one codec

dsa / Capstone — Quarry's Record Codec

Explore each scene at your own pace.

01 · The layout, as data

The layout, as data

Capstone — Quarry's Record Codec

Definition
A record codec packs each record's fields into one integer using shifts and masks with width checks, collects shard membership in a bitset for quick filtering, and compares wrapping epoch counters with masked subtraction.
In plain words
Think of squeezing a form with many boxes into a single tidy number, then reading it back without losing anything. A row of tick-boxes tells you which shards are worth opening, and a clock-style counter that wraps round still lets you tell which stamp is newer.

In short: the record codec is every earlier lesson in one pipeline — pack each record's fields with shifts and width checks (lessons 1–2) in O(1) per record, OR each accepted record's shard into a 64-bit bitset (lessons 3–4) for an O(1) "worth opening" test, and compare epochs with masked subtraction so the ring they count around (lesson

  1. doesn't break "newer than". Encoding n records is O(n) time and O(n) space for the output words, with O(1) extra space per record beyond that.

An 8-bit row where bits 1, 3 and 6 are lit green for the shards {1, 3, 6} a segment touched, labelled mask = 74

Eight lessons ago Quarry was a promise: a storage engine that keeps a hundred million record headers in 400 MB instead of 20 GB. You now have every piece that takes. This capstone joins them into the record codec, the part that turns a batch of incoming records into a segment. That's a list of packed 32-bit header words plus a small index that lets queries skip segments without reading them.

Here's what the codec has to do, and the lesson each part comes from:

Job Tool From
Pack id, shard, type and epoch into one word shifts, OR, width checks lessons 1–2
Refuse a record that doesn't fit value >> width must be 0 lesson 2
Answer "which shards are in this segment?" in one word a bitset, |=, popcount lessons 3–4
Compare epochs that wrap around at 2,048 masked subtraction lesson 1
Skip whole segments at query time masks as sets, & lessons 4 and 8

The layout, as data

Hard-coding shifts like << 20 in four places is how layouts drift apart. The codec keeps the layout as a table and derives every shift and mask from it. Changing the layout (lesson 2's repack) is then a one-line edit:

python

Packing walks the same table from the top. A field that doesn't fit makes the whole record invalid, and the codec rejects it instead of corrupting it:

What does this print?
LAYOUT = [("id", 12), ("shard", 6), ("type", 3), ("epoch", 11)]

def encode(record):
    w = 0
    for (name, width), value in zip(LAYOUT, record):
        if value < 0 or value >> width:
            return None
        w = (w << width) | value
    return w

print([encode(r) is None for r in ([4095, 63, 7, 2047], [4096, 0, 0, 0], [1, 2, 8, 3])])
Scene 1 of 8
Project context and further resources
You're building a piece ofQuarry — the record encoder
This piece — build_segment(): Packs a batch of records into header words and indexes which shards they touch.
Scenario A batch of records arrives for a new segment. The codec packs each header into 32 bits, turns away any record that would corrupt its neighbours, and stamps the segment with a 64-bit shard mask, so the query planner can skip it with a single AND.
build_segment.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
build_segment(records) → dictPacked words, rejected indices and the shard bitset for a segment.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.