forked from c4pt0r/pcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree_store.go
49 lines (42 loc) · 851 Bytes
/
btree_store.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
package main
/*
type btreeStore struct {
tree *btree.BTree
}
func (kv *KV) Less(than btree.Item) bool {
return bytes.Compare(kv.K, than.(*KV).K) < 0
}
func newBtreeStore() Store {
return &btreeStore{
tree: btree.New(2),
}
}
func (t *btreeStore) Scan(keyPref []byte, batchSize int) []*KV {
var res []*KV
i := 0
t.tree.AscendGreaterOrEqual(&KV{K: keyPref}, func(kv btree.Item) bool {
if i == batchSize {
return false
}
res = append(res, kv.(*KV))
return true
})
return res
}
func (t *btreeStore) Exists(key []byte) bool {
return t.tree.Has(&KV{K: key})
}
func (t *btreeStore) Put(kv *KV) error {
if !t.Exists(kv.K) {
t.tree.ReplaceOrInsert(kv)
return nil
}
return errors.New("key exists")
}
func (t *btreeStore) Get(key []byte) *KV {
if r := t.tree.Get(&KV{K: key}); r != nil {
return r.(*KV)
}
return nil
}
*/