-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.go
66 lines (53 loc) · 1.41 KB
/
index.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
package index
import (
"bitcask-go/data"
"bytes"
"github.com/google/btree"
)
// abstract index interface
type Indexer interface {
Put(key []byte, pos *data.LogRecordPos) *data.LogRecordPos
Get(key []byte) *data.LogRecordPos
Delete(key []byte) (*data.LogRecordPos, bool)
Iterator(reverse bool) Iterator
Size() int
Close() error
}
type IndexType = int8
const (
// BTree
Btree IndexType = iota + 1
// ARTree
ARtree
//B+Tree
BPtree
)
func NewIndexer(typ IndexType, dirPath string, sync bool) Indexer {
switch typ {
case Btree:
return NewBtree()
case ARtree:
return NewART()
case BPtree:
return NewBPlusTree(dirPath, sync)
default:
panic("unsupported index type")
}
}
type Item struct {
key []byte
pos *data.LogRecordPos
}
func (ai *Item) Less(bi btree.Item) bool {
return bytes.Compare(ai.key, bi.(*Item).key) == -1
}
// abstract index-iterator interface
type Iterator interface {
Rewind() // go back to the first data of iterator
Seek(key []byte) // find the first target key which is >= or <= params-key, and start traversing from target key
Next() // jump to the next key
Valid() bool // used to determine whether the traversal has been completed
Key() []byte // get key of current postion
Value() *data.LogRecordPos // get value of current positon
Close() // close iterator and release resources
}