Class: Riak::RObject
- 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
-
#bucket ⇒ Bucket
The bucket in which this object is contained.
-
#key ⇒ String
The key of this object within its bucket.
-
#prevent_stale_writes ⇒ Boolean
Whether to attempt to prevent stale writes using conditional PUT semantics, If-None-Match: * or If-Match: etag.
-
#siblings ⇒ Array<RContent>
Returns sibling values.
-
#vclock ⇒ String
(also: #causal_context, #vector_clock)
The Riak causal context/vector clock for the object.
Class Method Summary collapse
-
.load_from_mapreduce(client, response) ⇒ Array<RObject>
Loads a list of RObjects that were emitted from a MapReduce query.
-
.on_conflict {|robject| ... } ⇒ Object
Defines a callback to be invoked when there is conflict.
-
.on_conflict_hooks ⇒ Array<Proc>
The list of registered conflict callbacks.
Instance Method Summary collapse
-
#attempt_conflict_resolution ⇒ RObject
Attempts to resolve conflict using the registered conflict callbacks.
-
#conflict? ⇒ true, false
Whether this object has conflicting sibling objects (divergent vclocks).
-
#content ⇒ RContent
Returns the solitary sibling when not in conflict.
-
#delete(options = {}) ⇒ Object
Delete the object from Riak and freeze this instance.
-
#initialize(bucket, key = nil) { ... } ⇒ RObject
constructor
Create a new object manually.
-
#inspect ⇒ String
A representation suitable for IRB and debugging output.
-
#load_from_mapreduce(response) ⇒ RObject
Load object data from a map/reduce response item.
-
#preflist(options = {}) ⇒ Array<PreflistItem>
Retrieves a preflist for this RObject; useful for figuring out where in the cluster it is stored.
-
#reload(options = {}) ⇒ Riak::RObject
(also: #fetch)
Reload the object from Riak.
-
#store(options = {}) ⇒ Riak::RObject
Store the object in Riak.
-
#to_link(tag) ⇒ Object
Converts the object to a link suitable for linking other objects to it.
Methods included from Util::Translation
Methods included from Util::Escape
escape, maybe_escape, maybe_unescape, unescape
Constructor Details
#initialize(bucket, key = nil) { ... } ⇒ RObject
Create a new object manually
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/riak/robject.rb', line 99 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
#bucket ⇒ Bucket
Returns the bucket in which this object is contained.
21 22 23 |
# File 'lib/riak/robject.rb', line 21 def bucket @bucket end |
#key ⇒ String
Returns the key of this object within its bucket.
24 25 26 |
# File 'lib/riak/robject.rb', line 24 def key @key end |
#prevent_stale_writes ⇒ Boolean
Returns whether to attempt to prevent stale writes using conditional PUT semantics, If-None-Match: * or If-Match: etag.
37 38 39 |
# File 'lib/riak/robject.rb', line 37 def prevent_stale_writes @prevent_stale_writes end |
#siblings ⇒ Array<RContent>
Returns sibling values. If the object is not in conflict, then only one value will be present in the array.
178 179 180 |
# File 'lib/riak/robject.rb', line 178 def siblings @siblings end |
#vclock ⇒ String Also known as: causal_context, vector_clock
Returns the Riak causal context/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.
88 89 90 91 92 |
# File 'lib/riak/robject.rb', line 88 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
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.
49 50 51 |
# File 'lib/riak/robject.rb', line 49 def self.on_conflict(&conflict_hook) on_conflict_hooks << conflict_hook end |
.on_conflict_hooks ⇒ Array<Proc>
Returns the list of registered conflict callbacks.
54 55 56 |
# File 'lib/riak/robject.rb', line 54 def self.on_conflict_hooks @on_conflict_hooks ||= [] end |
Instance Method Details
#attempt_conflict_resolution ⇒ RObject
There is no guarantee the returned RObject will have been resolved
Attempts to resolve conflict using the registered conflict callbacks.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/riak/robject.rb', line 72 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).
189 190 191 |
# File 'lib/riak/robject.rb', line 189 def conflict? @siblings.size != 1 end |
#content ⇒ RContent
Returns the solitary sibling when not in conflict.
183 184 185 186 |
# File 'lib/riak/robject.rb', line 183 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.
167 168 169 170 171 172 |
# File 'lib/riak/robject.rb', line 167 def delete( = {}) return if key.blank? [:vclock] = vclock if vclock @bucket.delete(key, default()) freeze end |
#inspect ⇒ String
Returns A representation suitable for IRB and debugging output.
194 195 196 197 |
# File 'lib/riak/robject.rb', line 194 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.
117 118 119 120 121 122 123 124 125 |
# File 'lib/riak/robject.rb', line 117 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 |
#preflist(options = {}) ⇒ Array<PreflistItem>
Retrieves a preflist for this RObject; useful for figuring out where in the cluster it is stored.
209 210 211 |
# File 'lib/riak/robject.rb', line 209 def preflist( = {}) bucket.get_preflist key, end |
#reload(options = {}) ⇒ Riak::RObject Also known as: fetch
Reload the object from Riak. Will use conditional GETs when possible.
155 156 157 158 159 160 |
# File 'lib/riak/robject.rb', line 155 def reload( = {}) force = .delete(:force) return self unless @key && (@vclock || force) self.etag = self.last_modified = nil if force bucket.client.reload_object(self, default()) end |
#store(options = {}) ⇒ Riak::RObject
Store the object in Riak
140 141 142 143 144 145 146 |
# File 'lib/riak/robject.rb', line 140 def store( = {}) fail Conflict, self if conflict? fail ArgumentError, t('content_type_undefined') unless content_type.present? fail ArgumentError, t('zero_length_key') if key == '' @bucket.client.store_object(self, default()) self end |