Class: Riak::RObject
- Inherits:
-
Object
- Object
- Riak::RObject
- Extended by:
- 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.
-
- (Object) conflict
writeonly
Sets the attribute conflict.
-
- (String) content_type
The MIME content type of the object.
-
- (String) etag
The ETag header from the most recent HTTP response, useful for caching and reloading.
-
- (Hash<Set>) indexes
A hash of secondary indexes, where the key is the index name and the value is a Set of index entries for that index.
-
- (String) key
The key of this object within its bucket.
-
- (Time) last_modified
The Last-Modified header from the most recent HTTP response, useful for caching and reloading.
-
- (Set<Link>) links
A Set of Link objects for relationships between this object and other resources.
-
- (Hash) meta
A hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion.
-
- (Boolean) prevent_stale_writes
Whether to attempt to prevent stale writes using conditional PUT semantics, If-None-Match: * or If-Match: #etag.
-
- (Array<RObject>, Array<self>) siblings
Returns sibling objects when in conflict.
-
- (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).
-
- (Object) data
The unmarshaled form of #raw_data stored in riak at this object's key.
-
- (Object) data=(new_data)
The object stored.
-
- (Object) delete(options = {})
Delete the object from Riak and freeze this instance.
-
- (Object) deserialize(body)
Deserializes the internal object data from a Riak response.
-
- (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.
-
- (String) raw_data
Raw data stored in riak for this object's key.
-
- (String) raw_data=(new_raw_data)
The data stored.
-
- (Riak::RObject) reload(options = {})
(also: #fetch)
Reload the object from Riak.
-
- (Object) serialize(payload)
Serializes the internal object data for sending to 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) url
Generates a URL representing the object according to the client, bucket and key.
-
- (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
103 104 105 106 107 108 |
# File 'lib/riak/robject.rb', line 103 def initialize(bucket, key=nil) @bucket, @key = bucket, key @links, @meta = Set.new, {} @indexes = Hash.new {|h,k| h[k] = Set.new } 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 |
- (Object) conflict=(value) (writeonly)
Sets the attribute conflict
209 210 211 |
# File 'lib/riak/robject.rb', line 209 def conflict=(value) @conflict = value end |
- (String) content_type
The MIME content type of the object
27 28 29 |
# File 'lib/riak/robject.rb', line 27 def content_type @content_type end |
- (String) etag
The ETag header from the most recent HTTP response, useful for caching and reloading
36 37 38 |
# File 'lib/riak/robject.rb', line 36 def etag @etag end |
- (Hash<Set>) indexes
A hash of secondary indexes, where the key is the index name and the value is a Set of index entries for that index
47 48 49 |
# File 'lib/riak/robject.rb', line 47 def indexes @indexes 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 |
- (Time) last_modified
The Last-Modified header from the most recent HTTP response, useful for caching and reloading
39 40 41 |
# File 'lib/riak/robject.rb', line 39 def last_modified @last_modified end |
- (Set<Link>) links
A Set of Link objects for relationships between this object and other resources
33 34 35 |
# File 'lib/riak/robject.rb', line 33 def links @links end |
- (Hash) meta
A hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion
42 43 44 |
# File 'lib/riak/robject.rb', line 42 def @meta end |
- (Boolean) prevent_stale_writes
Whether to attempt to prevent stale writes using conditional PUT semantics, If-None-Match: * or If-Match: #etag
51 52 53 |
# File 'lib/riak/robject.rb', line 51 def prevent_stale_writes @prevent_stale_writes end |
- (Array<RObject>, Array<self>) siblings
Returns sibling objects when in conflict. in conflict
215 216 217 218 |
# File 'lib/riak/robject.rb', line 215 def siblings return [self] unless conflict? @siblings end |
- (String) vclock Also known as: vector_clock
The Riak vector clock for the object
30 31 32 |
# File 'lib/riak/robject.rb', line 30 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.
92 93 94 95 96 |
# File 'lib/riak/robject.rb', line 92 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.
63 64 65 |
# File 'lib/riak/robject.rb', line 63 def self.on_conflict(&conflict_hook) on_conflict_hooks << conflict_hook end |
+ (Array<Proc>) on_conflict_hooks
The list of registered conflict callbacks.
68 69 70 |
# File 'lib/riak/robject.rb', line 68 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.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/riak/robject.rb', line 76 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)
221 222 223 |
# File 'lib/riak/robject.rb', line 221 def conflict? @conflict.present? end |
- (Object) data
The unmarshaled form of #raw_data stored in riak at this object's key
133 134 135 136 137 138 139 140 |
# File 'lib/riak/robject.rb', line 133 def data if @raw_data && !@data raw = @raw_data.respond_to?(:read) ? @raw_data.read : @raw_data @data = deserialize(raw) @raw_data = nil end @data end |
- (Object) data=(new_data)
The object stored
144 145 146 147 148 149 150 151 |
# File 'lib/riak/robject.rb', line 144 def data=(new_data) if new_data.respond_to?(:read) raise ArgumentError.new(t("invalid_io_object")) end @raw_data = nil @data = new_data 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.
202 203 204 205 206 207 |
# File 'lib/riak/robject.rb', line 202 def delete(={}) return if key.blank? [:vclock] = vclock if vclock @bucket.delete(key, ) freeze end |
- (Object) deserialize(body)
Deserializes the internal object data from a Riak response. Differs based on the content-type. This method is called internally when loading the object. Automatically deserialized formats:
-
JSON (application/json)
-
YAML (text/yaml)
-
Marshal (application/x-ruby-marshal)
245 246 247 |
# File 'lib/riak/robject.rb', line 245 def deserialize(body) Serializers.deserialize(@content_type, body) end |
- (String) inspect
A representation suitable for IRB and debugging output
250 251 252 253 254 255 256 257 |
# File 'lib/riak/robject.rb', line 250 def inspect body = if @data || Serializers[content_type] data.inspect else @raw_data && "(#{@raw_data.size} bytes)" end "#<#{self.class.name} {#{bucket.name}#{"," + @key if @key}} [#{@content_type}]:#{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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/riak/robject.rb', line 115 def load_from_mapreduce(response) self.vclock = response['vclock'] if response['values'].size == 1 value = response['values'].first load_map_reduce_value(value) else @conflict = true @siblings = response['values'].map do |v| RObject.new(self.bucket, self.key) do |robj| robj.vclock = self.vclock robj.load_map_reduce_value(v) end end end self end |
- (String) raw_data
Raw data stored in riak for this object's key
154 155 156 157 158 159 160 |
# File 'lib/riak/robject.rb', line 154 def raw_data if @data && !@raw_data @raw_data = serialize(@data) @data = nil end @raw_data end |
- (String) raw_data=(new_raw_data)
The data stored
164 165 166 167 |
# File 'lib/riak/robject.rb', line 164 def raw_data=(new_raw_data) @data = nil @raw_data = new_raw_data end |
- (Riak::RObject) reload(options = {}) Also known as: fetch
Reload the object from Riak. Will use conditional GETs when possible.
190 191 192 193 194 195 |
# File 'lib/riak/robject.rb', line 190 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 |
- (Object) serialize(payload)
Serializes the internal object data for sending to Riak. Differs based on the content-type. This method is called internally when storing the object. Automatically serialized formats:
-
JSON (application/json)
-
YAML (text/yaml)
-
Marshal (application/x-ruby-marshal)
When given an IO-like object (e.g. File), no serialization will be done.
234 235 236 |
# File 'lib/riak/robject.rb', line 234 def serialize(payload) Serializers.serialize(@content_type, payload) end |
- (Riak::RObject) store(options = {})
Store the object in Riak
177 178 179 180 181 |
# File 'lib/riak/robject.rb', line 177 def store(={}) 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
269 270 271 |
# File 'lib/riak/robject.rb', line 269 def to_link(tag) Link.new(@bucket.name, @key, tag) end |
- (Object) url
Generates a URL representing the object according to the client, bucket and key. If the key is blank, the bucket URL will be returned (where the object will be submitted to when stored).
276 277 278 279 280 |
# File 'lib/riak/robject.rb', line 276 def url segments = [ @bucket.client.http_paths[:prefix], escape(@bucket.name)] segments << escape(@key) if @key @bucket.client.http.path(*segments).to_s end |
- (Object) walk(*params)
Walks links from this object to other objects in Riak.
261 262 263 264 |
# File 'lib/riak/robject.rb', line 261 def walk(*params) specs = WalkSpec.normalize(*params) @bucket.client.link_walk(self, specs) end |