insert.go raw
1 package lattice
2
3 // Insert places rec at key in branch and returns the record index. A key that
4 // already occupies a record slot is overwritten in place.
5 func (t *Tree) Insert(branch Branch, key Key, rec Record) (uint32, error) {
6 if rIdx, found, e := t.insertExisting(branch, key, rec, false); e != nil || found {
7 return rIdx, e
8 }
9 if t.MustNode(t.Roots[branch]).KeyCount() == C {
10 if err := t.splitRoot(branch); err != nil {
11 return 0, err
12 }
13 }
14 rIdx := t.allocRecord()
15 r := t.MustRecord(rIdx)
16 *r = rec
17 t.markRecDirty(rIdx)
18 if !t.SkipRecKey {
19 t.RecKey[rIdx] = key
20 }
21 if _, err := t.insertNonFull(t.Roots[branch], branch, key, rIdx); err != nil {
22 return 0, err
23 }
24 return rIdx, nil
25 }
26
27 // InsertRec is Insert with both cross-branch links reset to NullRec.
28 func (t *Tree) InsertRec(branch Branch, key Key, rec Record) (uint32, error) {
29 if rIdx, found, e := t.insertExisting(branch, key, rec, true); e != nil || found {
30 return rIdx, e
31 }
32 if t.MustNode(t.Roots[branch]).KeyCount() == C {
33 if err := t.splitRoot(branch); err != nil {
34 return 0, err
35 }
36 }
37 rIdx := t.allocRecord()
38 r := t.MustRecord(rIdx)
39 *r = rec
40 r.Link[0] = NullRec
41 r.Link[1] = NullRec
42 t.markRecDirty(rIdx)
43 if !t.SkipRecKey {
44 t.RecKey[rIdx] = key
45 }
46 if _, err := t.insertNonFull(t.Roots[branch], branch, key, rIdx); err != nil {
47 return 0, err
48 }
49 return rIdx, nil
50 }
51
52 // insertExisting overwrites the record of an existing key, reporting found only
53 // when the slot holds a real record. A key present with a NullRec pointer falls
54 // through to a normal insert.
55 func (t *Tree) insertExisting(branch Branch, key Key, rec Record, clearLinks bool) (recIdx uint32, found bool, err error) {
56 idx, pos, present, err := t.Walk(branch, key)
57 if err != nil || !present {
58 return NullRec, false, err
59 }
60 existing := t.MustNode(idx).RecPtrs[pos]
61 if existing == NullRec {
62 return NullRec, false, nil
63 }
64 r := t.MustRecord(existing)
65 *r = rec
66 if clearLinks {
67 r.Link[0] = NullRec
68 r.Link[1] = NullRec
69 }
70 t.markRecDirty(existing)
71 return existing, true, nil
72 }
73
74 func (t *Tree) insertNonFull(idx uint32, branch Branch, key Key, recIdx uint32) (uint32, error) {
75 for depth := 0; depth < MaxTreeDepth; depth++ {
76 node := t.MustNode(idx)
77 k := node.KeyCount()
78 p := 0
79 for p < k && node.Keys[p] < key {
80 p++
81 }
82 if p < k && node.Keys[p] == key {
83 return idx, nil
84 }
85 if node.IsLeaf() {
86 for i := k; i > p; i-- {
87 node.Keys[i] = node.Keys[i-1]
88 node.RecPtrs[i] = node.RecPtrs[i-1]
89 }
90 node.Keys[p] = key
91 node.RecPtrs[p] = recIdx
92 node.setCount(k + 1)
93 node.SetBranch(branch)
94 node.Mult = 1
95 t.markNodeDirty(idx)
96 return idx, nil
97 }
98 childIdx := node.Children[p]
99 if t.MustNode(childIdx).KeyCount() == C {
100 median, _, err := t.splitChild(idx, p)
101 if err != nil {
102 return 0, err
103 }
104 node = t.MustNode(idx)
105 if median == key {
106 for pp := 0; pp < node.KeyCount(); pp++ {
107 if node.Keys[pp] == key {
108 return idx, nil
109 }
110 }
111 }
112 if median < key {
113 p++
114 }
115 childIdx = t.MustNode(idx).Children[p]
116 }
117 idx = childIdx
118 }
119 return 0, errCycle("insertNonFull", branch)
120 }
121
122 // splitChild splits the full child at parent.Children[childPos], promotes its
123 // median key (index 4 for C=8) and returns it. Left keeps 0..3, right takes
124 // 5..7.
125 func (t *Tree) splitChild(parentIdx uint32, childPos int) (Key, uint32, error) {
126 fullIdx := t.MustNode(parentIdx).Children[childPos]
127 full := *t.MustNode(fullIdx)
128
129 const mid = C / 2
130 median := full.Keys[mid]
131 medianRec := full.RecPtrs[mid]
132
133 rightIdx := t.allocNode()
134 right := t.MustNode(rightIdx)
135 rightCount := C - mid - 1
136 for i := 0; i < rightCount; i++ {
137 right.Keys[i] = full.Keys[mid+1+i]
138 right.RecPtrs[i] = full.RecPtrs[mid+1+i]
139 }
140 right.setCount(rightCount)
141 right.SetBranch(full.GetBranch())
142 right.Mult = full.Mult
143 if !full.IsLeaf() {
144 for i := 0; i <= rightCount; i++ {
145 right.Children[i] = full.Children[mid+1+i]
146 }
147 }
148 t.markNodeDirty(rightIdx)
149
150 left := t.MustNode(fullIdx)
151 leftCount := mid
152 for i := 0; i < leftCount; i++ {
153 left.Keys[i] = full.Keys[i]
154 left.RecPtrs[i] = full.RecPtrs[i]
155 }
156 for i := leftCount; i < C; i++ {
157 left.Keys[i] = KeyZero
158 left.RecPtrs[i] = NullRec
159 }
160 left.setCount(leftCount)
161 if !full.IsLeaf() {
162 for i := 0; i <= leftCount; i++ {
163 left.Children[i] = full.Children[i]
164 }
165 for i := leftCount + 1; i <= C; i++ {
166 left.Children[i] = NullIndex
167 }
168 } else {
169 for i := range left.Children {
170 left.Children[i] = NullIndex
171 }
172 }
173 t.markNodeDirty(fullIdx)
174
175 parent := t.MustNode(parentIdx)
176 pk := parent.KeyCount()
177 for i := pk; i > childPos; i-- {
178 parent.Keys[i] = parent.Keys[i-1]
179 parent.RecPtrs[i] = parent.RecPtrs[i-1]
180 }
181 parent.Keys[childPos] = median
182 parent.RecPtrs[childPos] = medianRec
183 for i := pk + 1; i > childPos+1; i-- {
184 parent.Children[i] = parent.Children[i-1]
185 }
186 parent.Children[childPos+1] = rightIdx
187 parent.setCount(pk + 1)
188 t.markNodeDirty(parentIdx)
189
190 return median, medianRec, nil
191 }
192
193 func (t *Tree) splitRoot(branch Branch) error {
194 rootIdx := t.Roots[branch]
195 leftIdx := t.allocNode()
196 *t.MustNode(leftIdx) = *t.MustNode(rootIdx)
197 t.markNodeDirty(leftIdx)
198
199 root := t.MustNode(rootIdx)
200 *root = Node{}
201 initNodeChildren(root)
202 root.Children[0] = leftIdx
203 root.SetBranch(branch)
204 root.Mult = 1
205 t.markNodeDirty(rootIdx)
206
207 // The promoted median key and record pointer are the new root's only key,
208 // at index 0. The moxie source discards splitChild's median here, which
209 // leaves the new root with no separator, so a key equal to the promoted
210 // median becomes unreachable and its old record orphaned.
211 median, medianRec, err := t.splitChild(rootIdx, 0)
212 if err != nil {
213 return err
214 }
215 root = t.MustNode(rootIdx)
216 root.Keys[0] = median
217 root.RecPtrs[0] = medianRec
218 root.setCount(1)
219 t.markNodeDirty(rootIdx)
220 return nil
221 }
222