class Riak::Counter

A distributed counter that supports incrementing by positive and negative integers.

Attributes

bucket[RW]
client[RW]
key[RW]

Public Class Methods

new(bucket, key) click to toggle source

Create a Riak counter. @param [Bucket] bucket the {Riak::Bucket} for this counter @param [String] key the name of the counter

# 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

Public Instance Methods

decrement(amount = 1, options = {}) click to toggle source

Decrement the counter. @param amount [Integer] the amount to decrement the counter by. Negative values increment the counter. @param [Hash] options @option options [Boolean] :return_value whether to return the new counter value. Default false. @option options [Fixnum,String] :r (“quorum”) read quorum (numeric or symbolic) @option options [Fixnum] :w the “w” parameter (Write quorum) @option options [Fixnum] :dw the “dw” parameter (Durable-write quorum)

# File lib/riak/counter.rb, line 76
def decrement(amount = 1, options = {})
  increment(-amount, options)
end
decrement_and_return(amount = 1) click to toggle source

Decrement the counter and return its new value. @param amount [Integer] the amount to decrement the counter by. Negative values increment the counter.

# File lib/riak/counter.rb, line 45
def decrement_and_return(amount = 1)
  increment_and_return -amount
end
increment(amount = 1, options = {}) click to toggle source

Increment the counter. @param amount [Integer] the amount to increment the counter by @param [Hash] options @option options [Boolean] :return_value whether to return the new counter value. Default false. @option options [Fixnum,String] :r (“quorum”) read quorum (numeric or symbolic) @option options [Fixnum] :w the “w” parameter (Write quorum) @option options [Fixnum] :dw the “dw” parameter (Durable-write quorum)

# File lib/riak/counter.rb, line 58
def increment(amount = 1, options = {})
  validate_amount amount

  backend do |backend|
    backend.post_counter bucket, key, amount, options
  end
end
increment_and_return(amount = 1) click to toggle source

Increment the counter and return its new value. @param amount [Integer] the amount to increment the counter by.

# File lib/riak/counter.rb, line 38
def increment_and_return(amount = 1)
  increment amount, return_value: true
end
to_i(options = {})
Alias for: value
value(options = {}) click to toggle source

Retrieve the current value of the counter. @param [Hash] options @option options [Fixnum,String] :r (“quorum”) read quorum (numeric or symbolic)

# File lib/riak/counter.rb, line 29
def value(options = {})
  backend do |backend|
    backend.get_counter bucket, key, options
  end
end
Also aliased as: to_i

Private Instance Methods

backend(&blk) click to toggle source
# File lib/riak/counter.rb, line 89
def backend(&blk)
  begin
    return client.backend &blk
  rescue Riak::FailedRequest => e
    raise QuorumError.new e if e.message =~ /unsatisfied/
    raise e
  end
end
validate_amount(amount) click to toggle source
# File lib/riak/counter.rb, line 85
def validate_amount(amount)
  raise ArgumentError, t("counter.increment_by_integer") unless amount.is_a? Integer
end
validate_bucket() click to toggle source
# File lib/riak/counter.rb, line 81
def validate_bucket
  raise ArgumentError, t("counter.bucket_needs_allow_mult") unless bucket.allow_mult
end