riak-client-noenc 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitignore +42 -0
- data/.rspec +1 -0
- data/Gemfile +17 -0
- data/Guardfile +20 -0
- data/LICENSE.md +16 -0
- data/README.markdown +640 -0
- data/RELEASE_NOTES.md +392 -0
- data/Rakefile +119 -0
- data/lib/riak.rb +22 -0
- data/lib/riak/bucket.rb +297 -0
- data/lib/riak/bucket_properties.rb +74 -0
- data/lib/riak/bucket_type.rb +77 -0
- data/lib/riak/bucket_typed/bucket.rb +121 -0
- data/lib/riak/client.rb +433 -0
- data/lib/riak/client/beefcake/bucket_properties_operator.rb +178 -0
- data/lib/riak/client/beefcake/crdt/counter_loader.rb +18 -0
- data/lib/riak/client/beefcake/crdt/map_loader.rb +64 -0
- data/lib/riak/client/beefcake/crdt/set_loader.rb +18 -0
- data/lib/riak/client/beefcake/crdt_loader.rb +84 -0
- data/lib/riak/client/beefcake/crdt_operator.rb +223 -0
- data/lib/riak/client/beefcake/footer +4 -0
- data/lib/riak/client/beefcake/header +6 -0
- data/lib/riak/client/beefcake/message_codes.rb +89 -0
- data/lib/riak/client/beefcake/message_overlay.rb +87 -0
- data/lib/riak/client/beefcake/messages.rb +772 -0
- data/lib/riak/client/beefcake/object_methods.rb +112 -0
- data/lib/riak/client/beefcake/protocol.rb +105 -0
- data/lib/riak/client/beefcake/socket.rb +260 -0
- data/lib/riak/client/beefcake_protobuffs_backend.rb +538 -0
- data/lib/riak/client/decaying.rb +36 -0
- data/lib/riak/client/feature_detection.rb +120 -0
- data/lib/riak/client/instrumentation.rb +19 -0
- data/lib/riak/client/node.rb +49 -0
- data/lib/riak/client/protobuffs_backend.rb +143 -0
- data/lib/riak/client/search.rb +27 -0
- data/lib/riak/client/yokozuna.rb +52 -0
- data/lib/riak/conflict.rb +13 -0
- data/lib/riak/core_ext.rb +7 -0
- data/lib/riak/core_ext/blank.rb +53 -0
- data/lib/riak/core_ext/deep_dup.rb +13 -0
- data/lib/riak/core_ext/extract_options.rb +7 -0
- data/lib/riak/core_ext/json.rb +15 -0
- data/lib/riak/core_ext/slice.rb +18 -0
- data/lib/riak/core_ext/stringify_keys.rb +10 -0
- data/lib/riak/core_ext/symbolize_keys.rb +10 -0
- data/lib/riak/core_ext/to_param.rb +31 -0
- data/lib/riak/counter.rb +101 -0
- data/lib/riak/crdt.rb +21 -0
- data/lib/riak/crdt/base.rb +183 -0
- data/lib/riak/crdt/batch_counter.rb +19 -0
- data/lib/riak/crdt/batch_map.rb +41 -0
- data/lib/riak/crdt/counter.rb +82 -0
- data/lib/riak/crdt/inner_counter.rb +81 -0
- data/lib/riak/crdt/inner_flag.rb +42 -0
- data/lib/riak/crdt/inner_map.rb +75 -0
- data/lib/riak/crdt/inner_register.rb +26 -0
- data/lib/riak/crdt/inner_set.rb +102 -0
- data/lib/riak/crdt/map.rb +121 -0
- data/lib/riak/crdt/operation.rb +19 -0
- data/lib/riak/crdt/set.rb +166 -0
- data/lib/riak/crdt/typed_collection.rb +181 -0
- data/lib/riak/encoding.rb +6 -0
- data/lib/riak/errors/backend_creation.rb +9 -0
- data/lib/riak/errors/base.rb +9 -0
- data/lib/riak/errors/connection_error.rb +50 -0
- data/lib/riak/errors/crdt_error.rb +38 -0
- data/lib/riak/errors/failed_request.rb +58 -0
- data/lib/riak/errors/protobuffs_error.rb +11 -0
- data/lib/riak/errors/search_error.rb +35 -0
- data/lib/riak/i18n.rb +7 -0
- data/lib/riak/index_collection.rb +71 -0
- data/lib/riak/instrumentation.rb +6 -0
- data/lib/riak/json.rb +52 -0
- data/lib/riak/link.rb +96 -0
- data/lib/riak/list_buckets.rb +28 -0
- data/lib/riak/locale/en.yml +107 -0
- data/lib/riak/locale/fr.yml +51 -0
- data/lib/riak/map_reduce.rb +295 -0
- data/lib/riak/map_reduce/filter_builder.rb +103 -0
- data/lib/riak/map_reduce/phase.rb +98 -0
- data/lib/riak/map_reduce/results.rb +49 -0
- data/lib/riak/map_reduce_error.rb +7 -0
- data/lib/riak/multiget.rb +122 -0
- data/lib/riak/preflist_item.rb +7 -0
- data/lib/riak/rcontent.rb +173 -0
- data/lib/riak/robject.rb +222 -0
- data/lib/riak/search.rb +11 -0
- data/lib/riak/search/index.rb +87 -0
- data/lib/riak/search/query.rb +141 -0
- data/lib/riak/search/result_collection.rb +144 -0
- data/lib/riak/search/result_document.rb +129 -0
- data/lib/riak/search/schema.rb +65 -0
- data/lib/riak/secondary_index.rb +81 -0
- data/lib/riak/serializers.rb +73 -0
- data/lib/riak/stamp.rb +77 -0
- data/lib/riak/util/escape.rb +80 -0
- data/lib/riak/util/tcp_socket_extensions.rb +58 -0
- data/lib/riak/util/translation.rb +18 -0
- data/lib/riak/version.rb +3 -0
- data/lib/riak/walk_spec.rb +145 -0
- data/spec/failover/failover.rb +59 -0
- data/spec/fixtures/bitcask.txt +25 -0
- data/spec/fixtures/cat.jpg +0 -0
- data/spec/fixtures/multipart-basic-conflict.txt +15 -0
- data/spec/fixtures/multipart-blank.txt +7 -0
- data/spec/fixtures/multipart-mapreduce.txt +10 -0
- data/spec/fixtures/multipart-with-body.txt +16 -0
- data/spec/fixtures/multipart-with-marked-tombstones.txt +17 -0
- data/spec/fixtures/multipart-with-unmarked-tombstone.txt +16 -0
- data/spec/fixtures/server.cert.crt +15 -0
- data/spec/fixtures/server.cert.key +15 -0
- data/spec/fixtures/test.pem +1 -0
- data/spec/fixtures/yz_schema_template.xml +18 -0
- data/spec/integration/riak/bucket_types_spec.rb +270 -0
- data/spec/integration/riak/conflict_resolution_spec.rb +96 -0
- data/spec/integration/riak/counters_spec.rb +36 -0
- data/spec/integration/riak/crdt/configuration_spec.rb +37 -0
- data/spec/integration/riak/crdt_search_spec.rb +176 -0
- data/spec/integration/riak/crdt_spec.rb +250 -0
- data/spec/integration/riak/crdt_validation/map_spec.rb +63 -0
- data/spec/integration/riak/crdt_validation/set_spec.rb +122 -0
- data/spec/integration/riak/preflist_spec.rb +31 -0
- data/spec/integration/riak/properties_spec.rb +69 -0
- data/spec/integration/riak/protobuffs/interrupted_request_spec.rb +33 -0
- data/spec/integration/riak/protobuffs_backends_spec.rb +40 -0
- data/spec/integration/riak/search_spec.rb +104 -0
- data/spec/integration/riak/secondary_index_spec.rb +72 -0
- data/spec/integration/riak/security_spec.rb +100 -0
- data/spec/integration/riak/threading_spec.rb +150 -0
- data/spec/integration/yokozuna/index_spec.rb +61 -0
- data/spec/integration/yokozuna/queries_spec.rb +115 -0
- data/spec/integration/yokozuna/schema_spec.rb +49 -0
- data/spec/riak/beefcake_protobuffs_backend/bucket_properties_operator_spec.rb +247 -0
- data/spec/riak/beefcake_protobuffs_backend/crdt_operator_spec.rb +222 -0
- data/spec/riak/beefcake_protobuffs_backend/object_methods_spec.rb +23 -0
- data/spec/riak/beefcake_protobuffs_backend/protocol_spec.rb +189 -0
- data/spec/riak/beefcake_protobuffs_backend_spec.rb +162 -0
- data/spec/riak/bucket_properties_spec.rb +135 -0
- data/spec/riak/bucket_spec.rb +275 -0
- data/spec/riak/bucket_type_spec.rb +50 -0
- data/spec/riak/bucket_typed/bucket_spec.rb +62 -0
- data/spec/riak/client_spec.rb +246 -0
- data/spec/riak/core_ext/to_param_spec.rb +15 -0
- data/spec/riak/counter_spec.rb +122 -0
- data/spec/riak/crdt/counter_spec.rb +55 -0
- data/spec/riak/crdt/inner_counter_spec.rb +21 -0
- data/spec/riak/crdt/inner_flag_spec.rb +39 -0
- data/spec/riak/crdt/inner_map_spec.rb +47 -0
- data/spec/riak/crdt/inner_register_spec.rb +40 -0
- data/spec/riak/crdt/inner_set_spec.rb +33 -0
- data/spec/riak/crdt/map_spec.rb +78 -0
- data/spec/riak/crdt/set_spec.rb +61 -0
- data/spec/riak/crdt/shared_examples.rb +74 -0
- data/spec/riak/crdt/typed_collection_spec.rb +225 -0
- data/spec/riak/escape_spec.rb +72 -0
- data/spec/riak/feature_detection_spec.rb +77 -0
- data/spec/riak/index_collection_spec.rb +53 -0
- data/spec/riak/instrumentation_spec.rb +124 -0
- data/spec/riak/link_spec.rb +85 -0
- data/spec/riak/list_buckets_spec.rb +41 -0
- data/spec/riak/map_reduce/filter_builder_spec.rb +32 -0
- data/spec/riak/map_reduce/phase_spec.rb +142 -0
- data/spec/riak/map_reduce_spec.rb +434 -0
- data/spec/riak/multiget_spec.rb +81 -0
- data/spec/riak/node_spec.rb +26 -0
- data/spec/riak/robject_spec.rb +496 -0
- data/spec/riak/search/index_spec.rb +72 -0
- data/spec/riak/search/query_spec.rb +88 -0
- data/spec/riak/search/result_collection_spec.rb +89 -0
- data/spec/riak/search/result_document_spec.rb +106 -0
- data/spec/riak/search/schema_spec.rb +63 -0
- data/spec/riak/search_spec.rb +107 -0
- data/spec/riak/secondary_index_spec.rb +225 -0
- data/spec/riak/serializers_spec.rb +121 -0
- data/spec/riak/stamp_spec.rb +54 -0
- data/spec/riak/walk_spec_spec.rb +203 -0
- data/spec/spec_helper.rb +66 -0
- data/spec/support/certs/README.md +13 -0
- data/spec/support/certs/ca.crt +21 -0
- data/spec/support/certs/client.crl +13 -0
- data/spec/support/certs/client.crt +94 -0
- data/spec/support/certs/client.csr +18 -0
- data/spec/support/certs/client.key +27 -0
- data/spec/support/certs/empty_ca.crt +21 -0
- data/spec/support/certs/server.crl +13 -0
- data/spec/support/certs/server.crt +94 -0
- data/spec/support/certs/server.key +27 -0
- data/spec/support/crdt_search_config.rb +112 -0
- data/spec/support/crdt_search_fixtures.rb +42 -0
- data/spec/support/integration_setup.rb +10 -0
- data/spec/support/search_config.rb +83 -0
- data/spec/support/search_corpus_setup.rb +39 -0
- data/spec/support/test_client.rb +46 -0
- data/spec/support/test_client.yml.example +10 -0
- data/spec/support/unified_backend_examples.rb +380 -0
- data/spec/support/version_filter.rb +12 -0
- data/spec/support/wait_until.rb +20 -0
- metadata +511 -0
@@ -0,0 +1,121 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
|
4
|
+
# A distributed map of multiple fields, such as counters, flags, registers,
|
5
|
+
# sets, and, recursively, other maps, using the Riak 2 Data Types feature.
|
6
|
+
#
|
7
|
+
# Maps are complex, and the implementation is spread across many classes.
|
8
|
+
# You're looking at the top-level {Map} class, but there are also others
|
9
|
+
# that are also responsible for how maps work:
|
10
|
+
#
|
11
|
+
# * {InnerMap}: used for maps that live inside other maps
|
12
|
+
# * {BatchMap}: proxies multiple operations into a single Riak update request
|
13
|
+
# * {TypedCollection}: a collection of members of a single map, similar
|
14
|
+
# to a Ruby {Hash}
|
15
|
+
# * {InnerFlag}: a boolean value inside a map
|
16
|
+
# * {InnerRegister}: a {String} value inside a map
|
17
|
+
# * {InnerCounter}: a {Riak::Crdt::Counter}, but inside a map
|
18
|
+
# * {InnerSet}: a {Riak::Crdt::Set}, but inside a map
|
19
|
+
#
|
20
|
+
class Map < Base
|
21
|
+
attr_reader :counters, :flags, :maps, :registers, :sets
|
22
|
+
|
23
|
+
# Create a map instance. The bucket type is determined by the first of
|
24
|
+
# these sources:
|
25
|
+
#
|
26
|
+
# 1. The `bucket_type` String argument
|
27
|
+
# 2. A {BucketTyped::Bucket} as the `bucket` argument
|
28
|
+
# 3. The `Crdt::Base::DEFAULT_BUCKET_TYPES[:map]` entry
|
29
|
+
#
|
30
|
+
# @param bucket [Bucket] the {Riak::Bucket} for this map
|
31
|
+
# @param [String, nil] key The name of the map. A nil key makes
|
32
|
+
# Riak assign a key.
|
33
|
+
# @param [String] bucket_type The optional bucket type for this map.
|
34
|
+
# The default is in `Crdt::Base::DEFAULT_BUCKET_TYPES[:map]`.
|
35
|
+
# @param options [Hash]
|
36
|
+
def initialize(bucket, key, bucket_type = nil, options = {})
|
37
|
+
super(bucket, key, bucket_type || :map, options)
|
38
|
+
|
39
|
+
if key
|
40
|
+
initialize_collections
|
41
|
+
else
|
42
|
+
initialize_blank_collections
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Maps are frequently updated in batches. Use this method to get a
|
47
|
+
# {BatchMap} to turn multiple operations into a single Riak update
|
48
|
+
# request.
|
49
|
+
#
|
50
|
+
# @yieldparam batch_map [BatchMap] collects updates and other operations
|
51
|
+
def batch(*args)
|
52
|
+
batch_map = BatchMap.new self
|
53
|
+
|
54
|
+
yield batch_map
|
55
|
+
|
56
|
+
write_operations batch_map.operations, *args
|
57
|
+
end
|
58
|
+
|
59
|
+
# This method *for internal use only* is used to collect oprations from
|
60
|
+
# disparate sources to provide a user-friendly API.
|
61
|
+
#
|
62
|
+
# @api private
|
63
|
+
def operate(operation, *args)
|
64
|
+
batch *args do |m|
|
65
|
+
m.operate operation
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def pretty_print(pp)
|
70
|
+
super pp do
|
71
|
+
%w{counters flags maps registers sets}.each do |h|
|
72
|
+
pp.comma_breakable
|
73
|
+
pp.text "#{h}="
|
74
|
+
pp.pp send h
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_value_h
|
80
|
+
%w{counters flags maps registers sets}.map do |k|
|
81
|
+
[k, send(k).to_value_h]
|
82
|
+
end.to_h
|
83
|
+
end
|
84
|
+
|
85
|
+
alias :value :to_value_h
|
86
|
+
|
87
|
+
private
|
88
|
+
def vivify(data)
|
89
|
+
@counters = TypedCollection.new InnerCounter, self, data[:counters]
|
90
|
+
@flags = TypedCollection.new InnerFlag, self, data[:flags]
|
91
|
+
@maps = TypedCollection.new InnerMap, self, data[:maps]
|
92
|
+
@registers = TypedCollection.new InnerRegister, self, data[:registers]
|
93
|
+
@sets = TypedCollection.new InnerSet, self, data[:sets]
|
94
|
+
end
|
95
|
+
|
96
|
+
def initialize_collections(data = {})
|
97
|
+
reload if dirty?
|
98
|
+
end
|
99
|
+
|
100
|
+
def initialize_blank_collections
|
101
|
+
vivify counters: {}, flags: {}, maps: {}, registers: {}, sets: {}
|
102
|
+
end
|
103
|
+
|
104
|
+
def write_operations(operations, *args)
|
105
|
+
result = operator do |op|
|
106
|
+
op.operate(bucket.name,
|
107
|
+
key,
|
108
|
+
bucket_type,
|
109
|
+
operations,
|
110
|
+
*args
|
111
|
+
)
|
112
|
+
end
|
113
|
+
|
114
|
+
@key ||= result.key
|
115
|
+
|
116
|
+
# collections break dirty tracking
|
117
|
+
reload
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
# @api private
|
4
|
+
module Operation
|
5
|
+
class Update
|
6
|
+
attr_accessor :parent
|
7
|
+
attr_accessor :name
|
8
|
+
attr_accessor :type
|
9
|
+
attr_accessor :value
|
10
|
+
end
|
11
|
+
|
12
|
+
class Delete
|
13
|
+
attr_accessor :parent
|
14
|
+
attr_accessor :name
|
15
|
+
attr_accessor :type
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
|
4
|
+
# A distributed set containing strings, using the Riak 2 Data Types feature.
|
5
|
+
#
|
6
|
+
# Uses the Ruby standard library `::Set` frequently, so the full class names will
|
7
|
+
# be used frequently.
|
8
|
+
class Set < Base
|
9
|
+
|
10
|
+
# Create a set instance. The bucket type is determined by the first of
|
11
|
+
# these sources:
|
12
|
+
#
|
13
|
+
# 1. The `bucket_type` String argument
|
14
|
+
# 2. A {BucketTyped::Bucket} as the `bucket` argument
|
15
|
+
# 3. The `Crdt::Base::DEFAULT_BUCKET_TYPES[:set]` entry
|
16
|
+
#
|
17
|
+
# @param bucket [Bucket] the {Riak::Bucket} for this set
|
18
|
+
# @param [String, nil] key The name of the set. A nil key makes
|
19
|
+
# Riak assign a key.
|
20
|
+
# @param [String] bucket_type The optional bucket type for this set.
|
21
|
+
# @param options [Hash]
|
22
|
+
def initialize(bucket, key, bucket_type = nil, options = {})
|
23
|
+
super(bucket, key, bucket_type || :set, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Yields a `BatchSet` to proxy multiple set operations into a single
|
27
|
+
# Riak update. The `BatchSet` has the same methods as this
|
28
|
+
# {Riak::Crdt::Set}.
|
29
|
+
#
|
30
|
+
# @yieldparam batch_set [BatchSet] collects set operations
|
31
|
+
def vivify(value)
|
32
|
+
value.each(&:freeze)
|
33
|
+
@members = ::Set.new(value)
|
34
|
+
@members.freeze
|
35
|
+
end
|
36
|
+
|
37
|
+
def batch
|
38
|
+
batcher = BatchSet.new self
|
39
|
+
|
40
|
+
yield batcher
|
41
|
+
|
42
|
+
operate batcher.operations
|
43
|
+
end
|
44
|
+
|
45
|
+
# Gets the current set members from Riak if necessary, and return the
|
46
|
+
# stdlib `::Set` of them.
|
47
|
+
#
|
48
|
+
# @return [::Set] a Ruby standard library {::Set} of the members
|
49
|
+
# of this {Riak::Crdt::Set}
|
50
|
+
def members
|
51
|
+
reload if dirty?
|
52
|
+
@members
|
53
|
+
end
|
54
|
+
|
55
|
+
alias :value :members
|
56
|
+
|
57
|
+
# Cast this {Riak::Crdt::Set} to a Ruby {Array}.
|
58
|
+
#
|
59
|
+
# @return [Array] array of set members
|
60
|
+
def to_a
|
61
|
+
members.to_a
|
62
|
+
end
|
63
|
+
|
64
|
+
# Check to see if this structure has any members.
|
65
|
+
#
|
66
|
+
# @return [Boolean] if the structure is empty
|
67
|
+
def empty?
|
68
|
+
members.empty?
|
69
|
+
end
|
70
|
+
|
71
|
+
# Check to see if a given string is present in this data structure.
|
72
|
+
#
|
73
|
+
# @param [String] candidate string to check for inclusion in this structure
|
74
|
+
# @return [Boolean] if the structure includes
|
75
|
+
def include?(candidate)
|
76
|
+
members.include?(candidate)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Add a {String} to the {Riak::Crdt::Set}
|
80
|
+
#
|
81
|
+
# @param [String] element the element to add to the set
|
82
|
+
# @param [Hash] options
|
83
|
+
def add(element, options = {})
|
84
|
+
operate operation(:add, element), options
|
85
|
+
end
|
86
|
+
|
87
|
+
# Remove a {String} from the {Riak::Crdt::Set}
|
88
|
+
#
|
89
|
+
# @param [String] element to remove from the set
|
90
|
+
# @param [Hash] options
|
91
|
+
def remove(element, options = {})
|
92
|
+
raise CrdtError::SetRemovalWithoutContextError unless context?
|
93
|
+
operate operation(:remove, element), options
|
94
|
+
end
|
95
|
+
|
96
|
+
alias :delete :remove
|
97
|
+
|
98
|
+
def pretty_print(pp)
|
99
|
+
super pp do
|
100
|
+
pp.comma_breakable
|
101
|
+
pp.pp to_a
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
def vivify(value)
|
107
|
+
@members = value
|
108
|
+
end
|
109
|
+
|
110
|
+
def operation(direction, element)
|
111
|
+
Operation::Update.new.tap do |op|
|
112
|
+
op.type = :set
|
113
|
+
op.value = { direction => element }
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class BatchSet
|
118
|
+
def initialize(base)
|
119
|
+
@base = base
|
120
|
+
@adds = ::Set.new
|
121
|
+
@removes = ::Set.new
|
122
|
+
end
|
123
|
+
|
124
|
+
def add(element)
|
125
|
+
@adds.add element
|
126
|
+
end
|
127
|
+
|
128
|
+
def remove(element)
|
129
|
+
raise CrdtError::SetRemovalWithoutContextError.new unless context?
|
130
|
+
@removes.add element
|
131
|
+
end
|
132
|
+
|
133
|
+
alias :delete :remove
|
134
|
+
|
135
|
+
def include?(element)
|
136
|
+
members.include? element
|
137
|
+
end
|
138
|
+
|
139
|
+
def empty?
|
140
|
+
members.empty?
|
141
|
+
end
|
142
|
+
|
143
|
+
def context?
|
144
|
+
@base.context?
|
145
|
+
end
|
146
|
+
|
147
|
+
def to_a
|
148
|
+
members.to_a
|
149
|
+
end
|
150
|
+
|
151
|
+
def members
|
152
|
+
(@base + @adds).subtract @removes
|
153
|
+
end
|
154
|
+
|
155
|
+
alias :value :members
|
156
|
+
|
157
|
+
def operations
|
158
|
+
Operation::Update.new.tap do |op|
|
159
|
+
op.type = :set
|
160
|
+
op.value = {add: @adds.to_a, remove: @removes.to_a}
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
# A collection of elements of a given type inside a {Map}.
|
4
|
+
class TypedCollection
|
5
|
+
ALREADY_WRAPPED = ::Set.new [InnerCounter, InnerFlag, InnerMap]
|
6
|
+
NEEDS_NAME = ::Set.new [InnerCounter, InnerSet, InnerMap]
|
7
|
+
INITIALIZE_NIL = ::Set.new [InnerRegister]
|
8
|
+
|
9
|
+
# @api private
|
10
|
+
def initialize(type, parent, contents = {})
|
11
|
+
@type = type
|
12
|
+
@parent = parent
|
13
|
+
contents = {} if contents.nil?
|
14
|
+
stringified_contents = contents.stringify_keys
|
15
|
+
@contents = materialize_contents stringified_contents
|
16
|
+
end
|
17
|
+
|
18
|
+
def pretty_print(pp)
|
19
|
+
pp.object_group self do
|
20
|
+
pp.breakable
|
21
|
+
pp.text inspect_name
|
22
|
+
pp.comma_breakable
|
23
|
+
pp.text 'parent='
|
24
|
+
@parent.pretty_print_cycle(pp)
|
25
|
+
pp.comma_breakable
|
26
|
+
pp.text 'contents='
|
27
|
+
pp.pp @contents
|
28
|
+
end
|
29
|
+
# buf = []
|
30
|
+
# buf << inspect_name
|
31
|
+
# buf <<
|
32
|
+
# buf << "contents={#{inspect_contents}}"
|
33
|
+
# "#<#{self.class.name} #{buf.join ' '}>"
|
34
|
+
end
|
35
|
+
|
36
|
+
def pretty_print_cycle(pp)
|
37
|
+
pp.object_group self do
|
38
|
+
pp.breakable
|
39
|
+
@parent.pretty_print_cycle(pp)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def inspect_name
|
44
|
+
"contains=#{content_name}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def pretty_print_contents(_pp)
|
48
|
+
@contents.map do |k, v|
|
49
|
+
"#{k}=>#{v.inspect}"
|
50
|
+
end.join ', '
|
51
|
+
end
|
52
|
+
|
53
|
+
def content_name
|
54
|
+
@type.name
|
55
|
+
end
|
56
|
+
|
57
|
+
# @api private
|
58
|
+
def reparent(new_parent)
|
59
|
+
self.class.new(@type,
|
60
|
+
new_parent,
|
61
|
+
@contents)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Check if a value for a given key exists in this map.
|
65
|
+
#
|
66
|
+
# @param [String] key the key to check for
|
67
|
+
# @return [Boolean] if the key is inside this collection
|
68
|
+
def include?(key)
|
69
|
+
@contents.include? normalize_key(key)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Get the value for a given key
|
73
|
+
#
|
74
|
+
# @param [String] key the key to get the value for
|
75
|
+
# @return the value for the given key
|
76
|
+
def [](key)
|
77
|
+
key = normalize_key key
|
78
|
+
if include? key
|
79
|
+
candidate = @contents[key]
|
80
|
+
return candidate unless candidate.respond_to? :parent
|
81
|
+
return candidate if candidate.parent == self
|
82
|
+
end
|
83
|
+
|
84
|
+
return nil if initialize_nil?
|
85
|
+
|
86
|
+
new_instance = @type.new self
|
87
|
+
new_instance.name = key if needs_name?
|
88
|
+
|
89
|
+
new_instance
|
90
|
+
end
|
91
|
+
|
92
|
+
# Set the value for a given key. Operation of this method
|
93
|
+
# is only defined for {InnerCounter}, {InnerRegister}, and
|
94
|
+
# {InnerFlag} types.
|
95
|
+
#
|
96
|
+
# @param [String] key the key to set a new value for
|
97
|
+
# @param [Boolean, String, Integer] value the value to set at the key,
|
98
|
+
# or in the case of counters, the amount to increment
|
99
|
+
def []=(key, value)
|
100
|
+
key = normalize_key key
|
101
|
+
|
102
|
+
operation = @type.update value
|
103
|
+
operation.name = key
|
104
|
+
|
105
|
+
result = @parent.operate operation
|
106
|
+
|
107
|
+
@contents[key] = @type.new self, value
|
108
|
+
@contents[key].name = key if needs_name?
|
109
|
+
|
110
|
+
result
|
111
|
+
end
|
112
|
+
|
113
|
+
alias_method :increment, :[]=
|
114
|
+
|
115
|
+
# Remove the entry from the map.
|
116
|
+
#
|
117
|
+
# @param [String] key the key to remove from the map
|
118
|
+
def delete(key)
|
119
|
+
key = normalize_key key
|
120
|
+
operation = @type.delete
|
121
|
+
operation.name = key
|
122
|
+
|
123
|
+
@parent.operate operation
|
124
|
+
|
125
|
+
@contents.delete key
|
126
|
+
end
|
127
|
+
|
128
|
+
# @api private
|
129
|
+
def operate(key, inner_operation)
|
130
|
+
key = normalize_key key
|
131
|
+
|
132
|
+
inner_operation.name = key
|
133
|
+
|
134
|
+
@parent.operate inner_operation
|
135
|
+
end
|
136
|
+
|
137
|
+
# Does this set have the context necessary to remove elements?
|
138
|
+
#
|
139
|
+
# @return [Boolean] if the set has a defined context
|
140
|
+
def context?
|
141
|
+
!!@parent.context?
|
142
|
+
end
|
143
|
+
|
144
|
+
def to_value_h
|
145
|
+
return @contents unless NEEDS_NAME.include? @type
|
146
|
+
|
147
|
+
@contents.map do |k, v|
|
148
|
+
[k, v.value]
|
149
|
+
end.to_h
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
def normalize_key(unnormalized_key)
|
155
|
+
unnormalized_key.to_s
|
156
|
+
end
|
157
|
+
|
158
|
+
def initialize_nil?
|
159
|
+
INITIALIZE_NIL.include? @type
|
160
|
+
end
|
161
|
+
|
162
|
+
def needs_name?
|
163
|
+
NEEDS_NAME.include? @type
|
164
|
+
end
|
165
|
+
|
166
|
+
def materialize_contents(stringified_contents)
|
167
|
+
stringified_contents.keys.inject(Hash.new) do |new_contents, key|
|
168
|
+
new_contents.tap do |c|
|
169
|
+
content = stringified_contents[key]
|
170
|
+
if ALREADY_WRAPPED.include? content.class
|
171
|
+
c[key] = content
|
172
|
+
else
|
173
|
+
c[key] = @type.new self, content
|
174
|
+
end
|
175
|
+
c[key].name = key if needs_name?
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|