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

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

Create a new object manually

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

- (Bucket) bucket



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

def bucket
  @bucket
end

- (String) key



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

def key
  @key
end

- (Boolean) prevent_stale_writes

Returns 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

- (Array<RContent>) siblings

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



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

def siblings
  @siblings
end

- (String) vclock Also known as: vector_clock



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

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.



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

+ (Object) on_conflict {|robject| ... }

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

+ (Array<Proc>) on_conflict_hooks



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

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

Instance Method Details

- (RObject) attempt_conflict_resolution

Note:

There is no guarantee the returned RObject will have been resolved

Attempts to resolve conflict using the registered conflict callbacks.



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

- (true, false) conflict?



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

def conflict?
  @siblings.size != 1
end

- (RContent) content

Returns the solitary sibling when not in conflict.

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

- (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.

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

- (String) inspect



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

- (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.



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

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

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

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)



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

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

Store the object in Riak

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.

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



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

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