-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
49 lines (40 loc) · 896 Bytes
/
kv.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
package blockqueue
import (
"errors"
"log/slog"
"github.com/nutsdb/nutsdb"
"github.com/yudhasubki/blockqueue/pkg/etcd"
)
type kv struct {
db *etcd.Etcd
}
func newKv(etcd *etcd.Etcd) *kv {
return &kv{
db: etcd,
}
}
func (e *kv) readBucketTx(fn func(tx *nutsdb.Tx) error) error {
return e.db.Database().View(func(tx *nutsdb.Tx) error {
return fn(tx)
})
}
func (e *kv) updateBucketTx(fn func(tx *nutsdb.Tx) error) error {
return e.db.Database().Update(func(tx *nutsdb.Tx) error {
return fn(tx)
})
}
func (bucket *kv) createTxBucket(tx *nutsdb.Tx, structure uint16, bucketName string) error {
err := tx.NewBucket(structure, bucketName)
if err != nil {
if errors.Is(err, nutsdb.ErrBucketAlreadyExist) {
slog.Debug(
"bucket exist. skip create the bucket",
logPrefixErr, err,
logPrefixBucket, bucketName,
)
return nil
}
return err
}
return nil
}