Class: Riak::Client::HTTPBackend
- Includes:
- FeatureDetection, Configuration, ObjectMethods, TransportMethods, Util::Escape, Util::Translation
- Defined in:
- lib/riak/client/http_backend.rb,
lib/riak/client/http_backend/key_streamer.rb,
lib/riak/client/http_backend/configuration.rb,
lib/riak/client/http_backend/object_methods.rb,
lib/riak/client/http_backend/request_headers.rb,
lib/riak/client/http_backend/transport_methods.rb
Overview
The parent class for all backends that connect to Riak via HTTP. This class implements all of the universal backend API methods on behalf of subclasses, which need only implement the TransportMethods#perform method for library-specific semantics.
Direct Known Subclasses
Defined Under Namespace
Modules: Configuration, ObjectMethods, TransportMethods Classes: KeyStreamer, RequestHeaders
Constant Summary
Constants included from FeatureDetection
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
The Riak::Client that uses this backend.
-
#node ⇒ Object
readonly
The Riak::Client::Node that uses this backend.
Instance Method Summary collapse
-
#delete_file(filename) ⇒ Object
(Luwak) Deletes a file from the Luwak large-file interface.
-
#delete_object(bucket, key, options = {}) ⇒ Object
Deletes an object.
-
#fetch_object(bucket, key, options = {}) ⇒ RObject
Fetches an object by bucket/key.
-
#file_exists?(filename) ⇒ true, false
(Luwak) Detects whether a file exists in the Luwak large-file interface.
-
#get_bucket_props(bucket) ⇒ Hash
Fetches bucket properties.
-
#get_file(filename) {|chunk| ... } ⇒ IO?
(Luwak) Fetches a file from the Luwak large-file interface.
-
#get_index(bucket, index, query) ⇒ Array<String>
Performs a secondary-index query.
-
#initialize(client, node) ⇒ HTTPBackend
constructor
Create an HTTPBackend for the Riak::Client.
-
#link_walk(robject, walk_specs) ⇒ Array<Array<RObject>>
Performs a link-walking query.
-
#list_buckets ⇒ Array<String>
Lists known buckets.
-
#list_keys(bucket) {|Array<String>| ... } ⇒ Array<String>
List keys in a bucket.
-
#mapred(mr) {|Fixnum, Object| ... } ⇒ Array<Object>
Performs a MapReduce query.
-
#ping ⇒ true, false
Pings the server.
-
#reload_object(robject, options = {}) ⇒ Object
Reloads the data for a given RObject, a special case of #fetch_object.
-
#search(index, query, options = {}) ⇒ Object
(Riak Search) Performs a search query.
-
#set_bucket_props(bucket, props) ⇒ Object
Sets bucket properties.
-
#stats ⇒ Hash
Gets health statistics.
-
#store_file(*args) ⇒ String
(Luwak) Uploads a file to the Luwak large-file interface.
-
#store_object(robject, options = {}) ⇒ Object
Stores an object.
-
#update_search_index(index, updates) ⇒ Object
(Riak Search) Updates a search index (includes deletes).
Methods included from Configuration
#bucket_list_path, #bucket_properties_path, #index_eq_path, #index_range_path, #key_list_path, #link_walk_path, #luwak_path, #mapred_path, #object_path, #ping_path, #solr_select_path, #solr_update_path, #stats_path
Methods included from ObjectMethods
#load_object, #reload_headers, #store_headers
Methods included from TransportMethods
#basic_auth_header, #client_id, #default_headers, #delete, #get, #head, #path, #perform, #post, #put, #return_body?, #root_uri, #valid_response?, #verify_body!
Methods included from FeatureDetection
#get_server_version, #mapred_phaseless?, #pb_conditionals?, #pb_head?, #pb_indexes?, #pb_search?, #quorum_controls?, #server_version, #tombstone_vclocks?
Methods included from Util::Translation
Methods included from Util::Escape
#escape, #maybe_escape, #maybe_unescape, #unescape
Constructor Details
#initialize(client, node) ⇒ HTTPBackend
Create an HTTPBackend for the Riak::Client.
40 41 42 43 44 45 |
# File 'lib/riak/client/http_backend.rb', line 40 def initialize(client, node) raise ArgumentError, t("client_type", :client => client) unless Client === client raise ArgumentError, t("node_type", :node => node) unless Node === node @client = client @node = node end |
Instance Attribute Details
#client ⇒ Object (readonly)
The Riak::Client that uses this backend
32 33 34 |
# File 'lib/riak/client/http_backend.rb', line 32 def client @client end |
#node ⇒ Object (readonly)
The Riak::Client::Node that uses this backend
35 36 37 |
# File 'lib/riak/client/http_backend.rb', line 35 def node @node end |
Instance Method Details
#delete_file(filename) ⇒ Object
(Luwak) Deletes a file from the Luwak large-file interface.
296 297 298 |
# File 'lib/riak/client/http_backend.rb', line 296 def delete_file(filename) delete([204,404], luwak_path(filename)) end |
#delete_object(bucket, key, options = {}) ⇒ Object
Deletes an object
111 112 113 114 115 116 |
# File 'lib/riak/client/http_backend.rb', line 111 def delete_object(bucket, key, ={}) bucket = bucket.name if Bucket === bucket vclock = .delete(:vclock) headers = vclock ? {"X-Riak-VClock" => vclock} : {} delete([204, 404], object_path(bucket, key, ), headers) end |
#fetch_object(bucket, key, options = {}) ⇒ RObject
Fetches an object by bucket/key
67 68 69 70 71 |
# File 'lib/riak/client/http_backend.rb', line 67 def fetch_object(bucket, key, ={}) bucket = Bucket.new(client, bucket) if String === bucket response = get([200,300], object_path(bucket.name, key, )) load_object(RObject.new(bucket, key), response) end |
#file_exists?(filename) ⇒ true, false
(Luwak) Detects whether a file exists in the Luwak large-file interface.
289 290 291 292 |
# File 'lib/riak/client/http_backend.rb', line 289 def file_exists?(filename) result = head([200,404], luwak_path(filename)) result[:code] == 200 end |
#get_bucket_props(bucket) ⇒ Hash
Fetches bucket properties
121 122 123 124 125 |
# File 'lib/riak/client/http_backend.rb', line 121 def get_bucket_props(bucket) bucket = bucket.name if Bucket === bucket response = get(200, bucket_properties_path(bucket)) JSON.parse(response[:body])['props'] end |
#get_file(filename) {|chunk| ... } ⇒ IO?
(Luwak) Fetches a file from the Luwak large-file interface.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/riak/client/http_backend.rb', line 267 def get_file(filename, &block) if block_given? get(200, luwak_path(filename), &block) nil else tmpfile = LuwakFile.new(escape(filename)) begin response = get(200, luwak_path(filename)) do |chunk| tmpfile.write chunk end tmpfile.content_type = response[:headers]['content-type'].first tmpfile ensure tmpfile.close end end end |
#get_index(bucket, index, query) ⇒ Array<String>
Performs a secondary-index query.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/riak/client/http_backend.rb', line 220 def get_index(bucket, index, query) bucket = bucket.name if Bucket === bucket path = case query when Range raise ArgumentError, t('invalid_index_query', :value => query.inspect) unless String === query.begin || Integer === query.end index_range_path(bucket, index, query.begin, query.end) when String, Integer index_eq_path(bucket, index, query) else raise ArgumentError, t('invalid_index_query', :value => query.inspect) end response = get(200, path) JSON.parse(response[:body])['keys'] end |
#link_walk(robject, walk_specs) ⇒ Array<Array<RObject>>
Performs a link-walking query
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/riak/client/http_backend.rb', line 198 def link_walk(robject, walk_specs) response = get(200, link_walk_path(robject.bucket.name, robject.key, walk_specs)) if boundary = Util::Multipart.extract_boundary(response[:headers]['content-type'].first) Util::Multipart.parse(response[:body], boundary).map do |group| group.map do |obj| if obj[:headers] && !obj[:headers]['x-riak-deleted'] && !obj[:body].blank? && obj[:headers]['location'] link = Riak::Link.new(obj[:headers]['location'].first, "") load_object(RObject.new(client.bucket(link.bucket), link.key), obj) end end.compact end else [] end end |
#list_buckets ⇒ Array<String>
Lists known buckets
155 156 157 158 |
# File 'lib/riak/client/http_backend.rb', line 155 def list_buckets response = get(200, bucket_list_path) JSON.parse(response[:body])['buckets'] end |
#list_keys(bucket) {|Array<String>| ... } ⇒ Array<String>
List keys in a bucket
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/riak/client/http_backend.rb', line 142 def list_keys(bucket, &block) bucket = bucket.name if Bucket === bucket if block_given? get(200, key_list_path(bucket, :keys => 'stream'), {}, &KeyStreamer.new(block)) else response = get(200, key_list_path(bucket)) obj = JSON.parse(response[:body]) obj && obj['keys'].map {|k| unescape(k) } end end |
#mapred(mr) {|Fixnum, Object| ... } ⇒ Array<Object>
Performs a MapReduce query.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/riak/client/http_backend.rb', line 166 def mapred(mr) raise MapReduceError.new(t("empty_map_reduce_query")) if mr.query.empty? && !mapred_phaseless? if block_given? parser = Riak::Util::Multipart::StreamParser.new do |response| result = JSON.parse(response[:body]) yield result['phase'], result['data'] end post(200, mapred_path({:chunked => true}), mr.to_json, {"Content-Type" => "application/json", "Accept" => "application/json"}, &parser) nil else response = post(200, mapred_path, mr.to_json, {"Content-Type" => "application/json", "Accept" => "application/json"}) begin JSON.parse(response[:body]) rescue response end end end |
#ping ⇒ true, false
Pings the server
49 50 51 52 53 54 |
# File 'lib/riak/client/http_backend.rb', line 49 def ping get(200, ping_path) true rescue false end |
#reload_object(robject, options = {}) ⇒ Object
Reloads the data for a given RObject, a special case of #fetch_object.
74 75 76 77 78 79 80 81 |
# File 'lib/riak/client/http_backend.rb', line 74 def reload_object(robject, ={}) response = get([200,300,304], object_path(robject.bucket.name, robject.key, ), reload_headers(robject)) if response[:code].to_i == 304 robject else load_object(robject, response) end end |
#search(index, query, options = {}) ⇒ Object
(Riak Search) Performs a search query
241 242 243 244 245 246 247 248 |
# File 'lib/riak/client/http_backend.rb', line 241 def search(index, query, ={}) response = get(200, solr_select_path(index, query, .stringify_keys)) if response[:headers]['content-type'].include?("application/json") normalize_search_response JSON.parse(response[:body]) else response[:body] end end |
#set_bucket_props(bucket, props) ⇒ Object
Sets bucket properties
130 131 132 133 134 |
# File 'lib/riak/client/http_backend.rb', line 130 def set_bucket_props(bucket, props) bucket = bucket.name if Bucket === bucket body = {'props' => props}.to_json put(204, bucket_properties_path(bucket), body, {"Content-Type" => "application/json"}) end |
#stats ⇒ Hash
Gets health statistics
187 188 189 190 |
# File 'lib/riak/client/http_backend.rb', line 187 def stats response = get(200, stats_path) JSON.parse(response[:body]) end |
#store_file(filename, content_type, data) ⇒ String #store_file(content_type, data) ⇒ String
(Luwak) Uploads a file to the Luwak large-file interface.
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/riak/client/http_backend.rb', line 311 def store_file(*args) data, content_type, filename = args.reverse if filename put(204, luwak_path(filename), data, {"Content-Type" => content_type}) filename else response = post(201, luwak_path(nil), data, {"Content-Type" => content_type}) response[:headers]["location"].first.split("/").last end end |
#store_object(robject, options = {}) ⇒ Object
Stores an object
92 93 94 95 96 97 98 99 100 |
# File 'lib/riak/client/http_backend.rb', line 92 def store_object(robject, ={}) method, codes = if robject.key.present? [:put, [200,204,300]] else [:post, 201] end response = send(method, codes, object_path(robject.bucket.name, robject.key, ), robject.raw_data, store_headers(robject)) load_object(robject, response) if [:returnbody] end |
#update_search_index(index, updates) ⇒ Object
(Riak Search) Updates a search index (includes deletes).
255 256 257 |
# File 'lib/riak/client/http_backend.rb', line 255 def update_search_index(index, updates) post(200, solr_update_path(index), updates, {'Content-Type' => 'text/xml'}) end |