Class: Riak::Crdt::TypedCollection
- 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
-
#[](key) ⇒ Object
Get the value for a given key.
-
#[]=(key, value) ⇒ Object
(also: #increment)
Set the value for a given key.
- #content_name ⇒ Object
-
#context? ⇒ Boolean
Does this set have the context necessary to remove elements?.
-
#delete(key) ⇒ Object
Remove the entry from the map.
-
#include?(key) ⇒ Boolean
Check if a value for a given key exists in this map.
-
#initialize(type, parent, contents = {}) ⇒ TypedCollection
constructor
private
A new instance of TypedCollection.
- #inspect_name ⇒ Object
- #length ⇒ Object
- #operate(key, inner_operation) ⇒ Object private
- #pretty_print(pp) ⇒ Object
- #pretty_print_contents(_pp) ⇒ Object
- #pretty_print_cycle(pp) ⇒ Object
- #reparent(new_parent) ⇒ Object private
- #to_value_h ⇒ Object
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.
24 25 26 27 28 29 30 |
# File 'lib/riak/crdt/typed_collection.rb', line 24 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/riak/crdt/typed_collection.rb', line 90 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.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/riak/crdt/typed_collection.rb', line 113 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_name ⇒ Object
67 68 69 |
# File 'lib/riak/crdt/typed_collection.rb', line 67 def content_name @type.name end |
#context? ⇒ Boolean
Does this set have the context necessary to remove elements?
158 159 160 |
# File 'lib/riak/crdt/typed_collection.rb', line 158 def context? !!@parent.context? end |
#delete(key) ⇒ Object
Remove the entry from the map.
136 137 138 139 140 141 142 143 144 |
# File 'lib/riak/crdt/typed_collection.rb', line 136 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.
82 83 84 |
# File 'lib/riak/crdt/typed_collection.rb', line 82 def include?(key) @contents.include? normalize_key(key) end |
#inspect_name ⇒ Object
57 58 59 |
# File 'lib/riak/crdt/typed_collection.rb', line 57 def inspect_name "contains=#{content_name}" end |
#length ⇒ Object
129 130 131 |
# File 'lib/riak/crdt/typed_collection.rb', line 129 def length @contents.length 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.
147 148 149 150 151 152 153 |
# File 'lib/riak/crdt/typed_collection.rb', line 147 def operate(key, inner_operation) key = normalize_key(key) inner_operation.name = key @parent.operate inner_operation end |
#pretty_print(pp) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/riak/crdt/typed_collection.rb', line 32 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
61 62 63 64 65 |
# File 'lib/riak/crdt/typed_collection.rb', line 61 def pretty_print_contents(_pp) @contents.map do |k, v| "#{k}=>#{v.inspect}" end.join ', ' end |
#pretty_print_cycle(pp) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/riak/crdt/typed_collection.rb', line 50 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.
72 73 74 75 76 |
# File 'lib/riak/crdt/typed_collection.rb', line 72 def reparent(new_parent) self.class.new(@type, new_parent, @contents) end |
#to_value_h ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/riak/crdt/typed_collection.rb', line 162 def to_value_h return @contents unless NEEDS_NAME.include? @type @contents.map do |k, v| [k, v.value] end.to_h end |