-
Notifications
You must be signed in to change notification settings - Fork 0
/
lokidb.go
149 lines (115 loc) · 3.13 KB
/
lokidb.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Fast on-disk key value store
package engine
import (
"context"
"os"
"path/filepath"
"sync"
"github.com/lokidb/engine/consistent"
"github.com/lokidb/engine/cursor"
filestore "github.com/lokidb/engine/file_storage"
lrucache "github.com/lokidb/engine/lrucache"
)
const fileExtension = ".loki"
const filePrefix = "ldb-"
const aolFilename = "mutations_log"
// Features flags
const toggleAOL = false
type storage struct {
rootPath string
aolPath string
aolLock sync.Mutex
lruCache lrucache.Cache
fileStores map[string]*filestore.FileKeyValueStore
filesRing consistent.ConsistentHash
}
type KeyValueStore interface {
Set(string, []byte) error
Get(string, func(cursor.Cursor) ([]byte, error)) []byte
Del(string) bool
Keys() []string
Flush()
Search(context.Context, func(value []byte) bool) ([][]byte, error)
}
func New(rootPath string, cacheSize int, filesCount int) KeyValueStore {
s := new(storage)
s.rootPath = rootPath
s.aolPath = filepath.Join(rootPath, aolFilename+fileExtension)
s.lruCache = lrucache.New(cacheSize)
s.fileStores = createFileStores(s.rootPath, filesCount)
s.filesRing, _ = consistent.New(100000)
for filename := range s.fileStores {
(s.filesRing).AddMember(filename)
}
return s
}
func (s *storage) Set(key string, value []byte) error {
s.appendToLog("SET", key, value)
filename := s.filesRing.GetMemberForKey(key)
fileStore := s.fileStores[filename]
cacheValue := s.lruCache.Get(key)
if equals(cacheValue, value) {
return nil
}
s.lruCache.Push(key, value)
err := fileStore.Set(key, value)
return err
}
// get key from storage, specify valueReader to read only specific section from the value
// or nil for the full value
func (s *storage) Get(key string, valueReader func(cursor.Cursor) ([]byte, error)) []byte {
value := s.lruCache.Get(key)
if value != nil {
return value
}
filename := s.filesRing.GetMemberForKey(key)
fileStore := s.fileStores[filename]
value, _ = fileStore.Get(key, valueReader)
s.lruCache.Push(key, value)
return value
}
func (s *storage) Del(key string) bool {
s.appendToLog("DEL", key, nil)
filename := s.filesRing.GetMemberForKey(key)
fileStore := s.fileStores[filename]
s.lruCache.Del(key)
err := fileStore.Del(key)
return err == nil
}
func (s *storage) Keys() []string {
keys := make([]string, 0, 10000)
for _, filestore := range s.fileStores {
keys = append(keys, filestore.Keys()...)
}
return keys
}
// Delete all files and clear all RAM data
func (s *storage) Flush() {
if toggleAOL {
s.aolLock.Lock()
defer s.aolLock.Unlock()
os.Remove(s.aolPath)
}
s.lruCache.Clear()
var wg sync.WaitGroup
for _, fs := range s.fileStores {
wg.Add(1)
go func(fs *filestore.FileKeyValueStore) {
fs.Flush()
wg.Done()
}(fs)
}
wg.Wait()
}
// scan all the values in the store and filter them with the 'evaluate' function
func (s *storage) Search(ctx context.Context, evaluate func(value []byte) bool) ([][]byte, error) {
results := make([][]byte, 0, 1000)
for _, fs := range s.fileStores {
fsResults, err := fs.Search(ctx, evaluate)
if err != nil {
return nil, err
}
results = append(results, fsResults...)
}
return results, nil
}