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,19 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
class BatchCounter
|
4
|
+
attr_reader :accumulator
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@accumulator = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def increment(amount = 1)
|
11
|
+
@accumulator += amount
|
12
|
+
end
|
13
|
+
|
14
|
+
def decrement(amount = 1)
|
15
|
+
increment -amount
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
# A map that queues up its operations for the parent {Map} to send to
|
4
|
+
# Riak all at once.
|
5
|
+
class BatchMap
|
6
|
+
attr_reader :counters, :flags, :maps, :registers, :sets
|
7
|
+
|
8
|
+
# @api private
|
9
|
+
def initialize(parent)
|
10
|
+
@parent = parent
|
11
|
+
@queue = []
|
12
|
+
|
13
|
+
initialize_collections
|
14
|
+
end
|
15
|
+
|
16
|
+
# @api private
|
17
|
+
def operate(operation)
|
18
|
+
@queue << operation
|
19
|
+
end
|
20
|
+
|
21
|
+
# @api private
|
22
|
+
def operations
|
23
|
+
@queue.map do |q|
|
24
|
+
Operation::Update.new.tap do |op|
|
25
|
+
op.type = :map
|
26
|
+
op.value = q
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def initialize_collections
|
33
|
+
@counters = @parent.counters.reparent self
|
34
|
+
@flags = @parent.flags.reparent self
|
35
|
+
@maps = @parent.maps.reparent self
|
36
|
+
@registers = @parent.registers.reparent self
|
37
|
+
@sets = @parent.sets.reparent self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
|
4
|
+
# A distributed counter that supports incrementing and decrementing. This
|
5
|
+
# `Counter` uses the Riak 2 Data Types feature. If you're interested in
|
6
|
+
# Riak 1.4 Counters, see {Riak::Counter}.
|
7
|
+
class Counter < Base
|
8
|
+
|
9
|
+
# Create a counter instance. The bucket type is determined by the first of
|
10
|
+
# these sources:
|
11
|
+
#
|
12
|
+
# 1. The `bucket_type` String argument
|
13
|
+
# 2. A {BucketTyped::Bucket} as the `bucket` argument
|
14
|
+
# 3. The `Crdt::Base::DEFAULT_BUCKET_TYPES[:counter]` entry
|
15
|
+
#
|
16
|
+
# @param [Bucket] bucket the {Riak::Bucket} for this counter
|
17
|
+
# @param [String, nil] key The name of the counter. A nil key makes
|
18
|
+
# Riak assign a key.
|
19
|
+
# @param [String] bucket_type The optional bucket type for this counter.
|
20
|
+
# The default is in `Crdt::Base::DEFAULT_BUCKET_TYPES[:counter]`.
|
21
|
+
# @param [Hash] options
|
22
|
+
def initialize(bucket, key, bucket_type = nil, options = {})
|
23
|
+
super(bucket, key, bucket_type || :counter, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
# The current value of the counter; hits the server if the value has
|
27
|
+
# not been fetched or if the counter has been incremented.
|
28
|
+
def value
|
29
|
+
reload if dirty?
|
30
|
+
return @value
|
31
|
+
end
|
32
|
+
|
33
|
+
# Increment the counter.
|
34
|
+
#
|
35
|
+
# @param [Integer] amount
|
36
|
+
# @param [Hash] options
|
37
|
+
def increment(amount = 1, options = {})
|
38
|
+
operate operation(amount), options
|
39
|
+
end
|
40
|
+
|
41
|
+
# Yields a {BatchCounter} to turn multiple increments into a single
|
42
|
+
# Riak hit.
|
43
|
+
#
|
44
|
+
# @yieldparam [BatchCounter] batch_counter collects multiple increments
|
45
|
+
def batch
|
46
|
+
batcher = BatchCounter.new
|
47
|
+
|
48
|
+
yield batcher
|
49
|
+
|
50
|
+
increment batcher.accumulator
|
51
|
+
end
|
52
|
+
|
53
|
+
alias :to_i :value
|
54
|
+
|
55
|
+
# Decrement the counter.
|
56
|
+
#
|
57
|
+
# @param [Integer] amount
|
58
|
+
def decrement(amount = 1)
|
59
|
+
increment -amount
|
60
|
+
end
|
61
|
+
|
62
|
+
def pretty_print(pp)
|
63
|
+
super pp do
|
64
|
+
pp.comma_breakable
|
65
|
+
pp.text "value=#{value}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
def vivify(value)
|
71
|
+
@value = value
|
72
|
+
end
|
73
|
+
|
74
|
+
def operation(amount)
|
75
|
+
Operation::Update.new.tap do |op|
|
76
|
+
op.type = :counter
|
77
|
+
op.value = amount
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
# The {InnerCounter} lives inside a {Map}, or an {InnerMap} inside of a
|
4
|
+
# {Map}, and is accessed through a {TypedCollection}.
|
5
|
+
#
|
6
|
+
# Much like the {Riak::Crdt::Counter}, it provides an integer value that can
|
7
|
+
# be incremented and decremented.
|
8
|
+
class InnerCounter
|
9
|
+
# The name of this counter inside a map.
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
attr_accessor :name
|
13
|
+
|
14
|
+
# The value of this counter.
|
15
|
+
#
|
16
|
+
# @return [Integer] counter value
|
17
|
+
attr_reader :value
|
18
|
+
alias :to_i :value
|
19
|
+
|
20
|
+
# The parent of this counter.
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
attr_reader :parent
|
24
|
+
|
25
|
+
# @api private
|
26
|
+
def initialize(parent, value = 0)
|
27
|
+
@parent = parent
|
28
|
+
@value = value
|
29
|
+
end
|
30
|
+
|
31
|
+
# Increment the counter.
|
32
|
+
#
|
33
|
+
# @param [Integer] amount How much to increment the counter by.
|
34
|
+
def increment(amount = 1)
|
35
|
+
@parent.increment name, amount
|
36
|
+
end
|
37
|
+
|
38
|
+
# Decrement the counter. Opposite of increment.
|
39
|
+
#
|
40
|
+
# @param [Integer] amount How much to decrement from the counter.
|
41
|
+
def decrement(amount = 1)
|
42
|
+
increment -amount
|
43
|
+
end
|
44
|
+
|
45
|
+
# Perform multiple increments against this counter, and collapse
|
46
|
+
# them into a single operation.
|
47
|
+
#
|
48
|
+
# @yieldparam [BatchCounter] batch_counter actually collects the
|
49
|
+
# operations.
|
50
|
+
def batch
|
51
|
+
batcher = BatchCounter.new
|
52
|
+
|
53
|
+
yield batcher
|
54
|
+
|
55
|
+
increment batcher.accumulator
|
56
|
+
end
|
57
|
+
|
58
|
+
def pretty_print(pp)
|
59
|
+
pp.object_group self do
|
60
|
+
pp.breakable
|
61
|
+
pp.pp value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# @api private
|
66
|
+
def self.update(increment)
|
67
|
+
Operation::Update.new.tap do |op|
|
68
|
+
op.value = increment
|
69
|
+
op.type = :counter
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# @api private
|
74
|
+
def self.delete
|
75
|
+
Operation::Delete.new.tap do |op|
|
76
|
+
op.type = :counter
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
# The {InnerFlag} is a boolean member of a {Map}. Since flag operations are
|
4
|
+
# extremely simple, this object simply provides internal API methods for
|
5
|
+
# {TypedCollection} to use.
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
class InnerFlag
|
9
|
+
def self.new(parent, value = false)
|
10
|
+
ensure_boolean value
|
11
|
+
|
12
|
+
return value
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.update(value)
|
16
|
+
ensure_boolean value
|
17
|
+
|
18
|
+
Operation::Update.new.tap do |op|
|
19
|
+
op.value = value
|
20
|
+
op.type = :flag
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.delete
|
25
|
+
Operation::Delete.new.tap do |op|
|
26
|
+
op.type = :flag
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def self.ensure_boolean(value)
|
32
|
+
return if value.is_a? TrueClass
|
33
|
+
return if value.is_a? FalseClass
|
34
|
+
|
35
|
+
raise FlagError, t('crdt.flag.not_boolean')
|
36
|
+
end
|
37
|
+
|
38
|
+
class FlagError < ArgumentError
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
# A map that exists inside a {TypedCollection} inside another map.
|
4
|
+
class InnerMap
|
5
|
+
attr_reader :counters, :flags, :maps, :registers, :sets
|
6
|
+
|
7
|
+
attr_accessor :name
|
8
|
+
|
9
|
+
# The parent of this counter.
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
attr_reader :parent
|
13
|
+
|
14
|
+
# @api private
|
15
|
+
def initialize(parent, value = {})
|
16
|
+
@parent = parent
|
17
|
+
@value = value.symbolize_keys
|
18
|
+
|
19
|
+
initialize_collections
|
20
|
+
end
|
21
|
+
|
22
|
+
# @api private
|
23
|
+
def operate(inner_operation)
|
24
|
+
wrapped_operation = Operation::Update.new.tap do |op|
|
25
|
+
op.value = inner_operation
|
26
|
+
op.type = :map
|
27
|
+
end
|
28
|
+
|
29
|
+
@parent.operate(name, wrapped_operation)
|
30
|
+
end
|
31
|
+
|
32
|
+
def pretty_print(pp)
|
33
|
+
pp.object_group self do
|
34
|
+
%w{counters flags maps registers sets}.each do |h|
|
35
|
+
pp.comma_breakable
|
36
|
+
pp.text "#{h}="
|
37
|
+
pp.pp send h
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def pretty_print_cycle(pp)
|
43
|
+
pp.text "InnerMap"
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_value_h
|
47
|
+
%w{counters flags maps registers sets}.map do |k|
|
48
|
+
[k, send(k).to_value_h]
|
49
|
+
end.to_h
|
50
|
+
end
|
51
|
+
|
52
|
+
alias :value :to_value_h
|
53
|
+
|
54
|
+
# @api private
|
55
|
+
def self.delete
|
56
|
+
Operation::Delete.new.tap do |op|
|
57
|
+
op.type = :map
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def context?
|
62
|
+
@parent.context?
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
def initialize_collections
|
67
|
+
@counters = TypedCollection.new InnerCounter, self, @value[:counters]
|
68
|
+
@flags = TypedCollection.new InnerFlag, self, @value[:flags]
|
69
|
+
@maps = TypedCollection.new InnerMap, self, @value[:maps]
|
70
|
+
@registers = TypedCollection.new InnerRegister, self, @value[:registers]
|
71
|
+
@sets = TypedCollection.new InnerSet, self, @value[:sets]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
class InnerRegister < String
|
4
|
+
attr_reader :parent
|
5
|
+
|
6
|
+
def initialize(parent, *args, &block)
|
7
|
+
@parent = parent
|
8
|
+
super(*args, &block)
|
9
|
+
freeze
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.update(value)
|
13
|
+
Operation::Update.new.tap do |op|
|
14
|
+
op.value = value
|
15
|
+
op.type = :register
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.delete
|
20
|
+
Operation::Delete.new.tap do |op|
|
21
|
+
op.type = :register
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
# The {InnerSet} is similar to a {Riak::Crdt::Set}, except it is part of
|
4
|
+
# a {Map} (or an {InnerMap} inside of a {Map}). It is usually accessed
|
5
|
+
# through a {TypedCollection}.
|
6
|
+
#
|
7
|
+
# Just like a {Riak::Crdt::Set}, it's a set of {String Strings} that can
|
8
|
+
# be added to or removed from.
|
9
|
+
class InnerSet
|
10
|
+
# The name of this set inside a map.
|
11
|
+
#
|
12
|
+
# @api private
|
13
|
+
attr_accessor :name
|
14
|
+
|
15
|
+
# The {::Set} value of this {InnerSet}.
|
16
|
+
#
|
17
|
+
# @return [::Set] set value
|
18
|
+
attr_reader :value
|
19
|
+
alias :members :value
|
20
|
+
|
21
|
+
# The parent of this counter.
|
22
|
+
#
|
23
|
+
# @api private
|
24
|
+
attr_reader :parent
|
25
|
+
|
26
|
+
# @api private
|
27
|
+
def initialize(parent, value = [])
|
28
|
+
@parent = parent
|
29
|
+
frozen_value = value.to_a.tap{ |v| v.each(&:freeze) }
|
30
|
+
@value = ::Set.new frozen_value
|
31
|
+
@value.freeze
|
32
|
+
end
|
33
|
+
|
34
|
+
# Casts this {InnerSet} to an {Array}.
|
35
|
+
#
|
36
|
+
# @return [Array] an array of all the members of this set
|
37
|
+
def to_a
|
38
|
+
value.to_a
|
39
|
+
end
|
40
|
+
|
41
|
+
# Check if this {InnerSet} is empty.
|
42
|
+
#
|
43
|
+
# @return [Boolean} whether this structure is empty or not
|
44
|
+
def empty?
|
45
|
+
value.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
# Check if a given string is in this structure.
|
49
|
+
#
|
50
|
+
# @param [String] element candidate string to check for inclusion
|
51
|
+
# @return [Boolean] whether the candidate is in this set or not
|
52
|
+
def include?(element)
|
53
|
+
value.include? element
|
54
|
+
end
|
55
|
+
|
56
|
+
# Add a {String} to the {InnerSet}
|
57
|
+
#
|
58
|
+
# @param [String] element the element to add
|
59
|
+
def add(element)
|
60
|
+
@parent.operate name, update(add: element)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Remove a {String} from this set
|
64
|
+
#
|
65
|
+
# @param [String] element the element to remove
|
66
|
+
def remove(element)
|
67
|
+
raise CrdtError::SetRemovalWithoutContextError unless context?
|
68
|
+
@parent.operate name, update(remove: element)
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
# Does the map containing this set have the context necessary to remove elements?
|
73
|
+
#
|
74
|
+
# @return [Boolean] if the set has a defined context
|
75
|
+
def context?
|
76
|
+
@parent.context?
|
77
|
+
end
|
78
|
+
|
79
|
+
def pretty_print(pp)
|
80
|
+
pp.object_group self do
|
81
|
+
pp.breakable
|
82
|
+
pp.pp to_a
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# @api private
|
87
|
+
def update(changes)
|
88
|
+
Operation::Update.new.tap do |op|
|
89
|
+
op.value = changes.symbolize_keys
|
90
|
+
op.type = :set
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# @api private
|
95
|
+
def self.delete
|
96
|
+
Operation::Delete.new.tap do |op|
|
97
|
+
op.type = :set
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|