Skip to content

Commit b2532e6

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 b2532e6

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/redis/distributed.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,15 @@ def get(key)
279279

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

285285
def mapped_mget(*keys)
286-
raise CannotDistribute, :mapped_mget
286+
results = {}
287+
keys.group_by { |k| node_for k }.each do |node, subkeys|
288+
results.merge! node.mapped_mget(*subkeys)
289+
end
290+
results
287291
end
288292

289293
# 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)