class Riak::Crdt::TypedCollection

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

Constants

ALREADY_WRAPPED
INITIALIZE_NIL
NEEDS_NAME

Public Class Methods

new(type, parent, contents = {}) click to toggle source

@api private

# 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

Public Instance Methods

[](key) click to toggle source

Get the value for a given key

@param [String] key the key to get the value for @return the value for the given key

# 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) click to toggle source

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

@param [String] key the key to set a new value for @param [Boolean, String, Integer] value the value to set at the key,

or in the case of counters, the amount to increment
# 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
Also aliased as: increment
content_name() click to toggle source
# File lib/riak/crdt/typed_collection.rb, line 53
def content_name
  @type.name
end
context?() click to toggle source

Does this set have the context necessary to remove elements?

@return [Boolean] if the set has a defined context

# File lib/riak/crdt/typed_collection.rb, line 140
def context?
  !!@parent.context?
end
delete(key) click to toggle source

Remove the entry from the map.

@param [String] key the key to remove from the map

# 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) click to toggle source

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

@param [String] key the key to check for @return [Boolean] if the key is inside this collection

# File lib/riak/crdt/typed_collection.rb, line 68
def include?(key)
  @contents.include? normalize_key(key)
end
increment(key, value)
Alias for: []=
inspect_name() click to toggle source
# File lib/riak/crdt/typed_collection.rb, line 43
def inspect_name
  "contains=#{content_name}"
end
operate(key, inner_operation) click to toggle source

@api private

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

@api private

# File lib/riak/crdt/typed_collection.rb, line 58
def reparent(new_parent)
  self.class.new(@type,
                 new_parent,
                 @contents)
end
to_value_h() click to toggle source
# 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

Private Instance Methods

initialize_nil?() click to toggle source
# File lib/riak/crdt/typed_collection.rb, line 158
def initialize_nil?
  INITIALIZE_NIL.include? @type
end
materialize_contents(stringified_contents) click to toggle source
# File lib/riak/crdt/typed_collection.rb, line 166
def materialize_contents(stringified_contents)
  stringified_contents.keys.inject(Hash.new) do |new_contents, key|
    new_contents.tap do |c|
      content = stringified_contents[key]
      if ALREADY_WRAPPED.include? content.class
        c[key] = content
      else
        c[key] = @type.new self, content
      end
      c[key].name = key if needs_name?
    end
  end
end
needs_name?() click to toggle source
# File lib/riak/crdt/typed_collection.rb, line 162
def needs_name?
  NEEDS_NAME.include? @type
end
normalize_key(unnormalized_key) click to toggle source
# File lib/riak/crdt/typed_collection.rb, line 154
def normalize_key(unnormalized_key)
  unnormalized_key.to_s
end