forked from dgraph-io/ristretto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.go
710 lines (642 loc) · 17.5 KB
/
btree.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
* Copyright 2020 Dgraph Labs, Inc. and Contributors
*
* 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 z
import (
"fmt"
"math"
"os"
"reflect"
"strings"
"unsafe"
"github.com/forkiss/ristretto/z/simd"
)
var (
pageSize = os.Getpagesize()
maxKeys = (pageSize / 16) - 1
oneThird = int(float64(maxKeys) / 3)
)
const (
absoluteMax = uint64(math.MaxUint64 - 1)
minSize = 1 << 20
)
// Tree represents the structure for custom mmaped B+ tree.
// It supports keys in range [1, math.MaxUint64-1] and values [1, math.Uint64].
type Tree struct {
buffer *Buffer
data []byte
nextPage uint64
freePage uint64
stats TreeStats
}
func (t *Tree) initRootNode() {
// This is the root node.
t.newNode(0)
// This acts as the rightmost pointer (all the keys are <= this key).
t.Set(absoluteMax, 0)
}
// NewTree returns an in-memory B+ tree.
func NewTree(tag string) *Tree {
const defaultTag = "tree"
if tag == "" {
tag = defaultTag
}
t := &Tree{buffer: NewBuffer(minSize, tag)}
t.Reset()
return t
}
// NewTree returns a persistent on-disk B+ tree.
func NewTreePersistent(path string) (*Tree, error) {
t := &Tree{}
var err error
// Open the buffer from disk and set it to the maximum allocated size.
t.buffer, err = NewBufferPersistent(path, minSize)
if err != nil {
return nil, err
}
t.buffer.offset = uint64(len(t.buffer.buf))
t.data = t.buffer.Bytes()
// pageID can never be 0 if the tree has been initialized.
root := t.node(1)
isInitialized := root.pageID() != 0
if !isInitialized {
t.nextPage = 1
t.freePage = 0
t.initRootNode()
} else {
t.reinit()
}
return t, nil
}
// reinit sets the internal variables of a Tree, which are normally stored
// in-memory, but are lost when loading from disk.
func (t *Tree) reinit() {
// Calculate t.nextPage by finding the first node whose pageID is not set.
t.nextPage = 1
for int(t.nextPage)*pageSize < len(t.data) {
n := t.node(t.nextPage)
if n.pageID() == 0 {
break
}
t.nextPage++
}
maxPageId := t.nextPage - 1
// Calculate t.freePage by finding the page to which no other page points.
// This would be the head of the page linked list.
// tailPages[i] is true if pageId i+1 is not the head of the list.
tailPages := make([]bool, maxPageId)
// Mark all pages containing nodes as tail pages.
t.Iterate(func(n node) {
i := n.pageID() - 1
tailPages[i] = true
// If this is a leaf node, increment the stats.
if n.isLeaf() {
t.stats.NumLeafKeys += n.numKeys()
}
})
// pointedPages is a list of page IDs that the tail pages point to.
pointedPages := make([]uint64, 0)
for i, isTail := range tailPages {
if !isTail {
pageId := uint64(i) + 1
// Skip if nextPageId = 0, as that is equivalent to null page.
if nextPageId := t.node(pageId).uint64(0); nextPageId != 0 {
pointedPages = append(pointedPages, nextPageId)
}
t.stats.NumPagesFree++
}
}
// Mark all pages being pointed to as tail pages.
for _, pageId := range pointedPages {
i := pageId - 1
tailPages[i] = true
}
// There should only be one head page left.
for i, isTail := range tailPages {
if !isTail {
pageId := uint64(i) + 1
t.freePage = pageId
break
}
}
}
// Reset resets the tree and truncates it to maxSz.
func (t *Tree) Reset() {
// Tree relies on uninitialized data being zeroed out, so we need to Memclr
// the data before using it again.
Memclr(t.buffer.buf)
t.buffer.Reset()
t.buffer.AllocateOffset(minSize)
t.data = t.buffer.Bytes()
t.stats = TreeStats{}
t.nextPage = 1
t.freePage = 0
t.initRootNode()
}
// Close releases the memory used by the tree.
func (t *Tree) Close() error {
if t == nil {
return nil
}
return t.buffer.Release()
}
type TreeStats struct {
Allocated int // Derived.
Bytes int // Derived.
NumLeafKeys int // Calculated.
NumPages int // Derived.
NumPagesFree int // Calculated.
Occupancy float64 // Derived.
PageSize int // Derived.
}
// Stats returns stats about the tree.
func (t *Tree) Stats() TreeStats {
numPages := int(t.nextPage - 1)
out := TreeStats{
Bytes: numPages * pageSize,
Allocated: len(t.data),
NumLeafKeys: t.stats.NumLeafKeys,
NumPages: numPages,
NumPagesFree: t.stats.NumPagesFree,
PageSize: pageSize,
}
out.Occupancy = 100.0 * float64(out.NumLeafKeys) / float64(maxKeys*numPages)
return out
}
// BytesToUint64Slice converts a byte slice to a uint64 slice.
func BytesToUint64Slice(b []byte) []uint64 {
if len(b) == 0 {
return nil
}
var u64s []uint64
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&u64s))
hdr.Len = len(b) / 8
hdr.Cap = hdr.Len
hdr.Data = uintptr(unsafe.Pointer(&b[0]))
return u64s
}
func (t *Tree) newNode(bit uint64) node {
var pageId uint64
if t.freePage > 0 {
pageId = t.freePage
t.stats.NumPagesFree--
} else {
pageId = t.nextPage
t.nextPage++
offset := int(pageId) * pageSize
reqSize := offset + pageSize
if reqSize > len(t.data) {
t.buffer.AllocateOffset(reqSize - len(t.data))
t.data = t.buffer.Bytes()
}
}
n := t.node(pageId)
if t.freePage > 0 {
t.freePage = n.uint64(0)
}
zeroOut(n)
n.setBit(bit)
n.setAt(keyOffset(maxKeys), pageId)
return n
}
func getNode(data []byte) node {
return node(BytesToUint64Slice(data))
}
func zeroOut(data []uint64) {
for i := 0; i < len(data); i++ {
data[i] = 0
}
}
func (t *Tree) node(pid uint64) node {
// page does not exist
if pid == 0 {
return nil
}
start := pageSize * int(pid)
return getNode(t.data[start : start+pageSize])
}
// Set sets the key-value pair in the tree.
func (t *Tree) Set(k, v uint64) {
if k == math.MaxUint64 || k == 0 {
panic("Error setting zero or MaxUint64")
}
root := t.set(1, k, v)
if root.isFull() {
right := t.split(1)
left := t.newNode(root.bits())
// Re-read the root as the underlying buffer for tree might have changed during split.
root = t.node(1)
copy(left[:keyOffset(maxKeys)], root)
left.setNumKeys(root.numKeys())
// reset the root node.
zeroOut(root[:keyOffset(maxKeys)])
root.setNumKeys(0)
// set the pointers for left and right child in the root node.
root.set(left.maxKey(), left.pageID())
root.set(right.maxKey(), right.pageID())
}
}
// For internal nodes, they contain <key, ptr>.
// where all entries <= key are stored in the corresponding ptr.
func (t *Tree) set(pid, k, v uint64) node {
n := t.node(pid)
if n.isLeaf() {
t.stats.NumLeafKeys += n.set(k, v)
return n
}
// This is an internal node.
idx := n.search(k)
if idx >= maxKeys {
panic("search returned index >= maxKeys")
}
// If no key at idx.
if n.key(idx) == 0 {
n.setAt(keyOffset(idx), k)
n.setNumKeys(n.numKeys() + 1)
}
child := t.node(n.val(idx))
if child == nil {
child = t.newNode(bitLeaf)
n = t.node(pid)
n.setAt(valOffset(idx), child.pageID())
}
child = t.set(child.pageID(), k, v)
// Re-read n as the underlying buffer for tree might have changed during set.
n = t.node(pid)
if child.isFull() {
// Just consider the left sibling for simplicity.
// if t.shareWithSibling(n, idx) {
// return n
// }
nn := t.split(child.pageID())
// Re-read n and child as the underlying buffer for tree might have changed during split.
n = t.node(pid)
child = t.node(n.uint64(valOffset(idx)))
// Set child pointers in the node n.
// Note that key for right node (nn) already exist in node n, but the
// pointer is updated.
n.set(child.maxKey(), child.pageID())
n.set(nn.maxKey(), nn.pageID())
}
return n
}
// Get looks for key and returns the corresponding value.
// If key is not found, 0 is returned.
func (t *Tree) Get(k uint64) uint64 {
if k == math.MaxUint64 || k == 0 {
panic("Does not support getting MaxUint64/Zero")
}
root := t.node(1)
return t.get(root, k)
}
func (t *Tree) get(n node, k uint64) uint64 {
if n.isLeaf() {
return n.get(k)
}
// This is internal node
idx := n.search(k)
if idx == n.numKeys() || n.key(idx) == 0 {
return 0
}
child := t.node(n.uint64(valOffset(idx)))
assert(child != nil)
return t.get(child, k)
}
// DeleteBelow deletes all keys with value under ts.
func (t *Tree) DeleteBelow(ts uint64) {
root := t.node(1)
t.stats.NumLeafKeys = 0
t.compact(root, ts)
assert(root.numKeys() >= 1)
}
func (t *Tree) compact(n node, ts uint64) int {
if n.isLeaf() {
numKeys := n.compact(ts)
t.stats.NumLeafKeys += n.numKeys()
return numKeys
}
// Not leaf.
N := n.numKeys()
for i := 0; i < N; i++ {
assert(n.key(i) > 0)
childID := n.uint64(valOffset(i))
child := t.node(childID)
if rem := t.compact(child, ts); rem == 0 && i < N-1 {
// If no valid key is remaining we can drop this child. However, don't do that if this
// is the max key.
t.stats.NumLeafKeys -= child.numKeys()
child.setAt(0, t.freePage)
t.freePage = childID
n.setAt(valOffset(i), 0)
t.stats.NumPagesFree++
}
}
// We use ts=1 here because we want to delete all the keys whose value is 0, which means they no
// longer have a valid page for that key.
return n.compact(1)
}
func (t *Tree) iterate(n node, fn func(node)) {
fn(n)
if n.isLeaf() {
return
}
// Explore children.
for i := 0; i < maxKeys; i++ {
if n.key(i) == 0 {
return
}
childID := n.uint64(valOffset(i))
assert(childID > 0)
child := t.node(childID)
t.iterate(child, fn)
}
}
// Iterate iterates over the tree and executes the fn on each node.
func (t *Tree) Iterate(fn func(node)) {
root := t.node(1)
t.iterate(root, fn)
}
// IterateKV iterates through all keys and values in the tree.
// If newVal is non-zero, it will be set in the tree.
func (t *Tree) IterateKV(f func(key, val uint64) (newVal uint64)) {
t.Iterate(func(n node) {
// Only leaf nodes contain keys.
if !n.isLeaf() {
return
}
for i := 0; i < n.numKeys(); i++ {
key := n.key(i)
val := n.val(i)
// A zero value here means that this is a bogus entry.
if val == 0 {
continue
}
newVal := f(key, val)
if newVal != 0 {
n.setAt(valOffset(i), newVal)
}
}
})
}
func (t *Tree) print(n node, parentID uint64) {
n.print(parentID)
if n.isLeaf() {
return
}
pid := n.pageID()
for i := 0; i < maxKeys; i++ {
if n.key(i) == 0 {
return
}
childID := n.uint64(valOffset(i))
child := t.node(childID)
t.print(child, pid)
}
}
// Print iterates over the tree and prints all valid KVs.
func (t *Tree) Print() {
root := t.node(1)
t.print(root, 0)
}
// Splits the node into two. It moves right half of the keys from the original node to a newly
// created right node. It returns the right node.
func (t *Tree) split(pid uint64) node {
n := t.node(pid)
if !n.isFull() {
panic("This should be called only when n is full")
}
// Create a new node nn, copy over half the keys from n, and set the parent to n's parent.
nn := t.newNode(n.bits())
// Re-read n as the underlying buffer for tree might have changed during newNode.
n = t.node(pid)
rightHalf := n[keyOffset(maxKeys/2):keyOffset(maxKeys)]
copy(nn, rightHalf)
nn.setNumKeys(maxKeys - maxKeys/2)
// Remove entries from node n.
zeroOut(rightHalf)
n.setNumKeys(maxKeys / 2)
return nn
}
// shareWithSiblingXXX is unused for now. The idea is to move some keys to
// sibling when a node is full. But, I don't see any special benefits in our
// access pattern. It doesn't result in better occupancy ratios.
func (t *Tree) shareWithSiblingXXX(n node, idx int) bool {
if idx == 0 {
return false
}
left := t.node(n.val(idx - 1))
ns := left.numKeys()
if ns >= maxKeys/2 {
// Sibling is already getting full.
return false
}
right := t.node(n.val(idx))
// Copy over keys from right child to left child.
copied := copy(left[keyOffset(ns):], right[:keyOffset(oneThird)])
copied /= 2 // Considering that key-val constitute one key.
left.setNumKeys(ns + copied)
// Update the max key in parent node n for the left sibling.
n.setAt(keyOffset(idx-1), left.maxKey())
// Now move keys to left for the right sibling.
until := copy(right, right[keyOffset(oneThird):keyOffset(maxKeys)])
right.setNumKeys(until / 2)
zeroOut(right[until:keyOffset(maxKeys)])
return true
}
// Each node in the node is of size pageSize. Two kinds of nodes. Leaf nodes and internal nodes.
// Leaf nodes only contain the data. Internal nodes would contain the key and the offset to the
// child node.
// Internal node would have first entry as
// <0 offset to child>, <1000 offset>, <5000 offset>, and so on...
// Leaf nodes would just have: <key, value>, <key, value>, and so on...
// Last 16 bytes of the node are off limits.
// | pageID (8 bytes) | metaBits (1 byte) | 3 free bytes | numKeys (4 bytes) |
type node []uint64
func (n node) uint64(start int) uint64 { return n[start] }
// func (n node) uint32(start int) uint32 { return *(*uint32)(unsafe.Pointer(&n[start])) }
func keyOffset(i int) int { return 2 * i }
func valOffset(i int) int { return 2*i + 1 }
func (n node) numKeys() int { return int(n.uint64(valOffset(maxKeys)) & 0xFFFFFFFF) }
func (n node) pageID() uint64 { return n.uint64(keyOffset(maxKeys)) }
func (n node) key(i int) uint64 { return n.uint64(keyOffset(i)) }
func (n node) val(i int) uint64 { return n.uint64(valOffset(i)) }
func (n node) data(i int) []uint64 { return n[keyOffset(i):keyOffset(i+1)] }
func (n node) setAt(start int, k uint64) {
n[start] = k
}
func (n node) setNumKeys(num int) {
idx := valOffset(maxKeys)
val := n[idx]
val &= 0xFFFFFFFF00000000
val |= uint64(num)
n[idx] = val
}
func (n node) moveRight(lo int) {
hi := n.numKeys()
assert(hi != maxKeys)
// copy works despite of overlap in src and dst.
// See https://golang.org/pkg/builtin/#copy
copy(n[keyOffset(lo+1):keyOffset(hi+1)], n[keyOffset(lo):keyOffset(hi)])
}
const (
bitLeaf = uint64(1 << 63)
)
func (n node) setBit(b uint64) {
vo := valOffset(maxKeys)
val := n[vo]
val &= 0xFFFFFFFF
val |= b
n[vo] = val
}
func (n node) bits() uint64 {
return n.val(maxKeys) & 0xFF00000000000000
}
func (n node) isLeaf() bool {
return n.bits()&bitLeaf > 0
}
// isFull checks that the node is already full.
func (n node) isFull() bool {
return n.numKeys() == maxKeys
}
// Search returns the index of a smallest key >= k in a node.
func (n node) search(k uint64) int {
N := n.numKeys()
if N < 4 {
for i := 0; i < N; i++ {
if ki := n.key(i); ki >= k {
return i
}
}
return N
}
return int(simd.Search(n[:2*N], k))
// lo, hi := 0, N
// // Reduce the search space using binary seach and then do linear search.
// for hi-lo > 32 {
// mid := (hi + lo) / 2
// km := n.key(mid)
// if k == km {
// return mid
// }
// if k > km {
// // key is greater than the key at mid, so move right.
// lo = mid + 1
// } else {
// // else move left.
// hi = mid
// }
// }
// for i := lo; i <= hi; i++ {
// if ki := n.key(i); ki >= k {
// return i
// }
// }
// return N
}
func (n node) maxKey() uint64 {
idx := n.numKeys()
// idx points to the first key which is zero.
if idx > 0 {
idx--
}
return n.key(idx)
}
// compacts the node i.e., remove all the kvs with value < lo. It returns the remaining number of
// keys.
func (n node) compact(lo uint64) int {
N := n.numKeys()
mk := n.maxKey()
var left, right int
for right = 0; right < N; right++ {
if n.val(right) < lo && n.key(right) < mk {
// Skip over this key. Don't copy it.
continue
}
// Valid data. Copy it from right to left. Advance left.
if left != right {
copy(n.data(left), n.data(right))
}
left++
}
// zero out rest of the kv pairs.
zeroOut(n[keyOffset(left):keyOffset(right)])
n.setNumKeys(left)
// If the only key we have is the max key, and its value is less than lo, then we can indicate
// to the caller by returning a zero that it's OK to drop the node.
if left == 1 && n.key(0) == mk && n.val(0) < lo {
return 0
}
return left
}
func (n node) get(k uint64) uint64 {
idx := n.search(k)
// key is not found
if idx == n.numKeys() {
return 0
}
if ki := n.key(idx); ki == k {
return n.val(idx)
}
return 0
}
// set returns true if it added a new key.
func (n node) set(k, v uint64) (numAdded int) {
idx := n.search(k)
ki := n.key(idx)
if n.numKeys() == maxKeys {
// This happens during split of non-root node, when we are updating the child pointer of
// right node. Hence, the key should already exist.
assert(ki == k)
}
if ki > k {
// Found the first entry which is greater than k. So, we need to fit k
// just before it. For that, we should move the rest of the data in the
// node to the right to make space for k.
n.moveRight(idx)
}
// If the k does not exist already, increment the number of keys.
if ki != k {
n.setNumKeys(n.numKeys() + 1)
numAdded = 1
}
if ki == 0 || ki >= k {
n.setAt(keyOffset(idx), k)
n.setAt(valOffset(idx), v)
return
}
panic("shouldn't reach here")
}
func (n node) iterate(fn func(node, int)) {
for i := 0; i < maxKeys; i++ {
if k := n.key(i); k > 0 {
fn(n, i)
} else {
break
}
}
}
func (n node) print(parentID uint64) {
var keys []string
n.iterate(func(n node, i int) {
keys = append(keys, fmt.Sprintf("%d", n.key(i)))
})
if len(keys) > 8 {
copy(keys[4:], keys[len(keys)-4:])
keys[3] = "..."
keys = keys[:8]
}
fmt.Printf("%d Child of: %d num keys: %d keys: %s\n",
n.pageID(), parentID, n.numKeys(), strings.Join(keys, " "))
}