Sha256: 9d357ac03b5b3a111d833fe60d4a27324d954b7c1e579b6c8a08d8c4deaad018
Contents?: true
Size: 1.99 KB
Versions: 1
Compression:
Stored size: 1.99 KB
Contents
module Riak module Crdt # 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}. class Counter < Base # Create a counter instance. If not provided, the default bucket type # from {Riak::Crdt} will be used. # # @param [Bucket] bucket the {Riak::Bucket} for this counter # @param [String, nil] key The name of the counter. A nil key makes # Riak assign a key. # @param [String] bucket_type The optional bucket type for this counter. # The default is in `Crdt::Base::DEFAULT_BUCKET_TYPES[:counter]`. # @param [Hash] options def initialize(bucket, key, bucket_type=nil, options={}) super(bucket, key, bucket_type || DEFAULT_BUCKET_TYPES[:counter], options) end # The current value of the counter; hits the server if the value has # not been fetched or if the counter has been incremented. def value reload if dirty? return @value end # Increment the counter. # # @param [Integer] amount # @param [Hash] options def increment(amount=1, options={}) operate operation(amount), options end # Yields a {BatchCounter} to turn multiple increments into a single # Riak hit. # # @yieldparam [BatchCounter] batch_counter collects multiple increments def batch batcher = BatchCounter.new yield batcher increment batcher.accumulator end alias :to_i :value # Decrement the counter. # # @param [Integer] amount def decrement(amount=1) increment -amount end private def vivify(value) @value = value end def operation(amount) Operation::Update.new.tap do |op| op.type = :counter op.value = amount end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
riak-client-2.0.0.rc1 | lib/riak/crdt/counter.rb |