Class: Riakpb::Key

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

Overview

Represents and encapsulates operations on a Riakpb bucket. You may retrieve a bucket using Client#bucket, or create it manually and retrieve its meta-information later.

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(bucket, key, get_response = nil) ⇒ Key

Create a Riakpb bucket manually.

Parameters:

  • bucket (Bucket)

    the Bucket object within which this Key exists

  • options (Hash)

    a customizable set of options



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/riakpb/key.rb', line 24

def initialize(bucket, key, get_response=nil)
#      options.assert_valid_keys(:name, :vclock, :content)

  self.bucket   = bucket
  self.name     = key

  @contents     = Hash.new{|k,v| k[v] = Riakpb::Content.new(self)}

#      @contents[:new]

  load(get_response) unless get_response.nil?
end

Instance Attribute Details

#bucketRiakpb::Client

Returns the associated client.

Returns:



11
12
13
# File 'lib/riakpb/key.rb', line 11

def bucket
  @bucket
end

#nameString

Returns the bucket name.

Returns:

  • (String)

    the bucket name



14
15
16
# File 'lib/riakpb/key.rb', line 14

def name
  @name
end

#vclockString

Returns the bucket name.

Returns:

  • (String)

    the bucket name



17
18
19
# File 'lib/riakpb/key.rb', line 17

def vclock
  @vclock
end

Instance Method Details

#contentRiakpb::Content

“@contents” is an array of Content objects, though only contains more than one in the event that

there are siblings.

Returns:

  • (Riakpb::Content)

    the content of this Key instance’s value (ie, key/value)



143
144
145
146
147
148
149
# File 'lib/riakpb/key.rb', line 143

def content
  case @contents.size
  when 0 then @contents[:new]
  when 1 then contents[0]
  else        contents
  end
end

#content=(riak_contents) ⇒ Riakpb::Content

Sets the content object for this Key. I do not yet support siblings in this method and, therefore,

you may or may not destroy them if you use this and are not careful.

Parameters:

  • content (Riakpb::Content)

    a Content instance that should be contained within this Key

Returns:

Raises:

  • (ArgumentError)

    will yell at you if the supplied riak_content is not of the Content class



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/riakpb/key.rb', line 113

def content=(riak_contents)

  if riak_contents.is_a?(Protobuf::Field::FieldArray)
    @contents.clear
    
    return(false) if riak_contents.empty?

    riak_contents.each do |rc|
      @contents[rc.vtag].load(rc)
    end
    
    return(true)
  elsif riak_contents.is_a?(Riakpb::Content)

    @contents.clear

    @contents[riak_contents.vtag].load(riak_contents)

  elsif riak_contents.nil?
    @contents.clear

  else
    raise ArgumentError, t("riak_content_type")
  end # if riak_contents

end

#contentsArray<Riakpb::Content>

“@contents” is an array of Content objects. This gives you that entire array.

Returns:

  • (Array<Riakpb::Content>)

    the contents of this Key instance’s value and its siblings, if any



153
154
155
156
157
158
159
# File 'lib/riakpb/key.rb', line 153

def contents
  retr_c = []

  @contents.each{|k,v| retr_c << v}

  return(retr_c)
end

#delete(options = {}) ⇒ Object

Deletes this key from its Bucket container

Parameters:

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

    quorum options

Options Hash (options):

  • :rw (Fixnum)
    • the read/write quorum for the delete



252
253
254
# File 'lib/riakpb/key.rb', line 252

def delete(options={})
  bucket.delete(@name, options)
end

#empty?Boolean

Indicates whether or not the Key is empty

Returns:

  • (Boolean)

    true or false, whether or not the vclock/content is empty



81
82
83
84
# File 'lib/riakpb/key.rb', line 81

def empty?
  return(true) if @vclock.blank? && @contents.nil?
  return(false)
end

#get_linked(bucket, key, options = {}) ⇒ Key

Retrieves any Keys that are linked to, inside Content elements.

Returns:

  • (Key)

    the Key to which the Content is linked (may be empty if it does not exist)



94
95
96
# File 'lib/riakpb/key.rb', line 94

def get_linked(bucket, key, options={})
  @bucket.get_linked(bucket, key, options)
end

#inspectString

Returns a representation suitable for IRB and debugging output.

Returns:

  • (String)

    a representation suitable for IRB and debugging output



257
258
259
# File 'lib/riakpb/key.rb', line 257

def inspect
  "#<Riakpb::Key name=#{@name.inspect}, vclock=#{@vclock.inspect}, contents=#{contents.inspect}>"
end

#load(response) ⇒ Key

Load information for the key from the response object, Riakpb::RpbGetResp.

Parameters:

  • response (RpbGetResp/Hash)

    an RpbGetResp/RpbPutResp object or a Hash.

Returns:

  • (Key)

    self

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/riakpb/key.rb', line 41

def load(response)
  @blargh = response
  raise ArgumentError, t("response_type") unless response.is_a?(Protobuf::Message)

  self.vclock       = response.vclock if response.has_field?(:vclock)

  if response.has_field?(:content)
    self.content    = response.content
  elsif @contents.blank?
    @contents[:new]
  end

  return(self)
end

#load!(response) ⇒ Key

Load information for the key from Riakpb::RpbGetResp object.

Parameters:

  • response (RpbGetResp/Hash)

    an RpbGetResp/RpbPutResp object or a Hash.

Returns:

  • (Key)

    self

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/riakpb/key.rb', line 60

def load!(response)
  raise ArgumentError, t("response_type") unless response.is_a?(Riakpb::RpbGetResp)

  if response.has_field?(:vclock) and response.has_field?(:content)

    self.vclock   = response.vclock
    self.content  = response.content

  elsif response.has_field?(:vclock) or response.has_field?(:content)
    raise MalformedKeyError # This should never happen

  else
    raise KeyNotFoundError

  end

  return(self)
end

#reload!Object

Refreshes the Key and its content with fresh data, if there’s concern that separate updates may have taken place.

Returns:



88
89
90
# File 'lib/riakpb/key.rb', line 88

def reload!

end

#save(options = {}) ⇒ Object

Save the Key+Content instance in riak. TODO: Add in content checking, perhaps?

Parameters:

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

    a customizable set of options

Options Hash (options):

  • content (Content)

    Content instance to be saved in this Key. Must be specified if there are siblings.

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

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/riakpb/key.rb', line 167

def save(options={})
  rcontent = options[:content]

  if rcontent.nil?
    case contents.size
    when 0 then raise ArgumentError, t('empty_content')
    when 1 then rcontent = contents[0]
    else        raise SiblingError.new(self.name)
    end
  end

  options[:content] = rcontent.to_pb  if      rcontent.is_a?(Riakpb::Content)
  options[:content] = rcontent        if      rcontent.is_a?(Riakpb::RpbContent)
  options[:key]     = @name
  options[:vclock]  = @vclock         unless  @vclock.nil?

  begin
    response = @bucket.store(options)
    load(response)
    return(true) if @contents.count == 1
    return(false)
  rescue FailedRequest
    return(false)
  end
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 (Boolean)

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



197
198
199
200
201
202
203
204
205
# File 'lib/riakpb/key.rb', line 197

def save!(options={})
  begin
    save(options)
    return(true) if @contents.count == 1
    raise FailedRequest.new("save_resp_siblings", 1, @contents.count, @contents) if @contents.count > 1
  rescue FailedRequest
    raise FailedRequest.new("save_resp_err")
  end
end

Converts this Key into an array, that can be used by a Content, if desired.

Returns:

  • (Array)

    contains the name of the bucket and the name of the key



244
245
246
# File 'lib/riakpb/key.rb', line 244

def to_link
  [@bucket.name, @name]
end

“@contents” is an array of Content objects. This gives you that entire array.

Returns:



234
235
236
237
238
239
240
# File 'lib/riakpb/key.rb', line 234

def to_pb_link
  pb_link = Riakpb::RpbLink.new
  pb_link[:bucket]  = @bucket.name
  pb_link[:key]     = @name

  return(pb_link)
end

#to_pb_put(options = {}) ⇒ Riakpb::RpbPutReq

Creates an RpbPutReq instance, to be shipped off to riak and saved

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 (Boolean)

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

Returns:



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/riakpb/key.rb', line 212

def to_pb_put(options={})
  rcontent    = options[:content]

  if rcontent.nil?
    case contents.size
    when 0 then raise ArgumentError, t('empty_content')
    when 1 then rcontent = contents[0]
    else        raise SiblingError.new(self.name)
    end
  end

  pb_put_req              = Riakpb::RpbPutReq.new
  pb_put_req.key          = @name
  pb_put_req.content      = rcontent.to_pb  if      rcontent.is_a?(Riakpb::Content)
  pb_put_req.content      = rcontent        if      rcontent.is_a?(Riakpb::RpbContent)
  pb_put_req.vclock       = @vclock         unless  @vclock.nil?

  return(pb_put_req)
end