Class: Riakpb::Content

Inherits:
Object
  • Object
show all
Includes:
Util::MessageCode, Util::Translation
Defined in:
lib/riakpb/content.rb

Overview

Parent class of all object types supported by ripple. RObject represents the data and metadata stored in a bucket/key pair in the Riakpb database.

Constant Summary

Constants included from Util::MessageCode

Util::MessageCode::DEL_REQUEST, Util::MessageCode::DEL_RESPONSE, Util::MessageCode::ERROR_RESPONSE, Util::MessageCode::GET_BUCKET_REQUEST, Util::MessageCode::GET_BUCKET_RESPONSE, Util::MessageCode::GET_CLIENT_ID_REQUEST, Util::MessageCode::GET_CLIENT_ID_RESPONSE, Util::MessageCode::GET_REQUEST, Util::MessageCode::GET_RESPONSE, Util::MessageCode::GET_SERVER_INFO_REQUEST, Util::MessageCode::GET_SERVER_INFO_RESPONSE, Util::MessageCode::LIST_BUCKETS_REQUEST, Util::MessageCode::LIST_BUCKETS_RESPONSE, Util::MessageCode::LIST_KEYS_REQUEST, Util::MessageCode::LIST_KEYS_RESPONSE, Util::MessageCode::MAP_REDUCE_REQUEST, Util::MessageCode::MAP_REDUCE_RESPONSE, Util::MessageCode::MC_RESPONSE_FOR, Util::MessageCode::PING_REQUEST, Util::MessageCode::PING_RESPONSE, Util::MessageCode::PUT_REQUEST, Util::MessageCode::PUT_RESPONSE, Util::MessageCode::RESPONSE_CLASS_FOR, Util::MessageCode::SET_BUCKET_REQUEST, Util::MessageCode::SET_BUCKET_RESPONSE, Util::MessageCode::SET_CLIENT_ID_REQUEST, Util::MessageCode::SET_CLIENT_ID_RESPONSE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(key, options = {}) {|_self| ... } ⇒ Content

Returns a new instance of Content.

Parameters:

  • key (Riakpb::Key)

    Key instance that owns this Content (really, you should use the Key to get this)

  • contents (Hash)

    Any contents to initialize this instance with

Yields:

  • (_self)

Yield Parameters:

See Also:



52
53
54
55
56
57
58
59
60
61
# File 'lib/riakpb/content.rb', line 52

def initialize(key, options={})
  @key              = key unless key.nil?
  @links            = Hash.new{|k,v| k[v] = []}
  @usermeta         = {}
  @options          = options

  self.last_mod     = Time.now

  yield self if block_given?
end

Instance Attribute Details

#charsetString

Returns the charset of the object.

Returns:

  • (String)

    the charset of the object



22
23
24
# File 'lib/riakpb/content.rb', line 22

def charset
  @charset
end

#content_encodingString

Returns the content encoding of the object.

Returns:

  • (String)

    the content encoding of the object



25
26
27
# File 'lib/riakpb/content.rb', line 25

def content_encoding
  @content_encoding
end

#content_typeString

Returns the MIME content type of the object.

Returns:

  • (String)

    the MIME content type of the object



19
20
21
# File 'lib/riakpb/content.rb', line 19

def content_type
  @content_type
end

#keyKey

Returns the key in which this Content is stored.

Returns:

  • (Key)

    the key in which this Content is stored.



12
13
14
# File 'lib/riakpb/content.rb', line 12

def key
  @key
end

#last_modTime

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



34
35
36
# File 'lib/riakpb/content.rb', line 34

def last_mod
  @last_mod
end

#last_mod_usecsTime

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



38
39
40
# File 'lib/riakpb/content.rb', line 38

def last_mod_usecs
  @last_mod_usecs
end

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

Returns:

  • (Set<Link>)

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



31
32
33
# File 'lib/riakpb/content.rb', line 31

def links
  @links
end

#optionsObject

Returns the value of attribute options.



45
46
47
# File 'lib/riakpb/content.rb', line 45

def options
  @options
end

#usermetaHash

Returns a hash of any user-supplied metadata, consisting of a key/value pair.

Returns:

  • (Hash)

    a hash of any user-supplied metadata, consisting of a key/value pair



42
43
44
# File 'lib/riakpb/content.rb', line 42

def usermeta
  @usermeta
end

#valueString

Returns the data stored in Riakpb at this object’s key. Varies in format by content-type.

Returns:

  • (String)

    the data stored in Riakpb at this object’s key. Varies in format by content-type.



15
16
17
# File 'lib/riakpb/content.rb', line 15

def value
  @value
end

#vtagString

Returns the vtag of the object.

Returns:

  • (String)

    the vtag of the object



28
29
30
# File 'lib/riakpb/content.rb', line 28

def vtag
  @vtag
end

Instance Method Details

#[](attribute) ⇒ Object



63
64
65
# File 'lib/riakpb/content.rb', line 63

def [](attribute)
  instance_variable_get "@#{attribute}"
end

#inspectString

Returns A representation suitable for IRB and debugging output.

Returns:

  • (String)

    A representation suitable for IRB and debugging output



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/riakpb/content.rb', line 259

def inspect
  "#<#Riakpb::Content " + [
      (@value.nil?)             ? nil : "value=#{@value.inspect}",
      (@content_type.nil?)      ? nil : "content_type=#{@content_type.inspect}",
      (@charset.nil?)           ? nil : "charset=#{@charset.inspect}",
      (@content_encoding.nil?)  ? nil : "content_encoding=#{@content_encoding.inspect}",
      (@vtag.nil?)              ? nil : "vtag=#{@vtag.inspect}",
      (@links.nil?)             ? nil : "links=#{@_links.inspect}",
      (@last_mod.nil?)          ? nil : "last_mod=#{last_mod.inspect}",
      (@last_mod_usecs.nil?)    ? nil : "last_mod_usecs=#{last_mod_usecs.inspect}",
      (@usermeta.nil?)          ? nil : "usermeta=#{@usermeta.inspect}"

    ].compact.join(", ") + ">"
end

Internalizes a link to a Key, which will be saved on next call to… uh, save

Parameters:

  • tags (Hash)

    name of the tag, pointing to a Key instance, or an array [“bucket”, “key”]

Returns:

  • (Hash)

    links that this Content points to



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/riakpb/content.rb', line 152

def link_key(tags)
  raise TypeError.new t('invalid_tag') unless tags.is_a?(Hash) or tags.is_a?(Array)

  tags.each do |tag, link|
    case link
    when Array
      newlink = Riakpb::RpbLink.new
      newlink[:bucket]            = link[0]
      newlink[:key]               = link[1]
      raise ArgumentError.new t('invalid_tag') if link[0].nil? or link[1].nil?

      @links[tag.to_s]  <<  newlink

    when Riakpb::Key
      @links[tag.to_s] << link

    else
      raise TypeError.new t('invalid_tag')
    end
  end # tags.each do |tag, link|
end

#load(content) ⇒ Content

Load information for the content from the response object, Riakpb::RpbContent.

Parameters:

  • contents (RpbContent/Hash)

    an RpbContent object or a Hash.

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/riakpb/content.rb', line 70

def load(content)
  case content
  when Riakpb::RpbContent, Hash, Riakpb::Content
    last_mod          = content[:last_mod]
    last_mod_usecs    = content[:last_mod_usecs]

    if not (last_mod.blank? or last_mod_usecs.blank?)
      self.last_mod = Time.at "#{last_mod}.#{last_mod_usecs}".to_f
    end

    @content_type     = content[:content_type]     unless content[:content_type].blank?
    @charset          = content[:charset]          unless content[:charset].blank?
    @content_encoding = content[:content_encoding] unless content[:content_encoding].blank?
    @vtag             = content[:vtag]             unless content[:vtag].blank?
    self.links        = content[:links]            unless content[:links].blank?
    self.usermeta     = content[:usermeta]         unless content[:usermeta].blank?

    unless content[:value].blank?
      case @content_type
      when /json/
        @value = ActiveSupport::JSON.decode(content[:value]) 
      when /octet/
        @value = Marshal.load(content[:value])
      else
        @value = content[:value]
      end
    end
  else raise ArgumentError, t("riak_content_type")
  end

  return(self)
end

#merge(content) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/riakpb/content.rb', line 103

def merge(content)
  @links.merge(
    self.links  = content[:links]
  ) unless content[:links].blank?

  @usermega.merge(
    self.usermeta = content[:usermeta]
  ) unless content[:usermeta].blank?

  @content_type     = content[:content_type]     unless content[:content_type].blank?
  @value            = content[:value]
  @charset          = content[:charset]          unless content[:charset].blank?
  @content_encoding = content[:content_encoding] unless content[:content_encoding].blank?
  @vtag             = content[:vtag]             unless content[:vtag].blank?
  @last_mod         = content[:last_mod]
  @last_mod_usecs   = content[:last_mod_usecs]

  return(self)
end

#save(options = {}) ⇒ Object

Save the Content instance in riak.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • w (Fixnum) — default: write quorum

    how many replicas to write to before returning a successful response

  • dw (Fixnum)

    how many replicas to commit to durable storage before returning a successful response

  • return_body (true/false)

    whether or not to have riak return the key, once saved. default = true



127
128
129
130
131
132
133
134
# File 'lib/riakpb/content.rb', line 127

def save(options={})
  begin
    self.save!(options)
  rescue FailedRequest
    return(false)
  end
  return(true)
end

#save!(options = {}) ⇒ Object

Save the Content instance in riak. Raise/do not rescue on failure.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • w (Fixnum) — default: write quorum

    how many replicas to write to before returning a successful response

  • dw (Fixnum)

    how many replicas to commit to durable storage before returning a successful response

  • return_body (true/false)

    whether or not to have riak return the key, once saved. default = true



140
141
142
143
144
145
146
# File 'lib/riakpb/content.rb', line 140

def save!(options={})
  options[:content] = self
  options           = @options.merge(options)

  return(true) if @key.save(options)
  return(false) # Create and raise Error message for this?  Extend "Failed Request"?
end

#to_pbRiakpb::RpbContent

Returns An instance of a RpbContent, suitable for protobuf exchange.

Returns:



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/riakpb/content.rb', line 210

def to_pb
  rpb_content                   = Riakpb::RpbContent.new
  rpb_links                     = []

  @links.each do |tag, links|
    links.each do |link|
      case link
      when Riakpb::RpbLink
        rpb_links   <<  hlink
      when Riakpb::Key
        pb_link     =   link.to_pb_link
        pb_link.tag =   tag
        rpb_links   <<  pb_link
      end
    end
  end

  usermeta                      = []
  @usermeta.each do |key,value|
    pb_pair         =   Riakpb::RpbPair.new
    pb_pair[:key]   =   key
    pb_pair[:value] =   value
    usermeta        <<  pb_pair
  end

  catch(:redo) do
    case @content_type
    when /octet/
      rpb_content.value = Marshal.dump(@value) unless @value.blank?
    when /json/
      rpb_content.value = ActiveSupport::JSON.encode(@value) unless @value.blank?
    when "", nil
      @content_type     = "application/json"
      redo
    else
      rpb_content.value = @value.to_s unless @value.blank?
    end
  end

  rpb_content.content_type      = @content_type     unless @content_type.blank?
  rpb_content.charset           = @charset          unless @charset.blank? # || @charset.empty?
  rpb_content.content_encoding  = @content_encoding unless @content_encoding.blank? # || @content_encoding.empty?
  rpb_content.links             = rpb_links         unless rpb_links.blank?
  rpb_content.usermeta          = usermeta          unless usermeta.blank?

  return(rpb_content)
end