lib/riak/bucket.rb in riak-client-1.2.0 vs lib/riak/bucket.rb in riak-client-1.4.0
- old
+ new
@@ -1,8 +1,9 @@
require 'riak/util/translation'
require 'riak/client'
require 'riak/robject'
+require 'riak/counter'
require 'riak/failed_request'
module Riak
# 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.
@@ -22,10 +23,11 @@
# @param [Client] client the {Riak::Client} for this bucket
# @param [String] name the name of the bucket
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
+ raise ArgumentError, t('zero_length_bucket') if name == ''
@client, @name = client, name
end
# Retrieves a list of keys in this bucket.
# If a block is given, keys will be streamed through
@@ -33,16 +35,16 @@
# 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(&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, &block)
+ @client.list_keys(self, options, &block)
else
- @client.list_keys(self)
+ @client.list_keys(self, options)
end
end
# Sets internal properties on the bucket
# Note: this results in a request to the Riak server!
@@ -96,10 +98,19 @@
def get(key, options={})
@client.get_object(self, key, options)
end
alias :[] :get
+ # Retrieve multiple keys from this bucket.
+ # @param [Array<String>] 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 }
+ 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)
RObject.new(self, key).tap do |obj|
@@ -120,10 +131,18 @@
raise fr
end
end
end
+ # Gets a counter object. Counters initially hvae a value of zero, and can be
+ # incremented and decremented by integer values.
+ # @param [String] key the key of the counter to fetch
+ # @return [Counter]
+ def counter(key)
+ Riak::Counter.new self, key
+ end
+
# 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
@@ -153,11 +172,11 @@
# @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)
- client.get_index(self, index, query)
+ def get_index(index, query, options={})
+ client.get_index(self, index, query, options)
end
# @return [true, false] whether the bucket allows divergent siblings
def allow_mult
props['allow_mult']