Class: Riak::Client::HTTPBackend

Inherits:
Object
  • Object
show all
Includes:
Configuration, ObjectMethods, TransportMethods, Util::Escape, Util::Translation
Defined in:
riak-client/lib/riak/client/http_backend.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/http_backend/request_headers.rb,
riak-client/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

CurbBackend, ExconBackend, NetHTTPBackend

Defined Under Namespace

Modules: Configuration, ObjectMethods, TransportMethods

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

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_path!, #verify_path_and_body!

Methods included from Util::Translation

#i18n_scope, #t

Methods included from Util::Escape

#escape, #unescape

Constructor Details

- (HTTPBackend) initialize(client)

Create an HTTPBackend for the Riak::Client.

Parameters:

  • client (Client)

    the client

Raises:

  • (ArgumentError)


48
49
50
51
# File 'riak-client/lib/riak/client/http_backend.rb', line 48

def initialize(client)
  raise ArgumentError, t("client_type", :client => client) unless Client === client
  @client = client
end

Instance Attribute Details

- (Object) client (readonly)

The Riak::Client that uses this backend



44
45
46
# File 'riak-client/lib/riak/client/http_backend.rb', line 44

def client
  @client
end

Instance Method Details

- (Object) delete_object(bucket, key, rw = nil)

Deletes an object

Parameters:

  • bucket (Bucket, String)

    the bucket where the object lives

  • key (String)

    the key where the object lives

  • rw (Fixnum, String, Symbol) (defaults to: nil)

    the read/write quorum for the request



114
115
116
117
118
# File 'riak-client/lib/riak/client/http_backend.rb', line 114

def delete_object(bucket, key, rw=nil)
  bucket = bucket.name if Bucket === bucket
  options = rw ? {:rw => rw} : {}
  delete([204, 404], riak_kv_wm_raw, escape(bucket), escape(key), options, {})
end

- (RObject) fetch_object(bucket, key, r = nil)

Fetches an object by bucket/key

Parameters:

  • bucket (Bucket, String)

    the bucket where the object is stored

  • key (String)

    the key of the object

  • r (Fixnum, String, Symbol) (defaults to: nil)

    the read quorum for the request - how many nodes should concur on the read

Returns:



69
70
71
72
73
74
# File 'riak-client/lib/riak/client/http_backend.rb', line 69

def fetch_object(bucket, key, r=nil)
  bucket = Bucket.new(client, bucket) if String === bucket
  options = r ? {:r => r} : {}
  response = get([200,300],riak_kv_wm_raw, escape(bucket.name), escape(key), options, {})
  load_object(RObject.new(bucket, key), response)
end

- (Hash) get_bucket_props(bucket)

Fetches bucket properties

Parameters:

  • bucket (Bucket, String)

    the bucket properties to fetch

Returns:

  • (Hash)

    bucket properties



123
124
125
126
127
# File 'riak-client/lib/riak/client/http_backend.rb', line 123

def get_bucket_props(bucket)
  bucket = bucket.name if Bucket === bucket
  response = get(200, riak_kv_wm_raw, escape(bucket), {:keys => false, :props => true}, {})
  JSON.parse(response[:body])['props']
end

Performs a link-walking query

Parameters:

  • robject (RObject)

    the object to start at

  • walk_specs (Array<WalkSpec>)

    a list of walk specifications to process

Returns:



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'riak-client/lib/riak/client/http_backend.rb', line 199

def link_walk(robject, walk_specs)
  response = get(200, riak_kv_wm_link_walker, escape(robject.bucket.name), escape(robject.key), walk_specs.join("/"))
  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[:body] && obj[:headers]['location']
          bucket = $1 if obj[:headers]['location'].first =~ %r{/.*/(.*)/.*$}
          load_object(RObject.new(client.bucket(bucket), nil), obj)
        end
      end
    end
  else
    []
  end
end

- (Array<String>) list_buckets

Lists known buckets

Returns:



157
158
159
160
# File 'riak-client/lib/riak/client/http_backend.rb', line 157

def list_buckets
  response = get(200, riak_kv_wm_raw, {:buckets => true}, {})
  JSON.parse(response[:body])['buckets']
end

- (Array<String>) list_keys(bucket) {|Array<String>| ... }

List keys in a bucket

Parameters:

  • bucket (Bucket, String)

    the bucket to fetch the keys for

Yields:

  • (Array<String>)

    a list of keys from the current streamed chunk

Returns:

  • (Array<String>)

    the list of keys, if no block was given



144
145
146
147
148
149
150
151
152
153
# File 'riak-client/lib/riak/client/http_backend.rb', line 144

def list_keys(bucket, &block)
  bucket = bucket.name if Bucket === bucket
  if block_given?          
    get(200, riak_kv_wm_raw, escape(bucket), {:props => false, :keys => 'stream'}, {}, &KeyStreamer.new(block))
  else
    response = get(200, riak_kv_wm_raw, escape(bucket), {:props => false, :keys => true}, {})
    obj = JSON.parse(response[:body])
    obj && obj['keys'].map {|k| unescape(k) }
  end
end

- (Array<Object>) mapred(mr) {|Fixnum, Object| ... }

Performs a MapReduce query.

Parameters:

Yields:

  • (Fixnum, Object)

    the phase number and single result from the phase

Returns:

  • (Array<Object>)

    the list of results, if no block was given



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'riak-client/lib/riak/client/http_backend.rb', line 168

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, riak_kv_wm_mapred, {:chunked => true}, mr.to_json, {"Content-Type" => "application/json", "Accept" => "application/json"}, &parser)
    nil
  else
    response = post(200, riak_kv_wm_mapred, 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

Returns:

  • (true, false)

    whether the server is available



55
56
57
58
59
60
# File 'riak-client/lib/riak/client/http_backend.rb', line 55

def ping
  get(200, riak_kv_wm_ping, {}, {})
  true
rescue
  false
end

- (Object) reload_object(robject, r = nil)

Reloads the data for a given RObject, a special case of #fetch_object.



77
78
79
80
81
82
83
84
85
# File 'riak-client/lib/riak/client/http_backend.rb', line 77

def reload_object(robject, r = nil)
  options = r ? {:r => r} : {}
  response = get([200,300,304], riak_kv_wm_raw, escape(robject.bucket.name), escape(robject.key), options, reload_headers(robject))
  if response[:code].to_i == 304
    robject
  else
    load_object(robject, response)
  end
end

- (Object) set_bucket_props(bucket, props)

Sets bucket properties

Parameters:

  • bucket (Bucket, String)

    the bucket to set properties on

  • properties (Hash)

    the properties to set



132
133
134
135
136
# File 'riak-client/lib/riak/client/http_backend.rb', line 132

def set_bucket_props(bucket, props)
  bucket = bucket.name if Bucket === bucket
  body = {'props' => props}.to_json
  put(204, riak_kv_wm_raw, escape(bucket), body, {"Content-Type" => "application/json"})
end

- (Hash) stats

Gets health statistics

Returns:

  • (Hash)

    information about the server, including stats



188
189
190
191
# File 'riak-client/lib/riak/client/http_backend.rb', line 188

def stats
  response = get(200, riak_kv_wm_stats, {}, {})
  JSON.parse(response[:body])
end

- (Object) store_object(robject, returnbody = false, w = nil, dw = nil)

Stores an object

Parameters:

  • robject (RObject)

    the object to store

  • returnbody (true, false) (defaults to: false)

    (false) whether to update the object after write with the new value

  • w (Fixnum, String, Symbol) (defaults to: nil)

    the write quorum

  • dw (Fixnum, String, Symbol) (defaults to: nil)

    the durable write quorum



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'riak-client/lib/riak/client/http_backend.rb', line 93

def store_object(robject, returnbody=false, w=nil, dw=nil)
  query = {}.tap do |q|
    q[:returnbody] = returnbody unless returnbody.nil?
    q[:w] = w unless w.nil?
    q[:dw] = dw unless dw.nil?
  end
  method, codes, path = if robject.key.present?
                          [:put, [200,204,300], "#{escape(robject.bucket.name)}/#{escape(robject.key)}"]
                        else
                          [:post, 201, escape(robject.bucket.name)]
                        end
  response = send(method, codes, riak_kv_wm_raw, path, query, robject.raw_data, store_headers(robject))
  load_object(robject, response) if returnbody
end