-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkval-boltdb-functions.go
482 lines (460 loc) · 11.3 KB
/
kval-boltdb-functions.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package kvalbbolt
import (
"fmt"
bolt "go.etcd.io/bbolt"
"github.com/pkg/errors"
"regexp"
)
func initKvalresult() Kvalresult {
kr := Kvalresult{
Result: map[string]string{},
}
return kr
}
//Create bucket and key/value entries in bbolt from a kval structure
func createboltentries(kb Kvalboltdb) error {
var kq = kb.query
err := kb.DB.Update(func(tx *bolt.Tx) error {
var bucket *bolt.Bucket //we only ever need the 'last' bucket in memory
var err error
//create buckets
for index, bucketname := range kq.Buckets {
if index == 0 {
bucket, err = tx.CreateBucketIfNotExists([]byte(bucketname))
if err != nil {
return err
}
} else {
bucket, err = bucket.CreateBucketIfNotExists([]byte(bucketname))
if err != nil {
return err
}
}
}
//create key::values
if kq.Key != "" {
if kq.Value != "" {
//write value...
err = bucket.Put([]byte(kq.Key), []byte(kq.Value))
} else {
//write blank value if allowed... (UC: User may want to know unknown)
err = bucket.Put([]byte(kq.Key), []byte(""))
}
if err != nil {
return err
}
}
//commit transaction
return nil
})
return err
}
//Retrieve an entry from a bbolt db from a kval structure
func getboltentry(kb Kvalboltdb) (Kvalresult, error) {
var kr = initKvalresult()
var kq = kb.query
err := kb.DB.View(func(tx *bolt.Tx) error {
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
if bucket != nil {
val := bucket.Get([]byte(kq.Key))
kr.Result[kq.Key] = string(val)
}
//commit transaction
return nil
})
return kr, err
}
//Retrieve an entry from a bbolt db using regular expression
func getboltkeyregex(kb Kvalboltdb) (Kvalresult, error) {
var kq = kb.query
var kr = initKvalresult()
re, err := regexp.Compile(kq.Value)
if err != nil {
return kr, err
}
err = kb.DB.View(func(tx *bolt.Tx) error {
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
if bucket != nil {
bs := bucket.Stats()
if bs.KeyN > 0 {
cursor := bucket.Cursor()
k, v := cursor.First()
for k != nil {
if re.MatchString(string(k)) {
if v == nil {
kr.Result[string(k)] = Nestedbucket
} else {
kr.Result[string(k)] = string(v)
}
}
k, v = cursor.Next()
}
} else {
return errNoKVInBucket
}
}
//commit transaction
return nil
})
return kr, err
}
//Retrieve an entry from a bbolt db using regular expression
func getboltvalueregex(kb Kvalboltdb) (Kvalresult, error) {
var kq = kb.query
var kr = initKvalresult()
re, err := regexp.Compile(kq.Value)
if err != nil {
return kr, err
}
err = kb.DB.View(func(tx *bolt.Tx) error {
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
if bucket != nil {
bs := bucket.Stats()
if bs.KeyN > 0 {
cursor := bucket.Cursor()
k, v := cursor.First()
for k != nil {
if v != nil {
//nil means nested bucket: can't work with nested buckets for this search
if re.MatchString(string(v)) {
kr.Result[string(k)] = string(v)
}
}
k, v = cursor.Next()
}
} else {
return errNoKVInBucket
}
}
//commit transaction
return nil
})
return kr, err
}
//Retrieve all values from a single bucket per KVAL syntax
func getallfromrootbucket(kb Kvalboltdb) (Kvalresult, error) {
var kr = initKvalresult()
err := kb.DB.View(func(tx *bolt.Tx) error {
cursor := tx.Cursor()
k, v := cursor.First()
for k != nil {
if v == nil {
kr.Result[string(k)] = Nestedbucket
} else {
kr.Result[string(k)] = string(v)
}
k, v = cursor.Next()
}
return nil
})
return kr, err
}
//Retrieve all values from a single bucket per KVAL syntax
func getallfrombucket(kb Kvalboltdb) (Kvalresult, error) {
var kq = kb.query
var kr = initKvalresult()
err := kb.DB.View(func(tx *bolt.Tx) error {
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
if bucket != nil {
bs := bucket.Stats()
if bs.KeyN > 0 {
cursor := bucket.Cursor()
k, v := cursor.First()
for k != nil {
if v == nil {
kr.Result[string(k)] = Nestedbucket
} else {
kr.Result[string(k)] = string(v)
}
k, v = cursor.Next()
}
} else {
return errNoKVInBucket
}
}
//commit transaction
return nil
})
return kr, err
}
//Delete a single bucket from a bbolt db from a KVAL structure
func deletebucket(kb Kvalboltdb) error {
var kq = kb.query
err := kb.DB.Update(func(tx *bolt.Tx) error {
//as we're deleting a bucket we need a pointer to
//bucket level we're deleting minus one, that is
//the container of the bucket we're deleting
var delname = kq.Buckets[len(kq.Buckets)-1]
var searchindex = len(kq.Buckets) - 1
if searchindex == 0 {
//reset to one? this is the bucket we're deleting
searchindex = 1
delname = kq.Buckets[0]
err := tx.DeleteBucket([]byte(delname))
if err != nil {
return errors.Wrapf(err, "Bucket name: '%s'", delname)
}
} else {
bucketname := kq.Buckets[:searchindex]
bucket, err := gotobucket(tx, bucketname)
if err != nil {
return err
}
err = bucket.DeleteBucket([]byte(delname))
if err != nil {
return errors.Wrapf(err, "Bucket name: '%s'", delname)
}
}
return nil
})
return err
}
//Delete all the keys in a bbolt bucket leaving Bucket in tact
func deletebucketkeys(kb Kvalboltdb) error {
var kq = kb.query
err := kb.DB.Update(func(tx *bolt.Tx) error {
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
cursor := bucket.Cursor()
k, _ := cursor.First()
for k != nil {
err := bucket.Delete(k)
if err != nil {
if err == bolt.ErrIncompatibleValue {
//likely we're trying to delete a nested bucket
err = bucket.DeleteBucket(k)
if err != nil {
return err
}
} else {
return err
}
}
k, _ = cursor.Next()
}
return err
})
return err
}
//Delete a key and its corresponding value from a bbolt db
func deletekey(kb Kvalboltdb) error {
var kq = kb.query
err := kb.DB.Update(func(tx *bolt.Tx) error {
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
err = bucket.Delete([]byte(kb.query.Key))
if err != nil {
if err == bolt.ErrIncompatibleValue {
//likely we're trying to delete a nested bucket
err = bucket.DeleteBucket([]byte(kb.query.Key))
if err != nil {
return err
}
} else {
return err
}
}
return err
})
return err
}
//Turn a value for a given key to NULL based on KVAL capabilities
func nullifykeyvalue(kb Kvalboltdb) error {
var kq = kb.query
err := kb.DB.Update(func(tx *bolt.Tx) error {
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
err = bucket.Put([]byte(kq.Key), []byte(""))
if err != nil {
return err
}
return err
})
return err
}
//Rename a bucket (full OR empty) in a bbolt db
func renamebucket(kb Kvalboltdb) error {
var kq = kb.query
err := kb.DB.Update(func(tx *bolt.Tx) error {
var err error
var searchindex int
var newbucket, containerbucket *bolt.Bucket
bucketslen := len(kq.Buckets)
if bucketslen > 1 {
// we're working at the level of nested buckets
searchindex = bucketslen - 1
// Retrieve a container bucket in which to create our new bucket.
// it also contains the bucket that we're renaming, hence container.
// This will become a direct copy of the bucket we're renaming.
containerbucket, err = gotobucket(tx, kq.Buckets[:searchindex])
if err != nil {
return err
}
// create the new bucket here...
newbucket, err = containerbucket.CreateBucketIfNotExists([]byte(kq.Newname)) // n.b. bucket get
if err != nil {
return err
}
// get the bucket we're renaming
oldbucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
// copy the two buckets
err = copybuckets(oldbucket, newbucket)
if err != nil {
return err
}
// delete the origial bucket
oldname := []byte(kq.Buckets[len(kq.Buckets)-1:][0]) //n.b. bucket delete
err = containerbucket.DeleteBucket(oldname)
if err != nil {
return err
}
} else {
// we're working at the transaction level to create this new information
// create the new bucket here...
newbucket, err = tx.CreateBucketIfNotExists([]byte(kq.Newname)) //n.b. transaction get
if err != nil {
return err
}
// get the bucket we're renaming
oldbucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
// copy the two buckets
err = copybuckets(oldbucket, newbucket)
if err != nil {
return err
}
// delete the origial bucket
oldname := []byte(kq.Buckets[0])
err = tx.DeleteBucket(oldname) //n.b. transaction delete
if err != nil {
return err
}
}
// complete the transaction to allow bucket to be written
return nil
})
return err
}
// Helper function for rename to copy a bucket to a newly named bucket
func copybuckets(from, to *bolt.Bucket) error {
bs := from.Stats()
if bs.KeyN > 0 {
cursor := from.Cursor()
k, v := cursor.First()
for k != nil {
if v == nil {
//nested bucket
toNested, err := to.CreateBucketIfNotExists(k)
if err != nil {
return err
}
fromNested := from.Bucket(k)
copybuckets(fromNested, toNested)
} else {
to.Put(k, v)
}
k, v = cursor.Next()
}
} else {
return errNoKVInBucket
}
return nil
}
//Rename a key in a bbolt db based on described KVAL capabilities such as rename
func renamekey(kb Kvalboltdb) error {
var kq = kb.query
err := kb.DB.Update(func(tx *bolt.Tx) error {
//the bucket containing the key we're renaming
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
v := bucket.Get([]byte(kq.Key))
if v == nil {
return fmt.Errorf("Nil Value: Key doesn't exist or points to a nested bucket.")
}
err = bucket.Put([]byte(kq.Newname), v)
if err != nil {
return err
}
err = bucket.Delete([]byte(kq.Key))
if err != nil {
return err
}
return nil
})
return err
}
//Check to see if a key exists in a bbolt bucket, per KVAL LIS capabilities
func bucketkeyexists(kb Kvalboltdb) (Kvalresult, error) {
var kq = kb.query
var kr = initKvalresult()
err := kb.DB.Update(func(tx *bolt.Tx) error {
//the bucket containing the key we're renaming
bucket, err := gotobucket(tx, kq.Buckets)
if err != nil {
return err
}
if kq.Key != "" {
k := bucket.Get([]byte(kq.Key))
if k == nil {
return fmt.Errorf("Key '%s' does not exist.", kq.Key)
}
}
return nil
})
if err == nil {
kr.Exists = true
}
return kr, nil
}
//Retrieve a bucket pointer to work with from the bbolt db
func gotobucket(tx *bolt.Tx, bucketslice []string) (*bolt.Bucket, error) {
var bucket *bolt.Bucket
if len(bucketslice) > 0 {
for index, bucketname := range bucketslice {
if index == 0 { //need a bucket from our transaction pointer first
bucket = tx.Bucket([]byte(bucketname))
if bucket == nil { //only ever get nil if our root bucket doesn't exist
return bucket, errNilBucket
}
if len(bucketslice) == 1 && bucket != nil {
//return early, we've got out bucket
return bucket, nil
}
} else { //nested buckets, only returning if nil...
bucket = bucket.Bucket([]byte(bucketname))
if bucket == nil {
return bucket, errNilBucket
}
}
}
} else {
//gold plating at this point, easily handled elsewhere...
return bucket, errEmptyBucketSlice
}
return bucket, nil
}