forked from naqvijafar91/cuteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.go
53 lines (44 loc) · 1005 Bytes
/
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
package cutedb
// btree - Our in memory btree struct
type btree struct {
root node
}
// node - Interface for node
type node interface {
insertPair(value *pairs, bt *btree) error
getValue(key string) (string, error)
printTree(level int)
}
func (bt *btree) isRootNode(n node) bool {
return bt.root == n
}
// NewBtree - Create a new btree
func initializeBtree(path ...string) (*btree, error) {
bs := initBlockService()
dns := newDiskNodeService(bs)
root, err := dns.getRootNodeFromDisk()
if err != nil {
panic(err)
}
return &btree{root: root}, nil
}
// insert - insert element in tree
func (bt *btree) insert(value *pairs) error {
return bt.root.insertPair(value, bt)
}
func (bt *btree) get(key string) (string, bool, error) {
value, err := bt.root.getValue(key)
if err != nil {
return "", false, err
}
if value == "" {
return "", false, nil
}
return value, true, nil
}
func (bt *btree) setRootNode(n node) {
bt.root = n
}
func (bt *btree) print() {
bt.root.printTree(0)
}