/
index.go
92 lines (71 loc) · 1.95 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
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
package index
import (
"bytes"
"github.com/ByteStorage/FlyDB/engine/data"
"github.com/google/btree"
)
/*
Indexer index interface abstraction layer.
If you want to access other data structures,
you can directly implement this interface
*/
type Indexer interface {
// Put stores the position information of the key in the index.
Put(key []byte, pst *data.LogRecordPst) bool
// Get retrieves the position information of the key from the index.
Get(key []byte) *data.LogRecordPst
// Delete deletes the position information of the key from the index.
Delete(key []byte) bool
// Size returns the number of entries in the index.
Size() int
// Iterator returns an iterator for the index.
Iterator(reverse bool) Iterator
}
type IndexType = int8
const (
// Btree Index
Btree IndexType = iota + 1
// ART Index
ART
// SkipList Index
SkipListIndex
)
func NewIndexer(typeIndex IndexType, dirPath string) Indexer {
switch typeIndex {
case Btree:
return NewBTree()
case ART:
return NewART()
case SkipListIndex:
return NewSkipList()
default:
panic("unsupported index type")
}
}
type Item struct {
key []byte
pst *data.LogRecordPst
}
func (i *Item) Less(bi btree.Item) bool {
return bytes.Compare(i.key, bi.(*Item).key) == -1
}
// Iterator is a generic index iterator.
type Iterator interface {
// Rewind resets the iterator to the beginning, i.e., the first entry.
Rewind()
// Seek seeks to a target key that is >= or <= the given key, depending on the implementation.
Seek(key []byte)
// Next moves to the next key.
Next()
// Valid returns whether the iterator is still valid, i.e., if all keys have been traversed.
Valid() bool
// Key returns the key at the current iterator position.
Key() []byte
// Value returns the value (position information) at the current iterator position.
Value() *data.LogRecordPst
// Close closes the iterator and releases any resources.
Close()
}
func Compare(a, b []byte) int {
return bytes.Compare(a, b)
}