/
delete.go
279 lines (234 loc) · 6.3 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Copyright 2014 Workiva, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package btree
import "bytes"
func (t *Tr) DeleteItems(values ...interface{}) ([]*Item, error) {
if len(values) == 0 {
return nil, nil
}
keys := make(Keys, 0, len(values))
err := t.Apply(func(item *Item) {
keys = append(keys, &Key{Value: item.Value, Payload: item.Payload})
}, values...)
if err != nil {
return nil, err
}
// we need to sort the keys to ensure that a multidelete
// distributes deletes across a single node correctly
keys = keys.sort(t.config.Comparator)
err = t.delete(keys)
if err != nil {
return nil, err
}
t.Count -= len(keys)
return keys.toItems(), nil
}
func (t *Tr) delete(keys Keys) error {
if len(keys) == 0 {
return nil
}
toDelete := make([]*Key, 0, len(keys))
for i := 0; i < len(keys); {
key := keys[i]
mapping := make(map[string]*Node, 10)
path, err := t.iterativeFind(key.Value, t.Root)
if err != nil {
return err
}
pb := path.peek()
node := pb.n
isRoot := bytes.Compare(node.ID, t.Root) == 0
if !t.context.nodeExists(node.ID) {
cp := node.copy()
t.context.addNode(cp)
mapping[string(node.ID)] = cp
node = cp
}
base := node
toDelete = append(toDelete, key)
for j := i + 1; j <= len(keys); j++ {
i = j
if j == len(keys) {
break
}
neighbor := keys[j]
if t.config.Comparator(neighbor.Value, node.lastValue()) <= 0 {
toDelete = append(toDelete, neighbor)
} else {
break
}
}
if len(toDelete) > len(node.ChildValues)/4 {
node.multiDelete(t.config.Comparator, toDelete...)
} else {
for _, k := range toDelete {
node.delete(t.config.Comparator, k)
}
}
toDelete = toDelete[:0]
if isRoot {
t.Root = node.ID
continue
}
for pb.prev != nil {
parentBundle := pb.prev
parent := parentBundle.n
isRoot := bytes.Compare(parent.ID, t.Root) == 0
if !t.context.nodeExists(parent.ID) {
cp := parent.copy()
t.context.addNode(cp)
mapping[string(parent.ID)] = cp
parent = cp
} else {
mapping[string(parent.ID)] = parent
}
if isRoot {
t.Root = parent.ID
}
i := pb.prev.i
parent.replaceKeyAt(&Key{UUID: node.ID}, i)
node = parent
pb = pb.prev
}
path.pop()
err = t.walkupDelete(key, base, path, mapping)
if err != nil {
return err
}
}
n := t.context.getNode(t.Root)
if n.lenValues() == 0 {
t.Root = nil
}
return nil
}
// walkupDelete is similar to walkupInsert but is only done one at a time.
// This is because deletes can cause borrowing or merging with neighbors which makes
// the changes non-local.
// TODO: come up with a good way to parallelize this.
func (t *Tr) walkupDelete(key *Key, node *Node, path *path, mapping map[string]*Node) error {
needsMerged := t.config.NodeWidth / 2
if needsMerged < 1 {
needsMerged = 1
}
if node.lenValues() >= needsMerged {
return nil
}
if string(node.ID) == string(t.Root) {
if node.lenKeys() == 1 {
id := node.keyAt(0)
t.Root = id.UUID
}
return nil
}
var getSibling = func(parent *Node, i int) (*Node, error) {
key := parent.keyAt(i)
n, err := t.contextOrCachedNode(key.UUID, true)
if err != nil {
return nil, err
}
if !t.context.nodeExists(n.ID) {
cp := t.copyNode(n)
mapping[string(n.ID)] = cp
parent.replaceKeyAt(&Key{UUID: cp.ID}, i)
n = cp
}
return n, nil
}
parentBundle := path.pop()
parent := mapping[string(parentBundle.n.ID)]
_, i := parent.searchKey(t.config.Comparator, key.Value)
siblingPosition := i
if i == parent.lenValues() {
siblingPosition--
} else {
siblingPosition++
}
sibling, err := getSibling(parent, siblingPosition)
if err != nil {
return err
}
prepend := false
// thing are just easier if we make this swap so we can grok
// left to right always assuming node is on the left and sibling
// is on the right
if siblingPosition < i {
node, sibling = sibling, node
prepend = true
}
// first case, can we just borrow? if so, simply shift values from one node
// to the other. Once done, replace the parent value with the middle value
// shifted and return.
if (sibling.lenValues()+node.lenValues())/2 >= needsMerged {
if i == parent.lenValues() {
i--
}
var key *Key
var value interface{}
for node.lenValues() < needsMerged || sibling.lenValues() < needsMerged {
if prepend {
correctedValue, key := node.popValue(), node.popKey()
if node.IsLeaf {
sibling.prependValue(correctedValue)
sibling.prependKey(key)
parent.replaceValueAt(i, node.lastValue())
} else {
parentValue := parent.valueAt(i)
sibling.prependKey(key)
sibling.prependValue(parentValue)
parent.replaceValueAt(i, correctedValue)
}
} else {
value, key = sibling.popFirstValue(), sibling.popFirstKey()
correctedValue := value
if !node.IsLeaf {
correctedValue = parent.valueAt(i)
}
node.appendValue(correctedValue)
node.appendChild(key)
parent.replaceValueAt(i, value)
}
}
return nil
}
// the harder case, we need to merge with sibling, pull a value down
// from the parent, and recurse on this function
// easier case, merge the nodes and delete value and child from parent
if node.IsLeaf {
node.append(sibling)
if prepend {
parent.deleteKeyAt(i)
} else {
parent.deleteKeyAt(i + 1)
}
if i == parent.lenValues() {
i--
}
parent.deleteValueAt(i)
return t.walkupDelete(key, parent, path, mapping)
}
// harder case, need to pull a value down from the parent, insert
// value into the left node, append the nodes, and then delete
// the value from the parent
valueIndex := i
if i == parent.lenValues() {
valueIndex--
}
parentValue := parent.valueAt(valueIndex)
node.appendValue(parentValue)
node.append(sibling)
parent.deleteKeyAt(i)
parent.deleteValueAt(valueIndex)
parent.replaceKeyAt(&Key{UUID: node.ID}, valueIndex)
return t.walkupDelete(key, parent, path, mapping)
}