Class: Riak::Crdt::Map

Inherits:
Base show all
Defined in:
lib/riak/crdt/map.rb

Overview

A distributed map of multiple fields, such as counters, flags, registers, sets, and, recursively, other maps, using the Riak 2 Data Types feature.

Maps are complex, and the implementation is spread across many classes. You’re looking at the top-level Map class, but there are also others that are also responsible for how maps work:

Instance Attribute Summary collapse

Attributes inherited from Base

#bucket, #bucket_type, #key

Instance Method Summary collapse

Methods inherited from Base

#==, #context?, #dirty?, #inspect_name, #pretty_print_cycle, #reload

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(bucket, key, bucket_type = nil, options = {}) ⇒ Map

Create a map instance. The bucket type is determined by the first of these sources:

  1. The ‘bucket_type` String argument

  2. A BucketTyped::Bucket as the ‘bucket` argument

  3. The ‘Crdt::Base::DEFAULT_BUCKET_TYPES` entry

Parameters:



35
36
37
38
39
40
41
42
43
# File 'lib/riak/crdt/map.rb', line 35

def initialize(bucket, key, bucket_type = nil, options = {})
  super(bucket, key, bucket_type || :map, options)

  if key
    initialize_collections
  else
    initialize_blank_collections
  end
end

Instance Attribute Details

#countersObject (readonly)

Returns the value of attribute counters.



20
21
22
# File 'lib/riak/crdt/map.rb', line 20

def counters
  @counters
end

#flagsObject (readonly)

Returns the value of attribute flags.



20
21
22
# File 'lib/riak/crdt/map.rb', line 20

def flags
  @flags
end

#mapsObject (readonly)

Returns the value of attribute maps.



20
21
22
# File 'lib/riak/crdt/map.rb', line 20

def maps
  @maps
end

#registersObject (readonly)

Returns the value of attribute registers.



20
21
22
# File 'lib/riak/crdt/map.rb', line 20

def registers
  @registers
end

#setsObject (readonly)

Returns the value of attribute sets.



20
21
22
# File 'lib/riak/crdt/map.rb', line 20

def sets
  @sets
end

Instance Method Details

#batch(*args) {|batch_map| ... } ⇒ Object

Maps are frequently updated in batches. Use this method to get a BatchMap to turn multiple operations into a single Riak update request.

Yield Parameters:

  • batch_map (BatchMap)

    collects updates and other operations



50
51
52
53
54
55
56
# File 'lib/riak/crdt/map.rb', line 50

def batch(*args)
  batch_map = BatchMap.new self

  yield batch_map

  write_operations batch_map.operations, *args
end

#operate(operation, *args) ⇒ 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.

This method *for internal use only* is used to collect oprations from disparate sources to provide a user-friendly API.



62
63
64
65
66
# File 'lib/riak/crdt/map.rb', line 62

def operate(operation, *args)
  batch *args do |m|
    m.operate operation
  end
end

#pretty_print(pp) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/riak/crdt/map.rb', line 68

def pretty_print(pp)
  super pp do
    %w{counters flags maps registers sets}.each do |h|
      pp.comma_breakable
      pp.text "#{h}="
      pp.pp send h
    end
  end
end

#to_value_hObject Also known as: value



78
79
80
81
82
# File 'lib/riak/crdt/map.rb', line 78

def to_value_h
  %w{counters flags maps registers sets}.map do |k|
    [k, send(k).to_value_h]
  end.to_h
end