-
Notifications
You must be signed in to change notification settings - Fork 51
/
badger_db.go
304 lines (260 loc) · 6.51 KB
/
badger_db.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
//go:build badgerdb
// +build badgerdb
package db
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/dgraph-io/badger/v4"
)
func init() { registerDBCreator(BadgerDBBackend, badgerDBCreator) }
func badgerDBCreator(dbName, dir string) (DB, error) {
return NewBadgerDB(dbName, dir)
}
// NewBadgerDB creates a Badger key-value store backed to the
// directory dir supplied. If dir does not exist, it will be created.
func NewBadgerDB(dbName, dir string) (*BadgerDB, error) {
// Since Badger doesn't support database names, we join both to obtain
// the final directory to use for the database.
path := filepath.Join(dir, dbName)
if err := os.MkdirAll(path, 0o755); err != nil {
return nil, err
}
opts := badger.DefaultOptions(path)
opts.SyncWrites = false // note that we have Sync methods
opts.Logger = nil // badger is too chatty by default
return NewBadgerDBWithOptions(opts)
}
// NewBadgerDBWithOptions creates a BadgerDB key value store
// gives the flexibility of initializing a database with the
// respective options.
func NewBadgerDBWithOptions(opts badger.Options) (*BadgerDB, error) {
db, err := badger.Open(opts)
if err != nil {
return nil, err
}
return &BadgerDB{db: db}, nil
}
type BadgerDB struct {
db *badger.DB
}
var _ DB = (*BadgerDB)(nil)
func (b *BadgerDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errKeyEmpty
}
var val []byte
err := b.db.View(func(txn *badger.Txn) error {
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return nil
} else if err != nil {
return err
}
val, err = item.ValueCopy(nil)
if err == nil && val == nil {
val = []byte{}
}
return err
})
return val, err
}
func (b *BadgerDB) Has(key []byte) (bool, error) {
if len(key) == 0 {
return false, errKeyEmpty
}
var found bool
err := b.db.View(func(txn *badger.Txn) error {
_, err := txn.Get(key)
if err != nil && err != badger.ErrKeyNotFound {
return err
}
found = (err != badger.ErrKeyNotFound)
return nil
})
return found, err
}
func (b *BadgerDB) Set(key, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
return b.db.Update(func(txn *badger.Txn) error {
return txn.Set(key, value)
})
}
func withSync(db *badger.DB, err error) error {
if err != nil {
return err
}
return db.Sync()
}
func (b *BadgerDB) SetSync(key, value []byte) error {
return withSync(b.db, b.Set(key, value))
}
func (b *BadgerDB) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
return b.db.Update(func(txn *badger.Txn) error {
return txn.Delete(key)
})
}
func (b *BadgerDB) DeleteSync(key []byte) error {
return withSync(b.db, b.Delete(key))
}
func (b *BadgerDB) Close() error {
return b.db.Close()
}
func (b *BadgerDB) Print() error {
return nil
}
func (b *BadgerDB) iteratorOpts(start, end []byte, opts badger.IteratorOptions) (*badgerDBIterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
txn := b.db.NewTransaction(false)
iter := txn.NewIterator(opts)
iter.Rewind()
iter.Seek(start)
if opts.Reverse && iter.Valid() && bytes.Equal(iter.Item().Key(), start) {
// If we're going in reverse, our starting point was "end",
// which is exclusive.
iter.Next()
}
return &badgerDBIterator{
reverse: opts.Reverse,
start: start,
end: end,
txn: txn,
iter: iter,
}, nil
}
func (b *BadgerDB) Iterator(start, end []byte) (Iterator, error) {
opts := badger.DefaultIteratorOptions
return b.iteratorOpts(start, end, opts)
}
func (b *BadgerDB) ReverseIterator(start, end []byte) (Iterator, error) {
opts := badger.DefaultIteratorOptions
opts.Reverse = true
return b.iteratorOpts(end, start, opts)
}
func (b *BadgerDB) Stats() map[string]string {
return nil
}
func (b *BadgerDB) Compact(start, end []byte) error {
// Explicit compaction is not currently supported in badger
return nil
}
func (b *BadgerDB) NewBatch() Batch {
wb := &badgerDBBatch{
db: b.db,
wb: b.db.NewWriteBatch(),
firstFlush: make(chan struct{}, 1),
}
wb.firstFlush <- struct{}{}
return wb
}
var _ Batch = (*badgerDBBatch)(nil)
type badgerDBBatch struct {
db *badger.DB
wb *badger.WriteBatch
// Calling db.Flush twice panics, so we must keep track of whether we've
// flushed already on our own. If Write can receive from the firstFlush
// channel, then it's the first and only Flush call we should do.
//
// Upstream bug report:
// https://github.com/dgraph-io/badger/issues/1394
firstFlush chan struct{}
}
func (b *badgerDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
return b.wb.Set(key, value)
}
func (b *badgerDBBatch) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
return b.wb.Delete(key)
}
func (b *badgerDBBatch) Write() error {
select {
case <-b.firstFlush:
return b.wb.Flush()
default:
return fmt.Errorf("batch already flushed")
}
}
func (b *badgerDBBatch) WriteSync() error {
return withSync(b.db, b.Write())
}
func (b *badgerDBBatch) Close() error {
select {
case <-b.firstFlush: // a Flush after Cancel panics too
default:
}
b.wb.Cancel()
return nil
}
type badgerDBIterator struct {
reverse bool
start, end []byte
txn *badger.Txn
iter *badger.Iterator
lastErr error
}
func (i *badgerDBIterator) Close() error {
i.iter.Close()
i.txn.Discard()
return nil
}
func (i *badgerDBIterator) Domain() (start, end []byte) { return i.start, i.end }
func (i *badgerDBIterator) Error() error { return i.lastErr }
func (i *badgerDBIterator) Next() {
if !i.Valid() {
panic("iterator is invalid")
}
i.iter.Next()
}
func (i *badgerDBIterator) Valid() bool {
if !i.iter.Valid() {
return false
}
if len(i.end) > 0 {
key := i.iter.Item().Key()
if c := bytes.Compare(key, i.end); (!i.reverse && c >= 0) || (i.reverse && c < 0) {
// We're at the end key, or past the end.
return false
}
}
return true
}
// Key implements Iterator.
// The caller should not modify the contents of the returned slice.
// Instead, the caller should make a copy and work on the copy.
func (i *badgerDBIterator) Key() []byte {
if !i.Valid() {
panic("iterator is invalid")
}
return i.iter.Item().Key()
}
// Value implements Iterator.
// The returned slice is a copy of the original data, therefore it is safe to modify.
func (i *badgerDBIterator) Value() []byte {
if !i.Valid() {
panic("iterator is invalid")
}
val, err := i.iter.Item().ValueCopy(nil)
if err != nil {
i.lastErr = err
}
return val
}