Class: Riak::RObject

Inherits:
Object show all
Extended by:
Forwardable, Util::Escape, Util::Translation
Includes:
Util::Escape, Util::Translation
Defined in:
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, maybe_escape, maybe_unescape, unescape

Methods included from Util::Translation

i18n_scope, t

Constructor Details

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

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:



97
98
99
100
101
# File 'lib/riak/robject.rb', line 97

def initialize(bucket, key=nil)
  @bucket, @key = bucket, key
  @siblings = [ RContent.new(self) ]
  yield self if block_given?
end

Instance Attribute Details

#bucketBucket

Returns the bucket in which this object is contained

Returns:

  • (Bucket)

    the bucket in which this object is contained



21
22
23
# File 'lib/riak/robject.rb', line 21

def bucket
  @bucket
end

#keyString

Returns the key of this object within its bucket

Returns:

  • (String)

    the key of this object within its bucket



24
25
26
# File 'lib/riak/robject.rb', line 24

def key
  @key
end

#prevent_stale_writesBoolean

Returns 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:



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

def prevent_stale_writes
  @prevent_stale_writes
end

#siblingsArray<RContent>

Returns sibling values. If the object is not in conflict, then only one value will be present in the array.

Returns:

  • (Array<RContent>)

    an array of conflicting sibling values for this key, possibly containing only one



165
166
167
# File 'lib/riak/robject.rb', line 165

def siblings
  @siblings
end

#vclockString Also known as: vector_clock

Returns the Riak vector clock for the object

Returns:

  • (String)

    the Riak vector clock for the object



27
28
29
# File 'lib/riak/robject.rb', line 27

def vclock
  @vclock
end

Class Method Details

.load_from_mapreduce(client, response) ⇒ Array<RObject>

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:



86
87
88
89
90
# File 'lib/riak/robject.rb', line 86

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

.on_conflict {|robject| ... } ⇒ Object

Note:

Ripple registers its own document-level conflict handler, so if you're using ripple, you will probably want to use that instead.

Defines a callback to be invoked when there is conflict.

Yields:

  • The conflict callback.

Yield Parameters:

  • robject (RObject)

    The conflicted RObject

Yield Returns:

  • (RObject, nil)

    Either the resolved RObject or nil if your callback cannot resolve it. The next registered callback will be given the chance to resolve it.



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

def self.on_conflict(&conflict_hook)
  on_conflict_hooks << conflict_hook
end

.on_conflict_hooksArray<Proc>

Returns the list of registered conflict callbacks.

Returns:

  • (Array<Proc>)

    the list of registered conflict callbacks.



52
53
54
# File 'lib/riak/robject.rb', line 52

def self.on_conflict_hooks
  @on_conflict_hooks ||= []
end

Instance Method Details

#attempt_conflict_resolutionRObject

Note:

There is no guarantee the returned RObject will have been resolved

Attempts to resolve conflict using the registered conflict callbacks.

Returns:



70
71
72
73
74
75
76
77
78
79
# File 'lib/riak/robject.rb', line 70

def attempt_conflict_resolution
  return self unless conflict?

  self.class.on_conflict_hooks.each do |hook|
    result = hook.call(self)
    return result if result.is_a?(RObject)
  end

  self
end

#conflict?true, false

Returns Whether this object has conflicting sibling objects (divergent vclocks)

Returns:

  • (true, false)

    Whether this object has conflicting sibling objects (divergent vclocks)



176
177
178
# File 'lib/riak/robject.rb', line 176

def conflict?
  @siblings.size != 1
end

#contentRContent

Returns the solitary sibling when not in conflict.

Returns:

  • (RContent)

    the sole value/sibling on this object

Raises:

  • (Conflict)

    when multiple siblings are present



170
171
172
173
# File 'lib/riak/robject.rb', line 170

def content
  raise Conflict, self if conflict?
  @siblings.first
end

#delete(options = {}) ⇒ Object

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

See Also:



154
155
156
157
158
159
# File 'lib/riak/robject.rb', line 154

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

#inspectString

Returns A representation suitable for IRB and debugging output

Returns:

  • (String)

    A representation suitable for IRB and debugging output



181
182
183
184
# File 'lib/riak/robject.rb', line 181

def inspect
  body = @siblings.map {|s| s.inspect }.join(", ")
  "#<#{self.class.name} {#{bucket.name}#{"," + @key if @key}} [#{body}]>"
end

#load_from_mapreduce(response) ⇒ RObject

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:



108
109
110
111
112
113
114
115
116
# File 'lib/riak/robject.rb', line 108

def load_from_mapreduce(response)
  self.vclock = response['vclock']
  @siblings = response['values'].map do |v|
    RContent.new(self) do |rcontent|
      rcontent.load_map_reduce_value(v)
    end
  end
  self
end

#reload(options = {}) ⇒ Riak::RObject 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:



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

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

#store(options = {}) ⇒ Riak::RObject

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

  • (Conflict)

    if the object has siblings



127
128
129
130
131
132
133
# File 'lib/riak/robject.rb', line 127

def store(options={})
  raise Conflict, self if conflict?
  raise ArgumentError, t("content_type_undefined") unless content_type.present?
  raise ArgumentError, t("zero_length_key") if key == ''
  @bucket.client.store_object(self, options)
  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



189
190
191
# File 'lib/riak/robject.rb', line 189

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