Class: Riak::RObject
- Inherits:
-
Object
- Object
- Riak::RObject
- Extended by:
- Util::Escape
- Includes:
- Util::Escape, Util::Translation
- Defined in:
- riak-client/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.
-
- (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>, 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.
Instance Method Summary (collapse)
-
- (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
Methods included from Util::Translation
Constructor Details
- (RObject) initialize(bucket, key = nil) { ... }
Create a new object manually
75 76 77 78 79 |
# File 'riak-client/lib/riak/robject.rb', line 75 def initialize(bucket, key=nil) @bucket, @key = bucket, key @links, @meta = Set.new, {} yield self if block_given? end |
Instance Attribute Details
- (Bucket) bucket
The bucket in which this object is contained
32 33 34 |
# File 'riak-client/lib/riak/robject.rb', line 32 def bucket @bucket end |
- (Object) conflict=(value) (writeonly)
Sets the attribute conflict
174 175 176 |
# File 'riak-client/lib/riak/robject.rb', line 174 def conflict=(value) @conflict = value end |
- (String) content_type
The MIME content type of the object
38 39 40 |
# File 'riak-client/lib/riak/robject.rb', line 38 def content_type @content_type end |
- (String) etag
The ETag header from the most recent HTTP response, useful for caching and reloading
47 48 49 |
# File 'riak-client/lib/riak/robject.rb', line 47 def etag @etag end |
- (String) key
The key of this object within its bucket
35 36 37 |
# File 'riak-client/lib/riak/robject.rb', line 35 def key @key end |
- (Time) last_modified
The Last-Modified header from the most recent HTTP response, useful for caching and reloading
50 51 52 |
# File 'riak-client/lib/riak/robject.rb', line 50 def last_modified @last_modified end |
A Set of Link objects for relationships between this object and other resources
44 45 46 |
# File 'riak-client/lib/riak/robject.rb', line 44 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
53 54 55 |
# File 'riak-client/lib/riak/robject.rb', line 53 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
57 58 59 |
# File 'riak-client/lib/riak/robject.rb', line 57 def prevent_stale_writes @prevent_stale_writes end |
- (Array<RObject>, self) siblings
Returns sibling objects when in conflict.
179 180 181 182 |
# File 'riak-client/lib/riak/robject.rb', line 179 def siblings return self unless conflict? @siblings end |
- (String) vclock Also known as: vector_clock
The Riak vector clock for the object
41 42 43 |
# File 'riak-client/lib/riak/robject.rb', line 41 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.
64 65 66 67 68 |
# File 'riak-client/lib/riak/robject.rb', line 64 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 |
Instance Method Details
- (true, false) conflict?
Whether this object has conflicting sibling objects (divergent vclocks)
185 186 187 |
# File 'riak-client/lib/riak/robject.rb', line 185 def conflict? @conflict.present? end |
- (Object) data
The unmarshaled form of #raw_data stored in riak at this object’s key
104 105 106 107 108 109 110 |
# File 'riak-client/lib/riak/robject.rb', line 104 def data if @raw_data && !@data @data = deserialize(@raw_data) @raw_data = nil end @data end |
- (Object) data=(new_data)
The object stored
114 115 116 117 |
# File 'riak-client/lib/riak/robject.rb', line 114 def data=(new_data) @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.
168 169 170 171 172 |
# File 'riak-client/lib/riak/robject.rb', line 168 def delete(={}) return if key.blank? @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)
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'riak-client/lib/riak/robject.rb', line 219 def deserialize(body) case @content_type when /json/ JSON.parse(body, Riak.) when /yaml/ YAML.load(body) when "application/x-ruby-marshal" Marshal.load(body) else body end end |
- (String) inspect
A representation suitable for IRB and debugging output
233 234 235 236 237 238 239 240 |
# File 'riak-client/lib/riak/robject.rb', line 233 def inspect body = if @data || content_type =~ /json|yaml|marshal/ 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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'riak-client/lib/riak/robject.rb', line 86 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
120 121 122 123 124 125 126 |
# File 'riak-client/lib/riak/robject.rb', line 120 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
130 131 132 133 |
# File 'riak-client/lib/riak/robject.rb', line 130 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.
157 158 159 160 161 162 |
# File 'riak-client/lib/riak/robject.rb', line 157 def reload(={}) force = .delete(:force) return self unless @key && (@vclock || force) self.etag = self.last_modified = nil if force bucket.client.backend.reload_object(self, [:r]) 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.
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'riak-client/lib/riak/robject.rb', line 198 def serialize(payload) return payload if payload.respond_to?(:read) case @content_type when /json/ payload.to_json(Riak.) when /yaml/ YAML.dump(payload) when "application/x-ruby-marshal" Marshal.dump(payload) else payload.to_s end end |
- (Riak::RObject) store(options = {})
Store the object in Riak
143 144 145 146 147 148 |
# File 'riak-client/lib/riak/robject.rb', line 143 def store(={}) raise ArgumentError, t("content_type_undefined") unless @content_type.present? params = {:returnbody => true}.merge() @bucket.client.backend.store_object(self, params[:returnbody], params[:w], params[:dw]) self end |
- (Object) to_link(tag)
Converts the object to a link suitable for linking other objects to it
252 253 254 |
# File 'riak-client/lib/riak/robject.rb', line 252 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).
259 260 261 262 263 |
# File 'riak-client/lib/riak/robject.rb', line 259 def url segments = [ @bucket.client.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.
244 245 246 247 |
# File 'riak-client/lib/riak/robject.rb', line 244 def walk(*params) specs = WalkSpec.normalize(*params) @bucket.client.http.link_walk(self, specs) end |