Class: Riak::CRDT::TPNCounter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TPNCounter

Create a new Transaction PNCounter

Parameters:

  • options (Hash)

    :actor [String]
    :history_length [Integer]
    



13
14
15
16
# File 'lib/crdt/tpncounter.rb', line 13

def initialize(options)
  self.p = TGCounter.new(options)
  self.n = TGCounter.new(options)
end

Instance Attribute Details

#nObject

Returns the value of attribute n.



5
6
7
# File 'lib/crdt/tpncounter.rb', line 5

def n
  @n
end

#pObject

Returns the value of attribute p.



5
6
7
# File 'lib/crdt/tpncounter.rb', line 5

def p
  @p
end

Class Method Details

.from_json(json, options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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

Instance Method Details

#decrement(transaction, value) ⇒ Object

Increment this actor’s negative transaction array, overwriting if the value exists

Parameters:

  • transaction (String)
  • value (Integer)


47
48
49
# File 'lib/crdt/tpncounter.rb', line 47

def decrement(transaction, value)
  self.n.increment(transaction, value)
end

#has_transaction?(transaction) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# 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) ⇒ Object

Increment this actor’s positive transaction array, overwriting if the value exists

Parameters:

  • transaction (String)
  • value (Integer)


40
41
42
# File 'lib/crdt/tpncounter.rb', line 40

def increment(transaction, value)
  self.p.increment(transaction, value)
end

#merge(other) ⇒ Object



59
60
61
62
# File 'lib/crdt/tpncounter.rb', line 59

def merge(other)
  self.p.merge(other.p)
  self.n.merge(other.n)
end

#to_jsonObject



18
19
20
21
22
23
24
# 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

#valueObject



51
52
53
# File 'lib/crdt/tpncounter.rb', line 51

def value
  self.p.value - self.n.value
end