-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
299 lines (255 loc) · 6.4 KB
/
key.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
package bucketeer
import (
"encoding"
"encoding/binary"
"encoding/json"
"github.com/boltdb/bolt"
)
type Key interface {
KeyBytes() []byte
}
type ByteKey []byte
func NewByteKey(key []byte) ByteKey {
return ByteKey(key)
}
func (k ByteKey) KeyBytes() []byte {
return k
}
type StringKey string
func NewStringKey(key string) StringKey {
return StringKey(key)
}
func (k StringKey) KeyBytes() []byte {
return []byte(k)
}
type Uint64Key uint64
func NewUint64Key(key uint64) Uint64Key {
return Uint64Key(key)
}
func (k Uint64Key) KeyBytes() (b []byte) {
b = make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(k))
return
}
type Int64Key int64
func NewInt64Key(key int64) Int64Key {
return Int64Key(key)
}
func (k Int64Key) KeyBytes() (b []byte) {
b = make([]byte, 8)
k2 := uint64(1<<63) ^ uint64(k)
binary.BigEndian.PutUint64(b, k2)
return
}
type TextKey struct {
encoding.TextMarshaler
}
func NewTextKey(keyObj encoding.TextMarshaler) *TextKey {
return &TextKey{keyObj}
}
func (k TextKey) KeyBytes() (b []byte) {
var err error
if b, err = k.MarshalText(); err != nil {
panic(err.Error())
}
return
}
type BinaryKey struct {
encoding.BinaryMarshaler
}
func NewBinaryKey(keyObj encoding.BinaryMarshaler) *BinaryKey {
return &BinaryKey{keyObj}
}
func (k BinaryKey) KeyBytes() (b []byte) {
var err error
if b, err = k.MarshalBinary(); err != nil {
panic(err.Error())
}
return
}
type JsonKey struct {
keyObj interface{}
}
func NewJsonKey(keyObj interface{}) *JsonKey {
return &JsonKey{keyObj}
}
func (k JsonKey) KeyBytes() (b []byte) {
var err error
if b, err = json.Marshal(k.keyObj); err != nil {
panic(err.Error())
}
return
}
/*
Keyfarer encapsulates the components needed to resolve a key in BoltDB and provides convenience methods for setting and retrieving the value
*/
type Keyfarer struct {
bb *Bucketeer
key []byte
}
func NewKeyfarer(bb *Bucketeer, key []byte) (kf *Keyfarer) {
kf = &Keyfarer{
bb: bb,
key: key,
}
return
}
/*
PutByteValue sets the value for the key.
*/
func (kf *Keyfarer) PutByteValue(value []byte) error {
bf := func(b *bolt.Bucket) error {
return b.Put(kf.key, value)
}
return kf.bb.Update(bf)
}
/*
PutByteValue sets the value for the key.
*/
func (kf *Keyfarer) PutStringValue(value string) (err error) {
bf := func(b *bolt.Bucket) error {
return b.Put(kf.key, []byte(value))
}
return kf.bb.Update(bf)
}
/*
PutTextValue marshals the provided object into its textual form and sets it as the value for the key.
*/
func (kf *Keyfarer) PutTextValue(valueObj encoding.TextMarshaler) (err error) {
bf := func(b *bolt.Bucket) error {
return PutTextValue(b, kf.key, valueObj)
}
return kf.bb.Update(bf)
}
/*
PutBinaryValue marshals the provided object into its binary form and sets it as the value for the key.
*/
func (kf *Keyfarer) PutBinaryValue(valueObj encoding.BinaryMarshaler) error {
bf := func(b *bolt.Bucket) error {
return PutBinaryValue(b, kf.key, valueObj)
}
return kf.bb.Update(bf)
}
/*
PutJsonValue marshals the provided object into its JSON form and sets it as the value for the key.
*/
func (kf *Keyfarer) PutJsonValue(valueObj interface{}) error {
bf := func(b *bolt.Bucket) error {
return PutJsonValue(b, kf.key, valueObj)
}
return kf.bb.Update(bf)
}
func (kf *Keyfarer) PutVarintValue(value int64) error {
bf := func(b *bolt.Bucket) error {
return PutVarintValue(b, kf.key, value)
}
return kf.bb.Update(bf)
}
func (kf *Keyfarer) PutUvarintValue(value uint64) error {
bf := func(b *bolt.Bucket) error {
return PutUvarintValue(b, kf.key, value)
}
return kf.bb.Update(bf)
}
/*
GetByteValue gets the key's value as a byte slice.
*/
func (kf *Keyfarer) GetByteValue() (value []byte, err error) {
bf := func(b *bolt.Bucket) (err error) {
value = GetByteValue(b, kf.key)
return
}
err = kf.bb.View(bf)
return
}
/*
GetStringValue gets the key's value as a string.
*/
func (kf *Keyfarer) GetStringValue() (value string, err error) {
bf := func(b *bolt.Bucket) (err error) {
if v := b.Get(kf.key); len(v) != 0 {
value = string(v)
}
return
}
err = kf.bb.View(bf)
return
}
/*
UnmarshalTextValue gets the key's value and unmarshals it into the provided object.
*/
func (kf *Keyfarer) UnmarshalTextValue(valueObj encoding.TextUnmarshaler) error {
bf := func(b *bolt.Bucket) error {
return UnmarshalTextValue(b, kf.key, valueObj)
}
return kf.bb.View(bf)
}
/*
UnmarshalBinaryValue gets the key's value and unmarshals it into the provided object.
*/
func (kf *Keyfarer) UnmarshalBinaryValue(valueObj encoding.BinaryUnmarshaler) error {
bf := func(b *bolt.Bucket) error {
return UnmarshalBinaryValue(b, kf.key, valueObj)
}
return kf.bb.View(bf)
}
/*
UnmarshalJsonValue gets the key's value and unmarshals it into the provided object.
*/
func (kf *Keyfarer) UnmarshalJsonValue(valueObj interface{}) error {
bf := func(b *bolt.Bucket) error {
return UnmarshalJsonValue(b, kf.key, valueObj)
}
return kf.bb.View(bf)
}
func (kf *Keyfarer) GetVarintValue() (value int64, err error) {
bf := func(b *bolt.Bucket) (err error) {
value, err = GetVarintValue(b, kf.key)
return
}
err = kf.bb.View(bf)
return
}
func (kf *Keyfarer) GetUvarintValue() (value uint64, err error) {
bf := func(b *bolt.Bucket) (err error) {
value, err = GetUvarintValue(b, kf.key)
return
}
err = kf.bb.View(bf)
return
}
func (kf *Keyfarer) IncrementInt64Value(value int64) (newValue int64, err error) {
bf := func(b *bolt.Bucket) (err error) {
newValue, err = IncrementInt64Value(b, kf.key, value)
return
}
err = kf.bb.Update(bf)
return
}
func (kf *Keyfarer) IncrementUint64Value(value uint64) (newValue uint64, err error) {
bf := func(b *bolt.Bucket) (err error) {
newValue, err = IncrementUint64Value(b, kf.key, value)
return
}
err = kf.bb.Update(bf)
return
}
/*
ViewValue gets the key's value and passes it to the provided function for arbitrary use. The byte slice is only valid within the scope of the function.
*/
func (kf *Keyfarer) ViewValue(viewFunc func(value []byte) error) error {
return ViewValue(kf.bb.db, kf.bb.path, kf.key, viewFunc)
}
/*
ViewValue gets the key's value and passes it to the provided function for arbitrary use. The byte slice is only valid within the scope of the function.
*/
func ViewValue(db *bolt.DB, path Path, key []byte, viewFunc func(value []byte) error) (err error) {
txf := func(tx *bolt.Tx) (err error) {
if value := GetValueInTx(tx, path, key); value != nil {
err = viewFunc(value)
}
return
}
err = db.View(txf)
return
}