Class: Riak::RObject

Inherits:
Object show all
Extended by:
Util::Escape
Includes:
Util::Escape, Util::Translation
Defined in:
riak-client/lib/riak/robject.rb

Overview

Represents the data and metadata stored in a bucket/key pair in the Riak database, the base unit of data manipulation.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Util::Escape

escape, unescape

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

- (RObject) initialize(bucket, key = nil) { ... }

Create a new object manually

Parameters:

  • bucket (Bucket)

    the bucket in which the object exists

  • key (String) (defaults to: nil)

    the key at which the object resides. If nil, a key will be assigned when the object is saved.

Yields:

  • self the new RObject

See Also:



75
76
77
78
79
# File 'riak-client/lib/riak/robject.rb', line 75

def initialize(bucket, key=nil)
  @bucket, @key = bucket, key
  @links, @meta = Set.new, {}
  yield self if block_given?
end

Instance Attribute Details

- (Bucket) bucket

The bucket in which this object is contained

Returns:

  • (Bucket)

    the bucket in which this object is contained



32
33
34
# File 'riak-client/lib/riak/robject.rb', line 32

def bucket
  @bucket
end

- (Object) conflict=(value) (writeonly)

Sets the attribute conflict

Parameters:

  • value

    the value to set the attribute conflict to.



174
175
176
# File 'riak-client/lib/riak/robject.rb', line 174

def conflict=(value)
  @conflict = value
end

- (String) content_type

The MIME content type of the object

Returns:

  • (String)

    the MIME content type of the object



38
39
40
# File 'riak-client/lib/riak/robject.rb', line 38

def content_type
  @content_type
end

- (String) etag

The ETag header from the most recent HTTP response, useful for caching and reloading

Returns:

  • (String)

    the ETag header from the most recent HTTP response, useful for caching and reloading



47
48
49
# File 'riak-client/lib/riak/robject.rb', line 47

def etag
  @etag
end

- (String) key

The key of this object within its bucket

Returns:

  • (String)

    the key of this object within its bucket



35
36
37
# File 'riak-client/lib/riak/robject.rb', line 35

def key
  @key
end

- (Time) last_modified

The Last-Modified header from the most recent HTTP response, useful for caching and reloading

Returns:

  • (Time)

    the Last-Modified header from the most recent HTTP response, useful for caching and reloading



50
51
52
# File 'riak-client/lib/riak/robject.rb', line 50

def last_modified
  @last_modified
end

A Set of Link objects for relationships between this object and other resources

Returns:

  • (Set<Link>)

    a Set of Link objects for relationships between this object and other resources



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

def links
  @links
end

- (Hash) meta

A hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion

Returns:

  • (Hash)

    a hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion



53
54
55
# File 'riak-client/lib/riak/robject.rb', line 53

def meta
  @meta
end

- (Boolean) prevent_stale_writes

Whether to attempt to prevent stale writes using conditional PUT semantics, If-None-Match: * or If-Match: #etag

Returns:

  • (Boolean)

    whether to attempt to prevent stale writes using conditional PUT semantics, If-None-Match: * or If-Match: #etag

See Also:



57
58
59
# File 'riak-client/lib/riak/robject.rb', line 57

def prevent_stale_writes
  @prevent_stale_writes
end

- (Array<RObject>, self) siblings

Returns sibling objects when in conflict.

Returns:

  • (Array<RObject>)

    an array of conflicting sibling objects for this key

  • (self)

    this object when not in conflict



179
180
181
182
# File 'riak-client/lib/riak/robject.rb', line 179

def siblings
  return self unless conflict?
  @siblings
end

- (String) vclock Also known as: vector_clock

The Riak vector clock for the object

Returns:

  • (String)

    the Riak vector clock for the object



41
42
43
# File 'riak-client/lib/riak/robject.rb', line 41

def vclock
  @vclock
end

Class Method Details

+ (Array<RObject>) load_from_mapreduce(client, response)

Loads a list of RObjects that were emitted from a MapReduce query.

Parameters:

  • client (Client)

    A Riak::Client with which the results will be associated

  • response (Array<Hash>)

    A list of results a MapReduce job. Each entry should contain these keys: bucket, key, vclock, values

Returns:



64
65
66
67
68
# File 'riak-client/lib/riak/robject.rb', line 64

def self.load_from_mapreduce(client, response)
  response.map do |item|
    RObject.new(client[unescape(item['bucket'])], unescape(item['key'])).load_from_mapreduce(item)
  end
end

Instance Method Details

- (true, false) conflict?

Whether this object has conflicting sibling objects (divergent vclocks)

Returns:

  • (true, false)

    Whether this object has conflicting sibling objects (divergent vclocks)



185
186
187
# File 'riak-client/lib/riak/robject.rb', line 185

def conflict?
  @conflict.present?
end

- (Object) data

The unmarshaled form of #raw_data stored in riak at this object’s key

Returns:

  • (Object)

    the unmarshaled form of #raw_data stored in riak at this object’s key



104
105
106
107
108
109
110
# File 'riak-client/lib/riak/robject.rb', line 104

def data
  if @raw_data && !@data
    @data = deserialize(@raw_data)
    @raw_data = nil
  end
  @data
end

- (Object) data=(new_data)

The object stored

Parameters:

  • unmarshaled (Object)

    form of the data to be stored in riak. Object will be serialized using #serialize if a known content_type is used. Setting this overrides values stored with #raw_data=

Returns:

  • (Object)

    the object stored



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

def data=(new_data)
  @raw_data = nil
  @data = new_data
end

- (Object) delete(options = {})

Delete the object from Riak and freeze this instance. Will work whether or not the object actually exists in the Riak database.



168
169
170
171
172
# File 'riak-client/lib/riak/robject.rb', line 168

def delete(options={})
  return if key.blank?
  @bucket.delete(key, options)
  freeze
end

- (Object) deserialize(body)

Deserializes the internal object data from a Riak response. Differs based on the content-type. This method is called internally when loading the object. Automatically deserialized formats:

  • JSON (application/json)

  • YAML (text/yaml)

  • Marshal (application/x-ruby-marshal)

Parameters:

  • body (String)

    the serialized response body



219
220
221
222
223
224
225
226
227
228
229
230
# File 'riak-client/lib/riak/robject.rb', line 219

def deserialize(body)
  case @content_type
  when /json/
    JSON.parse(body, Riak.json_options)
  when /yaml/
    YAML.load(body)
  when "application/x-ruby-marshal"
    Marshal.load(body)
  else
    body
  end
end

- (String) inspect

A representation suitable for IRB and debugging output

Returns:

  • (String)

    A representation suitable for IRB and debugging output



233
234
235
236
237
238
239
240
# File 'riak-client/lib/riak/robject.rb', line 233

def inspect
  body = if @data || content_type =~ /json|yaml|marshal/
           data.inspect
         else
           @raw_data && "(#{@raw_data.size} bytes)"
         end
  "#<#{self.class.name} {#{bucket.name}#{"," + @key if @key}} [#{@content_type}]:#{body}>"
end

- (RObject) load_from_mapreduce(response)

Load object data from a map/reduce response item. This method is used by RObject::load_from_mapreduce to instantiate the necessary objects.

Parameters:

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'riak-client/lib/riak/robject.rb', line 86

def load_from_mapreduce(response)
  self.vclock = response['vclock']
  if response['values'].size == 1
    value = response['values'].first
    load_map_reduce_value(value)
  else
    @conflict = true
    @siblings = response['values'].map do |v|
      RObject.new(self.bucket, self.key) do |robj|
        robj.vclock = self.vclock
        robj.load_map_reduce_value(v)
      end
    end
  end
  self
end

- (String) raw_data

Raw data stored in riak for this object’s key

Returns:

  • (String)

    raw data stored in riak for this object’s key



120
121
122
123
124
125
126
# File 'riak-client/lib/riak/robject.rb', line 120

def raw_data
  if @data && !@raw_data
    @raw_data = serialize(@data)
    @data = nil
  end
  @raw_data
end

- (String) raw_data=(new_raw_data)

The data stored

Parameters:

  • the (String, IO-like)

    raw data to be stored in riak at this key, will not be marshaled or manipulated prior to storage. Overrides any data stored by #data=

Returns:

  • (String)

    the data stored



130
131
132
133
# File 'riak-client/lib/riak/robject.rb', line 130

def raw_data=(new_raw_data)
  @data = nil
  @raw_data = new_raw_data
end

- (Riak::RObject) reload(options = {}) Also known as: fetch

Reload the object from Riak. Will use conditional GETs when possible.

Parameters:

  • options (Hash) (defaults to: {})

    query parameters

Options Hash (options):

  • :r (Fixnum)

    the “r” parameter (Read quorum)

  • :force (Boolean)

    will force a reload request if the vclock is not present, useful for reloading the object after a store (not passed in the query params)

Returns:



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

def reload(options={})
  force = options.delete(:force)
  return self unless @key && (@vclock || force)
  self.etag = self.last_modified = nil if force
  bucket.client.backend.reload_object(self, options[:r])
end

- (Object) serialize(payload)

Serializes the internal object data for sending to Riak. Differs based on the content-type. This method is called internally when storing the object. Automatically serialized formats:

  • JSON (application/json)

  • YAML (text/yaml)

  • Marshal (application/x-ruby-marshal)

When given an IO-like object (e.g. File), no serialization will be done.

Parameters:

  • payload (Object)

    the data to serialize



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

def serialize(payload)
  return payload if payload.respond_to?(:read)
  case @content_type
  when /json/
    payload.to_json(Riak.json_options)
  when /yaml/
    YAML.dump(payload)
  when "application/x-ruby-marshal"
    Marshal.dump(payload)
  else
    payload.to_s
  end
end

- (Riak::RObject) store(options = {})

Store the object in Riak

Parameters:

  • options (Hash) (defaults to: {})

    query parameters

Options Hash (options):

  • :r (Fixnum)

    the “r” parameter (Read quorum for the implicit read performed when validating the store operation)

  • :w (Fixnum)

    the “w” parameter (Write quorum)

  • :dw (Fixnum)

    the “dw” parameter (Durable-write quorum)

  • :returnbody (Boolean) — default: true

    whether to return the result of a successful write in the body of the response. Set to false for fire-and-forget updates, set to true to immediately have access to the object’s stored representation.

Returns:

Raises:

  • (ArgumentError)

    if the content_type is not defined



143
144
145
146
147
148
# File 'riak-client/lib/riak/robject.rb', line 143

def store(options={})
  raise ArgumentError, t("content_type_undefined") unless @content_type.present?
  params = {:returnbody => true}.merge(options)
  @bucket.client.backend.store_object(self, params[:returnbody], params[:w], params[:dw])
  self
end

Converts the object to a link suitable for linking other objects to it

Parameters:

  • tag (String)

    the tag to apply to the link



252
253
254
# File 'riak-client/lib/riak/robject.rb', line 252

def to_link(tag)
  Link.new(@bucket.name, @key, tag)
end

- (Object) url

Generates a URL representing the object according to the client, bucket and key. If the key is blank, the bucket URL will be returned (where the object will be submitted to when stored).



259
260
261
262
263
# File 'riak-client/lib/riak/robject.rb', line 259

def url
  segments = [ @bucket.client.prefix, escape(@bucket.name)]
  segments << escape(@key) if @key
  @bucket.client.http.path(*segments).to_s
end

- (Object) walk(*params)

Walks links from this object to other objects in Riak.

Parameters:



244
245
246
247
# File 'riak-client/lib/riak/robject.rb', line 244

def walk(*params)
  specs = WalkSpec.normalize(*params)
  @bucket.client.http.link_walk(self, specs)
end