Class: Riak::Client::HTTPBackend
- Inherits:
-
Object
- Object
- Riak::Client::HTTPBackend
- Includes:
- 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
Instance Attribute Summary (collapse)
-
- (Object) client
readonly
The Riak::Client that uses this backend.
-
- (Object) node
readonly
The Riak::Client::Node that uses this backend.
Instance Method Summary (collapse)
-
- (Object) delete_file(filename)
(Luwak) Deletes a file from the Luwak large-file interface.
-
- (Object) delete_object(bucket, key, options = {})
Deletes an object.
-
- (RObject) fetch_object(bucket, key, options = {})
Fetches an object by bucket/key.
-
- (true, false) file_exists?(filename)
(Luwak) Detects whether a file exists in the Luwak large-file interface.
-
- (Hash) get_bucket_props(bucket)
Fetches bucket properties.
-
- (IO?) get_file(filename) {|chunk| ... }
(Luwak) Fetches a file from the Luwak large-file interface.
-
- (Array<String>) get_index(bucket, index, query)
Performs a secondary-index query.
-
- (HTTPBackend) initialize(client, node)
constructor
Create an HTTPBackend for the Riak::Client.
-
- (Array<Array<RObject>>) link_walk(robject, walk_specs)
Performs a link-walking query.
-
- (Array<String>) list_buckets
Lists known buckets.
-
- (Array<String>) list_keys(bucket) {|Array<String>| ... }
List keys in a bucket.
-
- (Array<Object>) mapred(mr) {|Fixnum, Object| ... }
Performs a MapReduce query.
-
- (true, false) ping
Pings the server.
-
- (Object) reload_object(robject, options = {})
Reloads the data for a given RObject, a special case of #fetch_object.
-
- (Object) search(index, query, options = {})
(Riak Search) Performs a search query.
-
- (Object) set_bucket_props(bucket, props)
Sets bucket properties.
-
- (Hash) stats
Gets health statistics.
-
- (String) store_file(*args)
(Luwak) Uploads a file to the Luwak large-file interface.
-
- (Object) store_object(robject, options = {})
Stores an object.
-
- (Object) update_search_index(index, updates)
(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 Util::Translation
Methods included from Util::Escape
#escape, #maybe_escape, #maybe_unescape, #unescape
Constructor Details
- (HTTPBackend) initialize(client, node)
Create an HTTPBackend for the Riak::Client.
38 39 40 41 42 43 |
# File 'lib/riak/client/http_backend.rb', line 38 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
- (Object) client (readonly)
The Riak::Client that uses this backend
30 31 32 |
# File 'lib/riak/client/http_backend.rb', line 30 def client @client end |
- (Object) node (readonly)
The Riak::Client::Node that uses this backend
33 34 35 |
# File 'lib/riak/client/http_backend.rb', line 33 def node @node end |
Instance Method Details
- (Object) delete_file(filename)
(Luwak) Deletes a file from the Luwak large-file interface.
293 294 295 |
# File 'lib/riak/client/http_backend.rb', line 293 def delete_file(filename) delete([204,404], luwak_path(filename)) end |
- (Object) delete_object(bucket, key, options = {})
Deletes an object
109 110 111 112 113 114 |
# File 'lib/riak/client/http_backend.rb', line 109 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 |
- (RObject) fetch_object(bucket, key, options = {})
Fetches an object by bucket/key
65 66 67 68 69 |
# File 'lib/riak/client/http_backend.rb', line 65 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 |
- (true, false) file_exists?(filename)
(Luwak) Detects whether a file exists in the Luwak large-file interface.
286 287 288 289 |
# File 'lib/riak/client/http_backend.rb', line 286 def file_exists?(filename) result = head([200,404], luwak_path(filename)) result[:code] == 200 end |
- (Hash) get_bucket_props(bucket)
Fetches bucket properties
119 120 121 122 123 |
# File 'lib/riak/client/http_backend.rb', line 119 def get_bucket_props(bucket) bucket = bucket.name if Bucket === bucket response = get(200, bucket_properties_path(bucket)) JSON.parse(response[:body])['props'] end |
- (IO?) get_file(filename) {|chunk| ... }
(Luwak) Fetches a file from the Luwak large-file interface.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/riak/client/http_backend.rb', line 264 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 |
- (Array<String>) get_index(bucket, index, query)
Performs a secondary-index query.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/riak/client/http_backend.rb', line 217 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 |
- (Array<Array<RObject>>) link_walk(robject, walk_specs)
Performs a link-walking query
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/riak/client/http_backend.rb', line 195 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 |
- (Array<String>) list_buckets
Lists known buckets
153 154 155 156 |
# File 'lib/riak/client/http_backend.rb', line 153 def list_buckets response = get(200, bucket_list_path) JSON.parse(response[:body])['buckets'] end |
- (Array<String>) list_keys(bucket) {|Array<String>| ... }
List keys in a bucket
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/riak/client/http_backend.rb', line 140 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 |
- (Array<Object>) mapred(mr) {|Fixnum, Object| ... }
Performs a MapReduce query.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/riak/client/http_backend.rb', line 164 def mapred(mr) 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 |
- (true, false) ping
Pings the server
47 48 49 50 51 52 |
# File 'lib/riak/client/http_backend.rb', line 47 def ping get(200, ping_path) true rescue false end |
- (Object) reload_object(robject, options = {})
Reloads the data for a given RObject, a special case of #fetch_object.
72 73 74 75 76 77 78 79 |
# File 'lib/riak/client/http_backend.rb', line 72 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 |
- (Object) search(index, query, options = {})
(Riak Search) Performs a search query
238 239 240 241 242 243 244 245 |
# File 'lib/riak/client/http_backend.rb', line 238 def search(index, query, ={}) response = get(200, solr_select_path(index, query, .stringify_keys)) if response[:headers]['content-type'].include?("application/json") JSON.parse(response[:body]) else response[:body] end end |
- (Object) set_bucket_props(bucket, props)
Sets bucket properties
128 129 130 131 132 |
# File 'lib/riak/client/http_backend.rb', line 128 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 |
- (Hash) stats
Gets health statistics
184 185 186 187 |
# File 'lib/riak/client/http_backend.rb', line 184 def stats response = get(200, stats_path) JSON.parse(response[:body]) end |
- (String) store_file(filename, content_type, data) - (String) store_file(content_type, data)
(Luwak) Uploads a file to the Luwak large-file interface.
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/riak/client/http_backend.rb', line 308 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 |
- (Object) store_object(robject, options = {})
Stores an object
90 91 92 93 94 95 96 97 98 |
# File 'lib/riak/client/http_backend.rb', line 90 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 |
- (Object) update_search_index(index, updates)
(Riak Search) Updates a search index (includes deletes).
252 253 254 |
# File 'lib/riak/client/http_backend.rb', line 252 def update_search_index(index, updates) post(200, solr_update_path(index), updates, {'Content-Type' => 'text/xml'}) end |