-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
255 lines (229 loc) · 6.53 KB
/
bucket.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
package bucketeer
import (
"encoding"
"fmt"
"github.com/boltdb/bolt"
)
/*
Bucketeer encapsulates the components needed to resolve a bucket in BoltDB and provides convenience methods for initializing Keyfarers for various key types.
*/
type Bucketeer struct {
db *bolt.DB
path Path
}
/*
New creates a Bucketeer for the provided database and bucket names.
*/
func New(db *bolt.DB, bucketNames ...string) (bb *Bucketeer) {
bb = &Bucketeer{
db: db,
path: NewPath(bucketNames...),
}
return
}
/*
ForPath creates a new Bucketeer for the provided database and bucket path.
*/
func ForPath(db *bolt.DB, path Path) (bb *Bucketeer) {
bb = &Bucketeer{
db: db,
path: path,
}
return
}
/*
EnsurePathBuckets creates any buckets along the provided path if they do not exist.
*/
func (bb *Bucketeer) EnsurePathBuckets() error {
return EnsurePathBuckets(bb.db, bb.path)
}
/*
EnsureNestedBucket creates a nested bucket if it does not exist. The bucket's full parent path must exist.
*/
func (bb *Bucketeer) EnsureNestedBucket(bucket string) error {
return EnsureNestedBucket(bb.db, bb.path, bucket)
}
/*
InNestedBucket creates a new Bucketeer for a nested bucket with the provided name.
*/
func (bb *Bucketeer) InNestedBucket(bucket string) *Bucketeer {
return ForPath(bb.db, bb.path.Nest(bucket))
}
/*
DeleteNestedBucket deletes a nested bucket with the provided name.
*/
func (bb *Bucketeer) DeleteNestedBucket(bucket string) error {
bf := func(b *bolt.Bucket) error {
return b.DeleteBucket([]byte(bucket))
}
return bb.Update(bf)
}
/*
GetBucketStats retrieves the BucketStats for the current bucket.
*/
func (bb *Bucketeer) GetBucketStats() (stats bolt.BucketStats, err error) {
bf := func(b *bolt.Bucket) (err error) {
stats = b.Stats()
return
}
err = bb.View(bf)
return
}
/*
View executes the provided function in a View transaction.
*/
func (bb *Bucketeer) View(viewFunc func(b *bolt.Bucket) error) error {
return ViewInBucket(bb.db, bb.path, viewFunc)
}
/*
Update executes the provided function in an Update transaction.
*/
func (bb *Bucketeer) Update(updateFunc func(b *bolt.Bucket) error) error {
return UpdateInBucket(bb.db, bb.path, updateFunc)
}
/*
UpdateWithSequence executes the provided function in an Update transaction, and supplies the next sequence value for the bucket.
*/
func (bb *Bucketeer) UpdateWithSequence(updateFunc func(b *bolt.Bucket, sequence uint64) error) (sequence uint64, err error) {
bf := func(b *bolt.Bucket) (err error) {
if sequence, err = b.NextSequence(); err != nil {
return
}
err = updateFunc(b, sequence)
return
}
err = bb.Update(bf)
return
}
/*
ForByteKey creates a new Keyfarer for the provided key.
*/
func (bb *Bucketeer) ForKey(key Key) *Keyfarer {
return NewKeyfarer(bb, key.KeyBytes())
}
/*
ForByteKey creates a new Keyfarer for the provided key name.
*/
func (bb *Bucketeer) ForByteKey(key []byte) *Keyfarer {
return bb.ForKey(NewByteKey(key))
}
/*
ForStringKey creates a new Keyfarer for the provided key name.
*/
func (bb *Bucketeer) ForStringKey(key string) *Keyfarer {
return bb.ForKey(NewStringKey(key))
}
/*
ForUint64Key creates a new Keyfarer for the provided key name. The key value is stored in big-endian, fixed-length format so it is byte-sortable with other uint64 keys.
*/
func (bb *Bucketeer) ForUint64Key(key uint64) *Keyfarer {
return bb.ForKey(NewUint64Key(key))
}
/*
ForInt64Key creates a new Keyfarer for the provided key name. The key value is shifted to always be a positive number, and is stored in big-endian, fixed-length format so it is byte-sortable with other int64 keys.
*/
func (bb *Bucketeer) ForInt64Key(key int64) *Keyfarer {
return bb.ForKey(NewInt64Key(key))
}
/*
ForTextKey creates a new Keyfarer for the textual form of the provided object. If there is an error marshaling the object to text, this function will panic.
*/
func (bb *Bucketeer) ForTextKey(keyObj encoding.TextMarshaler) *Keyfarer {
return bb.ForKey(NewTextKey(keyObj))
}
/*
ForBinaryKey creates a new Keyfarer for the binary form of the provided object. If there is an error marshaling the object to binary, this function will panic.
*/
func (bb *Bucketeer) ForBinaryKey(keyObj encoding.BinaryMarshaler) *Keyfarer {
return bb.ForKey(NewBinaryKey(keyObj))
}
/*
ForJsonKey creates a new Keyfarer for the JSON form of the provided object. If there is an error marshaling the object to JSON, this function will panic.
*/
func (bb *Bucketeer) ForJsonKey(keyObj interface{}) *Keyfarer {
return bb.ForKey(NewJsonKey(keyObj))
}
/*
EnsurePathBuckets creates any buckets along the provided path if they do not exist.
*/
func EnsurePathBuckets(db *bolt.DB, path Path) (err error) {
if len(path) == 0 {
panic("Path must have at least one element")
}
txf := func(tx *bolt.Tx) (err error) {
var b *bolt.Bucket
b, err = tx.CreateBucketIfNotExists(path[0])
if err != nil || b == nil || len(path) == 1 {
return
}
for _, bucket := range path[1:] {
b, err = b.CreateBucketIfNotExists(bucket)
if err != nil || b == nil {
return
}
}
return
}
err = db.Update(txf)
return
}
/*
EnsureNestedBucket creates a nested bucket if it does not exist. The bucket's full parent path must exist.
*/
func EnsureNestedBucket(db *bolt.DB, path Path, bucket string) (err error) {
txf := func(tx *bolt.Tx) (err error) {
var b *bolt.Bucket
if b = GetBucket(tx, path); b == nil {
err = fmt.Errorf("Did not find one or more path buckets: %s", path.String())
return
}
_, err = b.CreateBucketIfNotExists([]byte(bucket))
return
}
err = db.Update(txf)
return
}
/*
GetBucket retrieves the last (innermost) bucket of the provided path for use within a transaction. The bucket's full parent path must exist.
*/
func GetBucket(tx *bolt.Tx, path Path) (b *bolt.Bucket) {
if len(path) == 0 {
panic("Path must have at least one element")
}
b = tx.Bucket(path[0])
if len(path) == 1 || b == nil {
return
}
for _, bucket := range path[1:] {
if b = b.Bucket(bucket); b == nil {
return
}
}
return
}
/*
ViewInBucket executes the provided function in a View transaction.
*/
func ViewInBucket(db *bolt.DB, path Path, viewFunc func(b *bolt.Bucket) error) (err error) {
txf := func(tx *bolt.Tx) (err error) {
if b := GetBucket(tx, path); b != nil {
err = viewFunc(b)
}
return
}
err = db.View(txf)
return
}
/*
UpdateInBucket executes the provided function in an Update transaction.
*/
func UpdateInBucket(db *bolt.DB, path Path, updateFunc func(b *bolt.Bucket) error) (err error) {
txf := func(tx *bolt.Tx) (err error) {
if b := GetBucket(tx, path); b != nil {
err = updateFunc(b)
}
return
}
err = db.Update(txf)
return
}