-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexes.go
57 lines (44 loc) · 1.13 KB
/
indexes.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
package main
import (
"fmt"
"github.com/spy16/kiwi/index/bptree"
)
type IndexStorageType string
const (
File IndexStorageType = "file"
Memory IndexStorageType = "memory"
)
type Index struct {
tree *bptree.BPlusTree
}
func NewIndex(name string, driverType IndexStorageType) (*Index, error) {
var (
err error
tree *bptree.BPlusTree // Adjust the type to match the actual return type of bptree.Open
)
switch driverType {
case Memory:
tree, err = bptree.Open(":memory:", nil)
case File:
tree, err = bptree.Open(name, nil)
default:
return nil, fmt.Errorf("invalid driver type")
}
if err != nil {
return nil, err
}
return &Index{tree: tree}, nil
}
func (index *Index) Put(key []byte, value uint64) error {
return index.tree.Put(key, value)
}
func (index *Index) Get(key []byte) (uint64, error) {
return index.tree.Get(key)
}
func (index *Index) Del(key []byte) (uint64, error) {
return index.tree.Del(key)
}
// from the given key scan left or right depending on reverse flag
func (index *Index) Scan(key []byte, reverse bool, scanFn func(key []byte, v uint64) bool) error {
return index.tree.Scan(key, reverse, scanFn)
}