Class: Riak::Crdt::InnerCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/riak/crdt/inner_counter.rb

Overview

The InnerCounter lives inside a Map, or an InnerMap inside of a Map, and is accessed through a TypedCollection.

Much like the Counter, it provides an integer value that can be incremented and decremented.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, value = 0) ⇒ InnerCounter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of InnerCounter.



40
41
42
43
# File 'lib/riak/crdt/inner_counter.rb', line 40

def initialize(parent, value = 0)
  @parent = parent
  @value = value
end

Instance Attribute Details

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of this counter inside a map.



26
27
28
# File 'lib/riak/crdt/inner_counter.rb', line 26

def name
  @name
end

#parentObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parent of this counter.



37
38
39
# File 'lib/riak/crdt/inner_counter.rb', line 37

def parent
  @parent
end

#valueInteger (readonly) Also known as: to_i

The value of this counter.

Returns:

  • (Integer)

    counter value



31
32
33
# File 'lib/riak/crdt/inner_counter.rb', line 31

def value
  @value
end

Class Method Details

.deleteObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
92
# File 'lib/riak/crdt/inner_counter.rb', line 88

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

.update(increment) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
83
84
85
# File 'lib/riak/crdt/inner_counter.rb', line 80

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

Instance Method Details

#batch {|batch_counter| ... } ⇒ Object

Perform multiple increments against this counter, and collapse them into a single operation.

Yield Parameters:

  • batch_counter (BatchCounter)

    actually collects the operations.



64
65
66
67
68
69
70
# File 'lib/riak/crdt/inner_counter.rb', line 64

def batch
  batcher = BatchCounter.new

  yield batcher

  increment batcher.accumulator
end

#decrement(amount = 1) ⇒ Object

Decrement the counter. Opposite of increment.

Parameters:

  • amount (Integer) (defaults to: 1)

    How much to decrement from the counter.



55
56
57
# File 'lib/riak/crdt/inner_counter.rb', line 55

def decrement(amount = 1)
  increment -amount
end

#increment(amount = 1) ⇒ Object

Increment the counter.

Parameters:

  • amount (Integer) (defaults to: 1)

    How much to increment the counter by.



48
49
50
# File 'lib/riak/crdt/inner_counter.rb', line 48

def increment(amount = 1)
  @parent.increment name, amount
end

#pretty_print(pp) ⇒ Object



72
73
74
75
76
77
# File 'lib/riak/crdt/inner_counter.rb', line 72

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