@@ -20,24 +20,24 @@ import (
20
20
21
21
const fileMode = 0600
22
22
23
- // boltDB is KVStore implementation based bolt DB
24
- type boltDB struct {
23
+ // BoltDB is KVStore implementation based bolt DB
24
+ type BoltDB struct {
25
25
db * bolt.DB
26
26
path string
27
27
config config.DB
28
28
}
29
29
30
30
// NewBoltDB instantiates an BoltDB with implements KVStore
31
- func NewBoltDB (cfg config.DB ) KVStore {
32
- return & boltDB {
31
+ func NewBoltDB (cfg config.DB ) * BoltDB {
32
+ return & BoltDB {
33
33
db : nil ,
34
34
path : cfg .DbPath ,
35
35
config : cfg ,
36
36
}
37
37
}
38
38
39
39
// Start opens the BoltDB (creates new file if not existing yet)
40
- func (b * boltDB ) Start (_ context.Context ) error {
40
+ func (b * BoltDB ) Start (_ context.Context ) error {
41
41
db , err := bolt .Open (b .path , fileMode , nil )
42
42
if err != nil {
43
43
return errors .Wrap (ErrIO , err .Error ())
@@ -47,7 +47,7 @@ func (b *boltDB) Start(_ context.Context) error {
47
47
}
48
48
49
49
// Stop closes the BoltDB
50
- func (b * boltDB ) Stop (_ context.Context ) error {
50
+ func (b * BoltDB ) Stop (_ context.Context ) error {
51
51
if b .db != nil {
52
52
if err := b .db .Close (); err != nil {
53
53
return errors .Wrap (ErrIO , err .Error ())
@@ -57,7 +57,7 @@ func (b *boltDB) Stop(_ context.Context) error {
57
57
}
58
58
59
59
// Put inserts a <key, value> record
60
- func (b * boltDB ) Put (namespace string , key , value []byte ) (err error ) {
60
+ func (b * BoltDB ) Put (namespace string , key , value []byte ) (err error ) {
61
61
for c := uint8 (0 ); c < b .config .NumRetries ; c ++ {
62
62
if err = b .db .Update (func (tx * bolt.Tx ) error {
63
63
bucket , err := tx .CreateBucketIfNotExists ([]byte (namespace ))
@@ -76,7 +76,7 @@ func (b *boltDB) Put(namespace string, key, value []byte) (err error) {
76
76
}
77
77
78
78
// Get retrieves a record
79
- func (b * boltDB ) Get (namespace string , key []byte ) ([]byte , error ) {
79
+ func (b * BoltDB ) Get (namespace string , key []byte ) ([]byte , error ) {
80
80
var value []byte
81
81
err := b .db .View (func (tx * bolt.Tx ) error {
82
82
bucket := tx .Bucket ([]byte (namespace ))
@@ -102,7 +102,7 @@ func (b *boltDB) Get(namespace string, key []byte) ([]byte, error) {
102
102
}
103
103
104
104
// Filter returns <k, v> pair in a bucket that meet the condition
105
- func (b * boltDB ) Filter (namespace string , cond Condition , minKey , maxKey []byte ) ([][]byte , [][]byte , error ) {
105
+ func (b * BoltDB ) Filter (namespace string , cond Condition , minKey , maxKey []byte ) ([][]byte , [][]byte , error ) {
106
106
var fk , fv [][]byte
107
107
if err := b .db .View (func (tx * bolt.Tx ) error {
108
108
bucket := tx .Bucket ([]byte (namespace ))
@@ -148,7 +148,7 @@ func (b *boltDB) Filter(namespace string, cond Condition, minKey, maxKey []byte)
148
148
}
149
149
150
150
// Range retrieves values for a range of keys
151
- func (b * boltDB ) Range (namespace string , key []byte , count uint64 ) ([][]byte , error ) {
151
+ func (b * BoltDB ) Range (namespace string , key []byte , count uint64 ) ([][]byte , error ) {
152
152
value := make ([][]byte , count )
153
153
err := b .db .View (func (tx * bolt.Tx ) error {
154
154
bucket := tx .Bucket ([]byte (namespace ))
@@ -182,7 +182,7 @@ func (b *boltDB) Range(namespace string, key []byte, count uint64) ([][]byte, er
182
182
}
183
183
184
184
// GetBucketByPrefix retrieves all bucket those with const namespace prefix
185
- func (b * boltDB ) GetBucketByPrefix (namespace []byte ) ([][]byte , error ) {
185
+ func (b * BoltDB ) GetBucketByPrefix (namespace []byte ) ([][]byte , error ) {
186
186
allKey := make ([][]byte , 0 )
187
187
err := b .db .View (func (tx * bolt.Tx ) error {
188
188
if err := tx .ForEach (func (name []byte , b * bolt.Bucket ) error {
@@ -201,7 +201,7 @@ func (b *boltDB) GetBucketByPrefix(namespace []byte) ([][]byte, error) {
201
201
}
202
202
203
203
// GetKeyByPrefix retrieves all keys those with const prefix
204
- func (b * boltDB ) GetKeyByPrefix (namespace , prefix []byte ) ([][]byte , error ) {
204
+ func (b * BoltDB ) GetKeyByPrefix (namespace , prefix []byte ) ([][]byte , error ) {
205
205
allKey := make ([][]byte , 0 )
206
206
err := b .db .View (func (tx * bolt.Tx ) error {
207
207
buck := tx .Bucket (namespace )
@@ -220,7 +220,7 @@ func (b *boltDB) GetKeyByPrefix(namespace, prefix []byte) ([][]byte, error) {
220
220
}
221
221
222
222
// Delete deletes a record,if key is nil,this will delete the whole bucket
223
- func (b * boltDB ) Delete (namespace string , key []byte ) (err error ) {
223
+ func (b * BoltDB ) Delete (namespace string , key []byte ) (err error ) {
224
224
numRetries := b .config .NumRetries
225
225
for c := uint8 (0 ); c < numRetries ; c ++ {
226
226
if key == nil {
@@ -250,7 +250,7 @@ func (b *boltDB) Delete(namespace string, key []byte) (err error) {
250
250
}
251
251
252
252
// WriteBatch commits a batch
253
- func (b * boltDB ) WriteBatch (kvsb batch.KVStoreBatch ) (err error ) {
253
+ func (b * BoltDB ) WriteBatch (kvsb batch.KVStoreBatch ) (err error ) {
254
254
succeed := true
255
255
kvsb .Lock ()
256
256
defer func () {
@@ -306,12 +306,25 @@ func (b *boltDB) WriteBatch(kvsb batch.KVStoreBatch) (err error) {
306
306
return err
307
307
}
308
308
309
+ // BucketExists returns true if bucket exists
310
+ func (b * BoltDB ) BucketExists (namespace string ) bool {
311
+ var exist bool
312
+ _ = b .db .View (func (tx * bolt.Tx ) error {
313
+ bucket := tx .Bucket ([]byte (namespace ))
314
+ if bucket != nil {
315
+ exist = true
316
+ }
317
+ return nil
318
+ })
319
+ return exist
320
+ }
321
+
309
322
// ======================================
310
323
// below functions used by RangeIndex
311
324
// ======================================
312
325
313
326
// Insert inserts a value into the index
314
- func (b * boltDB ) Insert (name []byte , key uint64 , value []byte ) error {
327
+ func (b * BoltDB ) Insert (name []byte , key uint64 , value []byte ) error {
315
328
var err error
316
329
for i := uint8 (0 ); i < b .config .NumRetries ; i ++ {
317
330
if err = b .db .Update (func (tx * bolt.Tx ) error {
@@ -346,7 +359,7 @@ func (b *boltDB) Insert(name []byte, key uint64, value []byte) error {
346
359
}
347
360
348
361
// Seek returns value by the key
349
- func (b * boltDB ) Seek (name []byte , key uint64 ) ([]byte , error ) {
362
+ func (b * BoltDB ) Seek (name []byte , key uint64 ) ([]byte , error ) {
350
363
var value []byte
351
364
err := b .db .View (func (tx * bolt.Tx ) error {
352
365
bucket := tx .Bucket (name )
@@ -367,7 +380,7 @@ func (b *boltDB) Seek(name []byte, key uint64) ([]byte, error) {
367
380
}
368
381
369
382
// Remove removes an existing key
370
- func (b * boltDB ) Remove (name []byte , key uint64 ) error {
383
+ func (b * BoltDB ) Remove (name []byte , key uint64 ) error {
371
384
var err error
372
385
for i := uint8 (0 ); i < b .config .NumRetries ; i ++ {
373
386
if err = b .db .Update (func (tx * bolt.Tx ) error {
@@ -399,7 +412,7 @@ func (b *boltDB) Remove(name []byte, key uint64) error {
399
412
}
400
413
401
414
// Purge deletes an existing key and all keys before it
402
- func (b * boltDB ) Purge (name []byte , key uint64 ) error {
415
+ func (b * BoltDB ) Purge (name []byte , key uint64 ) error {
403
416
var err error
404
417
for i := uint8 (0 ); i < b .config .NumRetries ; i ++ {
405
418
if err = b .db .Update (func (tx * bolt.Tx ) error {
@@ -432,7 +445,7 @@ func (b *boltDB) Purge(name []byte, key uint64) error {
432
445
// ======================================
433
446
434
447
// intentionally fail to test DB can successfully rollback
435
- func (b * boltDB ) batchPutForceFail (namespace string , key [][]byte , value [][]byte ) error {
448
+ func (b * BoltDB ) batchPutForceFail (namespace string , key [][]byte , value [][]byte ) error {
436
449
return b .db .Update (func (tx * bolt.Tx ) error {
437
450
bucket , err := tx .CreateBucketIfNotExists ([]byte (namespace ))
438
451
if err != nil {
0 commit comments