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



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

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.



12
13
14
# File 'lib/riak/crdt/inner_counter.rb', line 12

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.



23
24
25
# File 'lib/riak/crdt/inner_counter.rb', line 23

def parent
  @parent
end

#valueInteger (readonly) Also known as: to_i

The value of this counter.

Returns:

  • (Integer)

    counter value



17
18
19
# File 'lib/riak/crdt/inner_counter.rb', line 17

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.



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

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.



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

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.



50
51
52
53
54
55
56
# File 'lib/riak/crdt/inner_counter.rb', line 50

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.



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

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.



34
35
36
# File 'lib/riak/crdt/inner_counter.rb', line 34

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

#pretty_print(pp) ⇒ Object



58
59
60
61
62
63
# File 'lib/riak/crdt/inner_counter.rb', line 58

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