-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode.go
More file actions
84 lines (68 loc) · 1.62 KB
/
Copy pathnode.go
File metadata and controls
84 lines (68 loc) · 1.62 KB
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
package btree
import "bytes"
type node interface {
isLeaf() bool
keyAt(int) []byte
numOfKeys() int
leftMostKey() []byte
get([]byte) []byte
insert([]byte, []byte, uint64) *insertResult
delete([]byte, uint64, node, int) *deleteResult
iterateNext(*iterator) bool
}
type baseNode struct {
tree *MVCCBtree
revision uint64
keys [][]byte
}
func (n *baseNode) minKeys() int {
return (n.tree.GetOrder()+1)/2 - 1
}
func (n *baseNode) maxKeys() int {
return n.tree.GetOrder() - 1
}
func (n *baseNode) keyAt(pos int) []byte {
return n.keys[pos]
}
func (n *baseNode) numOfKeys() int {
return len(n.keys)
}
func (n *baseNode) splitPivot() int {
return n.tree.GetOrder() / 2
}
// find equal or greater key pos. return exist(euqal) and index
func (n *baseNode) findPos(key []byte) (bool, int) {
for i := 0; i < len(n.keys); i++ {
c := bytes.Compare(n.keys[i], key)
if c == 0 {
return true, i
} else if c > 0 {
return false, i
}
}
return false, len(n.keys)
}
func (n *baseNode) insertKeyAt(idx int, key []byte) {
lastCnt := len(n.keys)
n.keys = append(n.keys, nil)
for i := lastCnt - 1; i >= idx; i-- {
n.keys[i+1] = n.keys[i]
}
n.keys[idx] = key
}
// for delete: select a sibling for borrow or merge
func (n *baseNode) selectSibling(parent node, parentPos int) int {
if parentPos == 0 {
return 1
}
if parentPos == parent.numOfKeys() {
return parentPos - 1
}
leftSibling := (parent.(*internalNode)).childAt(parentPos - 1)
rightSibling := (parent.(*internalNode)).childAt(parentPos + 1)
if leftSibling.numOfKeys() >= rightSibling.numOfKeys() {
return parentPos - 1
} else {
return parentPos + 1
}
}