-
Notifications
You must be signed in to change notification settings - Fork 183
/
index.go
72 lines (57 loc) · 1.81 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
package lotusdb
import (
"github.com/rosedblabs/diskhash"
)
const (
// indexFileExt is the file extension for index files.
indexFileExt = "INDEX.%d"
)
// Index is the interface for index implementations.
// An index is a key-value store that maps keys to chunk positions.
// The index is used to find the chunk position of a key.
//
// Currently, the only implementation is a BoltDB index.
// But you can implement your own index if you want.
type Index interface {
// PutBatch put batch records to index
PutBatch(keyPositions []*KeyPosition, matchKeyFunc ...diskhash.MatchKeyFunc) ([]*KeyPosition, error)
// Get chunk position by key
Get(key []byte, matchKeyFunc ...diskhash.MatchKeyFunc) (*KeyPosition, error)
// DeleteBatch delete batch records from index
DeleteBatch(keys [][]byte, matchKeyFunc ...diskhash.MatchKeyFunc) ([]*KeyPosition, error)
// Sync sync index data to disk
Sync() error
// Close index
Close() error
}
// open the specified index according to the index type
// currently, we support two index types: BTree and Hash,
// both of them are disk-based index.
func openIndex(options indexOptions) (Index, error) {
switch options.indexType {
case BTree:
return openBTreeIndex(options)
case Hash:
return openHashIndex(options)
default:
panic("unknown index type")
}
}
type IndexType int8
const (
// BTree is the BoltDB index type.
BTree IndexType = iota
// Hash is the diskhash index type.
// see: https://github.com/rosedblabs/diskhash
Hash
)
type indexOptions struct {
indexType IndexType
dirPath string // index directory path
partitionNum int // index partition nums for sharding
keyHashFunction func([]byte) uint64 // hash function for sharding
}
func (io *indexOptions) getKeyPartition(key []byte) int {
hashFn := io.keyHashFunction
return int(hashFn(key) % uint64(io.partitionNum))
}