← Back to all stagesStage 8Updated Debug Output:
Stage 13: B-Tree with Variable-Length Cells
Concept
Adapt the B-tree to store variable-length cells using the slotted page format. The key change: leaf page capacity depends on the actual data size, not a fixed cell count.
What It Teaches
- Variable capacity: With fixed 512-byte cells, a leaf always held ~7 rows. With variable-length cells, a leaf storing short rows (name="a") might hold 50+ rows, while long rows (name=200 chars) might fit only 5. The B-tree adapts automatically.
- Byte-based split trigger: Instead of
num_cells > MAX_CELLS, split whenfree_space < min_record_size + slot_size. This teaches the student to think in bytes, not counts. - Split strategy: On split, divide cells so each half gets roughly equal byte usage (not equal cell count). This balances the tree for varied row sizes.
- Internal nodes remain fixed: Internal node cells are
[key:4][child_page:4]= 8 bytes always. Only leaf cells are variable-length.
Learning Objectives
- Modify
btree_insertto use the slotted page format for leaf cells. - Implement byte-based split detection (
free_space < threshold). - Implement split logic that divides cells by byte usage.
- Verify that small rows produce more cells per leaf than Part 1.
- Verify that large rows trigger splits with fewer cells.
- Update
btree dumpto show cell byte sizes. - Ensure
btree find,btree structure, andSELECTall work with the new format.
Updated Debug Output: btree dump <page>
[BTREE] Page 0: type=LEAF num_cells=12 free_space=1200
[BTREE] Cell 0: key=1 (22 bytes)
[BTREE] Cell 1: key=2 (18 bytes)
[BTREE] Cell 2: key=3 (45 bytes)
How to Run
make test-c-stage13