Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/riak/bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ def get(key, options = {})
# @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.perform @client, pairs
results.keys.inject(Hash.new) do |mem, var|
mem[var[1]] = results[var]
mem
end
perform_multi(Multiget, keys)
end

# Check multiple keys from this bucket.
# @param [Array<String>] keys array of keys to check
# @return [Hash<String, Riak::RObject>] hash of keys to true/false
def exist_many(keys)
perform_multi(Multiexist, keys)
end

# Create a new blank object
Expand Down Expand Up @@ -192,12 +194,11 @@ 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 = { })
def get_preflist(key, options = {})
type = self.type.name if needs_type?
client.get_preflist self, key, type, options
end
Expand Down Expand Up @@ -298,5 +299,13 @@ def ==(other)
return false unless self.client == other.client
return equal_bytes?(self.name, other.name)
end

private

def perform_multi(klass, keys)
pairs = keys.map { |k| [self, k] }
results = klass.perform @client, pairs
results.each_with_object({}) { |(pair, value), all| all[pair[1]] = value }
end
end
end
78 changes: 76 additions & 2 deletions spec/riak/bucket_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'spec_helper'

SingleCov.covered! if defined?(SingleCov)

describe Riak::Bucket do
before :each do
@client = Riak::Client.new
Expand Down Expand Up @@ -178,8 +180,19 @@

@results = @bucket.get_many %w{key1 key2}

expect(@results['key1']).to eq(@object1)
expect(@results['key2']).to eq(@object2)
expect(@results).to eq('key1' => @object1, 'key2' => @object2)
end
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making this test catch when unexpected keys were added


describe "checking if multiple objects exist" do
it 'checks each object individually' do
expect(@bucket).to receive(:get).with('key1', head: true).and_return(true)
expect(@bucket).to receive(:get).with('key2', head: true).
and_raise(Riak::ProtobuffsFailedRequest.new(:not_found, "not found"))

@results = @bucket.exist_many %w{key1 key2}

expect(@results).to eq('key1' => true, 'key2' => false)
end
end

Expand Down Expand Up @@ -272,4 +285,65 @@
@bucket.delete('bar', :rw => "all")
end
end

describe "Retrieves a preflist" do
it "finds the list" do
expect(@client).to receive(:get_preflist).with(@bucket, 'KEY', nil, {}).and_return(['yep'])
expect(@bucket.get_preflist('KEY')).to eq(['yep'])
end

it "adds the type when needed" do
expect(@bucket).to receive(:type).and_return(double(name: "NAME"))
expect(@bucket).to receive(:needs_type?).and_return(true)
expect(@client).to receive(:get_preflist).with(@bucket, 'KEY', 'NAME', {}).and_return(['yep'])
expect(@bucket.get_preflist('KEY')).to eq(['yep'])
end
end

describe "#enable_index!" do
let(:props) { {'search' => true, 'precommit' => ["old"]} }

before { expect(@client).to receive(:get_bucket_props).and_return(props) }

it "does nothing when enabled" do
@bucket.enable_index!
end

it "enables when disabled" do
props['search'] = false
expect(@client).to receive(:set_bucket_props).
with(@bucket, "precommit" => ["old", {"mod" => "riak_search_kv_hook", "fun" => "precommit"}], "search" => true)
@bucket.enable_index!
end
end

describe "#disable_index!" do
let(:props) { {'search' => true, 'precommit' => ["old", {"mod" => "riak_search_kv_hook", "fun" => "precommit"}]} }

before { expect(@client).to receive(:get_bucket_props).and_return(props) }

it "does nothing when disabled" do
props['search'] = false
props['precommit'] = []
@bucket.disable_index!
end

it "disables when enabled" do
expect(@client).to receive(:set_bucket_props).
with(@bucket, "precommit" => ["old"], "search" => false)
@bucket.disable_index!
end
end

describe "#inspect" do
it "returns a nice name" do
expect(@bucket.inspect).to eq("#<Riak::Bucket {foo}>")
end
end

describe "#pretty_print" do
it "prints nicely" do
expect { pp(@bucket) }.to output("#<Riak::Bucket name=foo>\n").to_stdout
end
end
end