Sha256: 590effcfa95f525a7b32b6c9ee1d2a91a5d871ef9fcad6c28de1e1e20e5544bd

Contents?: true

Size: 1.92 KB

Versions: 12

Compression:

Stored size: 1.92 KB

Contents

module Riak
  module Crdt
    # The {InnerCounter} lives inside a {Map}, or an {InnerMap} inside of a
    # {Map}, and is accessed through a {TypedCollection}.
    #
    # Much like the {Riak::Crdt::Counter}, it provides an integer value that can
    # be incremented and decremented.
    class InnerCounter
      # The name of this counter inside a map.
      #
      # @api private
      attr_accessor :name

      # The value of this counter.
      #
      # @return [Integer] counter value
      attr_reader :value
      alias :to_i :value

      # The parent of this counter.
      #
      # @api private
      attr_reader :parent

      # @api private
      def initialize(parent, value = 0)
        @parent = parent
        @value = value
      end

      # Increment the counter.
      #
      # @param [Integer] amount How much to increment the counter by.
      def increment(amount = 1)
        @parent.increment name, amount
      end

      # Decrement the counter. Opposite of increment.
      #
      # @param [Integer] amount How much to decrement from the counter.
      def decrement(amount = 1)
        increment -amount
      end

      # Perform multiple increments against this counter, and collapse
      # them into a single operation.
      #
      # @yieldparam [BatchCounter] batch_counter actually collects the
      #                            operations.
      def batch
        batcher = BatchCounter.new

        yield batcher

        increment batcher.accumulator
      end

      def pretty_print(pp)
        pp.object_group self do
          pp.breakable
          pp.pp value
        end
      end

      # @api private
      def self.update(increment)
        Operation::Update.new.tap do |op|
          op.value = increment
          op.type = :counter
        end
      end

      # @api private
      def self.delete
        Operation::Delete.new.tap do |op|
          op.type = :counter
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
riak-client-2.6.0 lib/riak/crdt/inner_counter.rb
riak-client-2.5.0 lib/riak/crdt/inner_counter.rb
riak-client-2.4.1 lib/riak/crdt/inner_counter.rb
riak-client-2.4.0 lib/riak/crdt/inner_counter.rb
riak-client-2.4.0.pre1 lib/riak/crdt/inner_counter.rb
riak-client-2.3.2 lib/riak/crdt/inner_counter.rb
riak-client-2.3.1 lib/riak/crdt/inner_counter.rb
riak-client-2.3.0 lib/riak/crdt/inner_counter.rb
riak-client-2.2.2 lib/riak/crdt/inner_counter.rb
riak-client-2.2.1 lib/riak/crdt/inner_counter.rb
riak-client-noenc-1.0.0 lib/riak/crdt/inner_counter.rb
riak-client-2.2.0 lib/riak/crdt/inner_counter.rb