Class: Riak::Counter

Inherits:
Object show all
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

Instance Method Summary collapse

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(bucket, key) ⇒ Counter

Create a Riak counter.

Parameters:

  • bucket (Bucket)

    the Bucket for this counter

  • key (String)

    the name of the counter

Raises:

  • (ArgumentError)

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

#bucketObject

Returns the value of attribute bucket


9
10
11
# File 'lib/riak/counter.rb', line 9

def bucket
  @bucket
end

#clientObject

Returns the value of attribute client


11
12
13
# File 'lib/riak/counter.rb', line 11

def client
  @client
end

#keyObject

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)

Parameters:

  • amount (Integer) (defaults to: 1)

    the amount to decrement the counter by. Negative

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :return_value (Boolean)

    whether to return the new counter

  • :r (Fixnum, String) — default: "quorum"

    read quorum (numeric or

  • :w (Fixnum)

    the “w” parameter (Write quorum)

  • :dw (Fixnum)

    the “dw” parameter (Durable-write quorum)


76
77
78
# File 'lib/riak/counter.rb', line 76

def decrement(amount = 1, options = {})
  increment(-amount, options)
end

#decrement_and_return(amount = 1) ⇒ Object

Decrement the counter and return its new value. values increment the counter.

Parameters:

  • amount (Integer) (defaults to: 1)

    the amount to decrement the counter by. Negative


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)

Parameters:

  • amount (Integer) (defaults to: 1)

    the amount to increment the counter by

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :return_value (Boolean)

    whether to return the new counter

  • :r (Fixnum, String) — default: "quorum"

    read quorum (numeric or

  • :w (Fixnum)

    the “w” parameter (Write quorum)

  • :dw (Fixnum)

    the “dw” parameter (Durable-write quorum)


58
59
60
61
62
63
64
# 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) ⇒ Object

Increment the counter and return its new value.

Parameters:

  • amount (Integer) (defaults to: 1)

    the amount to increment the counter by.


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)

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :r (Fixnum, String) — default: "quorum"

    read quorum (numeric or


29
30
31
32
33
# File 'lib/riak/counter.rb', line 29

def value(options = {})
  backend do |backend|
    backend.get_counter bucket, key, options
  end
end