lib/riak/bucket.rb in riak-client-0.9.8 vs lib/riak/bucket.rb in riak-client-1.0.0.beta
- old
+ new
@@ -1,32 +1,19 @@
-# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
require 'riak/util/translation'
-require 'riak/util/escape'
require 'riak/client'
require 'riak/robject'
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.
class Bucket
include Util::Translation
- include Util::Escape
+ # (Riak Search) The precommit specification for kv/search integration
+ SEARCH_PRECOMMIT_HOOK = {"mod" => "riak_search_kv_hook", "fun" => "precommit"}
+
# @return [Riak::Client] the associated client
attr_reader :client
# @return [String] the bucket name
attr_reader :name
@@ -38,26 +25,25 @@
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
- # Accesses or retrieves a list of keys in this bucket.
+ # 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 retained in the local Bucket object.
- # @param [Hash] options extra options
+ # results of the operation will not be returned to the caller.
# @yield [Array<String>] a list of keys from the current chunk
- # @option options [Boolean] :reload (false) If present, will force reloading of the bucket's keys from Riak
# @return [Array<String>] Keys in this bucket
- def keys(options={}, &block)
+ # @note This operation has serious performance implications and
+ # should not be used in production applications.
+ def keys(&block)
+ warn(t('list_keys', :backtrace => caller.join("\n "))) unless Riak.disable_list_keys_warnings
if block_given?
- @client.backend.list_keys(self, &block)
- @keys = nil
- elsif @keys.nil? || options[:reload]
- @keys = @client.backend.list_keys(self)
+ @client.list_keys(self, &block)
+ else
+ @client.list_keys(self)
end
- @keys
end
# Sets internal properties on the bucket
# Note: this results in a request to the Riak server!
# @param [Hash] properties new properties for the bucket
@@ -78,30 +64,30 @@
# @raise [FailedRequest] if the new properties were not accepted by the Riakserver
# @see #n_value, #allow_mult, #r, #w, #dw, #rw
def props=(properties)
raise ArgumentError, t("hash_type", :hash => properties.inspect) unless Hash === properties
props.merge!(properties)
- @client.backend.set_bucket_props(self, properties)
+ @client.set_bucket_props(self, properties)
props
end
alias :'properties=' :'props='
# @return [Hash] Internal Riak bucket properties.
# @see #props=
def props
- @props ||= @client.backend.get_bucket_props(self)
+ @props ||= @client.get_bucket_props(self)
end
alias :properties :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
# @return [Riak::RObject] the object
# @raise [FailedRequest] if the object is not found or some other error occurs
def get(key, options={})
- @client.backend.fetch_object(self, key, options[:r])
+ @client.get_object(self, key, options)
end
alias :[] :get
# Create a new blank object
# @param [String] key the key of the new object
@@ -143,15 +129,29 @@
alias :exist? :exists?
# 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 [Fixnum] :rw - the read/write quorum for the
+ # delete
+ # @option options [String] :vclock - the vector clock of the
+ # object being deleted
def delete(key, options={})
- client.backend.delete_object(self, key, options[:rw])
+ 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)
+ client.get_index(self, index, query)
+ end
+
# @return [true, false] whether the bucket allows divergent siblings
def allow_mult
props['allow_mult']
end
@@ -175,20 +175,38 @@
self.props = {'n_val' => value}
value
end
alias :'n_val=' :'n_value='
- [:r,:w,:dw,:rw].each do |q|
- class_eval <<-CODE
- def #{q}
- props["#{q}"]
- end
+ %w(r w dw rw).each do |q|
+ define_method(q) { props[q] }
+ define_method("#{q}=") { |value|
+ self.props = { q => value }
+ value
+ }
+ end
- def #{q}=(value)
- self.props = {"#{q}" => value}
- value
- end
- CODE
+ # (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}
+ end
+ end
+
+ # (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}
+ 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['precommit'].include?(SEARCH_PRECOMMIT_HOOK)
end
# @return [String] a representation suitable for IRB and debugging output
def inspect
"#<Riak::Bucket {#{name}}#{" keys=[#{keys.join(',')}]" if defined?(@keys)}>"