-
Notifications
You must be signed in to change notification settings - Fork 678
/
binary_search_tree.go
380 lines (321 loc) · 8.23 KB
/
binary_search_tree.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package lsmkv
import (
"bytes"
"github.com/weaviate/weaviate/adapters/repos/db/lsmkv/rbtree"
"github.com/weaviate/weaviate/entities/lsmkv"
)
type binarySearchTree struct {
root *binarySearchNode
}
// returns net additions of insert in bytes, and previous secondary keys
func (t *binarySearchTree) insert(key, value []byte, secondaryKeys [][]byte) (int, [][]byte) {
if t.root == nil {
t.root = &binarySearchNode{
key: key,
value: value,
secondaryKeys: secondaryKeys,
colourIsRed: false, // root node is always black
}
return len(key) + len(value), nil
}
addition, newRoot, previousSecondaryKeys := t.root.insert(key, value, secondaryKeys)
if newRoot != nil {
t.root = newRoot
}
t.root.colourIsRed = false // Can be flipped in the process of balancing, but root is always black
return addition, previousSecondaryKeys
}
func (t *binarySearchTree) get(key []byte) ([]byte, error) {
if t.root == nil {
return nil, lsmkv.NotFound
}
return t.root.get(key)
}
func (t *binarySearchTree) setTombstone(key []byte, secondaryKeys [][]byte) {
if t.root == nil {
// we need to actively insert a node with a tombstone, even if this node is
// not present because we still need to propagate the delete into the disk
// segments. It could refer to an entity which was created in a previous
// segment and is thus unknown to this memtable
t.root = &binarySearchNode{
key: key,
value: nil,
tombstone: true,
secondaryKeys: secondaryKeys,
colourIsRed: false, // root node is always black
}
return
}
newRoot := t.root.setTombstone(key, secondaryKeys)
if newRoot != nil {
t.root = newRoot
}
t.root.colourIsRed = false // Can be flipped in the process of balancing, but root is always black
}
func (t *binarySearchTree) flattenInOrder() []*binarySearchNode {
if t.root == nil {
return nil
}
return t.root.flattenInOrder()
}
type countStats struct {
upsertKeys [][]byte
tombstonedKeys [][]byte
}
func (c *countStats) hasUpsert(needle []byte) bool {
if c == nil {
return false
}
for _, hay := range c.upsertKeys {
if bytes.Equal(needle, hay) {
return true
}
}
return false
}
func (c *countStats) hasTombstone(needle []byte) bool {
if c == nil {
return false
}
for _, hay := range c.tombstonedKeys {
if bytes.Equal(needle, hay) {
return true
}
}
return false
}
func (t *binarySearchTree) countStats() *countStats {
stats := &countStats{}
if t.root == nil {
return stats
}
t.root.countStats(stats)
return stats
}
type binarySearchNode struct {
key []byte
value []byte
secondaryKeys [][]byte
left *binarySearchNode
right *binarySearchNode
parent *binarySearchNode
tombstone bool
colourIsRed bool
}
func (n *binarySearchNode) Parent() rbtree.Node {
if n == nil {
return nil
}
return n.parent
}
func (n *binarySearchNode) SetParent(parent rbtree.Node) {
if n == nil {
addNewSearchNodeReceiver(&n)
}
if parent == nil {
n.parent = nil
return
}
n.parent = parent.(*binarySearchNode)
}
func (n *binarySearchNode) Left() rbtree.Node {
if n == nil {
return nil
}
return n.left
}
func (n *binarySearchNode) SetLeft(left rbtree.Node) {
if n == nil {
addNewSearchNodeReceiver(&n)
}
if left == nil {
n.left = nil
return
}
n.left = left.(*binarySearchNode)
}
func (n *binarySearchNode) Right() rbtree.Node {
if n == nil {
return nil
}
return n.right
}
func (n *binarySearchNode) SetRight(right rbtree.Node) {
if n == nil {
addNewSearchNodeReceiver(&n)
}
if right == nil {
n.right = nil
return
}
n.right = right.(*binarySearchNode)
}
func (n *binarySearchNode) IsRed() bool {
if n == nil {
return false
}
return n.colourIsRed
}
func (n *binarySearchNode) SetRed(isRed bool) {
n.colourIsRed = isRed
}
func (n *binarySearchNode) IsNil() bool {
return n == nil
}
func addNewSearchNodeReceiver(nodePtr **binarySearchNode) {
*nodePtr = &binarySearchNode{}
}
// returns net additions of insert in bytes
func (n *binarySearchNode) insert(key, value []byte, secondaryKeys [][]byte) (netAdditions int, newRoot *binarySearchNode, previousSecondaryKeys [][]byte) {
if bytes.Equal(key, n.key) {
// since the key already exists, we only need to take the difference
// between the existing value and the new one to determine net change
netAdditions = len(n.value) - len(value)
if netAdditions < 0 {
netAdditions *= -1
}
// assign new value to node
n.value = value
// reset tombstone in case it had one
n.tombstone = false
previousSecondaryKeys = n.secondaryKeys
n.secondaryKeys = secondaryKeys
newRoot = nil // tree root does not change when replacing node
return
}
if bytes.Compare(key, n.key) < 0 {
if n.left != nil {
netAdditions, newRoot, previousSecondaryKeys = n.left.insert(key, value, secondaryKeys)
return
} else {
n.left = &binarySearchNode{
key: key,
value: value,
secondaryKeys: secondaryKeys,
parent: n,
colourIsRed: true, // new nodes are always red, except root node which is handled in the tree itself
}
newRoot = binarySearchNodeFromRB(rbtree.Rebalance(n.left))
netAdditions = len(key) + len(value)
return
}
} else {
if n.right != nil {
netAdditions, newRoot, previousSecondaryKeys = n.right.insert(key, value, secondaryKeys)
return
} else {
n.right = &binarySearchNode{
key: key,
value: value,
secondaryKeys: secondaryKeys,
parent: n,
colourIsRed: true,
}
netAdditions = len(key) + len(value)
newRoot = binarySearchNodeFromRB(rbtree.Rebalance(n.right))
return
}
}
}
func (n *binarySearchNode) get(key []byte) ([]byte, error) {
if bytes.Equal(n.key, key) {
if !n.tombstone {
return n.value, nil
} else {
return nil, lsmkv.Deleted
}
}
if bytes.Compare(key, n.key) < 0 {
if n.left == nil {
return nil, lsmkv.NotFound
}
return n.left.get(key)
} else {
if n.right == nil {
return nil, lsmkv.NotFound
}
return n.right.get(key)
}
}
func (n *binarySearchNode) setTombstone(key []byte, secondaryKeys [][]byte) *binarySearchNode {
if bytes.Equal(n.key, key) {
n.value = nil
n.tombstone = true
n.secondaryKeys = secondaryKeys
return nil
}
if bytes.Compare(key, n.key) < 0 {
if n.left == nil {
n.left = &binarySearchNode{
key: key,
value: nil,
tombstone: true,
secondaryKeys: secondaryKeys,
parent: n,
colourIsRed: true,
}
return binarySearchNodeFromRB(rbtree.Rebalance(n.left))
}
return n.left.setTombstone(key, secondaryKeys)
} else {
if n.right == nil {
n.right = &binarySearchNode{
key: key,
value: nil,
tombstone: true,
secondaryKeys: secondaryKeys,
parent: n,
colourIsRed: true,
}
return binarySearchNodeFromRB(rbtree.Rebalance(n.right))
}
return n.right.setTombstone(key, secondaryKeys)
}
}
func (n *binarySearchNode) flattenInOrder() []*binarySearchNode {
var left []*binarySearchNode
var right []*binarySearchNode
if n.left != nil {
left = n.left.flattenInOrder()
}
if n.right != nil {
right = n.right.flattenInOrder()
}
right = append([]*binarySearchNode{n}, right...)
return append(left, right...)
}
// This is not very allocation friendly, since we basically need to allocate
// once for each element in the memtable. However, these results can
// potentially be cached, as we don't care about the intermediary results, just
// the net additions.
func (n *binarySearchNode) countStats(stats *countStats) {
if n.tombstone {
stats.tombstonedKeys = append(stats.tombstonedKeys, n.key)
} else {
stats.upsertKeys = append(stats.upsertKeys, n.key)
}
if n.left != nil {
n.left.countStats(stats)
}
if n.right != nil {
n.right.countStats(stats)
}
}
func binarySearchNodeFromRB(rbNode rbtree.Node) (bsNode *binarySearchNode) {
if rbNode == nil {
bsNode = nil
return
}
bsNode = rbNode.(*binarySearchNode)
return
}