Now the index accepts data. Today you insert a key and value into the root leaf, keeping the entries sorted and overwriting a value when the key already exists - the first real mutation the tree can perform.
Insert a key/value into the root leaf in sorted order, overwriting the value if the key is already present.
Put uses the search from the last lesson twice over. If the key is found, the
value is overwritten in place - a B+Tree stores each key once, so a repeat Put is
an update, not a second entry. If the key is missing, the search hands back the
insertion point, and the new key and value are spliced in there, shifting the later
entries right to keep everything sorted.
Today the leaf can always absorb the insert because the tree only holds a handful of keys - the order-3 leaf has room. That happy path is deliberate: keeping keys sorted on the way in is one idea, and handling a full leaf that has to split is a whole chapter of its own. For now the index genuinely works - you can put keys and, next lesson, get them back.
func (t *Tree) Put(key, val uint64) {leaf := parseLeaf(t.pager.ReadPage(t.root))i, found := searchLeaf(leaf.Keys, key)// found: leaf.Vals[i] = val// else: insert key at i and val at i (shift the rest right)t.pager.WritePage(t.root, serializeLeaf(leaf))}