-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBPlusTree.go
106 lines (91 loc) · 2.16 KB
/
BPlusTree.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
package BplusTree
import (
"unsafe"
)
type NodeType int
const (
LEAF_NODE NodeType = iota
NON_LEAF_NODE
)
const (
MAX_ORDER = 100
)
type bPlusTree struct {
order int //阶数
root interface{} // 可能是 treeLeafNode 或是 treeNonLeafNode
//compareFunc func(a, b interface{}) bool
binarySearch func(keys []interface{}, key interface{}, size int) int
}
type nodeComm struct {
parent *treeNonLeafNode
parentIndex int
link *link
size int //保存当前key的size
}
type treeLeafNode struct {
nodeComm
keys []interface{}
data []interface{}
}
type treeNonLeafNode struct {
nodeComm
keys []interface{}
subPtr []interface{} //仅两种可能 1.treeLeafNode 2.treeNonLeafNode
}
var leafFieldOffset uintptr
var nonLeafFieldOffset uintptr
func getLeaf(l **link) *treeLeafNode {
if l == nil {
return nil
}
return (*treeLeafNode)(unsafe.Pointer(uintptr(unsafe.Pointer(l)) - leafFieldOffset))
}
func getNonLeaf(l **link) *treeNonLeafNode {
if l == nil {
return nil
}
return (*treeNonLeafNode)(unsafe.Pointer(uintptr(unsafe.Pointer(l)) - nonLeafFieldOffset))
}
func init() {
dummy := &treeLeafNode{}
leafFieldOffset = uintptr(unsafe.Pointer(&dummy.link)) - uintptr(unsafe.Pointer(dummy))
nonLeafDummy := &treeNonLeafNode{}
nonLeafFieldOffset = uintptr(unsafe.Pointer(&nonLeafDummy.link)) - uintptr(unsafe.Pointer(nonLeafDummy))
}
/*
* 创建一颗指定阶数的B+树
*/
func InitBPlusTree(order int, compareFunc func(a, b interface{}) int, keyExample interface{}) *bPlusTree {
if order < 3 || order > MAX_ORDER {
panic("B+树的阶数不在范围内")
return nil
}
binarySearch := generateKeyBinarySearchFunc(compareFunc, keyExample)
return &bPlusTree{order, nil, binarySearch}
}
func newLeafNode(order int) *treeLeafNode {
return &treeLeafNode{
nodeComm{
nil,
-1,
newLink(),
0},
make([]interface{}, order+1),
make([]interface{}, order+1),}
/*
* 为啥要make order+1个空间呢 ?
* 因为为了分裂方便
*/
}
func newNonLeafNode(order int) *treeNonLeafNode {
return &treeNonLeafNode{
nodeComm{
nil,
-1,
newLink(),
0,
},
make([]interface{}, order),
make([]interface{}, order+1),
}
}