-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvs.go
178 lines (138 loc) · 3.29 KB
/
kvs.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
package bclient
import (
"fmt"
"go.etcd.io/bbolt"
)
type KV struct {
Bucket *Bucket `json:"bucket,omitempty"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type KVs []*KV
// NewKV returns a new empty KV.
func NewKV() *KV {
return &KV{}
}
// SetKey sets the key for a KV.
func (kv *KV) SetKey(k string) *KV {
kv.Key = k
return kv
}
// SetBucket sets the bucket value for a KV.
func (kv *KV) SetBucket(b *Bucket) *KV {
kv.Bucket = b
return kv
}
// SetValue sets the value for a KV.
func (kv *KV) SetValue(v string) *KV {
kv.Value = v
return kv
}
// write creates a bolt transaction function.
func (kv KV) write() *boltTxn {
return createKV(kv)
}
// write creates a bolt transaction function.
func (kvs KVs) write() *boltTxn {
return createKVs(kvs)
}
// delete creates a bolt transaction function.
func (kv KV) delete() *boltTxn {
return deleteKV(kv)
}
// delete creates a b olt transaction function
func (kvs KVs) delete() *boltTxn {
return deleteKVs(kvs)
}
// read fetches the data from the KV bucket/key in the KV and sets the KV value.
func (kv *KV) read() *boltTxn {
var btxn boltTxn
btxn.txn = func(tx *bbolt.Tx) error {
bucket := getBucket(tx, kv.Bucket)
if bucket == nil {
return bbolt.ErrBucketNotFound
}
kv.Value = string(bucket.Get([]byte(kv.Key)))
return nil
}
return &btxn
}
// read fetches the values for the KV pairs in the KV slice.
func (kvs KVs) read() *boltTxn {
var btxn boltTxn
btxn.txn = func(tx *bbolt.Tx) error {
for _, v := range kvs {
bucket := getBucket(tx, v.Bucket)
if bucket == nil {
return bbolt.ErrBucketNotFound
}
v.Value = string(bucket.Get([]byte(v.Key)))
}
return nil
}
return &btxn
}
// createKV creates a new RW bolt transaction for the given KV.
func createKV(kv KV) *boltTxn {
var btxn boltTxn
btxn.txn = func(tx *bbolt.Tx) error {
bucket := getBucket(tx, kv.Bucket)
if bucket == nil {
return bbolt.ErrBucketNotFound
}
if err := bucket.Put([]byte(kv.Key), []byte(kv.Value)); err != nil {
return fmt.Errorf("error creating kv pair")
}
return nil
}
return &btxn
}
// createKVs creates a new RW bolt transaction for the slice of KVs.
func createKVs(kvs KVs) *boltTxn {
var btxn boltTxn
btxn.txn = func(tx *bbolt.Tx) error {
for _, v := range kvs {
bucket := getBucket(tx, v.Bucket)
if bucket == nil {
return bbolt.ErrBucketNotFound
}
if err := bucket.Put([]byte(v.Key), []byte(v.Value)); err != nil {
return fmt.Errorf("error creating kv pair")
}
}
return nil
}
return &btxn
}
// deleteKV deletes a KV pair in a bucket.
func deleteKV(kv KV) *boltTxn {
var btxn boltTxn
btxn.txn = func(tx *bbolt.Tx) error {
bucket := getBucket(tx, kv.Bucket)
if bucket == nil {
return bbolt.ErrBucketNotFound
}
if err := bucket.Delete([]byte(kv.Key)); err != nil {
return fmt.Errorf("error deleting kv")
}
return nil
}
return &btxn
}
// deleteKVs deletes a set of KV pairs.
func deleteKVs(kvs KVs) *boltTxn {
var btxn boltTxn
btxn.txn = func(tx *bbolt.Tx) error {
for _, v := range kvs {
bucket := tx.Bucket([]byte(v.Bucket.Name))
if bucket == nil {
return bbolt.ErrBucketNotFound
}
if err := bucket.Delete([]byte(v.Key)); err != nil {
return fmt.Errorf("error deleting key")
}
}
return nil
}
return &btxn
}