-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbadger.go
364 lines (312 loc) · 7.69 KB
/
badger.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package raftbadger
import (
"errors"
"math"
"os"
"path"
"github.com/dgraph-io/badger/v3"
"github.com/dgraph-io/badger/v3/options"
"github.com/hashicorp/raft"
)
var (
// bucket for raft logs
prefixDBLogs = []byte("logs-")
// bucket for raft config
prefixDBMeta = []byte("meta-")
// not found the key
ErrNotFoundKey = raft.ErrLogNotFound
ErrNotFoundFirstIndex = errors.New("not found first index")
ErrNotFoundLastIndex = errors.New("not found last index")
)
// buildConfKey prefixDBLogs + key
func buildLogsKey(idx uint64) []byte {
bs := append([]byte{}, prefixDBLogs...)
return append(bs, uint64ToBytes(idx)...)
}
// buildMetaKey prefixDBConfig + key
func buildMetaKey(key []byte) []byte {
var out = make([]byte, 0, len(key)+len(prefixDBMeta))
out = append(out, prefixDBMeta...)
return append(out, key...)
}
// parseIndexByLogsKey parse the index from logs key
func parseIndexByLogsKey(key []byte) uint64 {
rawkey := key[len(prefixDBLogs):]
idx := bytesToUint64(rawkey)
return idx
}
// getPrefixDBLogs clone new prefixDBLogs object
func getPrefixDBLogs() []byte {
return append([]byte{}, prefixDBLogs...)
}
type Config struct {
DataPath string `yaml:"dbpath" json:"dbpath" toml:"dbpath"`
Compression string `yaml:"compression" json:"compression" toml:"compression"`
DisableLogger bool `yaml:"disable_logger" json:"disable_logger" toml:"disable_logger"`
}
// Storage
type Storage struct {
config Config
opts *badger.Options
db *badger.DB
}
// New badger storage object with config and badger options.
func New(config Config, opts *badger.Options) (*Storage, error) {
if config.DataPath == "" {
config.DataPath = os.TempDir()
}
// reset dir config
fpath := path.Join(config.DataPath)
if opts == nil {
pv := badger.DefaultOptions(fpath)
opts = &pv
}
if opts.Dir == "" {
opts.Dir = fpath
}
if opts.ValueDir == "" {
opts.ValueDir = fpath
}
if config.DisableLogger {
opts.Logger = &nullLogger{}
}
switch config.Compression {
case "zstd":
opts.Compression = options.ZSTD
case "snappy":
opts.Compression = options.Snappy
}
// init storage
store := &Storage{
config: config,
opts: opts,
}
var err error
store.db, err = badger.Open(*store.opts)
if err != nil {
return nil, err
}
return store, err
}
// FirstIndex get the first index from the Raft log.
func (s *Storage) FirstIndex() (uint64, error) {
var (
first = uint64(0)
err error
)
err = s.db.View(func(txn *badger.Txn) error {
iter := txn.NewIterator(badger.DefaultIteratorOptions) // order asc
defer iter.Close()
var has bool
iter.Seek(prefixDBLogs)
if iter.ValidForPrefix(prefixDBLogs) {
item := iter.Item()
first = parseIndexByLogsKey(item.Key())
has = true
}
if !has {
return ErrNotFoundFirstIndex
}
return nil
})
return first, err
}
var maxSeekKey = append(getPrefixDBLogs(), uint64ToBytes(math.MaxUint64)...)
// LastIndex get the last index from the Raft log.
func (s *Storage) LastIndex() (uint64, error) {
var (
last = uint64(0)
err error
)
err = s.db.View(func(txn *badger.Txn) error {
opts := badger.IteratorOptions{
PrefetchValues: true, // prefetch values
PrefetchSize: 1, // default 100
Reverse: true, // order desc
}
iter := txn.NewIterator(opts)
defer iter.Close()
var has bool
iter.Seek(maxSeekKey)
if iter.ValidForPrefix(prefixDBLogs) {
item := iter.Item()
key := item.Key()[len(prefixDBLogs):]
last = bytesToUint64(key)
has = true
}
if !has {
return ErrNotFoundLastIndex
}
return nil
})
return last, err
}
// GetLog is used to get a log from Badger by a given index.
func (s *Storage) GetLog(idx uint64, log *raft.Log) error {
return s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(buildLogsKey(idx)))
if item == nil {
return raft.ErrLogNotFound
}
var val []byte
val, err = item.ValueCopy(val)
if err != nil {
return err
}
return decodeMsgpack(val, log)
})
}
// StoreLog is used to store a single raft log
func (s *Storage) StoreLog(log *raft.Log) error {
return s.StoreLogs([]*raft.Log{log})
}
// StoreLogs is used to store a set of raft logs
func (s *Storage) StoreLogs(logs []*raft.Log) error {
maxBatchSize := s.db.MaxBatchSize()
min := uint64(0)
max := uint64(len(logs))
ranges := s.generateRanges(min, max, maxBatchSize)
for _, r := range ranges {
txn := s.db.NewTransaction(true)
defer txn.Discard()
for index := r.from; index < r.to; index++ {
log := logs[index]
key := buildLogsKey(log.Index)
out, err := encodeMsgpack(log)
if err != nil {
return err
}
if err := txn.Set(key, out.Bytes()); err != nil {
return err
}
}
if err := txn.Commit(); err != nil {
return err
}
}
return nil
}
type iteratorRange struct{ from, to uint64 }
func (s *Storage) generateRanges(min, max uint64, batchSize int64) []iteratorRange {
nSegments := int(math.Round(float64((max - min) / uint64(batchSize))))
segments := []iteratorRange{}
if (max - min) <= uint64(batchSize) {
segments = append(segments, iteratorRange{from: min, to: max})
return segments
}
for len(segments) < nSegments {
nextMin := min + uint64(batchSize)
segments = append(segments, iteratorRange{from: min, to: nextMin})
min = nextMin + 1
}
segments = append(segments, iteratorRange{from: min, to: max})
return segments
}
// DeleteRange is used to delete logs within a given range.
func (s *Storage) DeleteRange(min, max uint64) error {
txn := s.db.NewTransaction(true)
defer func() {
txn.Discard()
}()
// create iterator, reset seek offset
iter := txn.NewIterator(badger.DefaultIteratorOptions)
iter.Rewind()
minKey := buildLogsKey(min)
for iter.Seek(minKey); iter.ValidForPrefix(prefixDBLogs); iter.Next() {
item := iter.Item()
// parse the index from logs key
idx := parseIndexByLogsKey(item.Key())
if idx > max {
break
}
// del kv
delKey := buildLogsKey(idx)
if err := txn.Delete(delKey); err != nil {
iter.Close()
return err
}
}
iter.Close()
if err := txn.Commit(); err != nil {
return err
}
return nil
}
// Set is used to set kv
func (s *Storage) Set(key, val []byte) error {
err := s.db.Update(func(txn *badger.Txn) error {
key = buildMetaKey(key)
return txn.Set(key, val)
})
return err
}
// Get is used to get value by key
func (s *Storage) Get(key []byte) ([]byte, error) {
var (
val []byte
err error
)
err = s.db.View(func(txn *badger.Txn) error {
key = buildMetaKey(key)
item, err := txn.Get(key)
if err != nil {
return raft.ErrLogNotFound
}
val, err = item.ValueCopy(val)
if err != nil {
return err
}
return nil
})
return val, err
}
// SetUint64 key and val
func (s *Storage) SetUint64(key []byte, val uint64) error {
key = buildMetaKey(key)
s.Set(key, uint64ToBytes(val))
return nil
}
// GetUint64 returns uint64 value of key
func (s *Storage) GetUint64(key []byte) (uint64, error) {
key = buildMetaKey(key)
val, err := s.Get(key)
if err != nil {
return 0, err
}
return bytesToUint64(val), nil
}
// Delete kv of the key
func (s *Storage) Delete(key []byte) error {
key = buildMetaKey(key)
err := s.db.Update(func(txn *badger.Txn) error {
return txn.Delete(key)
})
return err
}
// DropAll drop all kvs in the badger db.
func (s *Storage) DropAll() error {
return s.db.DropAll()
}
// Sync db to disk
func (s *Storage) Sync() error {
return s.db.Sync()
}
// Close the storage connection
func (s *Storage) Close() error {
if s.db == nil {
return nil
}
return s.db.Close()
}
// GetDB return badger database instance.
func (s *Storage) GetDB() *badger.DB {
return s.db
}
// DeleteFiles delete badgerDB files
func (s *Storage) DeleteFiles() {
if s.db != nil {
s.Close() // close() is safe call
}
os.RemoveAll(s.db.Opts().Dir)
os.RemoveAll(s.db.Opts().ValueDir)
}