Class: Riak::RContent

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Util::Translation
Defined in:
lib/riak/rcontent.rb

Overview

Represents single (potentially-conflicted) value stored against a key in the Riak database. This includes the raw value as well as metadata.

Since:

  • 1.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Translation

#i18n_scope, #t

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.

Parameters:

  • robject (RObject)

    the object that this value belongs to

Yields:

  • self the new RContent

Since:

  • 1.1.0



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_encodingString

Returns the content encoding of the object, e.g. “gzip”.

Returns:

  • (String)

    the content encoding of the object, e.g. “gzip”

Since:

  • 1.1.0



22
23
24
# File 'lib/riak/rcontent.rb', line 22

def content_encoding
  @content_encoding
end

#content_typeString

Returns the MIME content type of the value.

Returns:

  • (String)

    the MIME content type of the value

Since:

  • 1.1.0



19
20
21
# File 'lib/riak/rcontent.rb', line 19

def content_type
  @content_type
end

#etagString

Returns the ETag header from the most recent HTTP response, useful for caching and reloading.

Returns:

  • (String)

    the ETag header from the most recent HTTP response, useful for caching and reloading

Since:

  • 1.1.0



28
29
30
# File 'lib/riak/rcontent.rb', line 28

def etag
  @etag
end

#indexesHash<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.

Returns:

  • (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

Since:

  • 1.1.0



39
40
41
# File 'lib/riak/rcontent.rb', line 39

def indexes
  @indexes
end

#last_modifiedTime

Returns the Last-Modified header from the most recent HTTP response, useful for caching and reloading.

Returns:

  • (Time)

    the Last-Modified header from the most recent HTTP response, useful for caching and reloading

Since:

  • 1.1.0



31
32
33
# File 'lib/riak/rcontent.rb', line 31

def last_modified
  @last_modified
end

Returns a Set of Link objects for relationships between this object and other resources.

Returns:

  • (Set<Link>)

    a Set of Link objects for relationships between this object and other resources

Since:

  • 1.1.0



25
26
27
# File 'lib/riak/rcontent.rb', line 25

def links
  @links
end

#metaHash

Returns a hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion.

Returns:

  • (Hash)

    a hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion

Since:

  • 1.1.0



34
35
36
# File 'lib/riak/rcontent.rb', line 34

def meta
  @meta
end

#robjectRiak::RObject

Returns the RObject to which this sibling belongs.

Returns:

Since:

  • 1.1.0



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.

Parameters:

Returns:

Since:

  • 1.1.0



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

#dataObject

Returns the unmarshaled form of #raw_data stored in riak at this object’s key.

Returns:

  • (Object)

    the unmarshaled form of #raw_data stored in riak at this object’s key

Since:

  • 1.1.0



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.

Parameters:

  • new_data (Object)

    unmarshaled form of the data to be stored in Riak. Object will be serialized using #serialize if a known content_type is used. Setting this overrides values stored with #raw_data=

Returns:

  • (Object)

    the object stored

Since:

  • 1.1.0



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.

Parameters:

Returns:

Since:

  • 1.1.0



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)

Parameters:

  • body (String)

    the serialized response body

Since:

  • 1.1.0



128
129
130
# File 'lib/riak/rcontent.rb', line 128

def deserialize(body)
  Serializers.deserialize(@content_type, body)
end

#inspectString

Returns A representation suitable for IRB and debugging output.

Returns:

  • (String)

    A representation suitable for IRB and debugging output

Since:

  • 1.1.0



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.

Since:

  • 1.1.0



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 |meta|
    Hash[
         meta.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_dataString

Returns raw data stored in riak for this object’s key.

Returns:

  • (String)

    raw data stored in riak for this object’s key

Since:

  • 1.1.0



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.

Parameters:

  • new_raw_data (String, IO-like)

    the raw data to be stored in Riak at this key, will not be marshaled or manipulated prior to storage. Overrides any data stored by #data=

Returns:

  • (String)

    the data stored

Since:

  • 1.1.0



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.

Parameters:

  • payload (Object)

    the data to serialize

Since:

  • 1.1.0



117
118
119
# File 'lib/riak/rcontent.rb', line 117

def serialize(payload)
  Serializers.serialize(@content_type, payload)
end