Class: Riak::Client
- Inherits:
-
Object
- Object
- Riak::Client
- Includes:
- Util::Escape, Util::Translation
- Defined in:
- riak-client/lib/riak/client.rb,
riak-client/lib/riak/search.rb,
riak-client/lib/riak/client/pump.rb,
riak-client/lib/riak/client/curb_backend.rb,
riak-client/lib/riak/client/http_backend.rb,
riak-client/lib/riak/client/excon_backend.rb,
riak-client/lib/riak/client/net_http_backend.rb,
riak-client/lib/riak/client/beefcake/messages.rb,
riak-client/lib/riak/client/protobuffs_backend.rb,
riak-client/lib/riak/client/beefcake/object_methods.rb,
riak-client/lib/riak/client/http_backend/key_streamer.rb,
riak-client/lib/riak/client/http_backend/configuration.rb,
riak-client/lib/riak/client/http_backend/object_methods.rb,
riak-client/lib/riak/client/beefcake_protobuffs_backend.rb,
riak-client/lib/riak/client/http_backend/request_headers.rb,
riak-client/lib/riak/client/http_backend/transport_methods.rb
Overview
A client connection to Riak.
Defined Under Namespace
Classes: CurbBackend, ExconBackend, HTTPBackend, NetHTTPBackend, ProtobuffsBackend
Constant Summary
- MAX_CLIENT_ID =
When using integer client IDs, the exclusive upper-bound of valid values.
4294967296- PROTOCOLS =
Array of valid protocols
%w[http https pbc]
- HOST_REGEX =
Regexp for validating hostnames, lifted from uri.rb in Ruby 1.8.6
/^(?:(?:(?:[a-zA-Z\d](?:[-a-zA-Z\d]*[a-zA-Z\d])?)\.)*(?:[a-zA-Z](?:[-a-zA-Z\d]*[a-zA-Z\d])?)\.?|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:(?:[a-fA-F\d]{1,4}:)*[a-fA-F\d]{1,4})?::(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))?)\])$/n- VALID_OPTIONS =
[:protocol, :host, :port, :http_port, :pb_port, :prefix, :client_id, :mapred, :luwak, :http_backend, :protobuffs_backend, :ssl, :basic_auth]
Instance Attribute Summary (collapse)
-
- (String) basic_auth
The user:pass for http basic authentication.
-
- (String) client_id
The internal client ID used by Riak to route responses.
-
- (String) host
The host or IP address for the Riak endpoint.
-
- (Symbol) http_backend
The HTTP backend/client to use.
-
- (Fixnum) http_port
The HTTP(S) port for the Riak endpoint.
-
- (String) luwak
The URL path to the luwak HTTP endpoint.
-
- (String) mapred
The URL path to the map-reduce HTTP endpoint.
-
- (Fixnum) pb_port
The Protocol Buffers port for the Riak endpoint.
-
- (String) prefix
The URL path prefix to the “raw” HTTP endpoint.
-
- (Symbol) protobuffs_backend
The Protocol Buffers backend/client to use.
-
- (String) protocol
The protocol to use for the Riak endpoint.
-
- (String) solr
The URL path prefix to the Solr HTTP endpoint.
-
- (Object) ssl
writeonly
Enables or disables SSL on the client to be utilized by the HTTP Backends.
-
- (Hash|nil) ssl_options
readonly
The SSL options that get built when using SSL.
Instance Method Summary (collapse)
-
- (HTTPBackend, ProtobuffsBackend) backend
Returns a backend for operations that are protocol-independent.
-
- (Bucket) bucket(name, options = {})
(also: #[])
Retrieves a bucket from Riak.
-
- (Array<Bucket>) buckets
(also: #list_buckets)
Lists buckets which have keys stored in them.
-
- (Object) delete_file(filename)
Deletes a file stored via the “Luwak” interface.
-
- (true, false) file_exists?(key)
(also: #file_exist?)
Checks whether a file exists in “Luwak”.
-
- (IO?) get_file(filename) {|chunk| ... }
Retrieves a large file/IO object from Riak via the “Luwak” interface.
-
- (HTTPBackend) http
Automatically detects and returns an appropriate HTTP backend.
-
- (Object) index(*args)
(also: #add_doc)
Adds documents to a search index via the Solr interface.
-
- (Client) initialize(options = {})
constructor
Creates a client connection to Riak.
-
- (String) inspect
A representation suitable for IRB and debugging output.
-
- (true, false) ping
Pings the Riak server to check for liveness.
-
- (Fixnum) port
Deprecated
Deprecated.
Ports for HTTP(S) and Protocol Buffers are segregated. Use #http_port or #pb_port.
-
- (Fixnum) port=(value)
Deprecated
Deprecated.
Ports for HTTP(S) and Protocol Buffers are segregated. Use #http_port= or #pb_port=.
-
- (ProtobuffsBackend) protobuffs
Automatically detects and returns an appropriate Protocol Buffers backend.
-
- (Object) remove(*args)
(also: #delete_doc, #deindex)
Removes documents from a search index via the Solr interface.
-
- (Hash) search(*args)
(also: #select)
Performs a search via the Solr interface.
-
- (Boolean) ssl_enabled?
Checks if SSL is enabled for HTTP.
-
- (String) store_file(*args)
Stores a large file/IO-like object in Riak via the “Luwak” interface.
Methods included from Util::Escape
Methods included from Util::Translation
Constructor Details
- (Client) initialize(options = {})
Creates a client connection to Riak
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'riak-client/lib/riak/search.rb', line 29 def initialize(={}) unless (.keys - VALID_OPTIONS).empty? raise ArgumentError, t("invalid_options") end self.protocol = [:protocol] || "http" self.ssl = [:ssl] if [:ssl] self.host = [:host] || "127.0.0.1" self.http_port = [:http_port] || 8098 self.pb_port = [:pb_port] || 8087 self.port = [:port] if [:port] self.prefix = [:prefix] || "/riak/" self.mapred = [:mapred] || "/mapred" self.luwak = [:luwak] || "/luwak" self.http_backend = [:http_backend] || :NetHTTP self.protobuffs_backend = [:protobuffs_backend] || :Beefcake self.basic_auth = [:basic_auth] if [:basic_auth] self.client_id = [:client_id] if [:client_id] end |
Instance Attribute Details
- (String) basic_auth
The user:pass for http basic authentication
59 60 61 |
# File 'riak-client/lib/riak/client.rb', line 59 def basic_auth @basic_auth end |
- (String) client_id
The internal client ID used by Riak to route responses
65 66 67 |
# File 'riak-client/lib/riak/client.rb', line 65 def client_id @client_id ||= backend.respond_to?(:get_client_id) ? backend.get_client_id : make_client_id end |
- (String) host
The host or IP address for the Riak endpoint
50 51 52 |
# File 'riak-client/lib/riak/client.rb', line 50 def host @host end |
- (Symbol) http_backend
The HTTP backend/client to use
81 82 83 |
# File 'riak-client/lib/riak/client.rb', line 81 def http_backend @http_backend end |
- (Fixnum) http_port
The HTTP(S) port for the Riak endpoint
53 54 55 |
# File 'riak-client/lib/riak/client.rb', line 53 def http_port @http_port end |
- (String) luwak
The URL path to the luwak HTTP endpoint
78 79 80 |
# File 'riak-client/lib/riak/client.rb', line 78 def luwak @luwak end |
- (String) mapred
The URL path to the map-reduce HTTP endpoint
75 76 77 |
# File 'riak-client/lib/riak/client.rb', line 75 def mapred @mapred end |
- (Fixnum) pb_port
The Protocol Buffers port for the Riak endpoint
56 57 58 |
# File 'riak-client/lib/riak/client.rb', line 56 def pb_port @pb_port end |
- (String) prefix
The URL path prefix to the “raw” HTTP endpoint
72 73 74 |
# File 'riak-client/lib/riak/client.rb', line 72 def prefix @prefix end |
- (Symbol) protobuffs_backend
The Protocol Buffers backend/client to use
84 85 86 |
# File 'riak-client/lib/riak/client.rb', line 84 def protobuffs_backend @protobuffs_backend end |
- (String) protocol
The protocol to use for the Riak endpoint
47 48 49 |
# File 'riak-client/lib/riak/client.rb', line 47 def protocol @protocol end |
- (String) solr
The URL path prefix to the Solr HTTP endpoint
24 25 26 |
# File 'riak-client/lib/riak/search.rb', line 24 def solr @solr end |
- (Object) ssl=(value) (writeonly)
Enables or disables SSL on the client to be utilized by the HTTP Backends
69 70 71 72 |
# File 'riak-client/lib/riak/client.rb', line 69 def ssl=(value) @ssl_options = Hash === value ? value : {} value ? ssl_enable : ssl_disable end |
- (Hash|nil) ssl_options (readonly)
The SSL options that get built when using SSL
62 63 64 |
# File 'riak-client/lib/riak/client.rb', line 62 def @ssl_options end |
Instance Method Details
- (HTTPBackend, ProtobuffsBackend) backend
Returns a backend for operations that are protocol-independent. You can change which type of backend is used by setting the #protocol.
273 274 275 276 277 278 279 280 |
# File 'riak-client/lib/riak/client.rb', line 273 def backend @backend ||= case @protocol.to_s when /https?/i http when /pbc/i protobuffs end end |
- (Bucket) bucket(name, options = {}) Also known as: []
Retrieves a bucket from Riak.
294 295 296 297 298 299 300 301 302 303 |
# File 'riak-client/lib/riak/client.rb', line 294 def bucket(name, ={}) unless (.keys - [:keys, :props]).empty? raise ArgumentError, "invalid options" end @bucket_cache ||= {} (@bucket_cache[name] ||= Bucket.new(self, name)).tap do |b| b.props if [:props] b.keys if [:keys] end end |
- (Array<Bucket>) buckets Also known as: list_buckets
This is an expensive operation and should be used only in development.
Lists buckets which have keys stored in them.
310 311 312 |
# File 'riak-client/lib/riak/client.rb', line 310 def buckets backend.list_buckets.map {|name| Bucket.new(self, name) } end |
- (Object) delete_file(filename)
Deletes a file stored via the “Luwak” interface
368 369 370 371 |
# File 'riak-client/lib/riak/client.rb', line 368 def delete_file(filename) http.delete([204,404], luwak, escape(filename)) true end |
- (true, false) file_exists?(key) Also known as: file_exist?
Checks whether a file exists in “Luwak”.
376 377 378 379 |
# File 'riak-client/lib/riak/client.rb', line 376 def file_exists?(key) result = http.head([200,404], luwak, escape(key)) result[:code] == 200 end |
- (IO?) get_file(filename) {|chunk| ... }
Retrieves a large file/IO object from Riak via the “Luwak” interface. Streams the data to a temporary file unless a block is given.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'riak-client/lib/riak/client.rb', line 348 def get_file(filename, &block) if block_given? http.get(200, luwak, escape(filename), &block) nil else tmpfile = LuwakFile.new(escape(filename)) begin response = http.get(200, luwak, escape(filename)) do |chunk| tmpfile.write chunk end tmpfile.content_type = response[:headers]['content-type'].first tmpfile ensure tmpfile.close end end end |
- (HTTPBackend) http
Automatically detects and returns an appropriate HTTP backend. The HTTP backend is used internally by the Riak client, but can also be used to access the server directly.
241 242 243 244 245 246 247 248 249 250 |
# File 'riak-client/lib/riak/client.rb', line 241 def http @http ||= begin klass = self.class.const_get("#{@http_backend}Backend") if klass.configured? klass.new(self) else raise t('http_configuration', :backend => @http_backend) end end end |
- (Object) index(index, *docs) - (Object) index(*docs) Also known as: add_doc
Adds documents to a search index via the Solr interface.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'riak-client/lib/riak/search.rb', line 71 def index(*args) index = args.shift if String === args.first # Documents must be hashes of fields raise ArgumentError.new(t("search_docs_require_id")) unless args.all? {|d| d.key?("id") || d.key?(:id) } xml = Builder::XmlMarkup.new xml.add do args.each do |doc| xml.doc do doc.each do |k,v| xml.field('name' => k.to_s) { xml.text!(v.to_s) } end end end end path = [solr, index, "update", xml.target!, {'Content-Type' => 'text/xml'}].compact http.post(200, *path) true end |
- (String) inspect
A representation suitable for IRB and debugging output.
383 384 385 |
# File 'riak-client/lib/riak/client.rb', line 383 def inspect "#<Riak::Client #{protocol}://#{host}:#{protocol == 'pbc' ? pb_port : http_port}>" end |
- (true, false) ping
Pings the Riak server to check for liveness.
284 285 286 |
# File 'riak-client/lib/riak/client.rb', line 284 def ping backend.ping end |
- (Fixnum) port
Ports for HTTP(S) and Protocol Buffers are segregated. Use #http_port or #pb_port.
The port of the Riak endpoint
160 161 162 163 164 165 166 167 168 |
# File 'riak-client/lib/riak/client.rb', line 160 def port warn(t('deprecated.port', :backtrace => caller.join("\n"))) case protocol when /http/i http_port when /pbc/i pb_port end end |
- (Fixnum) port=(value)
Ports for HTTP(S) and Protocol Buffers are segregated. Use #http_port= or #pb_port=.
Set the port number of the Riak endpoint. This must be an integer between 0 and 65535.
177 178 179 180 181 182 183 184 185 186 |
# File 'riak-client/lib/riak/client.rb', line 177 def port=(value) warn(t('deprecated.port', :backtrace => caller[0..2].join("\n "))) raise ArgumentError, t("port_invalid") unless (0..65535).include?(value) case protocol when /http/i self.http_port = value when /pbc/i self.pb_port = value end end |
- (ProtobuffsBackend) protobuffs
Automatically detects and returns an appropriate Protocol Buffers backend. The Protocol Buffers backend is used internally by the Riak client but can also be used to access the server directly.
258 259 260 261 262 263 264 265 266 267 |
# File 'riak-client/lib/riak/client.rb', line 258 def protobuffs @protobuffs ||= begin klass = self.class.const_get("#{@protobuffs_backend}ProtobuffsBackend") if klass.configured? klass.new(self) else raise t('protobuffs_configuration', :backend => @protobuffs_backend) end end end |
- (Object) remove(index, specs) - (Object) remove(specs) Also known as: delete_doc, deindex
Removes documents from a search index via the Solr interface.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'riak-client/lib/riak/search.rb', line 99 def remove(*args) index = args.shift if String === args.first raise ArgumentError.new(t("search_remove_requires_id_or_query")) unless args.all? { |s| s.include? :id or s.include? 'id' or s.include? :query or s.include? 'query' } xml = Builder::XmlMarkup.new xml.delete do args.each do |spec| spec.each do |k,v| xml.tag!(k.to_sym, v) end end end path = [solr, index, "update", xml.target!, {'Content-Type' => 'text/xml'}].compact http.post(200, *path) true end |
- (Hash) search(index, query, options = {}) - (Hash) search(query, options = {}) Also known as: select
Performs a search via the Solr interface.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'riak-client/lib/riak/search.rb', line 49 def search(*args) = args. index, query = args[-2], args[-1] # Allows nil index, while keeping it as first argument path = [solr, index, "select", {"q" => query, "wt" => "json"}.merge(.stringify_keys), {}].compact response = http.get(200, *path) if response[:headers]['content-type'].include?("application/json") JSON.parse(response[:body]) else response[:body] end end |
- (Boolean) ssl_enabled?
Checks if SSL is enabled for HTTP
233 234 235 |
# File 'riak-client/lib/riak/client.rb', line 233 def ssl_enabled? protocol == 'https' || @ssl_options.present? end |
- (String) store_file(filename, content_type, data) - (String) store_file(content_type, data)
Stores a large file/IO-like object in Riak via the “Luwak” interface.
326 327 328 329 330 331 332 333 334 335 |
# File 'riak-client/lib/riak/client.rb', line 326 def store_file(*args) data, content_type, filename = args.reverse if filename http.put(204, luwak, escape(filename), data, {"Content-Type" => content_type}) filename else response = http.post(201, luwak, data, {"Content-Type" => content_type}) response[:headers]["location"].first.split("/").last end end |