riak-client 1.4.5 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/Gemfile +0 -1
- data/{LICENSE → LICENSE.md} +0 -0
- data/README.markdown +211 -66
- data/RELEASE_NOTES.md +22 -47
- data/Rakefile +45 -0
- data/lib/riak.rb +1 -1
- data/lib/riak/bucket.rb +2 -2
- data/lib/riak/client.rb +22 -195
- data/lib/riak/client/beefcake/crdt_loader.rb +127 -0
- data/lib/riak/client/beefcake/crdt_operator.rb +222 -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 +29 -0
- data/lib/riak/client/beefcake/message_overlay.rb +61 -0
- data/lib/riak/client/beefcake/messages.rb +733 -371
- data/lib/riak/client/beefcake/object_methods.rb +1 -1
- data/lib/riak/client/beefcake/protocol.rb +105 -0
- data/lib/riak/client/beefcake/socket.rb +243 -0
- data/lib/riak/client/beefcake_protobuffs_backend.rb +262 -122
- data/lib/riak/client/node.rb +4 -75
- data/lib/riak/client/protobuffs_backend.rb +6 -14
- data/lib/riak/client/search.rb +0 -64
- data/lib/riak/client/yokozuna.rb +52 -0
- data/lib/riak/counter.rb +1 -1
- data/lib/riak/crdt.rb +21 -0
- data/lib/riak/crdt/base.rb +97 -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 +71 -0
- data/lib/riak/crdt/inner_counter.rb +74 -0
- data/lib/riak/crdt/inner_flag.rb +42 -0
- data/lib/riak/crdt/inner_map.rb +53 -0
- data/lib/riak/crdt/inner_register.rb +26 -0
- data/lib/riak/crdt/inner_set.rb +95 -0
- data/lib/riak/crdt/map.rb +88 -0
- data/lib/riak/crdt/operation.rb +19 -0
- data/lib/riak/crdt/set.rb +156 -0
- data/lib/riak/crdt/typed_collection.rb +131 -0
- data/lib/riak/errors/base.rb +9 -0
- data/lib/riak/errors/connection_error.rb +44 -0
- data/lib/riak/errors/crdt_error.rb +18 -0
- data/lib/riak/errors/failed_request.rb +56 -0
- data/lib/riak/errors/protobuffs_error.rb +11 -0
- data/lib/riak/i18n.rb +2 -0
- data/lib/riak/json.rb +1 -1
- data/lib/riak/locale/en.yml +26 -1
- data/lib/riak/locale/fr.yml +0 -1
- data/lib/riak/map_reduce.rb +1 -1
- data/lib/riak/map_reduce/results.rb +1 -1
- data/lib/riak/multiget.rb +1 -2
- data/lib/riak/rcontent.rb +8 -3
- data/lib/riak/robject.rb +2 -8
- data/lib/riak/secondary_index.rb +4 -4
- data/lib/riak/serializers.rb +1 -1
- data/lib/riak/util/escape.rb +3 -5
- data/lib/riak/version.rb +1 -1
- data/lib/riak/walk_spec.rb +7 -3
- data/riak-client.gemspec +10 -8
- data/spec/fixtures/bitcask.txt +25 -0
- data/spec/integration/riak/bucket_types_spec.rb +61 -0
- data/spec/integration/riak/counters_spec.rb +17 -32
- data/spec/integration/riak/crdt_spec.rb +181 -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/protobuffs_backends_spec.rb +9 -26
- data/spec/integration/riak/security_spec.rb +94 -0
- data/spec/integration/riak/threading_spec.rb +24 -67
- data/spec/integration/yokozuna/index_spec.rb +61 -0
- data/spec/integration/yokozuna/queries_spec.rb +116 -0
- data/spec/integration/yokozuna/schema_spec.rb +49 -0
- data/spec/riak/beefcake_protobuffs_backend/crdt_operator_spec.rb +222 -0
- data/spec/riak/beefcake_protobuffs_backend/object_methods_spec.rb +4 -4
- data/spec/riak/beefcake_protobuffs_backend/protocol_spec.rb +189 -0
- data/spec/riak/beefcake_protobuffs_backend/socket_spec.rb +151 -0
- data/spec/riak/beefcake_protobuffs_backend_spec.rb +68 -106
- data/spec/riak/bucket_spec.rb +81 -77
- data/spec/riak/client_spec.rb +43 -340
- data/spec/riak/core_ext/to_param_spec.rb +2 -2
- data/spec/riak/counter_spec.rb +20 -20
- data/spec/riak/crdt/counter_spec.rb +52 -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 +77 -0
- data/spec/riak/crdt/set_spec.rb +58 -0
- data/spec/riak/crdt/shared_examples.rb +74 -0
- data/spec/riak/crdt/typed_collection_spec.rb +231 -0
- data/spec/riak/escape_spec.rb +33 -37
- data/spec/riak/feature_detection_spec.rb +45 -45
- data/spec/riak/index_collection_spec.rb +12 -12
- data/spec/riak/link_spec.rb +34 -34
- data/spec/riak/list_buckets_spec.rb +7 -7
- data/spec/riak/map_reduce/filter_builder_spec.rb +6 -6
- data/spec/riak/map_reduce/phase_spec.rb +35 -35
- data/spec/riak/map_reduce_spec.rb +89 -87
- data/spec/riak/multiget_spec.rb +20 -15
- data/spec/riak/node_spec.rb +5 -152
- data/spec/riak/robject_spec.rb +95 -108
- data/spec/riak/search_spec.rb +17 -139
- data/spec/riak/secondary_index_spec.rb +49 -49
- data/spec/riak/serializers_spec.rb +9 -9
- data/spec/riak/stamp_spec.rb +9 -9
- data/spec/riak/walk_spec_spec.rb +46 -46
- data/spec/spec_helper.rb +14 -22
- data/spec/support/certs/README.md +13 -0
- data/spec/support/certs/ca.crt +22 -0
- data/spec/support/certs/client.crt +95 -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 +95 -0
- data/spec/support/certs/server.key +27 -0
- data/spec/support/integration_setup.rb +1 -1
- data/spec/support/search_corpus_setup.rb +29 -8
- data/spec/support/test_client.rb +46 -0
- data/spec/support/test_client.yml.example +10 -0
- data/spec/support/unified_backend_examples.rb +104 -83
- data/spec/support/version_filter.rb +2 -2
- data/spec/support/wait_until.rb +14 -0
- metadata +134 -132
- data/erl_src/riak_kv_test014_backend.beam +0 -0
- data/erl_src/riak_kv_test014_backend.erl +0 -189
- data/erl_src/riak_kv_test_backend.beam +0 -0
- data/erl_src/riak_kv_test_backend.erl +0 -731
- data/erl_src/riak_search_test_backend.beam +0 -0
- data/erl_src/riak_search_test_backend.erl +0 -175
- data/lib/riak/client/excon_backend.rb +0 -172
- data/lib/riak/client/http_backend.rb +0 -413
- data/lib/riak/client/http_backend/bucket_streamer.rb +0 -15
- data/lib/riak/client/http_backend/chunked_json_streamer.rb +0 -42
- data/lib/riak/client/http_backend/configuration.rb +0 -227
- data/lib/riak/client/http_backend/key_streamer.rb +0 -15
- data/lib/riak/client/http_backend/object_methods.rb +0 -114
- data/lib/riak/client/http_backend/request_headers.rb +0 -34
- data/lib/riak/client/http_backend/transport_methods.rb +0 -201
- data/lib/riak/client/instrumentation.rb +0 -25
- data/lib/riak/client/net_http_backend.rb +0 -82
- data/lib/riak/cluster.rb +0 -151
- data/lib/riak/failed_request.rb +0 -81
- data/lib/riak/instrumentation.rb +0 -6
- data/lib/riak/node.rb +0 -40
- data/lib/riak/node/configuration.rb +0 -304
- data/lib/riak/node/console.rb +0 -133
- data/lib/riak/node/control.rb +0 -207
- data/lib/riak/node/defaults.rb +0 -85
- data/lib/riak/node/generation.rb +0 -127
- data/lib/riak/node/log.rb +0 -34
- data/lib/riak/node/version.rb +0 -29
- data/lib/riak/search.rb +0 -3
- data/lib/riak/test_server.rb +0 -89
- data/lib/riak/util/headers.rb +0 -32
- data/lib/riak/util/multipart.rb +0 -52
- data/lib/riak/util/multipart/stream_parser.rb +0 -62
- data/spec/fixtures/munchausen.txt +0 -1033
- data/spec/integration/riak/cluster_spec.rb +0 -88
- data/spec/integration/riak/http_backends_spec.rb +0 -180
- data/spec/integration/riak/node_spec.rb +0 -170
- data/spec/integration/riak/test_server_spec.rb +0 -57
- data/spec/riak/excon_backend_spec.rb +0 -102
- data/spec/riak/headers_spec.rb +0 -21
- data/spec/riak/http_backend/configuration_spec.rb +0 -273
- data/spec/riak/http_backend/object_methods_spec.rb +0 -243
- data/spec/riak/http_backend/transport_methods_spec.rb +0 -97
- data/spec/riak/http_backend_spec.rb +0 -367
- data/spec/riak/instrumentation_spec.rb +0 -167
- data/spec/riak/multipart_spec.rb +0 -23
- data/spec/riak/net_http_backend_spec.rb +0 -15
- data/spec/riak/stream_parser_spec.rb +0 -53
- data/spec/support/drb_mock_server.rb +0 -39
- data/spec/support/http_backend_implementation_examples.rb +0 -253
- data/spec/support/mock_server.rb +0 -81
- data/spec/support/mocks.rb +0 -4
- data/spec/support/riak_test.rb +0 -77
- data/spec/support/sometimes.rb +0 -46
- data/spec/support/test_server.rb +0 -61
- data/spec/support/test_server.yml.example +0 -14
@@ -0,0 +1,71 @@
|
|
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. If not provided, the default bucket type
|
10
|
+
# from {Riak::Crdt} will be used.
|
11
|
+
#
|
12
|
+
# @param [Bucket] bucket the {Riak::Bucket} for this counter
|
13
|
+
# @param [String, nil] key The name of the counter. A nil key makes
|
14
|
+
# Riak assign a key.
|
15
|
+
# @param [String] bucket_type The optional bucket type for this counter.
|
16
|
+
# The default is in `Crdt::Base::DEFAULT_BUCKET_TYPES[:counter]`.
|
17
|
+
# @param [Hash] options
|
18
|
+
def initialize(bucket, key, bucket_type=nil, options={})
|
19
|
+
super(bucket, key, bucket_type || DEFAULT_BUCKET_TYPES[:counter], options)
|
20
|
+
end
|
21
|
+
|
22
|
+
# The current value of the counter; hits the server if the value has
|
23
|
+
# not been fetched or if the counter has been incremented.
|
24
|
+
def value
|
25
|
+
reload if dirty?
|
26
|
+
return @value
|
27
|
+
end
|
28
|
+
|
29
|
+
# Increment the counter.
|
30
|
+
#
|
31
|
+
# @param [Integer] amount
|
32
|
+
# @param [Hash] options
|
33
|
+
def increment(amount=1, options={})
|
34
|
+
operate operation(amount), options
|
35
|
+
end
|
36
|
+
|
37
|
+
# Yields a {BatchCounter} to turn multiple increments into a single
|
38
|
+
# Riak hit.
|
39
|
+
#
|
40
|
+
# @yieldparam [BatchCounter] batch_counter collects multiple increments
|
41
|
+
def batch
|
42
|
+
batcher = BatchCounter.new
|
43
|
+
|
44
|
+
yield batcher
|
45
|
+
|
46
|
+
increment batcher.accumulator
|
47
|
+
end
|
48
|
+
|
49
|
+
alias :to_i :value
|
50
|
+
|
51
|
+
# Decrement the counter.
|
52
|
+
#
|
53
|
+
# @param [Integer] amount
|
54
|
+
def decrement(amount=1)
|
55
|
+
increment -amount
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def vivify(value)
|
60
|
+
@value = value
|
61
|
+
end
|
62
|
+
|
63
|
+
def operation(amount)
|
64
|
+
Operation::Update.new.tap do |op|
|
65
|
+
op.type = :counter
|
66
|
+
op.value = amount
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,74 @@
|
|
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
|
+
# @api private
|
59
|
+
def self.update(increment)
|
60
|
+
Operation::Update.new.tap do |op|
|
61
|
+
op.value = increment
|
62
|
+
op.type = :counter
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# @api private
|
67
|
+
def self.delete
|
68
|
+
Operation::Delete.new.tap do |op|
|
69
|
+
op.type = :counter
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
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)
|
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,53 @@
|
|
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
|
+
# @api private
|
33
|
+
def self.delete
|
34
|
+
Operation::Delete.new.tap do |op|
|
35
|
+
op.type = :map
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def context?
|
40
|
+
@parent.context?
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def initialize_collections
|
45
|
+
@counters = TypedCollection.new InnerCounter, self, @value[:counters]
|
46
|
+
@flags = TypedCollection.new InnerFlag, self, @value[:flags]
|
47
|
+
@maps = TypedCollection.new InnerMap, self, @value[:maps]
|
48
|
+
@registers = TypedCollection.new InnerRegister, self, @value[:registers]
|
49
|
+
@sets = TypedCollection.new InnerSet, self, @value[:sets]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
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,95 @@
|
|
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
|
+
# @api private
|
80
|
+
def update(changes)
|
81
|
+
Operation::Update.new.tap do |op|
|
82
|
+
op.value = changes.symbolize_keys
|
83
|
+
op.type = :set
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# @api private
|
88
|
+
def self.delete
|
89
|
+
Operation::Delete.new.tap do |op|
|
90
|
+
op.type = :set
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,88 @@
|
|
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. If not provided, the default bucket type
|
24
|
+
# from {Riak::Crdt} will be used.
|
25
|
+
#
|
26
|
+
# @param bucket [Bucket] the {Riak::Bucket} for this map
|
27
|
+
# @param [String, nil] key The name of the map. A nil key makes
|
28
|
+
# Riak assign a key.
|
29
|
+
# @param [String] bucket_type The optional bucket type for this map.
|
30
|
+
# The default is in `Crdt::Base::DEFAULT_BUCKET_TYPES[:map]`.
|
31
|
+
# @param options [Hash]
|
32
|
+
def initialize(bucket, key, bucket_type=nil, options={})
|
33
|
+
super(bucket, key, bucket_type || DEFAULT_BUCKET_TYPES[:map], options)
|
34
|
+
|
35
|
+
initialize_collections
|
36
|
+
end
|
37
|
+
|
38
|
+
# Maps are frequently updated in batches. Use this method to get a
|
39
|
+
# {BatchMap} to turn multiple operations into a single Riak update
|
40
|
+
# request.
|
41
|
+
#
|
42
|
+
# @yieldparam batch_map [BatchMap] collects updates and other operations
|
43
|
+
def batch(*args)
|
44
|
+
batch_map = BatchMap.new self
|
45
|
+
|
46
|
+
yield batch_map
|
47
|
+
|
48
|
+
write_operations batch_map.operations, *args
|
49
|
+
end
|
50
|
+
|
51
|
+
# This method *for internal use only* is used to collect oprations from
|
52
|
+
# disparate sources to provide a user-friendly API.
|
53
|
+
#
|
54
|
+
# @api private
|
55
|
+
def operate(operation, *args)
|
56
|
+
batch *args do |m|
|
57
|
+
m.operate operation
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def vivify(data)
|
63
|
+
@counters = TypedCollection.new InnerCounter, self, data[:counters]
|
64
|
+
@flags = TypedCollection.new InnerFlag, self, data[:flags]
|
65
|
+
@maps = TypedCollection.new InnerMap, self, data[:maps]
|
66
|
+
@registers = TypedCollection.new InnerRegister, self, data[:registers]
|
67
|
+
@sets = TypedCollection.new InnerSet, self, data[:sets]
|
68
|
+
end
|
69
|
+
|
70
|
+
def initialize_collections(data={})
|
71
|
+
reload if dirty?
|
72
|
+
end
|
73
|
+
|
74
|
+
def write_operations(operations, *args)
|
75
|
+
operator do |op|
|
76
|
+
op.operate(bucket.name,
|
77
|
+
key,
|
78
|
+
bucket_type,
|
79
|
+
operations,
|
80
|
+
*args
|
81
|
+
)
|
82
|
+
end
|
83
|
+
# collections break dirty tracking
|
84
|
+
reload
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|