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



47
48
49
50
51
52
# File 'lib/riak/rcontent.rb', line 47

def initialize(robject)
  @robject = robject
  @links, @meta = Set.new, {}
  @indexes = new_index_hash
  yield self if block_given?
end

Instance Attribute Details

#content_typeString

Returns the MIME content type of the value.

Returns:

  • (String)

    the MIME content type of the value

Since:

  • 1.1.0



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

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



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

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



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

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



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

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



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

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



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

def meta
  @meta
end

#robjectRiak::RObject

Returns the RObject to which this sibling belongs.

Returns:

Since:

  • 1.1.0



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

def robject
  @robject
end

Instance Method Details

#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



62
63
64
65
66
67
68
69
# File 'lib/riak/rcontent.rb', line 62

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

#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



76
77
78
79
80
81
82
83
# File 'lib/riak/rcontent.rb', line 76

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

#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



124
125
126
# File 'lib/riak/rcontent.rb', line 124

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



129
130
131
132
133
134
135
136
# File 'lib/riak/rcontent.rb', line 129

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/riak/rcontent.rb', line 139

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



87
88
89
90
91
92
93
# File 'lib/riak/rcontent.rb', line 87

def raw_data
  if @data && !@raw_data
    @raw_data = 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



99
100
101
102
# File 'lib/riak/rcontent.rb', line 99

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



113
114
115
# File 'lib/riak/rcontent.rb', line 113

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