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
data/lib/riak.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'riak/core_ext'
|
2
|
+
require 'riak/client'
|
3
|
+
require 'riak/map_reduce'
|
4
|
+
require 'riak/util/translation'
|
5
|
+
require 'riak/crdt'
|
6
|
+
require 'riak/instrumentation'
|
7
|
+
|
8
|
+
# The Riak module contains all aspects of the client interface to
|
9
|
+
# Riak.
|
10
|
+
module Riak
|
11
|
+
# Utility classes and mixins
|
12
|
+
module Util; end
|
13
|
+
extend Util::Translation
|
14
|
+
|
15
|
+
class << self
|
16
|
+
# Only change this if you really know what you're doing. Better to
|
17
|
+
# err on the side of caution and assume you don't.
|
18
|
+
# @private
|
19
|
+
attr_accessor :disable_list_keys_warnings
|
20
|
+
end
|
21
|
+
self.disable_list_keys_warnings = false
|
22
|
+
end
|
data/lib/riak/bucket.rb
ADDED
@@ -0,0 +1,297 @@
|
|
1
|
+
require 'riak/util/translation'
|
2
|
+
require 'riak/client'
|
3
|
+
require 'riak/robject'
|
4
|
+
require 'riak/counter'
|
5
|
+
require 'riak/errors/failed_request'
|
6
|
+
|
7
|
+
module Riak
|
8
|
+
# Represents and encapsulates operations on a Riak bucket. You may retrieve a bucket
|
9
|
+
# using {Client#bucket}, or create it manually and retrieve its meta-information later.
|
10
|
+
class Bucket
|
11
|
+
include Util::Translation
|
12
|
+
|
13
|
+
# (Riak Search) The precommit specification for kv/search integration
|
14
|
+
SEARCH_PRECOMMIT_HOOK = {"mod" => "riak_search_kv_hook", "fun" => "precommit"}
|
15
|
+
|
16
|
+
# @return [Riak::Client] the associated client
|
17
|
+
attr_reader :client
|
18
|
+
|
19
|
+
# @return [String] the bucket name
|
20
|
+
attr_reader :name
|
21
|
+
|
22
|
+
# Create a Riak bucket manually.
|
23
|
+
# @param [Client] client the {Riak::Client} for this bucket
|
24
|
+
# @param [String] name the name of the bucket
|
25
|
+
def initialize(client, name)
|
26
|
+
raise ArgumentError, t("client_type", :client => client.inspect) unless Client === client
|
27
|
+
raise ArgumentError, t("string_type", :string => name.inspect) unless String === name
|
28
|
+
raise ArgumentError, t('zero_length_bucket') if name == ''
|
29
|
+
@client, @name = client, name
|
30
|
+
end
|
31
|
+
|
32
|
+
# Retrieves a list of keys in this bucket.
|
33
|
+
# If a block is given, keys will be streamed through
|
34
|
+
# the block (useful for large buckets). When streaming,
|
35
|
+
# results of the operation will not be returned to the caller.
|
36
|
+
# @yield [Array<String>] a list of keys from the current chunk
|
37
|
+
# @return [Array<String>] Keys in this bucket
|
38
|
+
# @note This operation has serious performance implications and
|
39
|
+
# should not be used in production applications.
|
40
|
+
def keys(options = {}, &block)
|
41
|
+
warn(t('list_keys', :backtrace => caller.join("\n "))) unless Riak.disable_list_keys_warnings
|
42
|
+
if block_given?
|
43
|
+
@client.list_keys(self, options, &block)
|
44
|
+
else
|
45
|
+
@client.list_keys(self, options)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Sets internal properties on the bucket
|
50
|
+
# Note: this results in a request to the Riak server!
|
51
|
+
# @param [Hash] properties new properties for the bucket
|
52
|
+
# @option properties [Fixnum] :n_val (3) The N value (replication factor)
|
53
|
+
# @option properties [true,false] :allow_mult (false) Whether to permit object siblings
|
54
|
+
# @option properties [true,false] :last_write_wins (false) Whether to ignore
|
55
|
+
# causal context in regular key-value buckets
|
56
|
+
# @option properties [Array<Hash>] :precommit ([]) precommit hooks
|
57
|
+
# @option properties [Array<Hash>] :postcommit ([])postcommit hooks
|
58
|
+
# @option properties [Fixnum,String] :r ("quorum") read quorum (numeric or
|
59
|
+
# symbolic)
|
60
|
+
# @option properties [Fixnum,String] :w ("quorum") write quorum (numeric or
|
61
|
+
# symbolic)
|
62
|
+
# @option properties [Fixnum,String] :dw ("quorum") durable write quorum
|
63
|
+
# (numeric or symbolic)
|
64
|
+
# @option properties [Fixnum,String] :rw ("quorum") delete quorum (numeric or
|
65
|
+
# symbolic)
|
66
|
+
# @return [Hash] the merged bucket properties
|
67
|
+
# @raise [FailedRequest] if the new properties were not accepted by the Riakserver
|
68
|
+
# @see #n_value, #allow_mult, #r, #w, #dw, #rw
|
69
|
+
def props=(properties)
|
70
|
+
raise ArgumentError, t("hash_type", :hash => properties.inspect) unless Hash === properties
|
71
|
+
props.merge!(properties)
|
72
|
+
@client.set_bucket_props(self, properties)
|
73
|
+
props
|
74
|
+
end
|
75
|
+
alias :'properties=' :'props='
|
76
|
+
|
77
|
+
# @return [Hash] Internal Riak bucket properties.
|
78
|
+
# @see #props=
|
79
|
+
def props
|
80
|
+
@props ||= @client.get_bucket_props(self)
|
81
|
+
end
|
82
|
+
alias :properties :props
|
83
|
+
|
84
|
+
# Clears bucket properties, reverting them to the defaults.
|
85
|
+
# @return [true, false] whether the properties were cleared
|
86
|
+
# @since Riak 1.3
|
87
|
+
def clear_props
|
88
|
+
@props = nil
|
89
|
+
@client.clear_bucket_props(self)
|
90
|
+
end
|
91
|
+
alias :clear_properties :clear_props
|
92
|
+
|
93
|
+
# Retrieve an object from within the bucket.
|
94
|
+
# @param [String] key the key of the object to retrieve
|
95
|
+
# @param [Hash] options query parameters for the request
|
96
|
+
# @option options [Fixnum] :r - the read quorum for the request - how many
|
97
|
+
# nodes should concur on the read
|
98
|
+
# @return [Riak::RObject] the object
|
99
|
+
# @raise [FailedRequest] if the object is not found or some other error
|
100
|
+
# occurs
|
101
|
+
def get(key, options = {})
|
102
|
+
@client.get_object(self, key, options)
|
103
|
+
end
|
104
|
+
alias :[] :get
|
105
|
+
|
106
|
+
# Retrieve multiple keys from this bucket.
|
107
|
+
# @param [Array<String>] keys array of keys to fetch
|
108
|
+
# @return [Hash<String, Riak::RObject>] hash of keys to objects
|
109
|
+
def get_many(keys)
|
110
|
+
pairs = keys.map{|k| [self, k]}
|
111
|
+
results = Multiget.get_all @client, pairs
|
112
|
+
results.keys.inject(Hash.new) do |mem, var|
|
113
|
+
mem[var[1]] = results[var]
|
114
|
+
mem
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Create a new blank object
|
119
|
+
# @param [String] key the key of the new object
|
120
|
+
# @return [RObject] the new, unsaved object
|
121
|
+
def new(key = nil)
|
122
|
+
RObject.new(self, key).tap do |obj|
|
123
|
+
obj.content_type = "application/json"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Fetches an object if it exists, otherwise creates a new one with the
|
128
|
+
# given key
|
129
|
+
# @param [String] key the key to fetch or create
|
130
|
+
# @return [RObject] the new or existing object
|
131
|
+
def get_or_new(key, options = {})
|
132
|
+
begin
|
133
|
+
get(key, options)
|
134
|
+
rescue Riak::FailedRequest => fr
|
135
|
+
if fr.not_found?
|
136
|
+
new(key)
|
137
|
+
else
|
138
|
+
raise fr
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
# Gets a counter object. Counters initially hvae a value of zero, and can be
|
144
|
+
# incremented and decremented by integer values.
|
145
|
+
# @param [String] key the key of the counter to fetch
|
146
|
+
# @return [Counter]
|
147
|
+
def counter(key)
|
148
|
+
Riak::Counter.new self, key
|
149
|
+
end
|
150
|
+
|
151
|
+
# Checks whether an object exists in Riak.
|
152
|
+
# @param [String] key the key to check
|
153
|
+
# @param [Hash] options quorum options
|
154
|
+
# @option options [Fixnum] :r - the read quorum value for the request (R)
|
155
|
+
# @return [true, false] whether the key exists in this bucket
|
156
|
+
def exists?(key, options = {})
|
157
|
+
begin
|
158
|
+
get(key, options.merge({ :head => true }))
|
159
|
+
true
|
160
|
+
rescue Riak::FailedRequest => e
|
161
|
+
raise e unless e.not_found?
|
162
|
+
false
|
163
|
+
end
|
164
|
+
end
|
165
|
+
alias :exist? :exists?
|
166
|
+
|
167
|
+
# Deletes a key from the bucket
|
168
|
+
# @param [String] key the key to delete
|
169
|
+
# @param [Hash] options quorum options
|
170
|
+
# @option options [Fixnum] :rw - the read/write quorum for the
|
171
|
+
# delete
|
172
|
+
# @option options [String] :vclock - the causal context/vector clock of the
|
173
|
+
# object being deleted
|
174
|
+
def delete(key, options = {})
|
175
|
+
client.delete_object(self, key, options)
|
176
|
+
end
|
177
|
+
|
178
|
+
# Queries a secondary index on the bucket.
|
179
|
+
# @note This will only work if your Riak installation supports 2I.
|
180
|
+
# @param [String] index the name of the index
|
181
|
+
# @param [String,Integer,Range] query the value of the index, or a
|
182
|
+
# Range of values to query
|
183
|
+
# @return [Array<String>] a list of keys that match the index
|
184
|
+
# query
|
185
|
+
def get_index(index, query, options = {})
|
186
|
+
client.get_index(self, index, query, options)
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
# Retrieves a preflist for the given key; useful for
|
191
|
+
# figuring out where in the cluster an object is stored.
|
192
|
+
# @param [String] key the key
|
193
|
+
# @return [Array<PreflistItem>] an array of preflist entries
|
194
|
+
def get_preflist(key, options = { })
|
195
|
+
type = self.type.name if needs_type?
|
196
|
+
client.get_preflist self, key, type, options
|
197
|
+
end
|
198
|
+
|
199
|
+
# @return [true, false] whether the bucket allows divergent siblings
|
200
|
+
def allow_mult
|
201
|
+
props['allow_mult']
|
202
|
+
end
|
203
|
+
|
204
|
+
# Set the allow_mult property. *NOTE* This will result in a PUT request to
|
205
|
+
# Riak.
|
206
|
+
# @param [true, false] value whether the bucket should allow siblings
|
207
|
+
def allow_mult=(value)
|
208
|
+
self.props = {'allow_mult' => value}
|
209
|
+
value
|
210
|
+
end
|
211
|
+
|
212
|
+
# @return [Fixnum] the N value, or number of replicas for this bucket
|
213
|
+
def n_value
|
214
|
+
props['n_val']
|
215
|
+
end
|
216
|
+
alias :n_val :n_value
|
217
|
+
|
218
|
+
# Set the N value (number of replicas). *NOTE* This will result in a PUT
|
219
|
+
# request to Riak. Setting this value after the bucket has objects stored in
|
220
|
+
# it may have unpredictable results.
|
221
|
+
# @param [Fixnum] value the number of replicas the bucket should keep of
|
222
|
+
# each object
|
223
|
+
def n_value=(value)
|
224
|
+
self.props = {'n_val' => value}
|
225
|
+
value
|
226
|
+
end
|
227
|
+
alias :'n_val=' :'n_value='
|
228
|
+
|
229
|
+
%w(r w dw rw).each do |q|
|
230
|
+
define_method(q) { props[q] }
|
231
|
+
define_method("#{q}=") { |value|
|
232
|
+
self.props = { q => value }
|
233
|
+
value
|
234
|
+
}
|
235
|
+
end
|
236
|
+
|
237
|
+
# (Riak Search) Installs a precommit hook that automatically indexes objects
|
238
|
+
# into riak_search.
|
239
|
+
def enable_index!
|
240
|
+
unless is_indexed?
|
241
|
+
self.props = {
|
242
|
+
"precommit" => (props['precommit'] + [SEARCH_PRECOMMIT_HOOK]),
|
243
|
+
"search" => true
|
244
|
+
}
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
# (Riak Search) Removes the precommit hook that automatically indexes
|
249
|
+
# objects into riak_search.
|
250
|
+
def disable_index!
|
251
|
+
if is_indexed?
|
252
|
+
self.props = {
|
253
|
+
"precommit" => (props['precommit'] - [SEARCH_PRECOMMIT_HOOK]),
|
254
|
+
"search" => false
|
255
|
+
}
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
# (Riak Search) Detects whether the bucket is automatically indexed into
|
260
|
+
# riak_search.
|
261
|
+
# @return [true,false] whether the bucket includes the search indexing hook
|
262
|
+
def is_indexed?
|
263
|
+
return true if props['search'] == true
|
264
|
+
return true if props.has_key?('precommit') &&
|
265
|
+
props['precommit'].include?(SEARCH_PRECOMMIT_HOOK)
|
266
|
+
false
|
267
|
+
end
|
268
|
+
|
269
|
+
# @return [String] a representation suitable for IRB and debugging output
|
270
|
+
def inspect
|
271
|
+
"#<Riak::Bucket {#{name}}>"
|
272
|
+
end
|
273
|
+
|
274
|
+
# Pretty prints the bucket for `pp` or `pry`.
|
275
|
+
def pretty_print(pp)
|
276
|
+
pp.object_group self do
|
277
|
+
pp.breakable
|
278
|
+
pp.text "name=#{name}"
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
# Does this {Bucket} have a non-default bucket type? {BucketTyped::Bucket}
|
283
|
+
# instances with non-default types return `true`.
|
284
|
+
# @return [Boolean] false
|
285
|
+
def needs_type?
|
286
|
+
false
|
287
|
+
end
|
288
|
+
|
289
|
+
# @return [true,false] whether the other is equivalent
|
290
|
+
def ==(other)
|
291
|
+
return false unless self.class == other.class
|
292
|
+
return false unless self.client == other.client
|
293
|
+
return false unless self.name == other.name
|
294
|
+
true
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'riak'
|
2
|
+
|
3
|
+
module Riak
|
4
|
+
# Provides a predictable and useful interface to bucket properties. Allows
|
5
|
+
# reading, reloading, and setting new values for bucket properties.
|
6
|
+
class BucketProperties
|
7
|
+
attr_reader :client
|
8
|
+
attr_reader :bucket
|
9
|
+
|
10
|
+
# Create a properties object for a bucket (including bucket-typed buckets).
|
11
|
+
# @param [Riak::Bucket, Riak::BucketTyped::Bucket] bucket
|
12
|
+
def initialize(bucket)
|
13
|
+
@bucket = bucket
|
14
|
+
@client = bucket.client
|
15
|
+
end
|
16
|
+
|
17
|
+
# Clobber the cached properties, and reload them from Riak.
|
18
|
+
def reload
|
19
|
+
@cached_props = nil
|
20
|
+
cached_props
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
# Write bucket properties and invalidate the cache in this object.
|
25
|
+
def store
|
26
|
+
client.backend do |be|
|
27
|
+
be.bucket_properties_operator.put bucket, cached_props
|
28
|
+
end
|
29
|
+
@cached_props = nil
|
30
|
+
return true
|
31
|
+
end
|
32
|
+
|
33
|
+
# Take bucket properties from a given {Hash} or {Riak::BucketProperties}
|
34
|
+
# object.
|
35
|
+
# @param [Hash<String, Object>, Riak::BucketProperties] other
|
36
|
+
def merge!(other)
|
37
|
+
cached_props.merge! other
|
38
|
+
end
|
39
|
+
|
40
|
+
# Convert the cached properties into a hash for merging.
|
41
|
+
# @return [Hash<String, Object>] the bucket properties in a {Hash}
|
42
|
+
def to_hash
|
43
|
+
cached_props
|
44
|
+
end
|
45
|
+
|
46
|
+
# Read a bucket property
|
47
|
+
# @param [String] property_name
|
48
|
+
# @return [Object] the bucket property's value
|
49
|
+
def [](property_name)
|
50
|
+
cached_props[property_name.to_s]
|
51
|
+
end
|
52
|
+
|
53
|
+
# Write a bucket property
|
54
|
+
# @param [String] property_name
|
55
|
+
# @param [Object] value
|
56
|
+
def []=(property_name, value)
|
57
|
+
value = unwrap_index(value) if property_name == 'search_index'
|
58
|
+
cached_props[property_name.to_s] = value
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def cached_props
|
63
|
+
@cached_props ||= client.backend do |be|
|
64
|
+
be.bucket_properties_operator.get bucket
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def unwrap_index(value)
|
69
|
+
return value.name if value.is_a? Riak::Search::Index
|
70
|
+
|
71
|
+
value
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'riak/bucket_typed/bucket'
|
2
|
+
require 'riak/errors/crdt_error'
|
3
|
+
|
4
|
+
module Riak
|
5
|
+
# A representation of a bucket type
|
6
|
+
class BucketType
|
7
|
+
attr_reader :client, :name
|
8
|
+
|
9
|
+
# The name of Riak's default bucket type.
|
10
|
+
DEFAULT_NAME = 'default'
|
11
|
+
|
12
|
+
# Create a bucket type object manually.
|
13
|
+
# @param [Client] client the {Riak::Client} for this bucket type
|
14
|
+
# @param [String] name the name of this bucket type
|
15
|
+
def initialize(client, name)
|
16
|
+
@client, @name = client, name
|
17
|
+
end
|
18
|
+
|
19
|
+
# Is this bucket type the default?
|
20
|
+
# @return [Boolean]
|
21
|
+
def default?
|
22
|
+
name == DEFAULT_NAME
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get a bucket of this type
|
26
|
+
# @param [String] bucket_name the name of this bucket
|
27
|
+
def bucket(bucket_name)
|
28
|
+
BucketTyped::Bucket.new client, bucket_name, self
|
29
|
+
end
|
30
|
+
|
31
|
+
# Pretty prints the bucket for `pp` or `pry`.
|
32
|
+
def pretty_print(pp)
|
33
|
+
pp.object_group self do
|
34
|
+
pp.breakable
|
35
|
+
pp.text "name=#{name}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Get the properties of this bucket type
|
40
|
+
# @return [Hash<Symbol,Object>]
|
41
|
+
def properties
|
42
|
+
@properties ||= client.backend do |be|
|
43
|
+
be.get_bucket_type_props name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
alias :props :properties
|
47
|
+
|
48
|
+
# Return the data type used for handling the CRDT stored in this bucket
|
49
|
+
# type.
|
50
|
+
# Returns `nil` for a non-CRDT bucket type.
|
51
|
+
# @raise [Riak::CrdtError::UnrecognizedDataType] if the bucket type has an
|
52
|
+
# unknown datatype
|
53
|
+
# @return [Class<Riak::Crdt::Base>] CRDT subclass to use with this bucket
|
54
|
+
# type
|
55
|
+
def data_type_class
|
56
|
+
return nil unless dt = properties[:datatype]
|
57
|
+
parent = Riak::Crdt
|
58
|
+
case dt
|
59
|
+
when 'counter'
|
60
|
+
parent::Counter
|
61
|
+
when 'map'
|
62
|
+
parent::Map
|
63
|
+
when 'set'
|
64
|
+
parent::Set
|
65
|
+
else
|
66
|
+
raise CrdtError::UnrecognizedDataType.new dt
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def ==(other)
|
71
|
+
return false unless self.class == other.class
|
72
|
+
return false unless self.client == other.client
|
73
|
+
return false unless self.name == other.name
|
74
|
+
true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|