riak-client 2.2.0.pre1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Gemfile +0 -2
  4. data/README.markdown +11 -7
  5. data/RELEASE_NOTES.md +29 -2
  6. data/Rakefile +5 -3
  7. data/lib/riak/bucket.rb +55 -23
  8. data/lib/riak/bucket_properties.rb +8 -1
  9. data/lib/riak/bucket_type.rb +29 -0
  10. data/lib/riak/bucket_typed/bucket.rb +15 -7
  11. data/lib/riak/client.rb +24 -8
  12. data/lib/riak/client/beefcake/bucket_properties_operator.rb +8 -8
  13. data/lib/riak/client/beefcake/crdt/set_loader.rb +1 -1
  14. data/lib/riak/client/beefcake/crdt_loader.rb +1 -1
  15. data/lib/riak/client/beefcake/crdt_operator.rb +9 -9
  16. data/lib/riak/client/beefcake/message_codes.rb +4 -0
  17. data/lib/riak/client/beefcake/message_overlay.rb +4 -0
  18. data/lib/riak/client/beefcake/messages.rb +35 -5
  19. data/lib/riak/client/beefcake/object_methods.rb +21 -13
  20. data/lib/riak/client/beefcake/protocol.rb +7 -7
  21. data/lib/riak/client/beefcake/socket.rb +2 -2
  22. data/lib/riak/client/beefcake_protobuffs_backend.rb +58 -33
  23. data/lib/riak/client/protobuffs_backend.rb +5 -5
  24. data/lib/riak/client/yokozuna.rb +3 -3
  25. data/lib/riak/core_ext/deep_dup.rb +1 -1
  26. data/lib/riak/counter.rb +10 -10
  27. data/lib/riak/crdt/base.rb +39 -21
  28. data/lib/riak/crdt/batch_counter.rb +5 -5
  29. data/lib/riak/crdt/batch_map.rb +2 -2
  30. data/lib/riak/crdt/counter.rb +7 -7
  31. data/lib/riak/crdt/inner_counter.rb +4 -4
  32. data/lib/riak/crdt/inner_flag.rb +3 -3
  33. data/lib/riak/crdt/inner_map.rb +1 -1
  34. data/lib/riak/crdt/inner_register.rb +1 -1
  35. data/lib/riak/crdt/inner_set.rb +5 -5
  36. data/lib/riak/crdt/map.rb +9 -9
  37. data/lib/riak/crdt/set.rb +10 -10
  38. data/lib/riak/crdt/typed_collection.rb +39 -36
  39. data/lib/riak/errors/base.rb +1 -1
  40. data/lib/riak/errors/crdt_error.rb +20 -0
  41. data/lib/riak/errors/search_error.rb +6 -0
  42. data/lib/riak/index_collection.rb +1 -1
  43. data/lib/riak/link.rb +5 -3
  44. data/lib/riak/locale/en.yml +5 -1
  45. data/lib/riak/map_reduce.rb +7 -7
  46. data/lib/riak/map_reduce/filter_builder.rb +2 -2
  47. data/lib/riak/map_reduce/phase.rb +2 -2
  48. data/lib/riak/preflist_item.rb +7 -0
  49. data/lib/riak/rcontent.rb +8 -8
  50. data/lib/riak/robject.rb +27 -14
  51. data/lib/riak/search.rb +1 -0
  52. data/lib/riak/search/index.rb +17 -3
  53. data/lib/riak/search/query.rb +14 -6
  54. data/lib/riak/search/result_collection.rb +56 -3
  55. data/lib/riak/search/result_document.rb +71 -1
  56. data/lib/riak/search/schema.rb +6 -6
  57. data/lib/riak/secondary_index.rb +20 -12
  58. data/lib/riak/serializers.rb +0 -1
  59. data/lib/riak/util/escape.rb +2 -2
  60. data/lib/riak/util/translation.rb +1 -2
  61. data/lib/riak/version.rb +1 -1
  62. data/lib/riak/walk_spec.rb +67 -32
  63. data/riak-client.gemspec +5 -4
  64. data/spec/integration/riak/bucket_types_spec.rb +35 -5
  65. data/spec/integration/riak/conflict_resolution_spec.rb +1 -1
  66. data/spec/integration/riak/counters_spec.rb +1 -1
  67. data/spec/integration/riak/crdt/configuration_spec.rb +37 -0
  68. data/spec/integration/riak/crdt_search_spec.rb +176 -0
  69. data/spec/integration/riak/crdt_spec.rb +9 -33
  70. data/spec/integration/riak/crdt_validation/map_spec.rb +4 -4
  71. data/spec/integration/riak/crdt_validation/set_spec.rb +13 -13
  72. data/spec/integration/riak/preflist_spec.rb +31 -0
  73. data/spec/integration/riak/protobuffs/interrupted_request_spec.rb +2 -2
  74. data/spec/integration/riak/protobuffs_backends_spec.rb +9 -2
  75. data/spec/integration/riak/search_spec.rb +3 -3
  76. data/spec/integration/riak/secondary_index_spec.rb +3 -3
  77. data/spec/integration/riak/security_spec.rb +7 -7
  78. data/spec/integration/yokozuna/queries_spec.rb +1 -1
  79. data/spec/riak/beefcake_protobuffs_backend/bucket_properties_operator_spec.rb +9 -9
  80. data/spec/riak/beefcake_protobuffs_backend/crdt_operator_spec.rb +9 -9
  81. data/spec/riak/beefcake_protobuffs_backend/protocol_spec.rb +5 -5
  82. data/spec/riak/beefcake_protobuffs_backend_spec.rb +8 -8
  83. data/spec/riak/bucket_properties_spec.rb +27 -6
  84. data/spec/riak/bucket_spec.rb +5 -5
  85. data/spec/riak/bucket_type_spec.rb +21 -5
  86. data/spec/riak/bucket_typed/bucket_spec.rb +62 -0
  87. data/spec/riak/client_spec.rb +36 -18
  88. data/spec/riak/counter_spec.rb +4 -4
  89. data/spec/riak/crdt/counter_spec.rb +2 -2
  90. data/spec/riak/crdt/inner_flag_spec.rb +2 -2
  91. data/spec/riak/crdt/inner_map_spec.rb +4 -4
  92. data/spec/riak/crdt/inner_register_spec.rb +1 -1
  93. data/spec/riak/crdt/map_spec.rb +4 -4
  94. data/spec/riak/crdt/shared_examples.rb +5 -5
  95. data/spec/riak/crdt/typed_collection_spec.rb +21 -21
  96. data/spec/riak/map_reduce/filter_builder_spec.rb +2 -2
  97. data/spec/riak/map_reduce/phase_spec.rb +4 -4
  98. data/spec/riak/map_reduce_spec.rb +60 -42
  99. data/spec/riak/multiget_spec.rb +2 -2
  100. data/spec/riak/robject_spec.rb +55 -14
  101. data/spec/riak/search/index_spec.rb +12 -2
  102. data/spec/riak/search/query_spec.rb +4 -4
  103. data/spec/riak/search/result_collection_spec.rb +6 -4
  104. data/spec/riak/search/result_document_spec.rb +52 -9
  105. data/spec/riak/search/schema_spec.rb +2 -2
  106. data/spec/riak/secondary_index_spec.rb +6 -6
  107. data/spec/riak/serializers_spec.rb +27 -10
  108. data/spec/riak/walk_spec_spec.rb +10 -6
  109. data/spec/spec_helper.rb +11 -2
  110. data/spec/support/crdt_search_config.rb +112 -0
  111. data/spec/support/crdt_search_fixtures.rb +42 -0
  112. data/spec/support/search_config.rb +7 -5
  113. data/spec/support/search_corpus_setup.rb +2 -2
  114. data/spec/support/test_client.rb +2 -2
  115. data/spec/support/unified_backend_examples.rb +5 -5
  116. data/spec/support/version_filter.rb +5 -3
  117. data/spec/support/wait_until.rb +9 -3
  118. metadata +36 -13
  119. data/spec/riak/bucket_typed/bucket.rb +0 -43
@@ -12,7 +12,7 @@ module Riak
12
12
  include Client::BeefcakeMessageCodes
13
13
  # Only create class methods, don't initialize
14
14
  class << self
15
- def new(host, port, options={})
15
+ def new(host, port, options = {})
16
16
  return start_tcp_socket(host, port) if options[:authentication].blank?
17
17
  return start_tls_socket(host, port, options[:authentication])
18
18
  end
@@ -217,7 +217,7 @@ module Riak
217
217
 
218
218
  # Write a protocol buffers message to whatever the current
219
219
  # socket is.
220
- def write_message(code, message='')
220
+ def write_message(code, message = '')
221
221
  if code.is_a? Symbol
222
222
  code = BeefcakeMessageCodes.index code
223
223
  end
@@ -81,7 +81,7 @@ module Riak
81
81
  return true
82
82
  end
83
83
 
84
- def fetch_object(bucket, key, options={})
84
+ def fetch_object(bucket, key, options = {})
85
85
  options = prune_unsupported_options(:GetReq, normalize_quorums(options))
86
86
  bucket = Bucket === bucket ? bucket.name : bucket
87
87
  req = RpbGetReq.new(options.merge(:bucket => maybe_encode(bucket), :key => maybe_encode(key)))
@@ -99,9 +99,10 @@ module Riak
99
99
  load_object(resp, template)
100
100
  end
101
101
 
102
- def reload_object(robject, options={})
102
+ def reload_object(robject, options = {})
103
103
  options = normalize_quorums(options)
104
104
  options[:bucket] = maybe_encode(robject.bucket.name)
105
+ options[:type] = maybe_encode(robject.bucket.type.name) if robject.bucket.needs_type?
105
106
  options[:key] = maybe_encode(robject.key)
106
107
  options[:if_modified] = maybe_encode Base64.decode64(robject.vclock) if robject.vclock
107
108
  req = RpbGetReq.new(prune_unsupported_options(:GetReq, options))
@@ -118,7 +119,7 @@ module Riak
118
119
  load_object(resp, robject)
119
120
  end
120
121
 
121
- def store_object(robject, options={})
122
+ def store_object(robject, options = {})
122
123
  options[:return_body] ||= options[:returnbody]
123
124
  options = normalize_quorums(options)
124
125
  if robject.prevent_stale_writes
@@ -144,8 +145,11 @@ module Riak
144
145
  load_object resp, robject
145
146
  end
146
147
 
147
- def delete_object(bucket, key, options={})
148
- bucket = Bucket === bucket ? bucket.name : bucket
148
+ def delete_object(bucket, key, options = {})
149
+ if bucket.is_a? Bucket
150
+ options[:type] = bucket.type.name if bucket.needs_type?
151
+ bucket = bucket.name
152
+ end
149
153
  options = normalize_quorums(options)
150
154
  options[:bucket] = maybe_encode(bucket)
151
155
  options[:key] = maybe_encode(key)
@@ -156,24 +160,45 @@ module Riak
156
160
  p.write :DelReq, req
157
161
  p.expect :DelResp
158
162
  end
159
-
163
+
160
164
  return true
161
165
  end
162
166
 
163
- def get_counter(bucket, key, options={})
164
- bucket = bucket.name if bucket.is_a? Bucket
167
+ def get_preflist(bucket, key, type = nil, options = {})
168
+ if type.nil? && bucket.is_a?(Riak::BucketTyped::Bucket)
169
+ type = bucket.type.name
170
+ end
171
+ bucket = bucket.name if bucket.is_a? Bucket
172
+ type = type.name if type.is_a? BucketType
173
+
174
+ message = RpbGetBucketKeyPreflistReq.new(
175
+ bucket: bucket,
176
+ key: key,
177
+ type: type
178
+ )
179
+
180
+ resp = protocol do |p|
181
+ p.write :GetBucketKeyPreflistReq, message
182
+ p.expect :GetBucketKeyPreflistResp, RpbGetBucketKeyPreflistResp
183
+ end
184
+
185
+ resp.preflist
186
+ end
187
+
188
+ def get_counter(bucket, key, options = {})
189
+ bucket = bucket.name if bucket.is_a? Bucket
165
190
 
166
191
  options = normalize_quorums(options)
167
192
  options[:bucket] = bucket
168
193
  options[:key] = key
169
-
194
+
170
195
  request = RpbCounterGetReq.new options
171
-
196
+
172
197
  resp = protocol do |p|
173
198
  p.write :CounterGetReq, request
174
199
  p.expect :CounterGetResp, RpbCounterGetResp, empty_body_acceptable: true
175
200
  end
176
-
201
+
177
202
  if :empty == resp
178
203
  return 0
179
204
  end
@@ -181,7 +206,7 @@ module Riak
181
206
  return resp.value || 0
182
207
  end
183
208
 
184
- def post_counter(bucket, key, amount, options={})
209
+ def post_counter(bucket, key, amount, options = {})
185
210
  bucket = bucket.name if bucket.is_a? Bucket
186
211
 
187
212
  options = normalize_quorums(options)
@@ -190,7 +215,7 @@ module Riak
190
215
  # TODO: raise if amount doesn't fit in sint64
191
216
  options[:amount] = amount
192
217
  options[:returnvalue] = options[:returnvalue] || options[:return_value]
193
-
218
+
194
219
  request = RpbCounterUpdateReq.new options
195
220
 
196
221
  resp = protocol do |p|
@@ -199,7 +224,7 @@ module Riak
199
224
  end
200
225
 
201
226
  return nil if :empty == resp
202
-
227
+
203
228
  return resp.value
204
229
  end
205
230
 
@@ -207,7 +232,7 @@ module Riak
207
232
  bucket_properties_operator.get bucket, options
208
233
  end
209
234
 
210
- def set_bucket_props(bucket, props, type=nil)
235
+ def set_bucket_props(bucket, props, type = nil)
211
236
  bucket_properties_operator.put bucket, props, type: type
212
237
  end
213
238
 
@@ -234,7 +259,7 @@ module Riak
234
259
  resp.props.to_hash
235
260
  end
236
261
 
237
- def list_keys(bucket, options={}, &block)
262
+ def list_keys(bucket, options = {}, &block)
238
263
  bucket = bucket.name if Bucket === bucket
239
264
  req = RpbListKeysReq.new(options.merge(:bucket => maybe_encode(bucket)))
240
265
 
@@ -259,13 +284,13 @@ module Riak
259
284
  end
260
285
 
261
286
  # override the simple list_buckets
262
- def list_buckets(options={}, &blk)
263
- if block_given?
287
+ def list_buckets(options = {}, &blk)
288
+ if block_given?
264
289
  return streaming_list_buckets options, &blk
265
290
  end
266
-
291
+
267
292
  raise t("streaming_bucket_list_without_block") if options[:stream]
268
-
293
+
269
294
  request = RpbListBucketsReq.new options
270
295
 
271
296
  resp = protocol do |p|
@@ -282,9 +307,9 @@ module Riak
282
307
  def mapred(mr, &block)
283
308
  raise MapReduceError.new(t("empty_map_reduce_query")) if mr.query.empty? && !mapred_phaseless?
284
309
  req = RpbMapRedReq.new(:request => mr.to_json, :content_type => "application/json")
285
-
310
+
286
311
  results = MapReduce::Results.new(mr)
287
-
312
+
288
313
  protocol do |p|
289
314
  p.write :MapRedReq, req
290
315
  while msg = p.expect(:MapRedResp, RpbMapRedResp)
@@ -296,11 +321,11 @@ module Riak
296
321
  end
297
322
  end
298
323
  end
299
-
324
+
300
325
  block_given? || results.report
301
326
  end
302
327
 
303
- def get_index(bucket, index, query, query_options={}, &block)
328
+ def get_index(bucket, index, query, query_options = {}, &block)
304
329
  return super unless pb_indexes?
305
330
  bucket = bucket.name if Bucket === bucket
306
331
  if Range === query
@@ -328,7 +353,7 @@ module Riak
328
353
  end
329
354
  end
330
355
 
331
- def search(index, query, options={})
356
+ def search(index, query, options = {})
332
357
  return super unless pb_search?
333
358
  options = options.symbolize_keys
334
359
  options[:op] = options.delete(:'q.op') if options[:'q.op']
@@ -347,9 +372,9 @@ module Riak
347
372
  return ret
348
373
  end
349
374
 
350
- def create_search_index(name, schema=nil, n_val=nil)
351
- index = RpbYokozunaIndex.new(:name => name, :schema => schema, :n_val => n_val)
352
- req = RpbYokozunaIndexPutReq.new(:index => index)
375
+ def create_search_index(name, schema = nil, n_val = nil, timeout = nil)
376
+ index = RpbYokozunaIndex.new(name: name, schema: schema, n_val: n_val)
377
+ req = RpbYokozunaIndexPutReq.new(index: index, timeout: timeout)
353
378
 
354
379
  protocol do |p|
355
380
  p.write :YokozunaIndexPutReq, req
@@ -359,7 +384,7 @@ module Riak
359
384
 
360
385
  def get_search_index(name)
361
386
  req = RpbYokozunaIndexGetReq.new(:name => name)
362
- begin
387
+ begin
363
388
  resp = protocol do |p|
364
389
  p.write :YokozunaIndexGetReq, req
365
390
  p.expect :YokozunaIndexGetResp, RpbYokozunaIndexGetResp, empty_body_acceptable: true
@@ -427,7 +452,7 @@ module Riak
427
452
  msglen, msgcode = header.unpack("NC")
428
453
  if msglen == 1
429
454
  case MESSAGE_CODES[msgcode]
430
- when :ListBucketsResp,
455
+ when :ListBucketsResp,
431
456
  :IndexResp
432
457
  []
433
458
  when :GetResp,
@@ -483,16 +508,16 @@ module Riak
483
508
  if !block_given?
484
509
  return IndexCollection.new_from_protobuf(resp)
485
510
  end
486
-
511
+
487
512
  content = resp.keys || resp.results || []
488
513
  yield content
489
-
514
+
490
515
  return if resp.done
491
516
  end
492
517
  rescue ProtobuffsErrorResponse => err
493
518
  if match = err.message.match(/indexes_not_supported,(\w+)/)
494
519
  old_err = err
495
- err = ProtobuffsFailedRequest.new(:indexes_not_supported,
520
+ err = ProtobuffsFailedRequest.new(:indexes_not_supported,
496
521
  t('index.wrong_backend', backend: match[1])
497
522
  )
498
523
  end
@@ -59,7 +59,7 @@ module Riak
59
59
  # @param [String] query the Lucene-style search query
60
60
  # @param [Hash] options ignored in MapReduce emulation
61
61
  # @return [Hash] the search results
62
- def search(index, query, options={})
62
+ def search(index, query, options = {})
63
63
  mr = Riak::MapReduce.new(client).search(index || 'search', query)
64
64
  unless mapred_phaseless?
65
65
  mr.reduce(%w[riak_kv_mapreduce reduce_identity], :arg => {:reduce_phase_only_1 => true}, :keep => true)
@@ -76,7 +76,7 @@ module Riak
76
76
  def teardown
77
77
  reset_socket
78
78
  end
79
-
79
+
80
80
  def socket
81
81
  @socket ||= new_socket
82
82
  end
@@ -90,7 +90,7 @@ module Riak
90
90
  def decode_response
91
91
  raise NotImplementedError
92
92
  end
93
-
93
+
94
94
  def new_socket
95
95
  raise NotImplementedError
96
96
  end
@@ -101,7 +101,7 @@ module Riak
101
101
  @socket = nil
102
102
  end
103
103
 
104
- def prune_unsupported_options(req,options={})
104
+ def prune_unsupported_options(req, options = {})
105
105
  unless quorum_controls?
106
106
  [:notfound_ok, :basic_quorum, :pr, :pw].each {|k| options.delete k }
107
107
  end
@@ -121,7 +121,7 @@ module Riak
121
121
  options
122
122
  end
123
123
 
124
- def normalize_quorums(options={})
124
+ def normalize_quorums(options = {})
125
125
  options.dup.tap do |o|
126
126
  [:r, :pr, :w, :pw, :dw, :rw].each do |k|
127
127
  next o[k] = normalize_quorum_value(o[k]) if o[k]
@@ -1,9 +1,9 @@
1
1
  module Riak
2
2
  class Client
3
- def create_search_index(name, schema=nil, n_val=nil)
3
+ def create_search_index(name, schema = nil, n_val = nil, timeout = nil)
4
4
  raise ArgumentError, t("zero_length_index") if name.nil? || name.empty?
5
5
  backend do |b|
6
- b.create_search_index(name, schema, n_val)
6
+ b.create_search_index(name, schema, n_val, timeout)
7
7
  end
8
8
  true
9
9
  end
@@ -49,4 +49,4 @@ module Riak
49
49
  end
50
50
  end
51
51
  end
52
- end
52
+ end
@@ -3,7 +3,7 @@ unless {}.respond_to?(:deep_dup)
3
3
  # Returns a deep copy of hash.
4
4
  def deep_dup
5
5
  duplicate = self.dup
6
- duplicate.each_pair do |k,v|
6
+ duplicate.each_pair do |k, v|
7
7
  tv = duplicate[k]
8
8
  duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
9
9
  end
data/lib/riak/counter.rb CHANGED
@@ -23,10 +23,10 @@ module Riak
23
23
  end
24
24
 
25
25
  # Retrieve the current value of the counter.
26
- # @param [Hash] options
26
+ # @param [Hash] options
27
27
  # @option options [Fixnum,String] :r ("quorum") read quorum (numeric or
28
28
  # symbolic)
29
- def value(options={})
29
+ def value(options = {})
30
30
  backend do |backend|
31
31
  backend.get_counter bucket, key, options
32
32
  end
@@ -35,27 +35,27 @@ module Riak
35
35
 
36
36
  # Increment the counter and return its new value.
37
37
  # @param amount [Integer] the amount to increment the counter by.
38
- def increment_and_return(amount=1)
38
+ def increment_and_return(amount = 1)
39
39
  increment amount, return_value: true
40
40
  end
41
41
 
42
42
  # Decrement the counter and return its new value.
43
- # @param amount [Integer] the amount to decrement the counter by. Negative
43
+ # @param amount [Integer] the amount to decrement the counter by. Negative
44
44
  # values increment the counter.
45
- def decrement_and_return(amount=1)
45
+ def decrement_and_return(amount = 1)
46
46
  increment_and_return -amount
47
47
  end
48
48
 
49
49
  # Increment the counter.
50
50
  # @param amount [Integer] the amount to increment the counter by
51
- # @param [Hash] options
51
+ # @param [Hash] options
52
52
  # @option options [Boolean] :return_value whether to return the new counter
53
53
  # value. Default false.
54
54
  # @option options [Fixnum,String] :r ("quorum") read quorum (numeric or
55
55
  # symbolic)
56
56
  # @option options [Fixnum] :w the "w" parameter (Write quorum)
57
57
  # @option options [Fixnum] :dw the "dw" parameter (Durable-write quorum)
58
- def increment(amount=1, options={})
58
+ def increment(amount = 1, options = {})
59
59
  validate_amount amount
60
60
 
61
61
  backend do |backend|
@@ -64,16 +64,16 @@ module Riak
64
64
  end
65
65
 
66
66
  # Decrement the counter.
67
- # @param amount [Integer] the amount to decrement the counter by. Negative
67
+ # @param amount [Integer] the amount to decrement the counter by. Negative
68
68
  # values increment the counter.
69
- # @param [Hash] options
69
+ # @param [Hash] options
70
70
  # @option options [Boolean] :return_value whether to return the new counter
71
71
  # value. Default false.
72
72
  # @option options [Fixnum,String] :r ("quorum") read quorum (numeric or
73
73
  # symbolic)
74
74
  # @option options [Fixnum] :w the "w" parameter (Write quorum)
75
75
  # @option options [Fixnum] :dw the "dw" parameter (Durable-write quorum)
76
- def decrement(amount=1, options={})
76
+ def decrement(amount = 1, options = {})
77
77
  increment(-amount, options)
78
78
  end
79
79
 
@@ -1,6 +1,5 @@
1
1
  module Riak
2
2
  module Crdt
3
-
4
3
  # Basic and shared code used by the top-level CRDTs. In particular, dirty-
5
4
  # tracking, loading, and operating is implemented by this class, and
6
5
  # the {Riak::Crdt::Set}, {Riak::Crdt::Counter}, and {Riak::Crdt::Map}
@@ -12,11 +11,10 @@ module Riak
12
11
  attr_reader :bucket
13
12
  attr_reader :bucket_type
14
13
 
15
- # Returns the key of this CRDT. Extremely useful when using a
14
+ # Returns the key of this CRDT. Extremely useful when using a
16
15
  # Riak-assigned key.
17
16
  attr_reader :key
18
17
 
19
-
20
18
  # Base CRDT initialization The bucket type is determined by the first of
21
19
  # these sources:
22
20
  #
@@ -32,16 +30,10 @@ module Riak
32
30
  # @param [String] bucket_type The optional bucket type for this counter.
33
31
  # The default is in `Crdt::Base::DEFAULT_BUCKET_TYPES[:counter]`.
34
32
  # @param [Hash] options
35
- def initialize(bucket, key, bucket_type, options={})
36
- raise ArgumentError, t("bucket_type", bucket: bucket.inspect) unless bucket.is_a? Bucket
37
-
38
- unless key.is_a? String or key.nil?
39
- raise ArgumentError, t("string_type", string: key.inspect)
40
- end
41
-
42
- @bucket = bucket
43
- @key = key
44
- set_bucket_type bucket_type
33
+ def initialize(bucket, key, bucket_type, options = {})
34
+ configure_bucket bucket
35
+ configure_key key
36
+ configure_bucket_type bucket_type
45
37
  @options = options
46
38
 
47
39
  @dirty = true
@@ -50,7 +42,7 @@ module Riak
50
42
  def dirty?
51
43
  @dirty
52
44
  end
53
-
45
+
54
46
  # Force a reload of this structure from Riak.
55
47
  def reload
56
48
  loader do |l|
@@ -69,6 +61,14 @@ module Riak
69
61
  !!@context
70
62
  end
71
63
 
64
+ def ==(other)
65
+ return false unless self.class == other.class
66
+ return false unless self.bucket_type == other.bucket_type
67
+ return false unless self.bucket == other.bucket
68
+ return false unless self.key == other.key
69
+ return true
70
+ end
71
+
72
72
  def pretty_print(pp)
73
73
  pp.object_group self do
74
74
  pp.breakable
@@ -90,16 +90,18 @@ module Riak
90
90
  end
91
91
 
92
92
  def inspect_name
93
- "#<#{self.class.name} bucket=#{@bucket.name} key=#{@key} type=#{@bucket_type}>"
93
+ "#<#{self.class.name} bucket=#{@bucket.name} " \
94
+ "key=#{@key} type=#{@bucket_type}>"
94
95
  end
95
-
96
+
96
97
  private
98
+
97
99
  def client
98
100
  @bucket.client
99
101
  end
100
102
 
101
103
  def backend(&blk)
102
- client.backend &blk
104
+ client.backend(&blk)
103
105
  end
104
106
 
105
107
  def loader
@@ -107,7 +109,7 @@ module Riak
107
109
  yield be.crdt_loader
108
110
  end
109
111
  end
110
-
112
+
111
113
  def operator
112
114
  backend do |be|
113
115
  yield be.crdt_operator
@@ -115,7 +117,7 @@ module Riak
115
117
  end
116
118
 
117
119
  def operate(*args)
118
- options = Hash.new
120
+ options = {}
119
121
  options = args.pop if args.last.is_a? Hash
120
122
  options[:context] ||= @context
121
123
  result = operator do |op|
@@ -134,7 +136,7 @@ module Riak
134
136
  @dirty = true
135
137
  vivify_returnbody(result)
136
138
 
137
- return true
139
+ true
138
140
  end
139
141
 
140
142
  def vivify_returnbody(result)
@@ -149,7 +151,7 @@ module Riak
149
151
  end
150
152
  end
151
153
 
152
- def set_bucket_type(constructor_type)
154
+ def configure_bucket_type(constructor_type)
153
155
  @bucket_type = if constructor_type.is_a? String
154
156
  constructor_type
155
157
  elsif constructor_type.is_a? BucketType
@@ -160,6 +162,22 @@ module Riak
160
162
  DEFAULT_BUCKET_TYPES[constructor_type]
161
163
  end
162
164
  end
165
+
166
+ def configure_bucket(bucket)
167
+ unless bucket.is_a? Bucket
168
+ fail ArgumentError, t('bucket_type', bucket: bucket.inspect)
169
+ end
170
+
171
+ @bucket = bucket
172
+ end
173
+
174
+ def configure_key(key)
175
+ unless key.is_a?(String) || key.nil?
176
+ fail ArgumentError, t('string_type', string: key.inspect)
177
+ end
178
+
179
+ @key = key
180
+ end
163
181
  end
164
182
  end
165
183
  end