class Riak::Counter
A distributed counter that supports incrementing by positive and negative integers.
Attributes
Public Class Methods
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 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 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 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 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
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
Private Instance Methods
# 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
# File lib/riak/counter.rb, line 85 def validate_amount(amount) raise ArgumentError, t("counter.increment_by_integer") unless amount.is_a? Integer end
# File lib/riak/counter.rb, line 81 def validate_bucket raise ArgumentError, t("counter.bucket_needs_allow_mult") unless bucket.allow_mult end