/
index.go
64 lines (52 loc) · 1.06 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
package mewdb
import (
"unsafe"
"github.com/rosedblabs/wal"
cache "github.com/xgzlucario/GigaCache"
)
// Index
type Index struct {
m *cache.GigaCache
}
// NewIndex
func NewIndex() *Index {
return &Index{
m: cache.New(cache.DefaultOptions),
}
}
type Keydir = *wal.ChunkPosition
// Get
func (i *Index) Get(key []byte) (keydir Keydir, ok bool) {
val, _, ok := i.m.Get(b2s(key))
if !ok {
return
}
keydir = wal.DecodeChunkPosition(val)
return keydir, true
}
// Set
func (i *Index) Set(key []byte, keydir Keydir, ttl int64) {
i.m.Set(b2s(key), keydir.Encode())
}
// Delete
func (i *Index) Delete(key []byte) bool {
return i.m.Remove(b2s(key))
}
// Scan
func (i *Index) Scan(f func(key []byte, ts int64, keydir Keydir) (next bool)) {
i.m.Scan(func(key, val []byte, ts int64) bool {
return f(key, ts, wal.DecodeChunkPosition(val))
})
}
// Len
func (i *Index) Len() int {
return int(i.m.Stat().Len)
}
// GC
func (i *Index) GC() {
i.m.Migrate()
}
// b2s converts byte slice to string unsafe.
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}