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/robject.rb
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'riak/rcontent'
|
3
|
+
require 'riak/conflict'
|
4
|
+
require 'riak/util/translation'
|
5
|
+
require 'riak/util/escape'
|
6
|
+
require 'riak/bucket'
|
7
|
+
require 'riak/link'
|
8
|
+
require 'riak/walk_spec'
|
9
|
+
|
10
|
+
module Riak
|
11
|
+
# Represents the data and metadata stored in a bucket/key pair in
|
12
|
+
# the Riak database, the base unit of data manipulation.
|
13
|
+
class RObject
|
14
|
+
include Util::Translation
|
15
|
+
extend Util::Translation
|
16
|
+
include Util::Escape
|
17
|
+
extend Util::Escape
|
18
|
+
extend Forwardable
|
19
|
+
|
20
|
+
# @return [Bucket] the bucket in which this object is contained
|
21
|
+
attr_accessor :bucket
|
22
|
+
|
23
|
+
# @return [String] the key of this object within its bucket
|
24
|
+
attr_accessor :key
|
25
|
+
|
26
|
+
# @return [String] the Riak causal context/vector clock for the object
|
27
|
+
attr_accessor :vclock
|
28
|
+
|
29
|
+
alias :causal_context :vclock
|
30
|
+
alias :causal_context= :vclock=
|
31
|
+
alias :vector_clock :vclock
|
32
|
+
alias :vector_clock= :vclock=
|
33
|
+
|
34
|
+
# @return [Boolean] whether to attempt to prevent stale writes using
|
35
|
+
# conditional PUT semantics, If-None-Match: * or If-Match: etag
|
36
|
+
# @see http://wiki.basho.com/display/RIAK/REST+API#RESTAPI-Storeaneworexistingobjectwithakey Riak Rest API Docs
|
37
|
+
attr_accessor :prevent_stale_writes
|
38
|
+
|
39
|
+
# Defines a callback to be invoked when there is conflict.
|
40
|
+
#
|
41
|
+
# @yield The conflict callback.
|
42
|
+
# @yieldparam [RObject] robject The conflicted RObject
|
43
|
+
# @yieldreturn [RObject, nil] Either the resolved RObject or nil if your
|
44
|
+
# callback cannot resolve it. The next registered
|
45
|
+
# callback will be given the chance to resolve it.
|
46
|
+
#
|
47
|
+
# @note Ripple registers its own document-level conflict handler, so if you're
|
48
|
+
# using ripple, you will probably want to use that instead.
|
49
|
+
def self.on_conflict(&conflict_hook)
|
50
|
+
on_conflict_hooks << conflict_hook
|
51
|
+
end
|
52
|
+
|
53
|
+
# @return [Array<Proc>] the list of registered conflict callbacks.
|
54
|
+
def self.on_conflict_hooks
|
55
|
+
@on_conflict_hooks ||= []
|
56
|
+
end
|
57
|
+
|
58
|
+
def_delegators :content, :content_type, :content_type=,
|
59
|
+
:links, :links=,
|
60
|
+
:etag, :etag=,
|
61
|
+
:last_modified, :last_modified=,
|
62
|
+
:meta, :meta=,
|
63
|
+
:indexes, :indexes=,
|
64
|
+
:data, :data=,
|
65
|
+
:raw_data, :raw_data=,
|
66
|
+
:deserialize, :serialize
|
67
|
+
|
68
|
+
# Attempts to resolve conflict using the registered conflict callbacks.
|
69
|
+
#
|
70
|
+
# @return [RObject] the RObject
|
71
|
+
# @note There is no guarantee the returned RObject will have been resolved
|
72
|
+
def attempt_conflict_resolution
|
73
|
+
return self unless conflict?
|
74
|
+
|
75
|
+
self.class.on_conflict_hooks.each do |hook|
|
76
|
+
result = hook.call(self)
|
77
|
+
return result if result.is_a?(RObject)
|
78
|
+
end
|
79
|
+
|
80
|
+
self
|
81
|
+
end
|
82
|
+
|
83
|
+
# Loads a list of RObjects that were emitted from a MapReduce
|
84
|
+
# query.
|
85
|
+
# @param [Client] client A Riak::Client with which the results will be associated
|
86
|
+
# @param [Array<Hash>] response A list of results a MapReduce job. Each entry should contain these keys: bucket, key, vclock, values
|
87
|
+
# @return [Array<RObject>] An array of RObject instances
|
88
|
+
def self.load_from_mapreduce(client, response)
|
89
|
+
response.map do |item|
|
90
|
+
RObject.new(client[unescape(item['bucket'])], unescape(item['key'])).load_from_mapreduce(item)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Create a new object manually
|
95
|
+
# @param [Bucket] bucket the bucket in which the object exists
|
96
|
+
# @param [String] key the key at which the object resides. If nil, a key will be assigned when the object is saved.
|
97
|
+
# @yield self the new RObject
|
98
|
+
# @see Bucket#get
|
99
|
+
def initialize(bucket, key = nil)
|
100
|
+
@bucket, @key = bucket, key
|
101
|
+
|
102
|
+
# fix a require-loop
|
103
|
+
require 'riak/bucket_typed/bucket'
|
104
|
+
|
105
|
+
if @bucket.is_a? BucketTyped::Bucket
|
106
|
+
@type = @bucket.type.name
|
107
|
+
end
|
108
|
+
@siblings = [ RContent.new(self) ]
|
109
|
+
yield self if block_given?
|
110
|
+
end
|
111
|
+
|
112
|
+
# Load object data from a map/reduce response item.
|
113
|
+
# This method is used by RObject::load_from_mapreduce to instantiate the
|
114
|
+
# necessary objects.
|
115
|
+
# @param [Hash] response a response from {Riak::MapReduce}
|
116
|
+
# @return [RObject] self
|
117
|
+
def load_from_mapreduce(response)
|
118
|
+
self.vclock = response['vclock']
|
119
|
+
@siblings = response['values'].map do |v|
|
120
|
+
RContent.new(self) do |rcontent|
|
121
|
+
rcontent.load_map_reduce_value(v)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
self
|
125
|
+
end
|
126
|
+
|
127
|
+
# Store the object in Riak
|
128
|
+
# @param [Hash] options query parameters
|
129
|
+
# @option options [Fixnum] :r the "r" parameter (Read quorum for the
|
130
|
+
# implicit read performed when validating the store operation)
|
131
|
+
# @option options [Fixnum] :w the "w" parameter (Write quorum)
|
132
|
+
# @option options [Fixnum] :dw the "dw" parameter (Durable-write quorum)
|
133
|
+
# @option options [Boolean] :returnbody (true) whether to return the result
|
134
|
+
# of a successful write in the body of the response. Set to false for
|
135
|
+
# fire-and-forget updates, set to true to immediately have access to the
|
136
|
+
# object's stored representation.
|
137
|
+
# @return [Riak::RObject] self
|
138
|
+
# @raise [ArgumentError] if the content_type is not defined
|
139
|
+
# @raise [Conflict] if the object has siblings
|
140
|
+
def store(options = {})
|
141
|
+
fail Conflict, self if conflict?
|
142
|
+
fail ArgumentError, t('content_type_undefined') unless content_type.present?
|
143
|
+
fail ArgumentError, t('zero_length_key') if key == ''
|
144
|
+
@bucket.client.store_object(self, default(options))
|
145
|
+
self
|
146
|
+
end
|
147
|
+
|
148
|
+
# Reload the object from Riak. Will use conditional GETs when possible.
|
149
|
+
# @param [Hash] options query parameters
|
150
|
+
# @option options [Fixnum] :r the "r" parameter (Read quorum)
|
151
|
+
# @option options [Boolean] :force will force a reload request if
|
152
|
+
# the vclock is not present, useful for reloading the object after
|
153
|
+
# a store (not passed in the query params)
|
154
|
+
# @return [Riak::RObject] self
|
155
|
+
def reload(options = {})
|
156
|
+
force = options.delete(:force)
|
157
|
+
return self unless @key && (@vclock || force)
|
158
|
+
self.etag = self.last_modified = nil if force
|
159
|
+
bucket.client.reload_object(self, default(options))
|
160
|
+
end
|
161
|
+
|
162
|
+
alias :fetch :reload
|
163
|
+
|
164
|
+
# Delete the object from Riak and freeze this instance. Will work whether or not the object actually
|
165
|
+
# exists in the Riak database.
|
166
|
+
# @see Bucket#delete
|
167
|
+
def delete(options = {})
|
168
|
+
return if key.blank?
|
169
|
+
options[:vclock] = vclock if vclock
|
170
|
+
@bucket.delete(key, default(options))
|
171
|
+
freeze
|
172
|
+
end
|
173
|
+
|
174
|
+
# Returns sibling values. If the object is not in conflict, then
|
175
|
+
# only one value will be present in the array.
|
176
|
+
# @return [Array<RContent>] an array of conflicting sibling values
|
177
|
+
# for this key, possibly containing only one
|
178
|
+
attr_accessor :siblings
|
179
|
+
|
180
|
+
# Returns the solitary sibling when not in conflict.
|
181
|
+
# @return [RContent] the sole value/sibling on this object
|
182
|
+
# @raise [Conflict] when multiple siblings are present
|
183
|
+
def content
|
184
|
+
raise Conflict, self if conflict?
|
185
|
+
@siblings.first
|
186
|
+
end
|
187
|
+
|
188
|
+
# @return [true,false] Whether this object has conflicting sibling objects (divergent vclocks)
|
189
|
+
def conflict?
|
190
|
+
@siblings.size != 1
|
191
|
+
end
|
192
|
+
|
193
|
+
# @return [String] A representation suitable for IRB and debugging output
|
194
|
+
def inspect
|
195
|
+
body = @siblings.map {|s| s.inspect }.join(", ")
|
196
|
+
"#<#{self.class.name} {#{bucket.name}#{"," + @key if @key}} [#{body}]>"
|
197
|
+
end
|
198
|
+
|
199
|
+
# Converts the object to a link suitable for linking other objects
|
200
|
+
# to it
|
201
|
+
# @param [String] tag the tag to apply to the link
|
202
|
+
def to_link(tag)
|
203
|
+
Link.new(@bucket.name, @key, tag)
|
204
|
+
end
|
205
|
+
|
206
|
+
# Retrieves a preflist for this RObject; useful for
|
207
|
+
# figuring out where in the cluster it is stored.
|
208
|
+
# @return [Array<PreflistItem>] an array of preflist entries
|
209
|
+
def preflist(options = {})
|
210
|
+
bucket.get_preflist key, options
|
211
|
+
end
|
212
|
+
|
213
|
+
private
|
214
|
+
|
215
|
+
def default(options)
|
216
|
+
return options unless options.is_a? Hash
|
217
|
+
return options unless @type
|
218
|
+
|
219
|
+
{type: @type}.merge options
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
data/lib/riak/search.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'riak/search'
|
2
|
+
require 'riak/errors/search_error'
|
3
|
+
|
4
|
+
module Riak::Search
|
5
|
+
# A {Riak::Search::Index} is how Solr finds documents in Riak Search 2. A
|
6
|
+
# bucket or bucket type property must be configured to use the index in order
|
7
|
+
# for new and updated documents to be indexed and searchable.
|
8
|
+
class Index
|
9
|
+
# @!attribute [r] name
|
10
|
+
# @return [String] the name of the index
|
11
|
+
attr_reader :name
|
12
|
+
|
13
|
+
# @!attribute [r] client
|
14
|
+
# @return [Riak::Client] the client to operate on the index with
|
15
|
+
attr_reader :client
|
16
|
+
|
17
|
+
# Initializes an index object, that may or may not exist.
|
18
|
+
#
|
19
|
+
# @param [Riak::Client] client the client connected to the Riak cluster
|
20
|
+
# you wish to operate on
|
21
|
+
# @param [String] name the name of the index
|
22
|
+
def initialize(client, name)
|
23
|
+
@client = client
|
24
|
+
@name = name
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [Boolean] does this index exist on Riak?
|
28
|
+
def exists?
|
29
|
+
!!index_data
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Integer] N-value/replication parameter of this index
|
33
|
+
def n_val
|
34
|
+
index_data[:n_val]
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [String] schema name of this index
|
38
|
+
def schema
|
39
|
+
index_data[:schema]
|
40
|
+
end
|
41
|
+
|
42
|
+
# Attempt to create this index
|
43
|
+
#
|
44
|
+
# @raise [Riak::SearchError::IndexExistsError] if an index with the given
|
45
|
+
# name already exists
|
46
|
+
def create!(schema = nil, n_val = nil, timeout = nil)
|
47
|
+
raise Riak::SearchError::IndexExistsError.new name if exists?
|
48
|
+
|
49
|
+
@client.backend do |b|
|
50
|
+
b.create_search_index name, schema, n_val, timeout
|
51
|
+
end
|
52
|
+
|
53
|
+
@index_data = nil
|
54
|
+
|
55
|
+
true
|
56
|
+
end
|
57
|
+
|
58
|
+
# Create a {Riak::Search::Query} using this index and client
|
59
|
+
#
|
60
|
+
# @param [String] term the query term
|
61
|
+
# @param [Hash] options a hash of options to set attributes on the query
|
62
|
+
# @return [Riak::Search::Query] a query using this index
|
63
|
+
def query(term, options = { })
|
64
|
+
Riak::Search::Query.new(@client, self, term, options)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def index_data
|
69
|
+
return @index_data if defined?(@index_data) && @index_data
|
70
|
+
|
71
|
+
id = nil
|
72
|
+
|
73
|
+
begin
|
74
|
+
id = @client.backend do |b|
|
75
|
+
b.get_search_index name
|
76
|
+
end.index
|
77
|
+
rescue Riak::ProtobuffsFailedRequest => e
|
78
|
+
return nil if e.not_found?
|
79
|
+
raise e
|
80
|
+
end
|
81
|
+
|
82
|
+
id = id.first if id.is_a? Array
|
83
|
+
|
84
|
+
return @index_data = id
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'riak/search'
|
2
|
+
|
3
|
+
module Riak::Search
|
4
|
+
# A {Riak::Search::Query} wraps a Solr query for Riak Search 2.
|
5
|
+
class Query
|
6
|
+
|
7
|
+
# @!attribute rows
|
8
|
+
# @return [Numeric] the number of rows to return from the query
|
9
|
+
attr_accessor :rows
|
10
|
+
|
11
|
+
# @!attribute start
|
12
|
+
# @return [Numeric] the offset into the total result set to get results for
|
13
|
+
attr_accessor :start
|
14
|
+
|
15
|
+
# @!attribute sort
|
16
|
+
# @return [String] how Solr should sort the result set
|
17
|
+
attr_accessor :sort
|
18
|
+
|
19
|
+
# @!attribute filter
|
20
|
+
# @return [String] have Solr filter the results prior to returning them
|
21
|
+
attr_accessor :filter
|
22
|
+
|
23
|
+
# @!attribute df
|
24
|
+
# @return [Array<String>] default fields for Solr to search
|
25
|
+
attr_accessor :df
|
26
|
+
|
27
|
+
# @!attribute op
|
28
|
+
# @return [String] Solr search operator
|
29
|
+
attr_accessor :op
|
30
|
+
|
31
|
+
# @!attribute fl
|
32
|
+
# @return [Array<String>] fields for Solr to return
|
33
|
+
attr_accessor :fl
|
34
|
+
|
35
|
+
# @!attribute [r] term
|
36
|
+
# @return [String] the term to query
|
37
|
+
attr_reader :term
|
38
|
+
|
39
|
+
# @!attribute [r] index
|
40
|
+
# @return [Riak::Search::Index] the index to query against
|
41
|
+
attr_reader :index
|
42
|
+
|
43
|
+
# @!attribute [r] client
|
44
|
+
# @return [Riak::Client] the client to query against
|
45
|
+
attr_reader :client
|
46
|
+
|
47
|
+
# Initializes a query object.
|
48
|
+
#
|
49
|
+
# @param [Riak::Client] client the client connected to the Riak cluster
|
50
|
+
# @param [String,Riak::Search::Index] index the index to query, either a
|
51
|
+
# {Riak::Search::Index} instance or a {String}
|
52
|
+
# @param [String] term the query term
|
53
|
+
# @param [Hash] options a hash of options to quickly set attributes
|
54
|
+
def initialize(client, index, term, options = { })
|
55
|
+
@client = client
|
56
|
+
validate_index index
|
57
|
+
@term = term
|
58
|
+
@options = options.symbolize_keys
|
59
|
+
|
60
|
+
set_defaults
|
61
|
+
consume_options
|
62
|
+
end
|
63
|
+
|
64
|
+
# Get results from the query. Performs the query when called the first time.
|
65
|
+
#
|
66
|
+
# @return [Riak::Search::ResultCollection] collection of results
|
67
|
+
def results
|
68
|
+
return @results if defined? @results
|
69
|
+
|
70
|
+
@results = ResultCollection.new @client, raw_results
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def index_name
|
76
|
+
return @index if @index.is_a? String
|
77
|
+
return @index.name
|
78
|
+
end
|
79
|
+
|
80
|
+
def validate_index(index)
|
81
|
+
if index.is_a? String
|
82
|
+
index = Riak::Search::Index.new @client, index
|
83
|
+
end
|
84
|
+
|
85
|
+
unless index.is_a? Riak::Search::Index
|
86
|
+
raise Riak::SearchError::IndexArgumentError.new index
|
87
|
+
end
|
88
|
+
|
89
|
+
unless index.exists?
|
90
|
+
raise Riak::SearchError::IndexNonExistError.new index.name
|
91
|
+
end
|
92
|
+
|
93
|
+
@index = index
|
94
|
+
end
|
95
|
+
|
96
|
+
def set_defaults
|
97
|
+
@rows = nil
|
98
|
+
@start = nil
|
99
|
+
|
100
|
+
@sort = nil
|
101
|
+
@filter = nil
|
102
|
+
|
103
|
+
@df = %w{text}
|
104
|
+
@op = nil
|
105
|
+
@fl = %w{_yz_rb _yz_rk _yz_rt score}
|
106
|
+
|
107
|
+
@presort = nil
|
108
|
+
end
|
109
|
+
|
110
|
+
def consume_options
|
111
|
+
@rows = @options[:rows] if @options[:rows]
|
112
|
+
@start = @options[:start] if @options[:start]
|
113
|
+
|
114
|
+
@sort = @options[:sort] if @options[:sort]
|
115
|
+
@filter = @options[:filter] if @options[:filter]
|
116
|
+
|
117
|
+
@df = @options[:df] if @options[:df]
|
118
|
+
@op = @options[:op] if @options[:op]
|
119
|
+
@fl = @options[:fl] if @options[:fl]
|
120
|
+
end
|
121
|
+
|
122
|
+
def prepare_options
|
123
|
+
configured_options = {
|
124
|
+
rows: @rows,
|
125
|
+
start: @start,
|
126
|
+
sort: @sort,
|
127
|
+
filter: @filter,
|
128
|
+
df: @df.join(' '),
|
129
|
+
op: @op,
|
130
|
+
fl: @fl
|
131
|
+
}
|
132
|
+
@options.merge configured_options
|
133
|
+
end
|
134
|
+
|
135
|
+
def raw_results
|
136
|
+
@client.backend do |be|
|
137
|
+
be.search index_name, @term, prepare_options
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|