-
Notifications
You must be signed in to change notification settings - Fork 46
/
kv_commands.go
1578 lines (1385 loc) · 49.5 KB
/
kv_commands.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015-present Basho Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package riak
import (
"fmt"
"reflect"
"strconv"
"time"
rpbRiakKV "github.com/basho/riak-go-client/rpb/riak_kv"
proto "github.com/golang/protobuf/proto"
)
// FetchValue
// RpbGetReq
// RpbGetResp
// ConflictResolver is an interface to handle sibling conflicts for a key
type ConflictResolver interface {
Resolve([]*Object) []*Object
}
// FetchValueCommand is used to fetch / get a value from Riak KV
type FetchValueCommand struct {
commandImpl
timeoutImpl
retryableCommandImpl
Response *FetchValueResponse
protobuf *rpbRiakKV.RpbGetReq
resolver ConflictResolver
}
// Name identifies this command
func (cmd *FetchValueCommand) Name() string {
return cmd.getName("FetchValue")
}
func (cmd *FetchValueCommand) constructPbRequest() (proto.Message, error) {
return cmd.protobuf, nil
}
func (cmd *FetchValueCommand) onSuccess(msg proto.Message) error {
cmd.success = true
if msg == nil {
cmd.Response = &FetchValueResponse{
IsNotFound: true,
IsUnchanged: false,
}
} else {
if rpbGetResp, ok := msg.(*rpbRiakKV.RpbGetResp); ok {
vclock := rpbGetResp.GetVclock()
response := &FetchValueResponse{
VClock: vclock,
IsUnchanged: rpbGetResp.GetUnchanged(),
IsNotFound: false,
}
if pbContent := rpbGetResp.GetContent(); pbContent == nil || len(pbContent) == 0 {
object := &Object{
IsTombstone: true,
BucketType: string(cmd.protobuf.Type),
Bucket: string(cmd.protobuf.Bucket),
Key: string(cmd.protobuf.Key),
}
response.Values = []*Object{object}
} else {
response.Values = make([]*Object, len(pbContent))
for i, content := range pbContent {
ro, err := fromRpbContent(content)
if err != nil {
return err
}
ro.VClock = vclock
ro.BucketType = string(cmd.protobuf.Type)
ro.Bucket = string(cmd.protobuf.Bucket)
ro.Key = string(cmd.protobuf.Key)
response.Values[i] = ro
}
if cmd.resolver != nil {
response.Values = cmd.resolver.Resolve(response.Values)
}
}
cmd.Response = response
} else {
return fmt.Errorf("[FetchValueCommand] could not convert %v to RpbGetResp", reflect.TypeOf(msg))
}
}
return nil
}
func (cmd *FetchValueCommand) getRequestCode() byte {
return rpbCode_RpbGetReq
}
func (cmd *FetchValueCommand) getResponseCode() byte {
return rpbCode_RpbGetResp
}
func (cmd *FetchValueCommand) getResponseProtobufMessage() proto.Message {
return &rpbRiakKV.RpbGetResp{}
}
// FetchValueResponse contains the response data for a FetchValueCommand
type FetchValueResponse struct {
IsNotFound bool
IsUnchanged bool
VClock []byte
Values []*Object
}
// FetchValueCommandBuilder type is required for creating new instances of FetchValueCommand
//
// command, err := NewFetchValueCommandBuilder().
// WithBucketType("myBucketType").
// WithBucket("myBucket").
// WithKey("myKey").
// Build()
type FetchValueCommandBuilder struct {
timeout time.Duration
protobuf *rpbRiakKV.RpbGetReq
resolver ConflictResolver
}
// NewFetchValueCommandBuilder is a factory function for generating the command builder struct
func NewFetchValueCommandBuilder() *FetchValueCommandBuilder {
builder := &FetchValueCommandBuilder{protobuf: &rpbRiakKV.RpbGetReq{}}
return builder
}
// WithConflictResolver builds the command object with a user defined ConflictResolver for handling conflicting key values
func (builder *FetchValueCommandBuilder) WithConflictResolver(resolver ConflictResolver) *FetchValueCommandBuilder {
builder.resolver = resolver
return builder
}
// WithBucketType sets the bucket-type to be used by the command. If omitted, 'default' is used
func (builder *FetchValueCommandBuilder) WithBucketType(bucketType string) *FetchValueCommandBuilder {
builder.protobuf.Type = []byte(bucketType)
return builder
}
// WithBucket sets the bucket to be used by the command
func (builder *FetchValueCommandBuilder) WithBucket(bucket string) *FetchValueCommandBuilder {
builder.protobuf.Bucket = []byte(bucket)
return builder
}
// WithKey sets the key to be used by the command to read / write values
func (builder *FetchValueCommandBuilder) WithKey(key string) *FetchValueCommandBuilder {
builder.protobuf.Key = []byte(key)
return builder
}
// WithR sets the number of nodes that must report back a successful read in order for the
// command operation to be considered a success by Riak. If ommitted, the bucket default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *FetchValueCommandBuilder) WithR(r uint32) *FetchValueCommandBuilder {
builder.protobuf.R = &r
return builder
}
// WithPr sets the number of primary nodes (N) that must be read from in order for the command
// operation to be considered a success by Riak. If ommitted, the bucket default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *FetchValueCommandBuilder) WithPr(pr uint32) *FetchValueCommandBuilder {
builder.protobuf.Pr = &pr
return builder
}
// WithNVal sets the number of times this command operation is replicated in the Cluster. If
// ommitted, the ring default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *FetchValueCommandBuilder) WithNVal(nval uint32) *FetchValueCommandBuilder {
builder.protobuf.NVal = &nval
return builder
}
// WithBasicQuorum sets basic_quorum, whether to return early in some failure cases (eg. when r=1
// and you get 2 errors and a success basic_quorum=true would return an error)
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-3/
func (builder *FetchValueCommandBuilder) WithBasicQuorum(basicQuorum bool) *FetchValueCommandBuilder {
builder.protobuf.BasicQuorum = &basicQuorum
return builder
}
// WithNotFoundOk sets notfound_ok, whether to treat notfounds as successful reads for the purposes
// of R
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-3/
func (builder *FetchValueCommandBuilder) WithNotFoundOk(notFoundOk bool) *FetchValueCommandBuilder {
builder.protobuf.NotfoundOk = ¬FoundOk
return builder
}
// WithIfModified tells Riak to only return the object if the vclock in Riak differs from what is
// provided
func (builder *FetchValueCommandBuilder) WithIfModified(ifModified []byte) *FetchValueCommandBuilder {
builder.protobuf.IfModified = ifModified
return builder
}
// WithHeadOnly returns only the meta data for the value, useful when objects contain large amounts
// of data
func (builder *FetchValueCommandBuilder) WithHeadOnly(headOnly bool) *FetchValueCommandBuilder {
builder.protobuf.Head = &headOnly
return builder
}
// WithReturnDeletedVClock sets the command to return a Tombstone if any our found for the key across
// all of the vnodes
func (builder *FetchValueCommandBuilder) WithReturnDeletedVClock(returnDeletedVClock bool) *FetchValueCommandBuilder {
builder.protobuf.Deletedvclock = &returnDeletedVClock
return builder
}
// WithTimeout sets a timeout to be used for this command operation
func (builder *FetchValueCommandBuilder) WithTimeout(timeout time.Duration) *FetchValueCommandBuilder {
timeoutMilliseconds := uint32(timeout / time.Millisecond)
builder.timeout = timeout
builder.protobuf.Timeout = &timeoutMilliseconds
return builder
}
// WithSloppyQuorum sets the sloppy_quorum for this Command
//
// See http://docs.basho.com/riak/latest/theory/concepts/Eventual-Consistency/
func (builder *FetchValueCommandBuilder) WithSloppyQuorum(sloppyQuorum bool) *FetchValueCommandBuilder {
builder.protobuf.SloppyQuorum = &sloppyQuorum
return builder
}
// Build validates the configuration options provided then builds the command
func (builder *FetchValueCommandBuilder) Build() (Command, error) {
if builder.protobuf == nil {
panic("builder.protobuf must not be nil")
}
if err := validateLocatable(builder.protobuf); err != nil {
return nil, err
}
return &FetchValueCommand{
timeoutImpl: timeoutImpl{
timeout: builder.timeout,
},
protobuf: builder.protobuf,
resolver: builder.resolver,
}, nil
}
// StoreValue
// RpbPutReq
// RpbPutResp
// StoreValueCommand used to store a value from Riak KV.
type StoreValueCommand struct {
commandImpl
timeoutImpl
retryableCommandImpl
Response *StoreValueResponse
value *Object
protobuf *rpbRiakKV.RpbPutReq
resolver ConflictResolver
}
// Name identifies this command
func (cmd *StoreValueCommand) Name() string {
return cmd.getName("StoreValue")
}
func (cmd *StoreValueCommand) constructPbRequest() (msg proto.Message, err error) {
value := cmd.value
// Some properties of the value override options
setProtobufFromValue(cmd.protobuf, cmd.value)
cmd.protobuf.Content, err = toRpbContent(value)
if err != nil {
return
}
msg = cmd.protobuf
return
}
func (cmd *StoreValueCommand) onSuccess(msg proto.Message) error {
cmd.success = true
if msg == nil {
cmd.Response = &StoreValueResponse{}
} else {
if rpbPutResp, ok := msg.(*rpbRiakKV.RpbPutResp); ok {
var responseKey string
if responseKeyBytes := rpbPutResp.GetKey(); responseKeyBytes != nil && len(responseKeyBytes) > 0 {
responseKey = string(responseKeyBytes)
}
vclock := rpbPutResp.GetVclock()
response := &StoreValueResponse{
VClock: vclock,
GeneratedKey: responseKey,
}
if pbContent := rpbPutResp.GetContent(); pbContent != nil && len(pbContent) > 0 {
response.Values = make([]*Object, len(pbContent))
for i, content := range pbContent {
ro, err := fromRpbContent(content)
if err != nil {
return err
}
ro.VClock = vclock
ro.BucketType = string(cmd.protobuf.Type)
ro.Bucket = string(cmd.protobuf.Bucket)
if responseKey == "" {
ro.Key = string(cmd.protobuf.Key)
} else {
ro.Key = responseKey
}
response.Values[i] = ro
}
if cmd.resolver != nil {
response.Values = cmd.resolver.Resolve(response.Values)
}
}
cmd.Response = response
} else {
return fmt.Errorf("[StoreValueCommand] could not convert %v to RpbPutResp", reflect.TypeOf(msg))
}
}
return nil
}
func (cmd *StoreValueCommand) getRequestCode() byte {
return rpbCode_RpbPutReq
}
func (cmd *StoreValueCommand) getResponseCode() byte {
return rpbCode_RpbPutResp
}
func (cmd *StoreValueCommand) getResponseProtobufMessage() proto.Message {
return &rpbRiakKV.RpbPutResp{}
}
func setProtobufFromValue(pb *rpbRiakKV.RpbPutReq, value *Object) {
if value.VClock != nil {
pb.Vclock = value.VClock
}
if value.BucketType != "" {
pb.Type = []byte(value.BucketType)
}
if value.Bucket != "" {
pb.Bucket = []byte(value.Bucket)
}
if value.Key != "" {
pb.Key = []byte(value.Key)
}
}
// StoreValueResponse contains the response data for a StoreValueCommand
type StoreValueResponse struct {
GeneratedKey string
VClock []byte
Values []*Object
}
// StoreValueCommandBuilder type is required for creating new instances of StoreValueCommand
//
// command, err := NewStoreValueCommandBuilder().
// WithBucketType("myBucketType").
// WithBucket("myBucket").
// Build()
type StoreValueCommandBuilder struct {
value *Object
timeout time.Duration
protobuf *rpbRiakKV.RpbPutReq
resolver ConflictResolver
}
// NewStoreValueCommandBuilder is a factory function for generating the command builder struct
func NewStoreValueCommandBuilder() *StoreValueCommandBuilder {
builder := &StoreValueCommandBuilder{protobuf: &rpbRiakKV.RpbPutReq{}}
return builder
}
// WithConflictResolver sets the ConflictResolver that should be used when sibling conflicts are found
// for this operation
func (builder *StoreValueCommandBuilder) WithConflictResolver(resolver ConflictResolver) *StoreValueCommandBuilder {
builder.resolver = resolver
return builder
}
// WithBucketType sets the bucket-type to be used by the command. If omitted, 'default' is used
func (builder *StoreValueCommandBuilder) WithBucketType(bucketType string) *StoreValueCommandBuilder {
builder.protobuf.Type = []byte(bucketType)
return builder
}
// WithBucket sets the bucket to be used by the command
func (builder *StoreValueCommandBuilder) WithBucket(bucket string) *StoreValueCommandBuilder {
builder.protobuf.Bucket = []byte(bucket)
return builder
}
// WithKey sets the key to be used by the command to read / write values
func (builder *StoreValueCommandBuilder) WithKey(key string) *StoreValueCommandBuilder {
builder.protobuf.Key = []byte(key)
return builder
}
// WithVClock sets the vclock for the object to be stored, providing causal context for conflicts
func (builder *StoreValueCommandBuilder) WithVClock(vclock []byte) *StoreValueCommandBuilder {
builder.protobuf.Vclock = vclock
return builder
}
// WithContent sets the object / value to be stored at the specified key
func (builder *StoreValueCommandBuilder) WithContent(object *Object) *StoreValueCommandBuilder {
setProtobufFromValue(builder.protobuf, object)
builder.value = object
return builder
}
// WithW sets the number of nodes that must report back a successful write in order for then
// command operation to be considered a success by Riak
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *StoreValueCommandBuilder) WithW(w uint32) *StoreValueCommandBuilder {
builder.protobuf.W = &w
return builder
}
// WithDw (durable writes) sets the number of nodes that must report back a successful write to
// backend storage in order for the command operation to be considered a success by Riak. If
// ommitted, the bucket default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *StoreValueCommandBuilder) WithDw(dw uint32) *StoreValueCommandBuilder {
builder.protobuf.Dw = &dw
return builder
}
// WithPw sets the number of primary nodes (N) that must report back a successful write in order for
// the command operation to be considered a success by Riak. If ommitted, the bucket default is
// used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *StoreValueCommandBuilder) WithPw(pw uint32) *StoreValueCommandBuilder {
builder.protobuf.Pw = &pw
return builder
}
// WithNVal sets the number of times this command operation is replicated in the Cluster. If
// ommitted, the ring default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *StoreValueCommandBuilder) WithNVal(nval uint32) *StoreValueCommandBuilder {
builder.protobuf.NVal = &nval
return builder
}
// WithReturnBody sets Riak to return the value within its response after completing the write
// operation
func (builder *StoreValueCommandBuilder) WithReturnBody(returnBody bool) *StoreValueCommandBuilder {
builder.protobuf.ReturnBody = &returnBody
return builder
}
// WithIfNotModified tells Riak to only update the object in Riak if the vclock provided matches the
// one currently in Riak
func (builder *StoreValueCommandBuilder) WithIfNotModified(ifNotModified bool) *StoreValueCommandBuilder {
builder.protobuf.IfNotModified = &ifNotModified
return builder
}
// WithIfNoneMatch tells Riak to store the object only if it does not already exist in the database
func (builder *StoreValueCommandBuilder) WithIfNoneMatch(ifNoneMatch bool) *StoreValueCommandBuilder {
builder.protobuf.IfNoneMatch = &ifNoneMatch
return builder
}
// WithReturnHead returns only the meta data for the value, useful when objects contain large amounts
// of data
func (builder *StoreValueCommandBuilder) WithReturnHead(returnHead bool) *StoreValueCommandBuilder {
builder.protobuf.ReturnHead = &returnHead
return builder
}
// WithTimeout sets a timeout to be used for this command operation
func (builder *StoreValueCommandBuilder) WithTimeout(timeout time.Duration) *StoreValueCommandBuilder {
timeoutMilliseconds := uint32(timeout / time.Millisecond)
builder.timeout = timeout
builder.protobuf.Timeout = &timeoutMilliseconds
return builder
}
// WithAsis sets the asis option
// Please note, this is an advanced feature, only use with caution
func (builder *StoreValueCommandBuilder) WithAsis(asis bool) *StoreValueCommandBuilder {
builder.protobuf.Asis = &asis
return builder
}
// WithSloppyQuorum sets the sloppy_quorum for this Command
// Please note, this is an advanced feature, only use with caution
//
// See http://docs.basho.com/riak/latest/theory/concepts/Eventual-Consistency/
func (builder *StoreValueCommandBuilder) WithSloppyQuorum(sloppyQuorum bool) *StoreValueCommandBuilder {
builder.protobuf.SloppyQuorum = &sloppyQuorum
return builder
}
// Build validates the configuration options provided then builds the command
func (builder *StoreValueCommandBuilder) Build() (Command, error) {
if builder.protobuf == nil {
panic("builder.protobuf must not be nil")
}
if err := validateLocatable(builder.protobuf); err != nil {
return nil, err
}
return &StoreValueCommand{
value: builder.value,
timeoutImpl: timeoutImpl{
timeout: builder.timeout,
},
protobuf: builder.protobuf,
resolver: builder.resolver}, nil
}
// DeleteValue
// RpbDelReq
// RpbDelResp
// DeleteValueCommand is used to delete a value from Riak KV.
type DeleteValueCommand struct {
commandImpl
timeoutImpl
retryableCommandImpl
Response bool
protobuf *rpbRiakKV.RpbDelReq
}
// Name identifies this command
func (cmd *DeleteValueCommand) Name() string {
return cmd.getName("DeleteValue")
}
func (cmd *DeleteValueCommand) constructPbRequest() (msg proto.Message, err error) {
msg = cmd.protobuf
return
}
func (cmd *DeleteValueCommand) onSuccess(msg proto.Message) error {
cmd.success = true
cmd.Response = true
return nil
}
func (cmd *DeleteValueCommand) getRequestCode() byte {
return rpbCode_RpbDelReq
}
func (cmd *DeleteValueCommand) getResponseCode() byte {
return rpbCode_RpbDelResp
}
func (cmd *DeleteValueCommand) getResponseProtobufMessage() proto.Message {
return nil
}
// DeleteValueCommandBuilder type is required for creating new instances of DeleteValueCommand
//
// deleteValue := NewDeleteValueCommandBuilder().
// WithBucketType("myBucketType").
// WithBucket("myBucket").
// WithKey("myKey").
// WithVClock(vclock).
// Build()
type DeleteValueCommandBuilder struct {
timeout time.Duration
protobuf *rpbRiakKV.RpbDelReq
}
// NewDeleteValueCommandBuilder is a factory function for generating the command builder struct
func NewDeleteValueCommandBuilder() *DeleteValueCommandBuilder {
builder := &DeleteValueCommandBuilder{protobuf: &rpbRiakKV.RpbDelReq{}}
return builder
}
// WithBucketType sets the bucket-type to be used by the command. If omitted, 'default' is used
func (builder *DeleteValueCommandBuilder) WithBucketType(bucketType string) *DeleteValueCommandBuilder {
builder.protobuf.Type = []byte(bucketType)
return builder
}
// WithBucket sets the bucket to be used by the command
func (builder *DeleteValueCommandBuilder) WithBucket(bucket string) *DeleteValueCommandBuilder {
builder.protobuf.Bucket = []byte(bucket)
return builder
}
// WithKey sets the key to be used by the command to read / write values
func (builder *DeleteValueCommandBuilder) WithKey(key string) *DeleteValueCommandBuilder {
builder.protobuf.Key = []byte(key)
return builder
}
// WithVClock sets the vector clock.
//
// If not set siblings may be created depending on bucket properties.
func (builder *DeleteValueCommandBuilder) WithVClock(vclock []byte) *DeleteValueCommandBuilder {
builder.protobuf.Vclock = vclock
return builder
}
// WithR sets the number of nodes that must report back a successful read in order for the
// command operation to be considered a success by Riak. If ommitted, the bucket default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *DeleteValueCommandBuilder) WithR(r uint32) *DeleteValueCommandBuilder {
builder.protobuf.R = &r
return builder
}
// WithW sets the number of nodes that must report back a successful write in order for then
// command operation to be considered a success by Riak. If ommitted, the bucket default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *DeleteValueCommandBuilder) WithW(w uint32) *DeleteValueCommandBuilder {
builder.protobuf.W = &w
return builder
}
// WithPr sets the number of primary nodes (N) that must be read from in order for the command
// operation to be considered a success by Riak. If ommitted, the bucket default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *DeleteValueCommandBuilder) WithPr(pr uint32) *DeleteValueCommandBuilder {
builder.protobuf.Pr = &pr
return builder
}
// WithPw sets the number of primary nodes (N) that must report back a successful write in order for
// the command operation to be considered a success by Riak. If ommitted, the bucket default is
// used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *DeleteValueCommandBuilder) WithPw(pw uint32) *DeleteValueCommandBuilder {
builder.protobuf.Pw = &pw
return builder
}
// WithDw (durable writes) sets the number of nodes that must report back a successful write to
// backend storage in order for the command operation to be considered a success by Riak. If
// ommitted, the bucket default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *DeleteValueCommandBuilder) WithDw(dw uint32) *DeleteValueCommandBuilder {
builder.protobuf.Dw = &dw
return builder
}
// WithRw (delete quorum) sets the number of nodes that must report back a successful delete to
// backend storage in order for the command operation to be considered a success by Riak. It
// represents the read and write operations that are completed internal to Riak to complete a delete.
// If ommitted, the bucket default is used.
//
// See http://basho.com/posts/technical/riaks-config-behaviors-part-2/
func (builder *DeleteValueCommandBuilder) WithRw(rw uint32) *DeleteValueCommandBuilder {
builder.protobuf.Rw = &rw
return builder
}
// WithTimeout sets a timeout to be used for this command operation
func (builder *DeleteValueCommandBuilder) WithTimeout(timeout time.Duration) *DeleteValueCommandBuilder {
timeoutMilliseconds := uint32(timeout / time.Millisecond)
builder.timeout = timeout
builder.protobuf.Timeout = &timeoutMilliseconds
return builder
}
// Build validates the configuration options provided then builds the command
func (builder *DeleteValueCommandBuilder) Build() (Command, error) {
if builder.protobuf == nil {
panic("builder.protobuf must not be nil")
}
if err := validateLocatable(builder.protobuf); err != nil {
return nil, err
}
return &DeleteValueCommand{
timeoutImpl: timeoutImpl{
timeout: builder.timeout,
},
protobuf: builder.protobuf,
}, nil
}
// ListBuckets
// RpbListBucketsReq
// RpbListBucketsResp
// ListBucketsCommand is used to list buckets in a bucket type
type ListBucketsCommand struct {
commandImpl
listingImpl
Response *ListBucketsResponse
protobuf *rpbRiakKV.RpbListBucketsReq
callback func(buckets []string) error
done bool
}
// Name identifies this command
func (cmd *ListBucketsCommand) Name() string {
return cmd.getName("ListBuckets")
}
func (cmd *ListBucketsCommand) isDone() bool {
if cmd.protobuf.GetStream() {
return cmd.done
}
return true
}
func (cmd *ListBucketsCommand) constructPbRequest() (msg proto.Message, err error) {
msg = cmd.protobuf
return
}
func (cmd *ListBucketsCommand) onSuccess(msg proto.Message) error {
cmd.success = true
if msg == nil {
cmd.done = true
cmd.Response = &ListBucketsResponse{}
} else {
if rpbListBucketsResp, ok := msg.(*rpbRiakKV.RpbListBucketsResp); ok {
cmd.done = rpbListBucketsResp.GetDone()
response := cmd.Response
if response == nil {
response = &ListBucketsResponse{}
cmd.Response = response
}
if rpbListBucketsResp.GetBuckets() != nil {
buckets := make([]string, len(rpbListBucketsResp.GetBuckets()))
for i, bucket := range rpbListBucketsResp.GetBuckets() {
buckets[i] = string(bucket)
}
if cmd.protobuf.GetStream() {
if cmd.callback == nil {
panic("ListBucketsCommand requires a callback when streaming.")
} else {
if err := cmd.callback(buckets); err != nil {
cmd.Response = nil
return err
}
}
} else {
response.Buckets = append(response.Buckets, buckets...)
}
}
} else {
cmd.done = true
return fmt.Errorf("[ListBucketsCommand] could not convert %v to RpbListBucketsResp", reflect.TypeOf(msg))
}
}
return nil
}
func (cmd *ListBucketsCommand) getRequestCode() byte {
return rpbCode_RpbListBucketsReq
}
func (cmd *ListBucketsCommand) getResponseCode() byte {
return rpbCode_RpbListBucketsResp
}
func (cmd *ListBucketsCommand) getResponseProtobufMessage() proto.Message {
return &rpbRiakKV.RpbListBucketsResp{}
}
// ListBucketsResponse contains the response data for a ListBucketsCommand
type ListBucketsResponse struct {
Buckets []string
}
// ListBucketsCommandBuilder type is required for creating new instances of ListBucketsCommand
//
// cb := func(buckets []string) error {
// // Do something with the result
// return nil
// }
// cmd, err := NewListBucketsCommandBuilder().
// WithBucketType("myBucketType").
// WithStreaming(true).
// WithCallback(cb).
// Build()
type ListBucketsCommandBuilder struct {
allowListing bool
callback func(buckets []string) error
protobuf *rpbRiakKV.RpbListBucketsReq
}
// NewListBucketsCommandBuilder is a factory function for generating the command builder struct
func NewListBucketsCommandBuilder() *ListBucketsCommandBuilder {
builder := &ListBucketsCommandBuilder{protobuf: &rpbRiakKV.RpbListBucketsReq{}}
return builder
}
// WithAllowListing will allow this command to be built and execute
func (builder *ListBucketsCommandBuilder) WithAllowListing() *ListBucketsCommandBuilder {
builder.allowListing = true
return builder
}
// WithBucketType sets the bucket-type to be used by the command. If omitted, 'default' is used
func (builder *ListBucketsCommandBuilder) WithBucketType(bucketType string) *ListBucketsCommandBuilder {
builder.protobuf.Type = []byte(bucketType)
return builder
}
// WithStreaming sets the command to provide a streamed response
//
// If true, a callback must be provided via WithCallback()
func (builder *ListBucketsCommandBuilder) WithStreaming(streaming bool) *ListBucketsCommandBuilder {
builder.protobuf.Stream = &streaming
return builder
}
// WithCallback sets the callback to be used when handling a streaming response
//
// Requires WithStreaming(true)
func (builder *ListBucketsCommandBuilder) WithCallback(callback func([]string) error) *ListBucketsCommandBuilder {
builder.callback = callback
return builder
}
// WithTimeout sets a timeout in milliseconds to be used for this command operation
func (builder *ListBucketsCommandBuilder) WithTimeout(timeout time.Duration) *ListBucketsCommandBuilder {
timeoutMilliseconds := uint32(timeout / time.Millisecond)
builder.protobuf.Timeout = &timeoutMilliseconds
return builder
}
// Build validates the configuration options provided then builds the command
func (builder *ListBucketsCommandBuilder) Build() (Command, error) {
if builder.protobuf == nil {
panic("builder.protobuf must not be nil")
}
if err := validateLocatable(builder.protobuf); err != nil {
return nil, err
}
if builder.protobuf.GetStream() && builder.callback == nil {
return nil, newClientError("ListBucketsCommand requires a callback when streaming.", nil)
}
if !builder.allowListing {
return nil, ErrListingDisabled
}
return &ListBucketsCommand{
listingImpl: listingImpl{
allowListing: builder.allowListing,
},
protobuf: builder.protobuf,
callback: builder.callback}, nil
}
// ListKeys
// RpbListKeysReq
// RpbListKeysResp
// ListKeysCommand is used to fetch a list of keys within a bucket from Riak KV
type ListKeysCommand struct {
commandImpl
timeoutImpl
listingImpl
Response *ListKeysResponse
protobuf *rpbRiakKV.RpbListKeysReq
streaming bool
callback func(keys []string) error
done bool
}
// Name identifies this command
func (cmd *ListKeysCommand) Name() string {
return cmd.getName("ListKeys")
}
func (cmd *ListKeysCommand) isDone() bool {
// NB: RpbListKeysReq is *always* streaming so no need to take
// cmd.streaming into account here, unlike RpbListBucketsReq
return cmd.done
}
func (cmd *ListKeysCommand) constructPbRequest() (msg proto.Message, err error) {
msg = cmd.protobuf
return
}
func (cmd *ListKeysCommand) onSuccess(msg proto.Message) error {
cmd.success = true
if msg == nil {
cmd.done = true
cmd.Response = &ListKeysResponse{}
} else {
if rpbListKeysResp, ok := msg.(*rpbRiakKV.RpbListKeysResp); ok {
cmd.done = rpbListKeysResp.GetDone()
response := cmd.Response
if response == nil {
response = &ListKeysResponse{}
cmd.Response = response
}
if rpbListKeysResp.GetKeys() != nil {
keys := make([]string, len(rpbListKeysResp.GetKeys()))
for i, key := range rpbListKeysResp.GetKeys() {
keys[i] = string(key)
}
if cmd.streaming {
if cmd.callback == nil {
panic("ListKeysCommand requires a callback when streaming.")
} else {
if err := cmd.callback(keys); err != nil {
cmd.Response = nil
return err
}
}
} else {
response.Keys = append(response.Keys, keys...)
}
}
} else {
cmd.done = true
return fmt.Errorf("[ListKeysCommand] could not convert %v to RpbListKeysResp", reflect.TypeOf(msg))
}
}
return nil
}
func (cmd *ListKeysCommand) getRequestCode() byte {
return rpbCode_RpbListKeysReq
}
func (cmd *ListKeysCommand) getResponseCode() byte {
return rpbCode_RpbListKeysResp
}
func (cmd *ListKeysCommand) getResponseProtobufMessage() proto.Message {
return &rpbRiakKV.RpbListKeysResp{}
}
// ListKeysResponse contains the response data for a ListKeysCommand
type ListKeysResponse struct {
Keys []string
}
// ListKeysCommandBuilder type is required for creating new instances of ListKeysCommand
//
// cb := func(buckets []string) error {
// // Do something with the result
// return nil
// }
// cmd, err := NewListKeysCommandBuilder().
// WithBucketType("myBucketType").
// WithBucket("myBucket").
// WithStreaming(true).
// WithCallback(cb).
// Build()
type ListKeysCommandBuilder struct {
allowListing bool
timeout time.Duration
protobuf *rpbRiakKV.RpbListKeysReq
streaming bool
callback func(buckets []string) error
}
// NewListKeysCommandBuilder is a factory function for generating the command builder struct
func NewListKeysCommandBuilder() *ListKeysCommandBuilder {
builder := &ListKeysCommandBuilder{protobuf: &rpbRiakKV.RpbListKeysReq{}}
return builder
}
// WithAllowListing will allow this command to be built and execute
func (builder *ListKeysCommandBuilder) WithAllowListing() *ListKeysCommandBuilder {
builder.allowListing = true
return builder