Class: Riak::RObject
- Inherits:
-
Object
- Object
- 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.
-
- (String) key
The key of this object within its bucket.
-
- (Boolean) prevent_stale_writes
Whether to attempt to prevent stale writes using conditional PUT semantics, If-None-Match: * or If-Match: #etag.
-
- (Array<RContent>) siblings
Returns sibling values.
-
- (String) vclock
(also: #vector_clock)
The Riak vector clock for the object.
Class Method Summary (collapse)
-
+ (Array<RObject>) load_from_mapreduce(client, response)
Loads a list of RObjects that were emitted from a MapReduce query.
-
+ (Object) on_conflict {|robject| ... }
Defines a callback to be invoked when there is conflict.
-
+ (Array<Proc>) on_conflict_hooks
The list of registered conflict callbacks.
Instance Method Summary (collapse)
-
- (RObject) attempt_conflict_resolution
Attempts to resolve conflict using the registered conflict callbacks.
-
- (true, false) conflict?
Whether this object has conflicting sibling objects (divergent vclocks).
-
- (RContent) content
Returns the solitary sibling when not in conflict.
-
- (Object) delete(options = {})
Delete the object from Riak and freeze this instance.
-
- (RObject) initialize(bucket, key = nil) { ... }
constructor
Create a new object manually.
-
- (String) inspect
A representation suitable for IRB and debugging output.
-
- (RObject) load_from_mapreduce(response)
Load object data from a map/reduce response item.
-
- (Riak::RObject) reload(options = {})
(also: #fetch)
Reload the object from Riak.
-
- (Riak::RObject) store(options = {})
Store the object in Riak.
-
- (Object) to_link(tag)
Converts the object to a link suitable for linking other objects to it.
-
- (Object) walk(*params)
Walks links from this object to other objects in Riak.
Methods included from Util::Escape
escape, maybe_escape, maybe_unescape, unescape
Methods included from Util::Translation
Constructor Details
- (RObject) initialize(bucket, key = nil) { ... }
Create a new object manually
96 97 98 99 100 |
# File 'lib/riak/robject.rb', line 96 def initialize(bucket, key=nil) @bucket, @key = bucket, key @siblings = [ RContent.new(self) ] yield self if block_given? end |
Instance Attribute Details
- (Bucket) bucket
The bucket in which this object is contained
21 22 23 |
# File 'lib/riak/robject.rb', line 21 def bucket @bucket end |
- (String) key
The key of this object within its bucket
24 25 26 |
# File 'lib/riak/robject.rb', line 24 def key @key end |
- (Boolean) prevent_stale_writes
Whether to attempt to prevent stale writes using conditional PUT semantics, If-None-Match: * or If-Match: #etag
34 35 36 |
# File 'lib/riak/robject.rb', line 34 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.
163 164 165 |
# File 'lib/riak/robject.rb', line 163 def siblings @siblings end |
- (String) vclock Also known as: vector_clock
The Riak vector clock for the object
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.
85 86 87 88 89 |
# File 'lib/riak/robject.rb', line 85 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| ... }
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.
46 47 48 |
# File 'lib/riak/robject.rb', line 46 def self.on_conflict(&conflict_hook) on_conflict_hooks << conflict_hook end |
+ (Array<Proc>) on_conflict_hooks
The list of registered conflict callbacks.
51 52 53 |
# File 'lib/riak/robject.rb', line 51 def self.on_conflict_hooks @on_conflict_hooks ||= [] end |
Instance Method Details
- (RObject) attempt_conflict_resolution
There is no guarantee the returned RObject will have been resolved
Attempts to resolve conflict using the registered conflict callbacks.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/riak/robject.rb', line 69 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?
Whether this object has conflicting sibling objects (divergent vclocks)
174 175 176 |
# File 'lib/riak/robject.rb', line 174 def conflict? @siblings.size != 1 end |
- (RContent) content
Returns the solitary sibling when not in conflict.
168 169 170 171 |
# File 'lib/riak/robject.rb', line 168 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.
152 153 154 155 156 157 |
# File 'lib/riak/robject.rb', line 152 def delete(={}) return if key.blank? [:vclock] = vclock if vclock @bucket.delete(key, ) freeze end |
- (String) inspect
A representation suitable for IRB and debugging output
179 180 181 182 |
# File 'lib/riak/robject.rb', line 179 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.
107 108 109 110 111 112 113 114 115 |
# File 'lib/riak/robject.rb', line 107 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.
140 141 142 143 144 145 |
# File 'lib/riak/robject.rb', line 140 def reload(={}) force = .delete(:force) return self unless @key && (@vclock || force) self.etag = self.last_modified = nil if force bucket.client.reload_object(self, ) end |
- (Riak::RObject) store(options = {})
Store the object in Riak
126 127 128 129 130 131 |
# File 'lib/riak/robject.rb', line 126 def store(={}) raise Conflict, self if conflict? raise ArgumentError, t("content_type_undefined") unless content_type.present? @bucket.client.store_object(self, ) self end |
- (Object) to_link(tag)
Converts the object to a link suitable for linking other objects to it
194 195 196 |
# File 'lib/riak/robject.rb', line 194 def to_link(tag) Link.new(@bucket.name, @key, tag) end |
- (Object) walk(*params)
Walks links from this object to other objects in Riak.
186 187 188 189 |
# File 'lib/riak/robject.rb', line 186 def walk(*params) specs = WalkSpec.normalize(*params) @bucket.client.link_walk(self, specs) end |