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
data/lib/riak/client/node.rb
CHANGED
@@ -6,8 +6,7 @@ module Riak
|
|
6
6
|
include Util::Translation
|
7
7
|
include Util::Escape
|
8
8
|
|
9
|
-
VALID_OPTIONS = [:host, :
|
10
|
-
:mapred, :luwak, :solr, :port, :basic_auth, :ssl_options, :ssl]
|
9
|
+
VALID_OPTIONS = [:host, :pb_port]
|
11
10
|
|
12
11
|
# For a score which halves in 10 seconds, choose
|
13
12
|
# ln(1/2)/10
|
@@ -15,32 +14,17 @@ module Riak
|
|
15
14
|
|
16
15
|
# What IP address or hostname does this node listen on?
|
17
16
|
attr_accessor :host
|
18
|
-
|
19
|
-
attr_accessor :http_port
|
17
|
+
|
20
18
|
# Which port does the protocol buffers interface listen on?
|
21
19
|
attr_accessor :pb_port
|
22
|
-
|
23
|
-
attr_accessor :http_paths
|
24
|
-
# A "user:password" string.
|
25
|
-
attr_reader :basic_auth
|
26
|
-
attr_accessor :ssl_options
|
20
|
+
|
27
21
|
# A Decaying rate of errors.
|
28
22
|
attr_reader :error_rate
|
29
23
|
|
30
24
|
def initialize(client, opts = {})
|
31
25
|
@client = client
|
32
|
-
@ssl = opts[:ssl]
|
33
|
-
@ssl_options = opts[:ssl_options]
|
34
26
|
@host = opts[:host] || "127.0.0.1"
|
35
|
-
@http_port = opts[:http_port] || opts[:port] || 8098
|
36
27
|
@pb_port = opts[:pb_port] || 8087
|
37
|
-
@http_paths = {
|
38
|
-
:prefix => opts[:prefix] || "/riak/",
|
39
|
-
:mapred => opts[:mapred] || "/mapred",
|
40
|
-
:luwak => opts[:luwak] || "/luwak",
|
41
|
-
:solr => opts[:solr] || "/solr" # Unused?
|
42
|
-
}.merge(opts[:http_paths] || {})
|
43
|
-
self.basic_auth = opts[:basic_auth]
|
44
28
|
|
45
29
|
@error_rate = Decaying.new
|
46
30
|
end
|
@@ -48,73 +32,18 @@ module Riak
|
|
48
32
|
def ==(o)
|
49
33
|
o.kind_of? Node and
|
50
34
|
@host == o.host and
|
51
|
-
@http_port == o.http_port and
|
52
35
|
@pb_port == o.pb_port
|
53
36
|
end
|
54
37
|
|
55
|
-
# Sets the HTTP Basic Authentication credentials.
|
56
|
-
# @param [String] value an auth string in the form "user:password"
|
57
|
-
def basic_auth=(value)
|
58
|
-
case value
|
59
|
-
when nil
|
60
|
-
@basic_auth = nil
|
61
|
-
when String
|
62
|
-
raise ArgumentError, t("invalid_basic_auth") unless value.to_s.split(':').length === 2
|
63
|
-
@basic_auth = value
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# Can this node be used for HTTP requests?
|
68
|
-
def http?
|
69
|
-
# TODO: Need to sort out capabilities
|
70
|
-
true
|
71
|
-
end
|
72
|
-
|
73
38
|
# Can this node be used for protocol buffers requests?
|
74
39
|
def protobuffs?
|
75
40
|
# TODO: Need to sort out capabilities
|
76
41
|
true
|
77
42
|
end
|
78
43
|
|
79
|
-
# Enables or disables SSL on this node to be utilized by the HTTP
|
80
|
-
# Backends
|
81
|
-
def ssl=(value)
|
82
|
-
case value
|
83
|
-
when TrueClass
|
84
|
-
@ssl_options ||= {}
|
85
|
-
when Hash
|
86
|
-
(@ssl_options ||= {}).merge!(value)
|
87
|
-
end
|
88
|
-
value ? ssl_enable : ssl_disable
|
89
|
-
end
|
90
|
-
|
91
|
-
# Checks if SSL is enabled for HTTP
|
92
|
-
def ssl_enabled?
|
93
|
-
@client.protocol == 'https' && @ssl_options.present?
|
94
|
-
end
|
95
|
-
|
96
44
|
def inspect
|
97
|
-
"#<Node #{@host}:#{@
|
98
|
-
end
|
99
|
-
|
100
|
-
protected
|
101
|
-
|
102
|
-
|
103
|
-
def ssl_enable
|
104
|
-
@client.protocol = 'https' unless @client.protocol == 'https'
|
105
|
-
@ssl_options[:pem] = File.read(@ssl_options[:pem_file]) if @ssl_options[:pem_file]
|
106
|
-
@ssl_options[:verify_mode] ||= "peer" if @ssl_options.stringify_keys.any? {|k,v| %w[pem ca_file ca_path].include?(k)}
|
107
|
-
@ssl_options[:verify_mode] ||= "none"
|
108
|
-
raise ArgumentError.new(t('invalid_ssl_verify_mode', :invalid => @ssl_options[:verify_mode])) unless %w[none peer].include?(@ssl_options[:verify_mode])
|
109
|
-
|
110
|
-
@ssl_options
|
45
|
+
"#<Node #{@host}:#{@pb_port}>"
|
111
46
|
end
|
112
|
-
|
113
|
-
def ssl_disable
|
114
|
-
@client.protocol = 'http' unless @client.protocol == 'http'
|
115
|
-
@ssl_options = nil
|
116
|
-
end
|
117
|
-
|
118
47
|
end
|
119
48
|
end
|
120
49
|
end
|
@@ -29,10 +29,6 @@ module Riak
|
|
29
29
|
@node = node
|
30
30
|
end
|
31
31
|
|
32
|
-
simple :ping, :PingReq
|
33
|
-
simple :get_client_id, :GetClientIdReq
|
34
|
-
simple :server_info, :GetServerInfoReq
|
35
|
-
|
36
32
|
# Performs a secondary-index query via emulation through MapReduce.
|
37
33
|
# @param [String, Bucket] bucket the bucket to query
|
38
34
|
# @param [String] index the index to query
|
@@ -72,6 +68,10 @@ module Riak
|
|
72
68
|
def teardown
|
73
69
|
reset_socket
|
74
70
|
end
|
71
|
+
|
72
|
+
def socket
|
73
|
+
@socket ||= new_socket
|
74
|
+
end
|
75
75
|
|
76
76
|
private
|
77
77
|
def get_server_version
|
@@ -82,17 +82,9 @@ module Riak
|
|
82
82
|
def decode_response
|
83
83
|
raise NotImplementedError
|
84
84
|
end
|
85
|
-
|
86
|
-
def socket
|
87
|
-
@socket ||= new_socket
|
88
|
-
end
|
89
|
-
|
85
|
+
|
90
86
|
def new_socket
|
91
|
-
|
92
|
-
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
|
93
|
-
#TODO: Should we set the client ID here?
|
94
|
-
# set_client_id @client.client_id
|
95
|
-
socket
|
87
|
+
raise NotImplementedError
|
96
88
|
end
|
97
89
|
|
98
90
|
def reset_socket
|
data/lib/riak/client/search.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'builder'
|
2
|
-
|
3
1
|
module Riak
|
4
2
|
class Client
|
5
3
|
# (Riak Search) Performs a search via the Solr interface.
|
@@ -25,67 +23,5 @@ module Riak
|
|
25
23
|
end
|
26
24
|
end
|
27
25
|
alias :select :search
|
28
|
-
|
29
|
-
# (Riak Search) Adds documents to a search index via the Solr interface.
|
30
|
-
# @overload index(index, *docs)
|
31
|
-
# Adds documents to the specified search index
|
32
|
-
# @param [String] index the index in which to add/update the given documents
|
33
|
-
# @param [Array<Hash>] docs unnested document hashes, with one key per field
|
34
|
-
# @overload index(*docs)
|
35
|
-
# Adds documents to the default search index
|
36
|
-
# @param [Array<Hash>] docs unnested document hashes, with one key per field
|
37
|
-
# @raise [ArgumentError] if any documents don't include 'id' key
|
38
|
-
def index(*args)
|
39
|
-
index = args.shift if String === args.first # Documents must be hashes of fields
|
40
|
-
raise ArgumentError.new(t("search_docs_require_id")) unless args.all? {|d| d.key?("id") || d.key?(:id) }
|
41
|
-
xml = Builder::XmlMarkup.new
|
42
|
-
xml.add do
|
43
|
-
args.each do |doc|
|
44
|
-
xml.doc do
|
45
|
-
doc.each do |k,v|
|
46
|
-
xml.field('name' => k.to_s) { xml.text!(v.to_s) }
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
http do |h|
|
52
|
-
h.update_search_index(index, xml.target!)
|
53
|
-
end
|
54
|
-
true
|
55
|
-
end
|
56
|
-
alias :add_doc :index
|
57
|
-
|
58
|
-
# (Riak Search) Removes documents from a search index via the Solr interface.
|
59
|
-
# @overload remove(index, specs)
|
60
|
-
# Removes documents from the specified index
|
61
|
-
# @param [String] index the index from which to remove documents
|
62
|
-
# @param [Array<Hash>] specs the specificaiton of documents to remove (must contain 'id' or 'query' keys)
|
63
|
-
# @overload remove(specs)
|
64
|
-
# Removes documents from the default index
|
65
|
-
# @param [Array<Hash>] specs the specification of documents to remove (must contain 'id' or 'query' keys)
|
66
|
-
# @raise [ArgumentError] if any document specs don't include 'id' or 'query' keys
|
67
|
-
def remove(*args)
|
68
|
-
index = args.shift if String === args.first
|
69
|
-
raise ArgumentError.new(t("search_remove_requires_id_or_query")) unless args.all? { |s|
|
70
|
-
s.include? :id or
|
71
|
-
s.include? 'id' or
|
72
|
-
s.include? :query or
|
73
|
-
s.include? 'query'
|
74
|
-
}
|
75
|
-
xml = Builder::XmlMarkup.new
|
76
|
-
xml.delete do
|
77
|
-
args.each do |spec|
|
78
|
-
spec.each do |k,v|
|
79
|
-
xml.tag!(k.to_sym, v)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
http do |h|
|
84
|
-
h.update_search_index(index, xml.target!)
|
85
|
-
end
|
86
|
-
true
|
87
|
-
end
|
88
|
-
alias :delete_doc :remove
|
89
|
-
alias :deindex :remove
|
90
26
|
end
|
91
27
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Riak
|
2
|
+
class Client
|
3
|
+
def create_search_index(name, schema=nil, n_val=nil)
|
4
|
+
raise ArgumentError, t("zero_length_index") if name.nil? || name.empty?
|
5
|
+
backend do |b|
|
6
|
+
b.create_search_index(name, schema, n_val)
|
7
|
+
end
|
8
|
+
true
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_search_index(name)
|
12
|
+
raise ArgumentError, t("zero_length_index") if name.nil? || name.empty?
|
13
|
+
resp = []
|
14
|
+
backend do |b|
|
15
|
+
resp = b.get_search_index(name)
|
16
|
+
end
|
17
|
+
resp.index && Array === resp.index ? resp.index.first : resp
|
18
|
+
end
|
19
|
+
|
20
|
+
def list_search_indexes()
|
21
|
+
resp = []
|
22
|
+
backend do |b|
|
23
|
+
resp = b.get_search_index(nil)
|
24
|
+
end
|
25
|
+
resp.index ? resp.index : resp
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete_search_index(name)
|
29
|
+
raise ArgumentError, t("zero_length_index") if name.nil? || name.empty?
|
30
|
+
backend do |b|
|
31
|
+
b.delete_search_index(name)
|
32
|
+
end
|
33
|
+
true
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_search_schema(name, content)
|
37
|
+
raise ArgumentError, t("zero_length_schema") if name.nil? || name.empty?
|
38
|
+
raise ArgumentError, t("zero_length_content") if content.nil? || content.empty?
|
39
|
+
backend do |b|
|
40
|
+
b.create_search_schema(name, content)
|
41
|
+
end
|
42
|
+
true
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_search_schema(name)
|
46
|
+
raise ArgumentError, t("zero_length_schema") if name.nil? || name.empty?
|
47
|
+
backend do |b|
|
48
|
+
return b.get_search_schema(name)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/riak/counter.rb
CHANGED
data/lib/riak/crdt.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'riak/errors/crdt_error'
|
2
|
+
|
3
|
+
%w{ operation base inner_register inner_flag counter inner_counter batch_counter map inner_map batch_map set inner_set typed_collection }.each do |f|
|
4
|
+
require "riak/crdt/#{f}"
|
5
|
+
end
|
6
|
+
|
7
|
+
module Riak
|
8
|
+
# Container module for Convergent Replicated Data Type
|
9
|
+
# features.
|
10
|
+
module Crdt
|
11
|
+
|
12
|
+
# These are the default bucket types for the three top-level data types.
|
13
|
+
# Broadly, CRDTs require allow_mult to be enabled, and the `datatype`
|
14
|
+
# property to be set to the appropriate atom (`counter`, `map`, or `set`).
|
15
|
+
DEFAULT_BUCKET_TYPES = {
|
16
|
+
counter: 'counters',
|
17
|
+
map: 'maps',
|
18
|
+
set: 'sets',
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module Riak
|
2
|
+
module Crdt
|
3
|
+
|
4
|
+
# Basic and shared code used by the top-level CRDTs. In particular, dirty-
|
5
|
+
# tracking, loading, and operating is implemented by this class, and
|
6
|
+
# the {Riak::Crdt::Set}, {Riak::Crdt::Counter}, and {Riak::Crdt::Map}
|
7
|
+
# classes implement everything else.
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
class Base
|
11
|
+
include Util::Translation
|
12
|
+
attr_reader :bucket
|
13
|
+
attr_reader :bucket_type
|
14
|
+
|
15
|
+
# Returns the key of this CRDT. Extremely useful when using a
|
16
|
+
# Riak-assigned key.
|
17
|
+
attr_reader :key
|
18
|
+
|
19
|
+
def initialize(bucket, key, bucket_type, options={})
|
20
|
+
raise ArgumentError, t("bucket_type", bucket: bucket.inspect) unless bucket.is_a? Bucket
|
21
|
+
|
22
|
+
unless key.is_a? String or key.nil?
|
23
|
+
raise ArgumentError, t("string_type", string: key.inspect)
|
24
|
+
end
|
25
|
+
|
26
|
+
@bucket = bucket
|
27
|
+
@key = key
|
28
|
+
@bucket_type = bucket_type
|
29
|
+
@options = options
|
30
|
+
|
31
|
+
@dirty = true
|
32
|
+
end
|
33
|
+
|
34
|
+
def dirty?
|
35
|
+
@dirty
|
36
|
+
end
|
37
|
+
|
38
|
+
# Force a reload of this structure from Riak.
|
39
|
+
def reload
|
40
|
+
loader do |l|
|
41
|
+
vivify l.load @bucket, @key, @bucket_type
|
42
|
+
@context = l.context
|
43
|
+
end
|
44
|
+
@dirty = false
|
45
|
+
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
# Does this CRDT have the context necessary to remove elements?
|
50
|
+
#
|
51
|
+
# @return [Boolean] if the set has a defined context
|
52
|
+
def context?
|
53
|
+
!!@context
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
def client
|
58
|
+
@bucket.client
|
59
|
+
end
|
60
|
+
|
61
|
+
def backend(&blk)
|
62
|
+
client.backend &blk
|
63
|
+
end
|
64
|
+
|
65
|
+
def loader
|
66
|
+
backend do |be|
|
67
|
+
yield be.crdt_loader
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def operator
|
72
|
+
backend do |be|
|
73
|
+
yield be.crdt_operator
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def operate(*args)
|
78
|
+
options = Hash.new
|
79
|
+
options = args.pop if args.last.is_a? Hash
|
80
|
+
options[:context] ||= @context
|
81
|
+
result = operator do |op|
|
82
|
+
response = op.operate(bucket.name,
|
83
|
+
key,
|
84
|
+
bucket_type,
|
85
|
+
*args,
|
86
|
+
options
|
87
|
+
)
|
88
|
+
|
89
|
+
break if :empty == response
|
90
|
+
@key = response.key if response.key
|
91
|
+
end
|
92
|
+
|
93
|
+
@dirty = true
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -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
|