-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbtree.go
More file actions
134 lines (122 loc) · 2.65 KB
/
btree.go
File metadata and controls
134 lines (122 loc) · 2.65 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
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
package btree
import (
"github.com/golang/protobuf/proto"
"time"
)
// Btree metadata
type Btree struct {
BtreeMetadata
gcIndex int64
dupnodelist map[int64]int
opChan chan *treeOperation
exitChan chan int
}
const (
// TreeSize is tree size
TreeSize = 1 << 10
// LeafSize is leaf size
LeafSize = 1 << 5
// NodeSize is node size
NodeSize = 1 << 6
)
// isNode, isLeaf is treenode tag
const (
isNode = iota
isLeaf
)
// NewBtree create a btree
func NewBtree() *Btree {
return NewBtreeSize(LeafSize, NodeSize)
}
// NewBtreeSize create new btree with custom leafsize/nodesize
func NewBtreeSize(leafsize int64, nodesize int64) *Btree {
tree := &Btree{
dupnodelist: make(map[int64]int),
opChan: make(chan *treeOperation),
BtreeMetadata: BtreeMetadata{
Root: proto.Int64(0),
Size: proto.Int64(TreeSize),
LeafMax: proto.Int64(leafsize),
NodeMax: proto.Int64(nodesize),
IndexCursor: proto.Int64(0),
Nodes: make([][]byte, TreeSize),
},
}
go tree.run()
return tree
}
func (t *Btree) run() {
tick := time.Tick(time.Second * 2)
for {
select {
case <-t.exitChan:
break
case op := <-t.opChan:
switch op.GetAction() {
case "insert":
op.errChan <- t.insert(op.TreeLog)
case "delete":
op.errChan <- t.dodelete(op.Key)
case "update":
op.errChan <- t.update(op.TreeLog)
case "search":
rst, err := t.search(op.Key)
op.valueChan <- rst
op.errChan <- err
}
t.Index = proto.Int64(t.GetIndexCursor())
case <-tick:
t.gc()
}
}
t.Marshal("treedump.tmp")
}
func (t *Btree) Sync(file string) {
//t.Marshal(file)
}
// Insert can insert record into a btree
func (t *Btree) Insert(key, value []byte) error {
q := &treeOperation{
valueChan: make(chan []byte),
errChan: make(chan error),
}
q.Action = proto.String("insert")
q.Key = key
q.Value = value
t.opChan <- q
return <-q.errChan
}
// Delete can delete record
func (t *Btree) Delete(key []byte) error {
q := &treeOperation{
valueChan: make(chan []byte),
errChan: make(chan error),
}
q.Action = proto.String("delete")
q.Key = key
t.opChan <- q
return <-q.errChan
}
// Search return value
func (t *Btree) Search(key []byte) ([]byte, error) {
q := &treeOperation{
valueChan: make(chan []byte),
errChan: make(chan error),
}
q.Action = proto.String("search")
q.Key = key
t.opChan <- q
return <-q.valueChan, <-q.errChan
}
// Update is used to update key/value
func (t *Btree) Update(key, value []byte) error {
q := &treeOperation{
valueChan: make(chan []byte),
errChan: make(chan error),
}
q.Action = proto.String("update")
q.Key = key
q.Value = value
t.opChan <- q
return <-q.errChan
}