Class: Riak::Crdt::TypedCollection

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

Overview

A collection of elements of a given type inside a Map.

Constant Summary collapse

ALREADY_WRAPPED =
::Set.new [InnerCounter, InnerFlag, InnerMap]
NEEDS_NAME =
::Set.new [InnerCounter, InnerSet, InnerMap]
INITIALIZE_NIL =
::Set.new [InnerRegister]

Instance Method Summary collapse

Constructor Details

#initialize(type, parent, contents = {}) ⇒ TypedCollection

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 TypedCollection


10
11
12
13
14
15
16
# File 'lib/riak/crdt/typed_collection.rb', line 10

def initialize(type, parent, contents = {})
  @type = type
  @parent = parent
  contents = {} if contents.nil?
  stringified_contents = contents.stringify_keys
  @contents = materialize_contents stringified_contents
end

Instance Method Details

#[](key) ⇒ Object

Get the value for a given key


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/riak/crdt/typed_collection.rb', line 76

def [](key)
  key = normalize_key key
  if include? key
    candidate = @contents[key]
    return candidate unless candidate.respond_to? :parent
    return candidate if candidate.parent == self
  end

  return nil if initialize_nil?

  new_instance = @type.new self
  new_instance.name = key if needs_name?

  new_instance
end

#[]=(key, value) ⇒ Object Also known as: increment

Set the value for a given key. Operation of this method is only defined for InnerCounter, InnerRegister, and InnerFlag types.


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/riak/crdt/typed_collection.rb', line 99

def []=(key, value)
  key = normalize_key key

  operation = @type.update value
  operation.name = key

  result = @parent.operate operation

  @contents[key] = @type.new self, value
  @contents[key].name = key if needs_name?

  result
end

#content_nameObject


53
54
55
# File 'lib/riak/crdt/typed_collection.rb', line 53

def content_name
  @type.name
end

#context?Boolean

Does this set have the context necessary to remove elements?


140
141
142
# File 'lib/riak/crdt/typed_collection.rb', line 140

def context?
  !!@parent.context?
end

#delete(key) ⇒ Object

Remove the entry from the map.


118
119
120
121
122
123
124
125
126
# File 'lib/riak/crdt/typed_collection.rb', line 118

def delete(key)
  key = normalize_key key
  operation = @type.delete
  operation.name = key

  @parent.operate operation

  @contents.delete key
end

#include?(key) ⇒ Boolean

Check if a value for a given key exists in this map.


68
69
70
# File 'lib/riak/crdt/typed_collection.rb', line 68

def include?(key)
  @contents.include? normalize_key(key)
end

#inspect_nameObject


43
44
45
# File 'lib/riak/crdt/typed_collection.rb', line 43

def inspect_name
  "contains=#{content_name}"
end

#operate(key, inner_operation) ⇒ 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.


129
130
131
132
133
134
135
# File 'lib/riak/crdt/typed_collection.rb', line 129

def operate(key, inner_operation)
  key = normalize_key key

  inner_operation.name = key

  @parent.operate inner_operation
end

#pretty_print(pp) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/riak/crdt/typed_collection.rb', line 18

def pretty_print(pp)
  pp.object_group self do
    pp.breakable
    pp.text inspect_name
    pp.comma_breakable
    pp.text 'parent='
    @parent.pretty_print_cycle(pp)
    pp.comma_breakable
    pp.text 'contents='
    pp.pp @contents
  end
  # buf = []
  # buf << inspect_name
  # buf <<
  # buf << "contents={#{inspect_contents}}"
  # "#<#{self.class.name} #{buf.join ' '}>"
end

#pretty_print_contents(_pp) ⇒ Object


47
48
49
50
51
# File 'lib/riak/crdt/typed_collection.rb', line 47

def pretty_print_contents(_pp)
  @contents.map do |k, v|
    "#{k}=>#{v.inspect}"
  end.join ', '
end

#pretty_print_cycle(pp) ⇒ Object


36
37
38
39
40
41
# File 'lib/riak/crdt/typed_collection.rb', line 36

def pretty_print_cycle(pp)
  pp.object_group self do
    pp.breakable
    @parent.pretty_print_cycle(pp)
  end
end

#reparent(new_parent) ⇒ 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.


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

def reparent(new_parent)
  self.class.new(@type,
                 new_parent,
                 @contents)
end

#to_value_hObject


144
145
146
147
148
149
150
# File 'lib/riak/crdt/typed_collection.rb', line 144

def to_value_h
  return @contents unless NEEDS_NAME.include? @type

  @contents.map do |k, v|
    [k, v.value]
  end.to_h
end