-
Notifications
You must be signed in to change notification settings - Fork 27
/
bucket.go
420 lines (374 loc) · 10.6 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package riak
import (
"github.com/tpjg/goriakpbc/pb"
)
// Implements access to a bucket and its properties
type Bucket struct {
bucket_type string
name string
client *Client
nval uint32
allowMult bool
lastWriteWins bool
search bool
searchIndex string
datatype string
consistent bool
}
// Return a new bucket object
func (c *Client) NewBucket(name string) (*Bucket, error) {
if name == "" {
return nil, NoBucketName
}
req := &pb.RpbGetBucketReq{
Bucket: []byte(name),
}
err, conn := c.request(req, rpbGetBucketReq)
if err != nil {
return nil, err
}
resp := &pb.RpbGetBucketResp{}
err = c.response(conn, resp)
if err != nil {
return nil, err
}
bucket := &Bucket{
name: name,
client: c,
nval: resp.Props.GetNVal(),
allowMult: resp.Props.GetAllowMult(),
lastWriteWins: resp.Props.GetLastWriteWins(),
search: resp.Props.GetSearch(),
searchIndex: string(resp.Props.GetSearchIndex()),
bucket_type: `default`,
}
return bucket, nil
}
func (c *Client) NewBucketType(btype, name string) (*Bucket, error) {
if name == "" || btype == "" {
return nil, NoBucketName
}
req := &pb.RpbGetBucketReq{
Bucket: []byte(name),
Type: []byte(btype),
}
err, conn := c.request(req, rpbGetBucketReq)
if err != nil {
return nil, err
}
resp := &pb.RpbGetBucketResp{}
err = c.response(conn, resp)
if err != nil {
return nil, err
}
bucket := &Bucket{
name: name,
bucket_type: btype,
client: c,
nval: resp.Props.GetNVal(),
allowMult: resp.Props.GetAllowMult(),
lastWriteWins: resp.Props.GetLastWriteWins(),
datatype: string(resp.Props.GetDatatype()),
consistent: resp.Props.GetConsistent(),
}
return bucket, nil
}
// Return a new bucket object. DEPRECATED, use NewBucket instead.
func (c *Client) Bucket(name string) (*Bucket, error) {
return c.NewBucket(name)
}
// Return the bucket name
func (b *Bucket) Name() string {
return b.name
}
// Return the nval property of a bucket
func (b *Bucket) NVal() uint32 {
return b.nval
}
// Return the allowMult property of a bucket
func (b *Bucket) AllowMult() bool {
return b.allowMult
}
// Return the lastWriteWins property of a bucket
func (b *Bucket) LastWriteWins() bool {
return b.lastWriteWins
}
// Return the search property of a bucket
func (b *Bucket) Search() bool {
return b.search
}
// Set the search property of a bucket
func (b *Bucket) SetSearch(search bool) (err error) {
props := &pb.RpbBucketProps{NVal: &b.nval, AllowMult: &b.allowMult, LastWriteWins: &b.lastWriteWins, Search: &search}
req := &pb.RpbSetBucketReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Props: props}
err, conn := b.client.request(req, rpbSetBucketReq)
if err != nil {
return err
}
err = b.client.response(conn, req)
if err != nil {
return err
}
b.search = search
return nil
}
// Set the search_index property of a bucket
func (b *Bucket) SetSearchIndex(searchIndex string) (err error) {
props := &pb.RpbBucketProps{SearchIndex: []byte(searchIndex)}
req := &pb.RpbSetBucketReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Props: props}
err, conn := b.client.request(req, rpbSetBucketReq)
if err != nil {
return err
}
err = b.client.response(conn, req)
if err != nil {
return err
}
b.searchIndex = searchIndex
return nil
}
// Return the search_index property of a bucket
func (b *Bucket) SearchIndex() string {
return b.searchIndex
}
// Set the nval property of a bucket
func (b *Bucket) SetNVal(nval uint32) (err error) {
props := &pb.RpbBucketProps{NVal: &nval, AllowMult: &b.allowMult, LastWriteWins: &b.lastWriteWins, Search: &b.search}
req := &pb.RpbSetBucketReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Props: props}
err, conn := b.client.request(req, rpbSetBucketReq)
if err != nil {
return err
}
err = b.client.response(conn, req)
if err != nil {
return err
}
b.nval = nval
return nil
}
// Set the allowMult property of a bucket
func (b *Bucket) SetAllowMult(allowMult bool) (err error) {
props := &pb.RpbBucketProps{NVal: &b.nval, AllowMult: &allowMult, LastWriteWins: &b.lastWriteWins, Search: &b.search}
req := &pb.RpbSetBucketReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Props: props}
err, conn := b.client.request(req, rpbSetBucketReq)
if err != nil {
return err
}
err = b.client.response(conn, req)
if err != nil {
return err
}
b.allowMult = allowMult
return nil
}
// Set the lastWriteWins property of a bucket
func (b *Bucket) SetLastWriteWins(lastWriteWins bool) (err error) {
props := &pb.RpbBucketProps{NVal: &b.nval, AllowMult: &b.allowMult, LastWriteWins: &lastWriteWins, Search: &b.search}
req := &pb.RpbSetBucketReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Props: props}
err, conn := b.client.request(req, rpbSetBucketReq)
if err != nil {
return err
}
err = b.client.response(conn, req)
if err != nil {
return err
}
b.lastWriteWins = lastWriteWins
return nil
}
// Delete a key/value from the bucket
func (b *Bucket) Delete(key string, options ...map[string]uint32) (err error) {
req := &pb.RpbDelReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Key: []byte(key)}
for _, omap := range options {
for k, v := range omap {
switch k {
case "r":
req.R = &v
case "pr":
req.Pr = &v
case "rw":
req.Rw = &v
case "w":
req.W = &v
case "dw":
req.Dw = &v
case "pw":
req.Pw = &v
}
}
}
err, conn := b.client.request(req, rpbDelReq)
if err != nil {
return err
}
err = b.client.response(conn, req)
if err != nil {
return err
}
return nil
}
// Delete directly from a bucket, without creating a bucket object first
func (c *Client) DeleteFrom(bucketname string, key string, options ...map[string]uint32) (err error) {
var bucket *Bucket
bucket, err = c.Bucket(bucketname)
if err != nil {
return
}
return bucket.Delete(key, options...)
}
// Create a new RObject
func (b *Bucket) NewObject(key string, options ...map[string]uint32) *RObject {
obj := &RObject{Key: key, Bucket: b,
Links: make([]Link, 0),
Meta: make(map[string]string),
Indexes: make(map[string][]string),
Options: options}
return obj
}
// Create a new RObject. DEPRECATED, use NewObject instead
func (b *Bucket) New(key string, options ...map[string]uint32) *RObject {
return b.NewObject(key, options...)
}
// Create a new RObject in a bucket directly, without creating a bucket object first
func (c *Client) NewObjectIn(bucketname string, key string, options ...map[string]uint32) (*RObject, error) {
bucket, err := c.Bucket(bucketname)
if err != nil {
return nil, err
}
return bucket.NewObject(key, options...), nil
}
// Test if an object exists
func (b *Bucket) Exists(key string, options ...map[string]uint32) (exists bool, err error) {
t := true
req := &pb.RpbGetReq{
Bucket: []byte(b.name),
Type: []byte(b.bucket_type),
Key: []byte(key),
NotfoundOk: &t,
Head: &t}
for _, omap := range options {
for k, v := range omap {
switch k {
case "r":
req.R = &v
case "pr":
req.Pr = &v
}
}
}
err, conn := b.client.request(req, rpbGetReq)
if err != nil {
return false, err
}
resp := &pb.RpbGetResp{}
err = b.client.response(conn, resp)
if err != nil {
return false, err
}
return len(resp.Content) != 0, nil
}
// Test if an object exists in a bucket directly, without creating a bucket object first
func (c *Client) ExistsIn(bucketname string, key string, options ...map[string]uint32) (exists bool, err error) {
bucket, err := c.Bucket(bucketname)
if err != nil {
return false, err
}
return bucket.Exists(key, options...)
}
// Return a list of keys using the index for a single key
func (b *Bucket) IndexQuery(index string, key string) (keys []string, err error) {
req := &pb.RpbIndexReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Index: []byte(index),
Qtype: pb.RpbIndexReq_eq.Enum(), Key: []byte(key)}
err, conn := b.client.request(req, rpbIndexReq)
if err != nil {
return nil, err
}
resp := &pb.RpbIndexResp{}
err = b.client.response(conn, resp)
if err != nil {
return nil, err
}
keys = make([]string, len(resp.Keys))
for i, v := range resp.Keys {
keys[i] = string(v)
}
return
}
// Return a page of keys using the index for a single key
func (b *Bucket) IndexQueryPage(index string, key string, results uint32, continuation string) (keys []string, next string, err error) {
req := &pb.RpbIndexReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Index: []byte(index),
Qtype: pb.RpbIndexReq_eq.Enum(), Key: []byte(key),
MaxResults: &results}
if continuation != "" {
req.Continuation = []byte(continuation)
}
err, conn := b.client.request(req, rpbIndexReq)
if err != nil {
return nil, "", err
}
resp := &pb.RpbIndexResp{}
err = b.client.response(conn, resp)
if err != nil {
return nil, "", err
}
keys = make([]string, len(resp.Keys))
for i, v := range resp.Keys {
keys[i] = string(v)
}
next = string(resp.Continuation)
return
}
// Return a list of keys using the index range query
func (b *Bucket) IndexQueryRange(index string, min string, max string) (keys []string, err error) {
req := &pb.RpbIndexReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Index: []byte(index),
Qtype: pb.RpbIndexReq_range.Enum(),
RangeMin: []byte(min), RangeMax: []byte(max)}
err, conn := b.client.request(req, rpbIndexReq)
if err != nil {
return nil, err
}
resp := &pb.RpbIndexResp{}
err = b.client.response(conn, resp)
if err != nil {
return nil, err
}
keys = make([]string, len(resp.Keys))
for i, v := range resp.Keys {
keys[i] = string(v)
}
return
}
// Return a page of keys using the index range query
func (b *Bucket) IndexQueryRangePage(index string, min string, max string, results uint32, continuation string) (keys []string, next string, err error) {
req := &pb.RpbIndexReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type), Index: []byte(index),
Qtype: pb.RpbIndexReq_range.Enum(),
RangeMin: []byte(min), RangeMax: []byte(max),
MaxResults: &results}
if continuation != "" {
req.Continuation = []byte(continuation)
}
err, conn := b.client.request(req, rpbIndexReq)
if err != nil {
return nil, "", err
}
resp := &pb.RpbIndexResp{}
err = b.client.response(conn, resp)
if err != nil {
return nil, "", err
}
keys = make([]string, len(resp.Keys))
for i, v := range resp.Keys {
keys[i] = string(v)
}
next = string(resp.Continuation)
return
}
// List all keys from bucket
func (b *Bucket) ListKeys() (response [][]byte, err error) {
req := &pb.RpbListKeysReq{Bucket: []byte(b.name), Type: []byte(b.bucket_type)}
err, conn := b.client.request(req, rpbListKeysReq)
if err != nil {
return nil, err
}
return b.client.mp_response(conn)
}