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

Parameters:

  • key (String)

    the key to get the value for

Returns:

  • the value for the 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.

Parameters:

  • key (String)

    the key to set a new value for

  • value (Boolean, String, Integer)

    the value to set at the key, or in the case of counters, the amount to increment



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?

Returns:

  • (Boolean)

    if the set has a defined context



144
145
146
# File 'lib/riak/crdt/typed_collection.rb', line 144

def context?
  !!@parent.context?
end

#delete(key) ⇒ Object

Remove the entry from the map.

Parameters:

  • key (String)

    the key to remove from the map



122
123
124
125
126
127
128
129
130
# File 'lib/riak/crdt/typed_collection.rb', line 122

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.

Parameters:

  • key (String)

    the key to check for

Returns:

  • (Boolean)

    if the key is inside this collection



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

#lengthObject



115
116
117
# File 'lib/riak/crdt/typed_collection.rb', line 115

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.



133
134
135
136
137
138
139
# File 'lib/riak/crdt/typed_collection.rb', line 133

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



148
149
150
151
152
153
154
# File 'lib/riak/crdt/typed_collection.rb', line 148

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

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