package lattice // Insert places rec at key in branch and returns the record index. A key that // already occupies a record slot is overwritten in place. func (t *Tree) Insert(branch Branch, key Key, rec Record) (uint32, error) { if rIdx, found, e := t.insertExisting(branch, key, rec, false); e != nil || found { return rIdx, e } if t.MustNode(t.Roots[branch]).KeyCount() == C { if err := t.splitRoot(branch); err != nil { return 0, err } } rIdx := t.allocRecord() r := t.MustRecord(rIdx) *r = rec t.markRecDirty(rIdx) if !t.SkipRecKey { t.RecKey[rIdx] = key } if _, err := t.insertNonFull(t.Roots[branch], branch, key, rIdx); err != nil { return 0, err } return rIdx, nil } // InsertRec is Insert with both cross-branch links reset to NullRec. func (t *Tree) InsertRec(branch Branch, key Key, rec Record) (uint32, error) { if rIdx, found, e := t.insertExisting(branch, key, rec, true); e != nil || found { return rIdx, e } if t.MustNode(t.Roots[branch]).KeyCount() == C { if err := t.splitRoot(branch); err != nil { return 0, err } } rIdx := t.allocRecord() r := t.MustRecord(rIdx) *r = rec r.Link[0] = NullRec r.Link[1] = NullRec t.markRecDirty(rIdx) if !t.SkipRecKey { t.RecKey[rIdx] = key } if _, err := t.insertNonFull(t.Roots[branch], branch, key, rIdx); err != nil { return 0, err } return rIdx, nil } // insertExisting overwrites the record of an existing key, reporting found only // when the slot holds a real record. A key present with a NullRec pointer falls // through to a normal insert. func (t *Tree) insertExisting(branch Branch, key Key, rec Record, clearLinks bool) (recIdx uint32, found bool, err error) { idx, pos, present, err := t.Walk(branch, key) if err != nil || !present { return NullRec, false, err } existing := t.MustNode(idx).RecPtrs[pos] if existing == NullRec { return NullRec, false, nil } r := t.MustRecord(existing) *r = rec if clearLinks { r.Link[0] = NullRec r.Link[1] = NullRec } t.markRecDirty(existing) return existing, true, nil } func (t *Tree) insertNonFull(idx uint32, branch Branch, key Key, recIdx uint32) (uint32, error) { for depth := 0; depth < MaxTreeDepth; depth++ { node := t.MustNode(idx) k := node.KeyCount() p := 0 for p < k && node.Keys[p] < key { p++ } if p < k && node.Keys[p] == key { return idx, nil } if node.IsLeaf() { for i := k; i > p; i-- { node.Keys[i] = node.Keys[i-1] node.RecPtrs[i] = node.RecPtrs[i-1] } node.Keys[p] = key node.RecPtrs[p] = recIdx node.setCount(k + 1) node.SetBranch(branch) node.Mult = 1 t.markNodeDirty(idx) return idx, nil } childIdx := node.Children[p] if t.MustNode(childIdx).KeyCount() == C { median, _, err := t.splitChild(idx, p) if err != nil { return 0, err } node = t.MustNode(idx) if median == key { for pp := 0; pp < node.KeyCount(); pp++ { if node.Keys[pp] == key { return idx, nil } } } if median < key { p++ } childIdx = t.MustNode(idx).Children[p] } idx = childIdx } return 0, errCycle("insertNonFull", branch) } // splitChild splits the full child at parent.Children[childPos], promotes its // median key (index 4 for C=8) and returns it. Left keeps 0..3, right takes // 5..7. func (t *Tree) splitChild(parentIdx uint32, childPos int) (Key, uint32, error) { fullIdx := t.MustNode(parentIdx).Children[childPos] full := *t.MustNode(fullIdx) const mid = C / 2 median := full.Keys[mid] medianRec := full.RecPtrs[mid] rightIdx := t.allocNode() right := t.MustNode(rightIdx) rightCount := C - mid - 1 for i := 0; i < rightCount; i++ { right.Keys[i] = full.Keys[mid+1+i] right.RecPtrs[i] = full.RecPtrs[mid+1+i] } right.setCount(rightCount) right.SetBranch(full.GetBranch()) right.Mult = full.Mult if !full.IsLeaf() { for i := 0; i <= rightCount; i++ { right.Children[i] = full.Children[mid+1+i] } } t.markNodeDirty(rightIdx) left := t.MustNode(fullIdx) leftCount := mid for i := 0; i < leftCount; i++ { left.Keys[i] = full.Keys[i] left.RecPtrs[i] = full.RecPtrs[i] } for i := leftCount; i < C; i++ { left.Keys[i] = KeyZero left.RecPtrs[i] = NullRec } left.setCount(leftCount) if !full.IsLeaf() { for i := 0; i <= leftCount; i++ { left.Children[i] = full.Children[i] } for i := leftCount + 1; i <= C; i++ { left.Children[i] = NullIndex } } else { for i := range left.Children { left.Children[i] = NullIndex } } t.markNodeDirty(fullIdx) parent := t.MustNode(parentIdx) pk := parent.KeyCount() for i := pk; i > childPos; i-- { parent.Keys[i] = parent.Keys[i-1] parent.RecPtrs[i] = parent.RecPtrs[i-1] } parent.Keys[childPos] = median parent.RecPtrs[childPos] = medianRec for i := pk + 1; i > childPos+1; i-- { parent.Children[i] = parent.Children[i-1] } parent.Children[childPos+1] = rightIdx parent.setCount(pk + 1) t.markNodeDirty(parentIdx) return median, medianRec, nil } func (t *Tree) splitRoot(branch Branch) error { rootIdx := t.Roots[branch] leftIdx := t.allocNode() *t.MustNode(leftIdx) = *t.MustNode(rootIdx) t.markNodeDirty(leftIdx) root := t.MustNode(rootIdx) *root = Node{} initNodeChildren(root) root.Children[0] = leftIdx root.SetBranch(branch) root.Mult = 1 t.markNodeDirty(rootIdx) // The promoted median key and record pointer are the new root's only key, // at index 0. The moxie source discards splitChild's median here, which // leaves the new root with no separator, so a key equal to the promoted // median becomes unreachable and its old record orphaned. median, medianRec, err := t.splitChild(rootIdx, 0) if err != nil { return err } root = t.MustNode(rootIdx) root.Keys[0] = median root.RecPtrs[0] = medianRec root.setCount(1) t.markNodeDirty(rootIdx) return nil }