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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ def punsubscribe(*channels)
end
end

# Inspect the state of the Pub/Sub subsystem.
# Inspect the state of the Pub/Sub subsystem.
# Possible subcommands: channels, numsub, numpat.
def pubsub(subcommand, *args)
synchronize do |client|
Expand Down
54 changes: 50 additions & 4 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,15 @@ def setnx(key, value)

# Set multiple keys to multiple values.
def mset(*args)
raise CannotDistribute, :mset
args.flatten!
node_args_hash = split_args_for_nodes(args)
node_args_hash.each do |node, part_args|
node.mset *part_args
end
end

def mapped_mset(hash)
raise CannotDistribute, :mapped_mset
mset(hash.to_a.flatten)
end

# Set multiple keys to multiple values, only if none of the keys exist.
Expand All @@ -279,11 +283,25 @@ def get(key)

# Get the values of all the given keys.
def mget(*keys)
raise CannotDistribute, :mget
# Split keys based on node
node_keys_hash = split_keys_for_nodes(keys)

# mget with node & set value to result
result = Array.new keys.size
node_keys_hash.each do |node, payload|
values = node.mget(*payload[:keys])

values.each_with_index do |value, index|
key_index = payload[:index][index]
result[key_index] = value
end
end
result
end

def mapped_mget(*keys)
raise CannotDistribute, :mapped_mget
values = mget(*keys)
Hash[keys.zip(values)]
end

# Overwrite part of a string at key starting at the specified offset.
Expand Down Expand Up @@ -839,6 +857,34 @@ def dup

protected

def split_keys_for_nodes(keys)
result = Hash.new
keys.each_with_index do |key, index|
node = node_for(key)
if !(payload = result[node])
payload = result[node] = {}
payload[:keys] = []
payload[:index] = []
end
payload[:keys] << key
payload[:index] << index
end
result
end

def split_args_for_nodes(args)
result = Hash.new
args.each_slice(2) do |key, value|
node = node_for(key)
if !(payload = result[node])
payload = result[node] = []
end
payload << key
payload << value
end
result
end

def on_each_node(command, *args)
nodes.map do |node|
node.send(command, *args)
Expand Down
38 changes: 26 additions & 12 deletions test/distributed_commands_on_strings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,41 @@ 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
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
Expand Down
6 changes: 3 additions & 3 deletions test/lint/hashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ def test_hincrbyfloat
target_version "2.5.4" do
r.hincrbyfloat("foo", "f1", 1.23)

assert_equal "1.23", r.hget("foo", "f1")
assert_equal "1.23", r.hget("foo", "f1").to_f.round(2).to_s

r.hincrbyfloat("foo", "f1", 0.77)

assert_equal "2", r.hget("foo", "f1")
assert_equal "2", r.hget("foo", "f1").to_f.round(0).to_s

r.hincrbyfloat("foo", "f1", -0.1)

assert_equal "1.9", r.hget("foo", "f1")
assert_equal "1.9", r.hget("foo", "f1").to_f.round(1).to_s
end
end
end
Expand Down