class Riak::CRDT::TPNCounter

Attributes

n[RW]
p[RW]

Public Class Methods

from_json(json, options) click to toggle source
# File lib/crdt/tpncounter.rb, line 26
def self.from_json(json, options)
  h = JSON.parse json
  raise ArgumentError.new 'unexpected type field in JSON' unless h['type'] == 'TPNCounter'

  pnc = new(options)
  pnc.p = TGCounter.from_hash(h['p'], options)
  pnc.n = TGCounter.from_hash(h['n'], options)

  return pnc
end
new(options) click to toggle source

Create a new Transaction PNCounter @param [Hash] options

{
  :actor [String]
  :history_length [Integer]
}
# File lib/crdt/tpncounter.rb, line 13
def initialize(options)
  self.p = TGCounter.new(options)
  self.n = TGCounter.new(options)
end

Public Instance Methods

decrement(transaction, value) click to toggle source

Increment this actor’s negative transaction array, overwriting if the value exists @param [String] transaction @param [Integer] value

# File lib/crdt/tpncounter.rb, line 47
def decrement(transaction, value)
  self.n.increment(transaction, value)
end
has_transaction?(transaction) click to toggle source
# File lib/crdt/tpncounter.rb, line 55
def has_transaction?(transaction)
  self.p.has_transaction?(transaction) || self.n.has_transaction?(transaction)
end
increment(transaction, value) click to toggle source

Increment this actor’s positive transaction array, overwriting if the value exists @param [String] transaction @param [Integer] value

# File lib/crdt/tpncounter.rb, line 40
def increment(transaction, value)
  self.p.increment(transaction, value)
end
merge(other) click to toggle source
# File lib/crdt/tpncounter.rb, line 59
def merge(other)
  self.p.merge(other.p)
  self.n.merge(other.n)
end
to_json() click to toggle source
# File lib/crdt/tpncounter.rb, line 18
def to_json
  {
      type: 'TPNCounter',
      p: self.p.to_hash,
      n: self.n.to_hash
  }.to_json
end
value() click to toggle source
# File lib/crdt/tpncounter.rb, line 51
def value
  self.p.value - self.n.value
end