-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobadger.go
230 lines (186 loc) · 4.68 KB
/
gobadger.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
package badger
import (
"fmt"
"strconv"
"sync"
"time"
"github.com/dgraph-io/badger"
"github.com/dgraph-io/badger/options"
)
// DB Interface
type DB interface {
Incr(k string, by int64) (int64, error)
Set(k, v string, ttl int) error
MSet(data map[string]string) error
Get(k string) (string, error)
MGet(keys []string) []string
Del(keys []string) error
Scan(ScannerOpt ScannerOptions) error
Size() int64
GC() error
}
// ScannerOptions - represents the options for a scanner
type ScannerOptions struct {
// from where to start
Offset string
// whether to include the value of the offset in the result or not
IncludeOffset bool
// the prefix that must be exists in each key in the iteration
Prefix string
// fetch the values (true) or this is a key only iteration (false)
FetchValues bool
// the handler that handles the incoming data
Handler func(k, v string) bool
}
// BadgerDB - represents a badger db implementation
type BadgerDB struct {
badger *badger.DB
countersLocks sync.RWMutex
}
// OpenBadger - Opens the specified path
func OpenBadger(path string) (*BadgerDB, error) {
opts := badger.DefaultOptions
opts.Dir = path
opts.ValueDir = path
opts.TableLoadingMode = options.LoadToRAM
opts.ValueLogLoadingMode = options.MemoryMap
bdb, err := badger.Open(opts)
if err != nil {
return nil, err
}
db := new(BadgerDB)
db.badger = bdb
db.countersLocks = sync.RWMutex{}
return db, nil
}
// Size - returns the size of the database (LSM + ValueLog) in bytes
func (db *BadgerDB) Size() int64 {
lsm, vlog := db.badger.Size()
return lsm + vlog
}
// GC - runs the garbage collector
func (db *BadgerDB) GC() error {
return db.badger.RunValueLogGC(0.5)
}
// Incr - increment the key by the specified value
func (db *BadgerDB) Incr(k string, by int64) (int64, error) {
db.countersLocks.Lock()
defer db.countersLocks.Unlock()
val, err := db.Get(k)
if err != nil {
val = ""
}
valFloat, _ := strconv.ParseInt(val, 10, 64)
valFloat += by
err = db.Set(k, fmt.Sprintf("%d", valFloat), -1)
if err != nil {
return 0, err
}
return valFloat, nil
}
// Set - sets a key with the specified value and optional ttl
func (db *BadgerDB) Set(k, v string, ttl int) error {
return db.badger.Update(func(txn *badger.Txn) (err error) {
if ttl < 1 {
err = txn.Set([]byte(k), []byte(v))
} else {
err = txn.SetWithTTL([]byte(k), []byte(v), time.Duration(ttl)*time.Millisecond)
}
return err
})
}
// MSet - sets multiple key-value pairs
func (db *BadgerDB) MSet(data map[string]string) error {
return db.badger.Update(func(txn *badger.Txn) (err error) {
for k, v := range data {
txn.Set([]byte(k), []byte(v))
}
return nil
})
}
// Get - fetches the value of the specified k
func (db *BadgerDB) Get(k string) (string, error) {
var data string
err := db.badger.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(k))
if err != nil {
return err
}
val, err := item.ValueCopy(nil)
if err != nil {
return err
}
data = string(val)
return nil
})
return data, err
}
// MGet - fetch multiple values of the specified keys
func (db *BadgerDB) MGet(keys []string) (data []string) {
db.badger.View(func(txn *badger.Txn) error {
for _, key := range keys {
item, err := txn.Get([]byte(key))
if err != nil {
data = append(data, "")
continue
}
val, err := item.ValueCopy(nil)
if err != nil {
data = append(data, "")
continue
}
data = append(data, string(val))
}
return nil
})
return data
}
// Del - removes key(s) from the store
func (db *BadgerDB) Del(keys []string) error {
return db.badger.Update(func(txn *badger.Txn) error {
for _, key := range keys {
txn.Delete([]byte(key))
}
return nil
})
}
// Scan - iterate over the whole store using the handler function
func (db *BadgerDB) Scan(scannerOpt ScannerOptions) error {
return db.badger.View(func(txn *badger.Txn) error {
iteratorOpts := badger.DefaultIteratorOptions
iteratorOpts.PrefetchValues = scannerOpt.FetchValues
it := txn.NewIterator(iteratorOpts)
defer it.Close()
start := func(it *badger.Iterator) {
if scannerOpt.Offset == "" {
it.Rewind()
} else {
it.Seek([]byte(scannerOpt.Offset))
if !scannerOpt.IncludeOffset && it.Valid() {
it.Next()
}
}
}
valid := func(it *badger.Iterator) bool {
if !it.Valid() {
return false
}
if scannerOpt.Prefix != "" && !it.ValidForPrefix([]byte(scannerOpt.Prefix)) {
return false
}
return true
}
for start(it); valid(it); it.Next() {
var k, v []byte
item := it.Item()
k = item.KeyCopy(nil)
if scannerOpt.FetchValues {
v, _ = item.ValueCopy(nil)
}
if !scannerOpt.Handler(string(k), string(v)) {
break
}
}
return nil
})
}