Class: Riak::Bucket
- Includes:
- Util::Translation
- Defined in:
- lib/riak/bucket.rb
Overview
Represents and encapsulates operations on a Riak bucket. You may retrieve a bucket using Client#bucket, or create it manually and retrieve its meta-information later.
Constant Summary collapse
- SEARCH_PRECOMMIT_HOOK =
(Riak Search) The precommit specification for kv/search integration
{"mod" => "riak_search_kv_hook", "fun" => "precommit"}
Instance Attribute Summary collapse
-
#client ⇒ Riak::Client
readonly
The associated client.
-
#name ⇒ String
readonly
The bucket name.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Whether the other is equivalent.
-
#allow_mult ⇒ true, false
Whether the bucket allows divergent siblings.
-
#allow_mult=(value) ⇒ Object
Set the allow_mult property.
-
#delete(key, options = {}) ⇒ Object
Deletes a key from the bucket.
-
#disable_index! ⇒ Object
(Riak Search) Removes the precommit hook that automatically indexes objects into riak_search.
-
#enable_index! ⇒ Object
(Riak Search) Installs a precommit hook that automatically indexes objects into riak_search.
-
#exists?(key, options = {}) ⇒ true, false
(also: #exist?)
Checks whether an object exists in Riak.
-
#get(key, options = {}) ⇒ Riak::RObject
(also: #[])
Retrieve an object from within the bucket.
-
#get_index(index, query) ⇒ Array<String>
Queries a secondary index on the bucket.
-
#get_or_new(key, options = {}) ⇒ RObject
Fetches an object if it exists, otherwise creates a new one with the given key.
-
#initialize(client, name) ⇒ Bucket
constructor
Create a Riak bucket manually.
-
#inspect ⇒ String
A representation suitable for IRB and debugging output.
-
#is_indexed? ⇒ true, false
(Riak Search) Detects whether the bucket is automatically indexed into riak_search.
-
#keys {|Array<String>| ... } ⇒ Array<String>
Retrieves a list of keys in this bucket.
-
#n_value ⇒ Fixnum
(also: #n_val)
The N value, or number of replicas for this bucket.
-
#n_value=(value) ⇒ Object
(also: #n_val=)
Set the N value (number of replicas).
-
#new(key = nil) ⇒ RObject
Create a new blank object.
-
#props ⇒ Hash
(also: #properties)
Internal Riak bucket properties.
-
#props=(properties) ⇒ Hash
(also: #properties=)
Sets internal properties on the bucket Note: this results in a request to the Riak server! symbolic) symbolic) (numeric or symbolic) symbolic).
Methods included from Util::Translation
Constructor Details
#initialize(client, name) ⇒ Bucket
Create a Riak bucket manually.
24 25 26 27 28 |
# File 'lib/riak/bucket.rb', line 24 def initialize(client, name) raise ArgumentError, t("client_type", :client => client.inspect) unless Client === client raise ArgumentError, t("string_type", :string => name.inspect) unless String === name @client, @name = client, name end |
Instance Attribute Details
#client ⇒ Riak::Client (readonly)
Returns the associated client.
16 17 18 |
# File 'lib/riak/bucket.rb', line 16 def client @client end |
#name ⇒ String (readonly)
Returns the bucket name.
19 20 21 |
# File 'lib/riak/bucket.rb', line 19 def name @name end |
Instance Method Details
#==(other) ⇒ true, false
Returns whether the other is equivalent.
217 218 219 |
# File 'lib/riak/bucket.rb', line 217 def ==(other) Bucket === other && other.client == client && other.name == name end |
#allow_mult ⇒ true, false
Returns whether the bucket allows divergent siblings.
154 155 156 |
# File 'lib/riak/bucket.rb', line 154 def allow_mult props['allow_mult'] end |
#allow_mult=(value) ⇒ Object
Set the allow_mult property. NOTE This will result in a PUT request to Riak.
160 161 162 163 |
# File 'lib/riak/bucket.rb', line 160 def allow_mult=(value) self.props = {'allow_mult' => value} value end |
#delete(key, options = {}) ⇒ Object
Deletes a key from the bucket
138 139 140 |
# File 'lib/riak/bucket.rb', line 138 def delete(key, ={}) client.delete_object(self, key, ) end |
#disable_index! ⇒ Object
(Riak Search) Removes the precommit hook that automatically indexes objects into riak_search.
198 199 200 201 202 |
# File 'lib/riak/bucket.rb', line 198 def disable_index! if is_indexed? self.props = {"precommit" => (props['precommit'] - [SEARCH_PRECOMMIT_HOOK]), "search" => false} end end |
#enable_index! ⇒ Object
(Riak Search) Installs a precommit hook that automatically indexes objects into riak_search.
190 191 192 193 194 |
# File 'lib/riak/bucket.rb', line 190 def enable_index! unless is_indexed? self.props = {"precommit" => (props['precommit'] + [SEARCH_PRECOMMIT_HOOK]), "search" => true} end end |
#exists?(key, options = {}) ⇒ true, false Also known as: exist?
Checks whether an object exists in Riak.
121 122 123 124 125 126 127 128 |
# File 'lib/riak/bucket.rb', line 121 def exists?(key, ={}) begin get(key, ) true rescue Riak::FailedRequest false end end |
#get(key, options = {}) ⇒ Riak::RObject Also known as: []
Retrieve an object from within the bucket.
87 88 89 |
# File 'lib/riak/bucket.rb', line 87 def get(key, ={}) @client.get_object(self, key, ) end |
#get_index(index, query) ⇒ Array<String>
This will only work if your Riak installation supports 2I.
Queries a secondary index on the bucket.
149 150 151 |
# File 'lib/riak/bucket.rb', line 149 def get_index(index, query) client.get_index(self, index, query) end |
#get_or_new(key, options = {}) ⇒ RObject
Fetches an object if it exists, otherwise creates a new one with the given key
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/riak/bucket.rb', line 104 def get_or_new(key, ={}) begin get(key, ) rescue Riak::FailedRequest => fr if fr.not_found? new(key) else raise fr end end end |
#inspect ⇒ String
Returns a representation suitable for IRB and debugging output.
212 213 214 |
# File 'lib/riak/bucket.rb', line 212 def inspect "#<Riak::Bucket {#{name}}#{" keys=[#{keys.join(',')}]" if defined?(@keys)}>" end |
#is_indexed? ⇒ true, false
(Riak Search) Detects whether the bucket is automatically indexed into riak_search.
207 208 209 |
# File 'lib/riak/bucket.rb', line 207 def is_indexed? props['search'] == true || props['precommit'].include?(SEARCH_PRECOMMIT_HOOK) end |
#keys {|Array<String>| ... } ⇒ Array<String>
This operation has serious performance implications and should not be used in production applications.
Retrieves a list of keys in this bucket. If a block is given, keys will be streamed through the block (useful for large buckets). When streaming, results of the operation will not be returned to the caller.
38 39 40 41 42 43 44 45 |
# File 'lib/riak/bucket.rb', line 38 def keys(&block) warn(t('list_keys', :backtrace => caller.join("\n "))) unless Riak.disable_list_keys_warnings if block_given? @client.list_keys(self, &block) else @client.list_keys(self) end end |
#n_value ⇒ Fixnum Also known as: n_val
Returns the N value, or number of replicas for this bucket.
166 167 168 |
# File 'lib/riak/bucket.rb', line 166 def n_value props['n_val'] end |
#n_value=(value) ⇒ Object Also known as: n_val=
Set the N value (number of replicas). NOTE This will result in a PUT request to Riak. Setting this value after the bucket has objects stored in it may have unpredictable results.
174 175 176 177 |
# File 'lib/riak/bucket.rb', line 174 def n_value=(value) self.props = {'n_val' => value} value end |
#new(key = nil) ⇒ RObject
Create a new blank object
95 96 97 98 99 |
# File 'lib/riak/bucket.rb', line 95 def new(key=nil) RObject.new(self, key).tap do |obj| obj.content_type = "application/json" end end |
#props ⇒ Hash Also known as: properties
Returns Internal Riak bucket properties.
76 77 78 |
# File 'lib/riak/bucket.rb', line 76 def props @props ||= @client.get_bucket_props(self) end |
#props=(properties) ⇒ Hash Also known as: properties=
Sets internal properties on the bucket Note: this results in a request to the Riak server! symbolic) symbolic) (numeric or symbolic) symbolic)
66 67 68 69 70 71 |
# File 'lib/riak/bucket.rb', line 66 def props=(properties) raise ArgumentError, t("hash_type", :hash => properties.inspect) unless Hash === properties props.merge!(properties) @client.set_bucket_props(self, properties) props end |