lib/riak/bucket.rb in ripple-0.5.1 vs lib/riak/bucket.rb in ripple-0.6.0
- old
+ new
@@ -32,11 +32,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
- @client, @name = client, name
+ @client, @name, @props = client, name, {}
end
# Load information for the bucket from a response given by the {Riak::Client::HTTPBackend}.
# Used mostly internally - use {Riak::Client#bucket} to get a {Bucket} instance.
# @param [Hash] response a response from {Riak::Client::HTTPBackend}
@@ -90,14 +90,64 @@
# @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={})
- response = @client.http.get(200, @client.prefix, name, key, options, {})
+ code = allow_mult ? [200,300] : 200
+ response = @client.http.get(code, @client.prefix, name, key, options, {})
RObject.new(self, key).load(response)
end
alias :[] :get
+ # 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|
+ obj.content_type = "application/json"
+ end
+ end
+
+ # 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={})
+ begin
+ get(key, options)
+ rescue Riak::FailedRequest => fr
+ if fr.code.to_i == 404
+ new(key)
+ else
+ raise fr
+ end
+ end
+ 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.
+ # @param [true, false] value whether the bucket should allow siblings
+ def allow_mult=(value)
+ self.props = props.merge('allow_mult' => value)
+ value
+ end
+
+ # @return [Fixnum] the N value, or number of replicas for this bucket
+ def n_value
+ props['n_val']
+ end
+
+ # 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 = props.merge('n_val' => value)
+ value
+ end
+
# @return [String] a representation suitable for IRB and debugging output
def inspect
"#<Riak::Bucket #{client.http.path(client.prefix, name).to_s}#{" keys=[#{keys.join(',')}]" if defined?(@keys)}>"
end
end