Skip to content

Commit 2e46039

Browse files
committed
Distributed: support mget and mapped_mget
Map the keys to their nodes, mget them on each node, and stitch the results together.
1 parent ea9f1d2 commit 2e46039

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/redis/distributed.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,16 @@ def get(key)
277277
node_for(key).get(key)
278278
end
279279

280-
# Get the values of all the given keys.
280+
# Get the values of all the given keys as an Array.
281281
def mget(*keys)
282-
raise CannotDistribute, :mget
282+
mapped_mget(*keys).values_at(*keys)
283283
end
284284

285+
# Get the values of all the given keys as a Hash.
285286
def mapped_mget(*keys)
286-
raise CannotDistribute, :mapped_mget
287+
keys.group_by { |k| node_for k }.inject({}) do |results, (node, subkeys)|
288+
results.merge! node.mapped_mget(*subkeys)
289+
end
287290
end
288291

289292
# Overwrite part of a string at key starting at the specified offset.

test/distributed_commands_on_strings_test.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,27 @@ class TestDistributedCommandsOnStrings < Test::Unit::TestCase
99
include Lint::Strings
1010

1111
def test_mget
12-
assert_raise Redis::Distributed::CannotDistribute do
13-
r.mget("foo", "bar")
14-
end
12+
r.set("foo", "s1")
13+
r.set("bar", "s2")
14+
15+
assert_equal ["s1", "s2"] , r.mget("foo", "bar")
16+
assert_equal ["s1", "s2", nil], r.mget("foo", "bar", "baz")
1517
end
1618

1719
def test_mget_mapped
18-
assert_raise Redis::Distributed::CannotDistribute do
19-
r.mapped_mget("foo", "bar")
20-
end
20+
r.set("foo", "s1")
21+
r.set("bar", "s2")
22+
23+
response = r.mapped_mget("foo", "bar")
24+
25+
assert_equal "s1", response["foo"]
26+
assert_equal "s2", response["bar"]
27+
28+
response = r.mapped_mget("foo", "bar", "baz")
29+
30+
assert_equal "s1", response["foo"]
31+
assert_equal "s2", response["bar"]
32+
assert_equal nil , response["baz"]
2133
end
2234

2335
def test_mset

0 commit comments

Comments
 (0)