Class: Riak::RContent
Overview
Represents single (potentially-conflicted) value stored against a key in the Riak database. This includes the raw value as well as metadata.
Instance Attribute Summary collapse
-
#content_encoding ⇒ String
The content encoding of the object, e.g.
-
#content_type ⇒ String
The MIME content type of the value.
-
#etag ⇒ String
The ETag header from the most recent HTTP response, useful for caching and reloading.
-
#indexes ⇒ Hash<Set>
A hash of secondary indexes, where the key is the index name and the value is a Set of index entries for that index.
-
#last_modified ⇒ Time
The Last-Modified header from the most recent HTTP response, useful for caching and reloading.
-
#links ⇒ Set<Link>
A Set of Link objects for relationships between this object and other resources.
-
#meta ⇒ Hash
A hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion.
-
#robject ⇒ Riak::RObject
The RObject to which this sibling belongs.
Instance Method Summary collapse
-
#compress(data) ⇒ String
Compresses the given string using gzip if #content_encoding is set to “gzip”.
-
#data ⇒ Object
The unmarshaled form of #raw_data stored in riak at this object’s key.
-
#data=(new_data) ⇒ Object
The object stored.
-
#decompress(data) ⇒ String
Decompresses the given string using gzip if #content_encoding is set to “gzip”.
-
#deserialize(body) ⇒ Object
Deserializes the internal object data from a Riak response.
-
#initialize(robject) { ... } ⇒ RContent
constructor
Creates a new object value.
-
#inspect ⇒ String
A representation suitable for IRB and debugging output.
- #load_map_reduce_value(hash) ⇒ Object private
-
#raw_data ⇒ String
Raw data stored in riak for this object’s key.
-
#raw_data=(new_raw_data) ⇒ String
The data stored.
-
#serialize(payload) ⇒ Object
Serializes the internal object data for sending to Riak.
Methods included from Util::Translation
Constructor Details
#initialize(robject) { ... } ⇒ RContent
Creates a new object value. This should not normally need to be called by users of the client. Normal, single-value use can rely on the delegating accessors on Riak::RObject.
51 52 53 54 55 56 |
# File 'lib/riak/rcontent.rb', line 51 def initialize(robject) @robject = robject @links, @meta = Set.new, {} @indexes = new_index_hash yield self if block_given? end |
Instance Attribute Details
#content_encoding ⇒ String
Returns the content encoding of the object, e.g. “gzip”.
22 23 24 |
# File 'lib/riak/rcontent.rb', line 22 def content_encoding @content_encoding end |
#content_type ⇒ String
Returns the MIME content type of the value.
19 20 21 |
# File 'lib/riak/rcontent.rb', line 19 def content_type @content_type end |
#etag ⇒ String
Returns the ETag header from the most recent HTTP response, useful for caching and reloading.
28 29 30 |
# File 'lib/riak/rcontent.rb', line 28 def etag @etag end |
#indexes ⇒ Hash<Set>
Returns a hash of secondary indexes, where the key is the index name and the value is a Set of index entries for that index.
39 40 41 |
# File 'lib/riak/rcontent.rb', line 39 def indexes @indexes end |
#last_modified ⇒ Time
Returns the Last-Modified header from the most recent HTTP response, useful for caching and reloading.
31 32 33 |
# File 'lib/riak/rcontent.rb', line 31 def last_modified @last_modified end |
#links ⇒ Set<Link>
Returns a Set of Link objects for relationships between this object and other resources.
25 26 27 |
# File 'lib/riak/rcontent.rb', line 25 def links @links end |
#meta ⇒ Hash
Returns a hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion.
34 35 36 |
# File 'lib/riak/rcontent.rb', line 34 def @meta end |
#robject ⇒ Riak::RObject
Returns the RObject to which this sibling belongs.
42 43 44 |
# File 'lib/riak/rcontent.rb', line 42 def robject @robject end |
Instance Method Details
#compress(data) ⇒ String
Compresses the given string using gzip if #content_encoding is set to “gzip”. Otherwise the given string is returned as-is. This method is called internally when storing the object.
137 138 139 140 |
# File 'lib/riak/rcontent.rb', line 137 def compress(data) return data unless content_encoding == "gzip" Util::Gzip.compress(data) end |
#data ⇒ Object
Returns the unmarshaled form of #raw_data stored in riak at this object’s key.
67 68 69 70 71 72 73 74 |
# File 'lib/riak/rcontent.rb', line 67 def data if @raw_data && !@data raw = @raw_data.respond_to?(:read) ? @raw_data.read : @raw_data @data = deserialize(decompress(raw)) @raw_data = nil end @data end |
#data=(new_data) ⇒ Object
Returns the object stored.
81 82 83 84 85 86 87 88 |
# File 'lib/riak/rcontent.rb', line 81 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 |
#decompress(data) ⇒ String
Decompresses the given string using gzip if #content_encoding is set to “gzip”. Otherwise the given string is returned as-is. This method is called internally when loading the object.
147 148 149 150 |
# File 'lib/riak/rcontent.rb', line 147 def decompress(data) return data unless content_encoding == "gzip" Util::Gzip.decompress(data) end |
#deserialize(body) ⇒ Object
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)
128 129 130 |
# File 'lib/riak/rcontent.rb', line 128 def deserialize(body) Serializers.deserialize(@content_type, body) end |
#inspect ⇒ String
Returns A representation suitable for IRB and debugging output.
153 154 155 156 157 158 159 160 |
# File 'lib/riak/rcontent.rb', line 153 def inspect body = if @data || Serializers[content_type] data.inspect else @raw_data && "(#{@raw_data.size} bytes)" end "#<#{self.class.name} [#{@content_type}]:#{body}>" end |
#load_map_reduce_value(hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/riak/rcontent.rb', line 163 def load_map_reduce_value(hash) = hash['metadata'] extract_if_present(, 'X-Riak-VTag', :etag) extract_if_present(, 'content-type', :content_type) extract_if_present(, 'X-Riak-Last-Modified', :last_modified) { |v| Time.httpdate( v ) } extract_if_present(, 'index', :indexes) do |entries| Hash[ entries.map {|k, v| [k, Set.new(Array(v))] } ] end extract_if_present(, 'Links', :links) do |links| Set.new( links.map { |l| Link.new(*l) } ) end extract_if_present(, 'X-Riak-Meta', :meta) do || Hash[ .map do |k, v| [k.sub(%r{^x-riak-meta-}i, ''), [v]] end ] end extract_if_present(hash, 'data', :data) { |v| deserialize(v) } end |
#raw_data ⇒ String
Returns raw data stored in riak for this object’s key.
91 92 93 94 95 96 97 |
# File 'lib/riak/rcontent.rb', line 91 def raw_data if @data && !@raw_data @raw_data = compress(serialize(@data)) @data = nil end @raw_data end |
#raw_data=(new_raw_data) ⇒ String
Returns the data stored.
103 104 105 106 |
# File 'lib/riak/rcontent.rb', line 103 def raw_data=(new_raw_data) @data = nil @raw_data = new_raw_data end |
#serialize(payload) ⇒ Object
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.
117 118 119 |
# File 'lib/riak/rcontent.rb', line 117 def serialize(payload) Serializers.serialize(@content_type, payload) end |