Class: Riakpb::Bucket
- Inherits:
-
Object
- Object
- Riakpb::Bucket
- Includes:
- Util::MessageCode, Util::Translation
- Defined in:
- lib/riakpb/bucket.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
-
#allow_mult ⇒ true, false
Whether the bucket allows divergent siblings.
-
#client ⇒ Riakpb::Client
readonly
The associated client.
-
#key_cache ⇒ Object
readonly
Returns the value of attribute key_cache.
-
#n_val ⇒ Fixnum
The N value, or number of replicas for this bucket.
-
#name ⇒ String
readonly
The bucket name.
Instance Method Summary collapse
-
#delete(key, rw = nil) ⇒ Object
Deletes a key from the bucket.
-
#destroy!(rw = nil) ⇒ Object
Wipes out all keys stored in the bucket, as of execution.
-
#get_linked(bucket, key, options = nil) ⇒ Riakpb::Key
Retrieves a Key from the given Bucket.
-
#initialize(client, name, options = {}) ⇒ Bucket
constructor
Create a Riakpb bucket manually.
-
#inspect ⇒ String
A representation suitable for IRB and debugging output.
-
#inspect! ⇒ String
A representation suitable for IRB and debugging output, including keys within this bucket.
- #junkshot(key, params) ⇒ Object
-
#key(key, options = {}) ⇒ Riakpb::Key
(also: #[])
Retrieve an object from within the bucket.
-
#key!(key, r = nil) ⇒ Riakpb::Key
Retrieve an object from within the bucket.
-
#keys ⇒ Array<String>
Accesses or retrieves a list of keys in this bucket.
-
#keys! ⇒ Array<String>
Accesses or retrieves a list of keys in this bucket.
-
#load(response) ⇒ Bucket
Load information for the bucket from a response given by the Client::HTTPBackend.
-
#store(options) ⇒ RpbPutResp
Retrieves a Key from the given Bucket.
Methods included from Util::Translation
Constructor Details
#initialize(client, name, options = {}) ⇒ Bucket
Create a Riakpb bucket manually.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/riakpb/bucket.rb', line 27 def initialize(client, name, ={}) .assert_valid_keys(:n_val, :allow_mult, :return_body) raise ArgumentError, t("client_type", :client => client.inspect) unless client.is_a?(Client) raise ArgumentError, t("string_type", :string => name.inspect) unless name.is_a?(String) @client = client @name = name self.n_val ||= [:n_val] self.allow_mult ||= [:allow_mult] @key_cache = Hash.new{|k,v| k[v] = Riakpb::Key.new(self, v)} end |
Instance Attribute Details
#allow_mult ⇒ true, false
Returns whether the bucket allows divergent siblings.
20 21 22 |
# File 'lib/riakpb/bucket.rb', line 20 def allow_mult @allow_mult end |
#client ⇒ Riakpb::Client (readonly)
Returns the associated client.
11 12 13 |
# File 'lib/riakpb/bucket.rb', line 11 def client @client end |
#key_cache ⇒ Object (readonly)
Returns the value of attribute key_cache.
22 23 24 |
# File 'lib/riakpb/bucket.rb', line 22 def key_cache @key_cache end |
#n_val ⇒ Fixnum
Returns the N value, or number of replicas for this bucket.
17 18 19 |
# File 'lib/riakpb/bucket.rb', line 17 def n_val @n_val end |
#name ⇒ String (readonly)
Returns the bucket name.
14 15 16 |
# File 'lib/riakpb/bucket.rb', line 14 def name @name end |
Instance Method Details
#delete(key, rw = nil) ⇒ Object
Deletes a key from the bucket
137 138 139 140 141 |
# File 'lib/riakpb/bucket.rb', line 137 def delete(key, rw=nil) key = key.name if key.is_a?(Riakpb::Key) @client.del_request(@name, key, rw) end |
#destroy!(rw = nil) ⇒ Object
Wipes out all keys stored in the bucket, as of execution
147 148 149 150 151 152 153 154 |
# File 'lib/riakpb/bucket.rb', line 147 def destroy!(rw=nil) keys! @keys.each do |key| @client.del_request(@name, key, rw) end # super.destroy end |
#get_linked(bucket, key, options = nil) ⇒ Riakpb::Key
Retrieves a Key from the given Bucket. Originally written for link retrieval.
102 103 104 |
# File 'lib/riakpb/bucket.rb', line 102 def get_linked(bucket, key, =nil) @client[bucket].key(key, ) end |
#inspect ⇒ String
Returns a representation suitable for IRB and debugging output.
196 197 198 |
# File 'lib/riakpb/bucket.rb', line 196 def inspect "#<Riakpb::Bucket name=#{@name}, props={n_val=>#{@n_val}, allow_mult=#{@allow_mult}}>" end |
#inspect! ⇒ String
Returns a representation suitable for IRB and debugging output, including keys within this bucket.
201 202 203 |
# File 'lib/riakpb/bucket.rb', line 201 def inspect! "#<Riakpb::Bucket name=#{@name}, props={n_val=>#{@n_val}, allow_mult=#{@allow_mult}}, keys=#{keys.inspect}>" end |
#junkshot(key, params) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/riakpb/bucket.rb', line 117 def junkshot(key, params) raise RuntimeError.new t('siblings_disallowed') unless @allow_mult == true params[:links] = parse_links(params[:links]) if params.has_key?(:links) params[:usermeta] = parse_links(params[:usermeta]) if params.has_key?(:usermeta) = params.slice :return_body, :w, :dw content = params.slice :value, :content_type, :charset, :content_encoding, :links, :usermeta key = key.name if key.is_a?(Riakpb::Key) [:key] = key [:content] = Riakpb::RpbContent.new(content) self.store() end |
#key(key, options = {}) ⇒ Riakpb::Key Also known as: []
Retrieve an object from within the bucket.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/riakpb/bucket.rb', line 70 def key(key, ={}) raise ArgumentError, t("fixnum_invalid", :num => [:r]) unless [:r].is_a?(NilClass) or [:r].is_a?(Fixnum) raise ArgumentError, t("string_invalid", :string => key) unless key.is_a?(String) if [:safely] == true and not @key_cache[key].empty? return(@key_cache[key]) end response = @client.get_request @name, key, [:r] @key_cache[key].load(response) end |
#key!(key, r = nil) ⇒ Riakpb::Key
Retrieve an object from within the bucket. Will raise an error message if key does not exist.
88 89 90 91 92 93 94 95 |
# File 'lib/riakpb/bucket.rb', line 88 def key!(key, r=nil) raise ArgumentError, t("string_invalid", :string => key) unless key.is_a?(String) raise ArgumentError, t("fixnum_invalid", :num => r) unless r.is_a?(Fixnum) or r.nil? response = @client.get_request @name, key, r Riakpb::Key.new(self, key).load!(response) end |
#keys ⇒ Array<String>
Accesses or retrieves a list of keys in this bucket. Needs to have expiration / cacheing, though not now.
56 57 58 |
# File 'lib/riakpb/bucket.rb', line 56 def keys @keys ||= @client.keys_in @name end |
#keys! ⇒ Array<String>
Accesses or retrieves a list of keys in this bucket. Needs to have expiration / cacheing, though not now.
62 63 64 |
# File 'lib/riakpb/bucket.rb', line 62 def keys! @keys = @client.keys_in @name end |
#load(response) ⇒ Bucket
Load information for the bucket from a response given by the Client::HTTPBackend. Used mostly internally - use Client#bucket to get a Riakpb::Bucket instance.
44 45 46 47 48 49 50 51 52 |
# File 'lib/riakpb/bucket.rb', line 44 def load(response) if response.is_a?(Riakpb::RpbGetBucketResp) @n_val = response.props.n_val @allow_mult = response.props.allow_mult return(self) end raise ArgumentError, t("response_type") end |
#store(options) ⇒ RpbPutResp
Retrieves a Key from the given Bucket. Originally written for link retrieval. Inserts a key in this bucket’s namespace, into riak.
112 113 114 115 |
# File 'lib/riakpb/bucket.rb', line 112 def store() [:bucket] = @name @client.put_request() end |