refactor: allow nested buckets

This commit is contained in:
Derrick Hammer 2024-03-09 07:15:54 -05:00
parent 0e82207cde
commit 42fa773b52
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 16 additions and 4 deletions

View File

@ -24,12 +24,23 @@ func (b *BboltDBKVStore) Open() error {
b.db = db b.db = db
} }
if b.bucket == nil && len(b.bucketName) > 0 { if len(b.bucketName) > 0 {
err := b.db.Update(func(txn *bbolt.Tx) error { err := b.db.Update(func(txn *bbolt.Tx) error {
bucket, err := txn.CreateBucketIfNotExists([]byte(b.bucketName)) var bucket *bbolt.Bucket
var err error
if b.bucket == nil {
bucket, err = txn.CreateBucketIfNotExists([]byte(b.bucketName))
if err != nil { if err != nil {
return err return err
} }
} else {
bucket, err = b.bucket.CreateBucketIfNotExists([]byte(b.bucketName))
if err != nil {
return err
}
}
b.bucket = bucket b.bucket = bucket
return nil return nil
}) })
@ -108,6 +119,7 @@ func (b *BboltDBKVStore) Delete(key []byte) error {
func (b *BboltDBKVStore) Bucket(prefix string) (KVStore, error) { func (b *BboltDBKVStore) Bucket(prefix string) (KVStore, error) {
return &BboltDBKVStore{ return &BboltDBKVStore{
db: b.db, db: b.db,
bucket: b.bucket,
bucketName: prefix, bucketName: prefix,
root: false, root: false,
}, nil }, nil