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.
65 66 67 68 69 70 |
# File 'lib/riak/rcontent.rb', line 65 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”.
36 37 38 |
# File 'lib/riak/rcontent.rb', line 36 def content_encoding @content_encoding end |
#content_type ⇒ String
Returns the MIME content type of the value.
33 34 35 |
# File 'lib/riak/rcontent.rb', line 33 def content_type @content_type end |
#etag ⇒ String
Returns the ETag header from the most recent HTTP response, useful for caching and reloading.
42 43 44 |
# File 'lib/riak/rcontent.rb', line 42 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.
53 54 55 |
# File 'lib/riak/rcontent.rb', line 53 def indexes @indexes end |
#last_modified ⇒ Time
Returns the Last-Modified header from the most recent HTTP response, useful for caching and reloading.
45 46 47 |
# File 'lib/riak/rcontent.rb', line 45 def last_modified @last_modified end |
#links ⇒ Set<Link>
Returns a Set of Link objects for relationships between this object and other resources.
39 40 41 |
# File 'lib/riak/rcontent.rb', line 39 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.
48 49 50 |
# File 'lib/riak/rcontent.rb', line 48 def @meta end |
#robject ⇒ Riak::RObject
Returns the RObject to which this sibling belongs.
56 57 58 |
# File 'lib/riak/rcontent.rb', line 56 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.
151 152 153 154 |
# File 'lib/riak/rcontent.rb', line 151 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.
81 82 83 84 85 86 87 88 |
# File 'lib/riak/rcontent.rb', line 81 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.
95 96 97 98 99 100 101 102 |
# File 'lib/riak/rcontent.rb', line 95 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.
161 162 163 164 |
# File 'lib/riak/rcontent.rb', line 161 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)
142 143 144 |
# File 'lib/riak/rcontent.rb', line 142 def deserialize(body) Serializers.deserialize(@content_type, body) end |
#inspect ⇒ String
Returns A representation suitable for IRB and debugging output.
167 168 169 170 171 172 173 174 |
# File 'lib/riak/rcontent.rb', line 167 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.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/riak/rcontent.rb', line 177 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.
105 106 107 108 109 110 111 |
# File 'lib/riak/rcontent.rb', line 105 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.
117 118 119 120 |
# File 'lib/riak/rcontent.rb', line 117 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.
131 132 133 |
# File 'lib/riak/rcontent.rb', line 131 def serialize(payload) Serializers.serialize(@content_type, payload) end |