Class: Riak::Crdt::Counter
Overview
A distributed counter that supports incrementing and decrementing. This ‘Counter` uses the Riak 2 Data Types feature. If you’re interested in Riak 1.4 Counters, see Riak::Counter.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#batch {|batch_counter| ... } ⇒ Object
Yields a BatchCounter to turn multiple increments into a single Riak hit.
-
#decrement(amount = 1) ⇒ Object
Decrement the counter.
-
#increment(amount = 1, options = {}) ⇒ Object
Increment the counter.
-
#initialize(bucket, key, bucket_type = nil, options = {}) ⇒ Counter
constructor
Create a counter instance.
- #pretty_print(pp) ⇒ Object
-
#value ⇒ Object
(also: #to_i)
The current value of the counter; hits the server if the value has not been fetched or if the counter has been incremented.
Methods inherited from Base
#==, #context?, #dirty?, #inspect_name, #pretty_print_cycle, #reload
Methods included from Util::Translation
Constructor Details
#initialize(bucket, key, bucket_type = nil, options = {}) ⇒ Counter
Create a counter instance. The bucket type is determined by the first of these sources:
-
The ‘bucket_type` String argument
-
A BucketTyped::Bucket as the ‘bucket` argument
-
The ‘Crdt::Base::DEFAULT_BUCKET_TYPES` entry
36 37 38 |
# File 'lib/riak/crdt/counter.rb', line 36 def initialize(bucket, key, bucket_type = nil, = {}) super(bucket, key, bucket_type || :counter, ) end |
Instance Method Details
#batch {|batch_counter| ... } ⇒ Object
Yields a BatchCounter to turn multiple increments into a single Riak hit.
59 60 61 62 63 64 65 |
# File 'lib/riak/crdt/counter.rb', line 59 def batch batcher = BatchCounter.new yield batcher increment batcher.accumulator end |
#decrement(amount = 1) ⇒ Object
Decrement the counter.
72 73 74 |
# File 'lib/riak/crdt/counter.rb', line 72 def decrement(amount = 1) increment -amount end |
#increment(amount = 1, options = {}) ⇒ Object
Increment the counter.
51 52 53 |
# File 'lib/riak/crdt/counter.rb', line 51 def increment(amount = 1, = {}) operate operation(amount), end |
#pretty_print(pp) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/riak/crdt/counter.rb', line 76 def pretty_print(pp) super pp do pp.comma_breakable pp.text "value=#{value}" end end |
#value ⇒ Object Also known as: to_i
The current value of the counter; hits the server if the value has not been fetched or if the counter has been incremented.
42 43 44 45 |
# File 'lib/riak/crdt/counter.rb', line 42 def value reload if dirty? return @value end |