Class: Riakpb::Key
- Inherits:
-
Object
- Object
- Riakpb::Key
- 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
-
#bucket ⇒ Riakpb::Client
readonly
The associated client.
-
#name ⇒ String
The bucket name.
-
#vclock ⇒ String
readonly
The bucket name.
Instance Method Summary collapse
-
#content ⇒ Riakpb::Content
“@contents” is an array of Content objects, though only contains more than one in the event that there are siblings.
-
#content=(riak_contents) ⇒ Riakpb::Content
Sets the content object for this Key.
-
#contents ⇒ Array<Riakpb::Content>
“@contents” is an array of Content objects.
-
#delete(options = {}) ⇒ Object
Deletes this key from its Bucket container.
-
#empty? ⇒ Boolean
Indicates whether or not the Key is empty.
-
#get_linked(bucket, key, options = {}) ⇒ Key
Retrieves any Keys that are linked to, inside Content elements.
-
#initialize(bucket, key, get_response = nil) ⇒ Key
constructor
Create a Riakpb bucket manually.
-
#inspect ⇒ String
A representation suitable for IRB and debugging output.
-
#load(response) ⇒ Key
Load information for the key from the response object, Riakpb::RpbGetResp.
-
#load!(response) ⇒ Key
Load information for the key from Riakpb::RpbGetResp object.
-
#reload! ⇒ Object
Refreshes the Key and its content with fresh data, if there’s concern that separate updates may have taken place.
-
#save(options = {}) ⇒ Object
Save the Key+Content instance in riak.
-
#save!(options = {}) ⇒ Object
Save the Content instance in riak.
-
#to_link ⇒ Array
(also: #to_input)
Converts this Key into an array, that can be used by a Content, if desired.
-
#to_pb_link ⇒ Riakpb::RpbLink
“@contents” is an array of Content objects.
-
#to_pb_put(options = {}) ⇒ Riakpb::RpbPutReq
Creates an RpbPutReq instance, to be shipped off to riak and saved.
Methods included from Util::Translation
Constructor Details
#initialize(bucket, key, get_response = nil) ⇒ Key
Create a Riakpb bucket manually.
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
#bucket ⇒ Riakpb::Client
Returns the associated client.
11 12 13 |
# File 'lib/riakpb/key.rb', line 11 def bucket @bucket end |
#name ⇒ String
Returns the bucket name.
14 15 16 |
# File 'lib/riakpb/key.rb', line 14 def name @name end |
#vclock ⇒ String
Returns the bucket name.
17 18 19 |
# File 'lib/riakpb/key.rb', line 17 def vclock @vclock end |
Instance Method Details
#content ⇒ Riakpb::Content
“@contents” is an array of Content objects, though only contains more than one in the event that
there are siblings.
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.
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 |
#contents ⇒ Array<Riakpb::Content>
“@contents” is an array of Content objects. This gives you that entire array.
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
252 253 254 |
# File 'lib/riakpb/key.rb', line 252 def delete(={}) bucket.delete(@name, ) end |
#empty? ⇒ Boolean
Indicates whether or not the Key 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.
94 95 96 |
# File 'lib/riakpb/key.rb', line 94 def get_linked(bucket, key, ={}) @bucket.get_linked(bucket, key, ) end |
#inspect ⇒ String
Returns 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.
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.
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.
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?
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(={}) rcontent = [: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 [:content] = rcontent.to_pb if rcontent.is_a?(Riakpb::Content) [:content] = rcontent if rcontent.is_a?(Riakpb::RpbContent) [:key] = @name [:vclock] = @vclock unless @vclock.nil? begin response = @bucket.store() 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.
197 198 199 200 201 202 203 204 205 |
# File 'lib/riakpb/key.rb', line 197 def save!(={}) begin save() 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 |
#to_link ⇒ Array Also known as: to_input
Converts this Key into an array, that can be used by a Content, if desired.
244 245 246 |
# File 'lib/riakpb/key.rb', line 244 def to_link [@bucket.name, @name] end |
#to_pb_link ⇒ Riakpb::RpbLink
“@contents” is an array of Content objects. This gives you that entire array.
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
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(={}) rcontent = [: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 |