lib/riak/bucket.rb in riak-client-2.2.0.pre1 vs lib/riak/bucket.rb in riak-client-2.2.0

- old
+ new

@@ -35,11 +35,11 @@ # results of the operation will not be returned to the caller. # @yield [Array<String>] a list of keys from the current chunk # @return [Array<String>] Keys in this bucket # @note This operation has serious performance implications and # should not be used in production applications. - def keys(options={}, &block) + def keys(options = {}, &block) warn(t('list_keys', :backtrace => caller.join("\n "))) unless Riak.disable_list_keys_warnings if block_given? @client.list_keys(self, options, &block) else @client.list_keys(self, options) @@ -49,11 +49,12 @@ # Sets internal properties on the bucket # Note: this results in a request to the Riak server! # @param [Hash] properties new properties for the bucket # @option properties [Fixnum] :n_val (3) The N value (replication factor) # @option properties [true,false] :allow_mult (false) Whether to permit object siblings - # @option properties [true,false] :last_write_wins (false) Whether to ignore vclocks + # @option properties [true,false] :last_write_wins (false) Whether to ignore + # causal context in regular key-value buckets # @option properties [Array<Hash>] :precommit ([]) precommit hooks # @option properties [Array<Hash>] :postcommit ([])postcommit hooks # @option properties [Fixnum,String] :r ("quorum") read quorum (numeric or # symbolic) # @option properties [Fixnum,String] :w ("quorum") write quorum (numeric or @@ -90,40 +91,46 @@ alias :clear_properties :clear_props # Retrieve an object from within the bucket. # @param [String] key the key of the object to retrieve # @param [Hash] options query parameters for the request - # @option options [Fixnum] :r - the read quorum for the request - how many nodes should concur on the read + # @option options [Fixnum] :r - the read quorum for the request - how many + # nodes should concur on the read # @return [Riak::RObject] the object - # @raise [FailedRequest] if the object is not found or some other error occurs - def get(key, options={}) + # @raise [FailedRequest] if the object is not found or some other error + # occurs + def get(key, options = {}) @client.get_object(self, key, options) end alias :[] :get # Retrieve multiple keys from this bucket. # @param [Array<String>] keys array of keys to fetch # @return [Hash<String, Riak::RObject>] hash of keys to objects def get_many(keys) pairs = keys.map{|k| [self, k]} results = Multiget.get_all @client, pairs - results.keys.inject(Hash.new) { |mem, var| mem[var[1]] = results[var]; mem } + results.keys.inject(Hash.new) do |mem, var| + mem[var[1]] = results[var] + mem + end end # Create a new blank object # @param [String] key the key of the new object # @return [RObject] the new, unsaved object - def new(key=nil) + def new(key = nil) RObject.new(self, key).tap do |obj| obj.content_type = "application/json" end end - # Fetches an object if it exists, otherwise creates a new one with the given key + # Fetches an object if it exists, otherwise creates a new one with the + # given key # @param [String] key the key to fetch or create # @return [RObject] the new or existing object - def get_or_new(key, options={}) + def get_or_new(key, options = {}) begin get(key, options) rescue Riak::FailedRequest => fr if fr.not_found? new(key) @@ -144,11 +151,11 @@ # Checks whether an object exists in Riak. # @param [String] key the key to check # @param [Hash] options quorum options # @option options [Fixnum] :r - the read quorum value for the request (R) # @return [true, false] whether the key exists in this bucket - def exists?(key, options={}) + def exists?(key, options = {}) begin get(key, options.merge({ :head => true })) true rescue Riak::FailedRequest => e raise e unless e.not_found? @@ -160,33 +167,44 @@ # Deletes a key from the bucket # @param [String] key the key to delete # @param [Hash] options quorum options # @option options [Fixnum] :rw - the read/write quorum for the # delete - # @option options [String] :vclock - the vector clock of the + # @option options [String] :vclock - the causal context/vector clock of the # object being deleted - def delete(key, options={}) + def delete(key, options = {}) client.delete_object(self, key, options) end # Queries a secondary index on the bucket. # @note This will only work if your Riak installation supports 2I. # @param [String] index the name of the index # @param [String,Integer,Range] query the value of the index, or a # Range of values to query # @return [Array<String>] a list of keys that match the index # query - def get_index(index, query, options={}) + def get_index(index, query, options = {}) client.get_index(self, index, query, options) end + + # Retrieves a preflist for the given key; useful for + # figuring out where in the cluster an object is stored. + # @param [String] key the key + # @return [Array<PreflistItem>] an array of preflist entries + def get_preflist(key, options = { }) + type = self.type.name if needs_type? + client.get_preflist self, key, type, options + end + # @return [true, false] whether the bucket allows divergent siblings def allow_mult props['allow_mult'] end - # Set the allow_mult property. *NOTE* This will result in a PUT request to Riak. + # Set the allow_mult property. *NOTE* This will result in a PUT request to + # Riak. # @param [true, false] value whether the bucket should allow siblings def allow_mult=(value) self.props = {'allow_mult' => value} value end @@ -195,13 +213,15 @@ def n_value props['n_val'] end alias :n_val :n_value - # 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. - # @param [Fixnum] value the number of replicas the bucket should keep of each object + # 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. + # @param [Fixnum] value the number of replicas the bucket should keep of + # each object def n_value=(value) self.props = {'n_val' => value} value end alias :'n_val=' :'n_value=' @@ -216,27 +236,36 @@ # (Riak Search) Installs a precommit hook that automatically indexes objects # into riak_search. def enable_index! unless is_indexed? - self.props = {"precommit" => (props['precommit'] + [SEARCH_PRECOMMIT_HOOK]), "search" => true} + self.props = { + "precommit" => (props['precommit'] + [SEARCH_PRECOMMIT_HOOK]), + "search" => true + } end end - # (Riak Search) Removes the precommit hook that automatically indexes objects - # into riak_search. + # (Riak Search) Removes the precommit hook that automatically indexes + # objects into riak_search. def disable_index! if is_indexed? - self.props = {"precommit" => (props['precommit'] - [SEARCH_PRECOMMIT_HOOK]), "search" => false} + self.props = { + "precommit" => (props['precommit'] - [SEARCH_PRECOMMIT_HOOK]), + "search" => false + } end end # (Riak Search) Detects whether the bucket is automatically indexed into # riak_search. # @return [true,false] whether the bucket includes the search indexing hook def is_indexed? - props['search'] == true || (props.has_key?('precommit') && props['precommit'].include?(SEARCH_PRECOMMIT_HOOK)) + return true if props['search'] == true + return true if props.has_key?('precommit') && + props['precommit'].include?(SEARCH_PRECOMMIT_HOOK) + false end # @return [String] a representation suitable for IRB and debugging output def inspect "#<Riak::Bucket {#{name}}>" @@ -257,9 +286,12 @@ false end # @return [true,false] whether the other is equivalent def ==(other) - Bucket === other && other.client == client && other.name == name + return false unless self.class == other.class + return false unless self.client == other.client + return false unless self.name == other.name + true end end end