From db567c470134d4e5b6fb752de04cc68817720b18 Mon Sep 17 00:00:00 2001 From: Adrian Gonzalez Date: Thu, 13 Nov 2014 01:11:58 -0800 Subject: [PATCH 1/3] add mget and mapped_mget to Redis::Distributed --- lib/redis/distributed.rb | 13 ++++++++--- test/distributed_commands_on_strings_test.rb | 24 +++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index 4bda232a4..ef472b814 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -279,11 +279,18 @@ def get(key) # Get the values of all the given keys. def mget(*keys) - raise CannotDistribute, :mget + result = mapped_mget(*keys) + keys.map { |key| result[key] } end - def mapped_mget(*keys) - raise CannotDistribute, :mapped_mget + def mapped_mget(*mget_keys) + keys_per_node = mget_keys.group_by { |key| node_for(key) } + + result = keys_per_node.each_with_object({}) do |(node, keys), accum| + values = node.mget(*keys) + accum.merge!(Hash[keys.zip(values)]) + end + result end # Overwrite part of a string at key starting at the specified offset. diff --git a/test/distributed_commands_on_strings_test.rb b/test/distributed_commands_on_strings_test.rb index ad83c12e5..e8a7f5456 100644 --- a/test/distributed_commands_on_strings_test.rb +++ b/test/distributed_commands_on_strings_test.rb @@ -9,15 +9,27 @@ class TestDistributedCommandsOnStrings < Test::Unit::TestCase include Lint::Strings def test_mget - assert_raise Redis::Distributed::CannotDistribute do - r.mget("foo", "bar") - end + r.set("foo", "s1") + r.set("bar", "s2") + + assert_equal ["s1", "s2"] , r.mget("foo", "bar") + assert_equal ["s1", "s2", nil], r.mget("foo", "bar", "baz") end def test_mget_mapped - assert_raise Redis::Distributed::CannotDistribute do - r.mapped_mget("foo", "bar") - end + r.set("foo", "s1") + r.set("bar", "s2") + + response = r.mapped_mget("foo", "bar") + + assert_equal "s1", response["foo"] + assert_equal "s2", response["bar"] + + response = r.mapped_mget("foo", "bar", "baz") + + assert_equal "s1", response["foo"] + assert_equal "s2", response["bar"] + assert_equal nil , response["baz"] end def test_mset From f28a03229307422b8aa7280949ff54199cb24259 Mon Sep 17 00:00:00 2001 From: Adrian Gonzalez Date: Thu, 13 Nov 2014 01:57:35 -0800 Subject: [PATCH 2/3] add mset and mapped_mset to Redis::Distributed --- lib/redis/distributed.rb | 10 +++++++--- test/distributed_commands_on_strings_test.rb | 14 ++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index ef472b814..092ce9e75 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -255,12 +255,16 @@ def setnx(key, value) end # Set multiple keys to multiple values. - def mset(*args) - raise CannotDistribute, :mset + def mset(*keys) + mapped_mset(Hash[keys.each_slice(2).to_a]) end def mapped_mset(hash) - raise CannotDistribute, :mapped_mset + keys_per_node = hash.group_by { |key, value| node_for(key) } + + keys_per_node.map do |node, mset| + node.mapped_mset(Hash[mset]) + end end # Set multiple keys to multiple values, only if none of the keys exist. diff --git a/test/distributed_commands_on_strings_test.rb b/test/distributed_commands_on_strings_test.rb index e8a7f5456..f25364159 100644 --- a/test/distributed_commands_on_strings_test.rb +++ b/test/distributed_commands_on_strings_test.rb @@ -33,15 +33,17 @@ def test_mget_mapped end def test_mset - assert_raise Redis::Distributed::CannotDistribute do - r.mset(:foo, "s1", :bar, "s2") - end + r.mset(:foo, "s1", :bar, "s2") + + assert_equal "s1", r.get("foo") + assert_equal "s2", r.get("bar") end def test_mset_mapped - assert_raise Redis::Distributed::CannotDistribute do - r.mapped_mset(:foo => "s1", :bar => "s2") - end + r.mapped_mset(:foo => "s1", :bar => "s2") + + assert_equal "s1", r.get("foo") + assert_equal "s2", r.get("bar") end def test_msetnx From dfced1c00288f102713b3378cd7a4a5a0220f4fb Mon Sep 17 00:00:00 2001 From: Adrian Gonzalez Date: Thu, 13 Nov 2014 15:59:19 -0800 Subject: [PATCH 3/3] replace each_with_object to inject to support ruby 1.8.7 --- lib/redis/distributed.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index 092ce9e75..e37c27440 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -290,7 +290,7 @@ def mget(*keys) def mapped_mget(*mget_keys) keys_per_node = mget_keys.group_by { |key| node_for(key) } - result = keys_per_node.each_with_object({}) do |(node, keys), accum| + result = keys_per_node.inject({}) do |accum, (node, keys)| values = node.mget(*keys) accum.merge!(Hash[keys.zip(values)]) end