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
22 23 24 |
# File 'lib/riak/crdt/counter.rb', line 22 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.
45 46 47 48 49 50 51 |
# File 'lib/riak/crdt/counter.rb', line 45 def batch batcher = BatchCounter.new yield batcher increment batcher.accumulator end |
#decrement(amount = 1) ⇒ Object
Decrement the counter.
58 59 60 |
# File 'lib/riak/crdt/counter.rb', line 58 def decrement(amount = 1) increment -amount end |
#increment(amount = 1, options = {}) ⇒ Object
Increment the counter.
37 38 39 |
# File 'lib/riak/crdt/counter.rb', line 37 def increment(amount = 1, = {}) operate operation(amount), end |
#pretty_print(pp) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/riak/crdt/counter.rb', line 62 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.
28 29 30 31 |
# File 'lib/riak/crdt/counter.rb', line 28 def value reload if dirty? return @value end |