riak-client-noenc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gitignore +42 -0
  4. data/.rspec +1 -0
  5. data/Gemfile +17 -0
  6. data/Guardfile +20 -0
  7. data/LICENSE.md +16 -0
  8. data/README.markdown +640 -0
  9. data/RELEASE_NOTES.md +392 -0
  10. data/Rakefile +119 -0
  11. data/lib/riak.rb +22 -0
  12. data/lib/riak/bucket.rb +297 -0
  13. data/lib/riak/bucket_properties.rb +74 -0
  14. data/lib/riak/bucket_type.rb +77 -0
  15. data/lib/riak/bucket_typed/bucket.rb +121 -0
  16. data/lib/riak/client.rb +433 -0
  17. data/lib/riak/client/beefcake/bucket_properties_operator.rb +178 -0
  18. data/lib/riak/client/beefcake/crdt/counter_loader.rb +18 -0
  19. data/lib/riak/client/beefcake/crdt/map_loader.rb +64 -0
  20. data/lib/riak/client/beefcake/crdt/set_loader.rb +18 -0
  21. data/lib/riak/client/beefcake/crdt_loader.rb +84 -0
  22. data/lib/riak/client/beefcake/crdt_operator.rb +223 -0
  23. data/lib/riak/client/beefcake/footer +4 -0
  24. data/lib/riak/client/beefcake/header +6 -0
  25. data/lib/riak/client/beefcake/message_codes.rb +89 -0
  26. data/lib/riak/client/beefcake/message_overlay.rb +87 -0
  27. data/lib/riak/client/beefcake/messages.rb +772 -0
  28. data/lib/riak/client/beefcake/object_methods.rb +112 -0
  29. data/lib/riak/client/beefcake/protocol.rb +105 -0
  30. data/lib/riak/client/beefcake/socket.rb +260 -0
  31. data/lib/riak/client/beefcake_protobuffs_backend.rb +538 -0
  32. data/lib/riak/client/decaying.rb +36 -0
  33. data/lib/riak/client/feature_detection.rb +120 -0
  34. data/lib/riak/client/instrumentation.rb +19 -0
  35. data/lib/riak/client/node.rb +49 -0
  36. data/lib/riak/client/protobuffs_backend.rb +143 -0
  37. data/lib/riak/client/search.rb +27 -0
  38. data/lib/riak/client/yokozuna.rb +52 -0
  39. data/lib/riak/conflict.rb +13 -0
  40. data/lib/riak/core_ext.rb +7 -0
  41. data/lib/riak/core_ext/blank.rb +53 -0
  42. data/lib/riak/core_ext/deep_dup.rb +13 -0
  43. data/lib/riak/core_ext/extract_options.rb +7 -0
  44. data/lib/riak/core_ext/json.rb +15 -0
  45. data/lib/riak/core_ext/slice.rb +18 -0
  46. data/lib/riak/core_ext/stringify_keys.rb +10 -0
  47. data/lib/riak/core_ext/symbolize_keys.rb +10 -0
  48. data/lib/riak/core_ext/to_param.rb +31 -0
  49. data/lib/riak/counter.rb +101 -0
  50. data/lib/riak/crdt.rb +21 -0
  51. data/lib/riak/crdt/base.rb +183 -0
  52. data/lib/riak/crdt/batch_counter.rb +19 -0
  53. data/lib/riak/crdt/batch_map.rb +41 -0
  54. data/lib/riak/crdt/counter.rb +82 -0
  55. data/lib/riak/crdt/inner_counter.rb +81 -0
  56. data/lib/riak/crdt/inner_flag.rb +42 -0
  57. data/lib/riak/crdt/inner_map.rb +75 -0
  58. data/lib/riak/crdt/inner_register.rb +26 -0
  59. data/lib/riak/crdt/inner_set.rb +102 -0
  60. data/lib/riak/crdt/map.rb +121 -0
  61. data/lib/riak/crdt/operation.rb +19 -0
  62. data/lib/riak/crdt/set.rb +166 -0
  63. data/lib/riak/crdt/typed_collection.rb +181 -0
  64. data/lib/riak/encoding.rb +6 -0
  65. data/lib/riak/errors/backend_creation.rb +9 -0
  66. data/lib/riak/errors/base.rb +9 -0
  67. data/lib/riak/errors/connection_error.rb +50 -0
  68. data/lib/riak/errors/crdt_error.rb +38 -0
  69. data/lib/riak/errors/failed_request.rb +58 -0
  70. data/lib/riak/errors/protobuffs_error.rb +11 -0
  71. data/lib/riak/errors/search_error.rb +35 -0
  72. data/lib/riak/i18n.rb +7 -0
  73. data/lib/riak/index_collection.rb +71 -0
  74. data/lib/riak/instrumentation.rb +6 -0
  75. data/lib/riak/json.rb +52 -0
  76. data/lib/riak/link.rb +96 -0
  77. data/lib/riak/list_buckets.rb +28 -0
  78. data/lib/riak/locale/en.yml +107 -0
  79. data/lib/riak/locale/fr.yml +51 -0
  80. data/lib/riak/map_reduce.rb +295 -0
  81. data/lib/riak/map_reduce/filter_builder.rb +103 -0
  82. data/lib/riak/map_reduce/phase.rb +98 -0
  83. data/lib/riak/map_reduce/results.rb +49 -0
  84. data/lib/riak/map_reduce_error.rb +7 -0
  85. data/lib/riak/multiget.rb +122 -0
  86. data/lib/riak/preflist_item.rb +7 -0
  87. data/lib/riak/rcontent.rb +173 -0
  88. data/lib/riak/robject.rb +222 -0
  89. data/lib/riak/search.rb +11 -0
  90. data/lib/riak/search/index.rb +87 -0
  91. data/lib/riak/search/query.rb +141 -0
  92. data/lib/riak/search/result_collection.rb +144 -0
  93. data/lib/riak/search/result_document.rb +129 -0
  94. data/lib/riak/search/schema.rb +65 -0
  95. data/lib/riak/secondary_index.rb +81 -0
  96. data/lib/riak/serializers.rb +73 -0
  97. data/lib/riak/stamp.rb +77 -0
  98. data/lib/riak/util/escape.rb +80 -0
  99. data/lib/riak/util/tcp_socket_extensions.rb +58 -0
  100. data/lib/riak/util/translation.rb +18 -0
  101. data/lib/riak/version.rb +3 -0
  102. data/lib/riak/walk_spec.rb +145 -0
  103. data/spec/failover/failover.rb +59 -0
  104. data/spec/fixtures/bitcask.txt +25 -0
  105. data/spec/fixtures/cat.jpg +0 -0
  106. data/spec/fixtures/multipart-basic-conflict.txt +15 -0
  107. data/spec/fixtures/multipart-blank.txt +7 -0
  108. data/spec/fixtures/multipart-mapreduce.txt +10 -0
  109. data/spec/fixtures/multipart-with-body.txt +16 -0
  110. data/spec/fixtures/multipart-with-marked-tombstones.txt +17 -0
  111. data/spec/fixtures/multipart-with-unmarked-tombstone.txt +16 -0
  112. data/spec/fixtures/server.cert.crt +15 -0
  113. data/spec/fixtures/server.cert.key +15 -0
  114. data/spec/fixtures/test.pem +1 -0
  115. data/spec/fixtures/yz_schema_template.xml +18 -0
  116. data/spec/integration/riak/bucket_types_spec.rb +270 -0
  117. data/spec/integration/riak/conflict_resolution_spec.rb +96 -0
  118. data/spec/integration/riak/counters_spec.rb +36 -0
  119. data/spec/integration/riak/crdt/configuration_spec.rb +37 -0
  120. data/spec/integration/riak/crdt_search_spec.rb +176 -0
  121. data/spec/integration/riak/crdt_spec.rb +250 -0
  122. data/spec/integration/riak/crdt_validation/map_spec.rb +63 -0
  123. data/spec/integration/riak/crdt_validation/set_spec.rb +122 -0
  124. data/spec/integration/riak/preflist_spec.rb +31 -0
  125. data/spec/integration/riak/properties_spec.rb +69 -0
  126. data/spec/integration/riak/protobuffs/interrupted_request_spec.rb +33 -0
  127. data/spec/integration/riak/protobuffs_backends_spec.rb +40 -0
  128. data/spec/integration/riak/search_spec.rb +104 -0
  129. data/spec/integration/riak/secondary_index_spec.rb +72 -0
  130. data/spec/integration/riak/security_spec.rb +100 -0
  131. data/spec/integration/riak/threading_spec.rb +150 -0
  132. data/spec/integration/yokozuna/index_spec.rb +61 -0
  133. data/spec/integration/yokozuna/queries_spec.rb +115 -0
  134. data/spec/integration/yokozuna/schema_spec.rb +49 -0
  135. data/spec/riak/beefcake_protobuffs_backend/bucket_properties_operator_spec.rb +247 -0
  136. data/spec/riak/beefcake_protobuffs_backend/crdt_operator_spec.rb +222 -0
  137. data/spec/riak/beefcake_protobuffs_backend/object_methods_spec.rb +23 -0
  138. data/spec/riak/beefcake_protobuffs_backend/protocol_spec.rb +189 -0
  139. data/spec/riak/beefcake_protobuffs_backend_spec.rb +162 -0
  140. data/spec/riak/bucket_properties_spec.rb +135 -0
  141. data/spec/riak/bucket_spec.rb +275 -0
  142. data/spec/riak/bucket_type_spec.rb +50 -0
  143. data/spec/riak/bucket_typed/bucket_spec.rb +62 -0
  144. data/spec/riak/client_spec.rb +246 -0
  145. data/spec/riak/core_ext/to_param_spec.rb +15 -0
  146. data/spec/riak/counter_spec.rb +122 -0
  147. data/spec/riak/crdt/counter_spec.rb +55 -0
  148. data/spec/riak/crdt/inner_counter_spec.rb +21 -0
  149. data/spec/riak/crdt/inner_flag_spec.rb +39 -0
  150. data/spec/riak/crdt/inner_map_spec.rb +47 -0
  151. data/spec/riak/crdt/inner_register_spec.rb +40 -0
  152. data/spec/riak/crdt/inner_set_spec.rb +33 -0
  153. data/spec/riak/crdt/map_spec.rb +78 -0
  154. data/spec/riak/crdt/set_spec.rb +61 -0
  155. data/spec/riak/crdt/shared_examples.rb +74 -0
  156. data/spec/riak/crdt/typed_collection_spec.rb +225 -0
  157. data/spec/riak/escape_spec.rb +72 -0
  158. data/spec/riak/feature_detection_spec.rb +77 -0
  159. data/spec/riak/index_collection_spec.rb +53 -0
  160. data/spec/riak/instrumentation_spec.rb +124 -0
  161. data/spec/riak/link_spec.rb +85 -0
  162. data/spec/riak/list_buckets_spec.rb +41 -0
  163. data/spec/riak/map_reduce/filter_builder_spec.rb +32 -0
  164. data/spec/riak/map_reduce/phase_spec.rb +142 -0
  165. data/spec/riak/map_reduce_spec.rb +434 -0
  166. data/spec/riak/multiget_spec.rb +81 -0
  167. data/spec/riak/node_spec.rb +26 -0
  168. data/spec/riak/robject_spec.rb +496 -0
  169. data/spec/riak/search/index_spec.rb +72 -0
  170. data/spec/riak/search/query_spec.rb +88 -0
  171. data/spec/riak/search/result_collection_spec.rb +89 -0
  172. data/spec/riak/search/result_document_spec.rb +106 -0
  173. data/spec/riak/search/schema_spec.rb +63 -0
  174. data/spec/riak/search_spec.rb +107 -0
  175. data/spec/riak/secondary_index_spec.rb +225 -0
  176. data/spec/riak/serializers_spec.rb +121 -0
  177. data/spec/riak/stamp_spec.rb +54 -0
  178. data/spec/riak/walk_spec_spec.rb +203 -0
  179. data/spec/spec_helper.rb +66 -0
  180. data/spec/support/certs/README.md +13 -0
  181. data/spec/support/certs/ca.crt +21 -0
  182. data/spec/support/certs/client.crl +13 -0
  183. data/spec/support/certs/client.crt +94 -0
  184. data/spec/support/certs/client.csr +18 -0
  185. data/spec/support/certs/client.key +27 -0
  186. data/spec/support/certs/empty_ca.crt +21 -0
  187. data/spec/support/certs/server.crl +13 -0
  188. data/spec/support/certs/server.crt +94 -0
  189. data/spec/support/certs/server.key +27 -0
  190. data/spec/support/crdt_search_config.rb +112 -0
  191. data/spec/support/crdt_search_fixtures.rb +42 -0
  192. data/spec/support/integration_setup.rb +10 -0
  193. data/spec/support/search_config.rb +83 -0
  194. data/spec/support/search_corpus_setup.rb +39 -0
  195. data/spec/support/test_client.rb +46 -0
  196. data/spec/support/test_client.yml.example +10 -0
  197. data/spec/support/unified_backend_examples.rb +380 -0
  198. data/spec/support/version_filter.rb +12 -0
  199. data/spec/support/wait_until.rb +20 -0
  200. metadata +511 -0
@@ -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
@@ -0,0 +1,11 @@
1
+ module Riak
2
+ module Search
3
+ end
4
+ end
5
+
6
+ require 'riak/search/index'
7
+ require 'riak/search/query'
8
+ require 'riak/search/result_collection'
9
+ require 'riak/search/result_document'
10
+ require 'riak/search/schema'
11
+ require 'riak/errors/search_error'
@@ -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