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

Yields:

  • self the new RObject

See Also:



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/riak/robject.rb', line 97

def initialize(bucket, key=nil)
  @bucket, @key = bucket, key

  # fix a require-loop
  require 'riak/bucket_typed/bucket'

  if @bucket.is_a? BucketTyped::Bucket
    @type = @bucket.type.name
  end
  @siblings = [ RContent.new(self) ]
  yield self if block_given?
end

Instance Attribute Details

#bucketBucket



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

def bucket
  @bucket
end

#keyString



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

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.



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

def siblings
  @siblings
end

#vclockString Also known as: vector_clock



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.



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>



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.



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



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

def conflict?
  @siblings.size != 1
end

#contentRContent

Returns the solitary sibling when not in conflict.

Raises:

  • (Conflict)

    when multiple siblings are present



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

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:



161
162
163
164
165
166
# File 'lib/riak/robject.rb', line 161

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

#inspectString



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

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.



115
116
117
118
119
120
121
122
123
# File 'lib/riak/robject.rb', line 115

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.

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)



149
150
151
152
153
154
# File 'lib/riak/robject.rb', line 149

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, default(options))
end

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

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



134
135
136
137
138
139
140
# File 'lib/riak/robject.rb', line 134

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, default(options))
  self
end

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



196
197
198
# File 'lib/riak/robject.rb', line 196

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