Class: Riak::Counter
- Includes:
- Util::Translation
- Defined in:
- lib/riak/counter.rb
Overview
A distributed counter that supports incrementing by positive and negative integers.
Defined Under Namespace
Classes: QuorumError
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#client ⇒ Object
Returns the value of attribute client.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
-
#decrement(amount = 1, options = {}) ⇒ Object
Decrement the counter.
-
#decrement_and_return(amount = 1) ⇒ Object
Decrement the counter and return its new value.
-
#increment(amount = 1, options = {}) ⇒ Object
Increment the counter.
-
#increment_and_return(amount = 1) ⇒ Object
Increment the counter and return its new value.
-
#initialize(bucket, key) ⇒ Counter
constructor
Create a Riak counter.
-
#value(options = {}) ⇒ Object
(also: #to_i)
Retrieve the current value of the counter.
Methods included from Util::Translation
Constructor Details
#initialize(bucket, key) ⇒ Counter
Create a Riak counter.
16 17 18 19 20 21 22 23 |
# File 'lib/riak/counter.rb', line 16 def initialize(bucket, key) raise ArgumentError, t('bucket_type', bucket: bucket.inspect) unless bucket.is_a? Bucket raise ArgumentError, t('string_type', string: key.inspect) unless key.is_a? String @bucket, @key = bucket, key @client = bucket.client validate_bucket end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
9 10 11 |
# File 'lib/riak/counter.rb', line 9 def bucket @bucket end |
#client ⇒ Object
Returns the value of attribute client.
11 12 13 |
# File 'lib/riak/counter.rb', line 11 def client @client end |
#key ⇒ Object
Returns the value of attribute key.
10 11 12 |
# File 'lib/riak/counter.rb', line 10 def key @key end |
Instance Method Details
#decrement(amount = 1, options = {}) ⇒ Object
Decrement the counter. values increment the counter. value. Default false. symbolic)
76 77 78 |
# File 'lib/riak/counter.rb', line 76 def decrement(amount = 1, = {}) increment(-amount, ) end |
#decrement_and_return(amount = 1) ⇒ Object
Decrement the counter and return its new value. values increment the counter.
45 46 47 |
# File 'lib/riak/counter.rb', line 45 def decrement_and_return(amount = 1) increment_and_return -amount end |
#increment(amount = 1, options = {}) ⇒ Object
Increment the counter. value. Default false. symbolic)
58 59 60 61 62 63 64 |
# File 'lib/riak/counter.rb', line 58 def increment(amount = 1, = {}) validate_amount amount backend do |backend| backend.post_counter bucket, key, amount, end end |
#increment_and_return(amount = 1) ⇒ Object
Increment the counter and return its new value.
38 39 40 |
# File 'lib/riak/counter.rb', line 38 def increment_and_return(amount = 1) increment amount, return_value: true end |
#value(options = {}) ⇒ Object Also known as: to_i
Retrieve the current value of the counter. symbolic)
29 30 31 32 33 |
# File 'lib/riak/counter.rb', line 29 def value( = {}) backend do |backend| backend.get_counter bucket, key, end end |