diff --git a/lib/redis.rb b/lib/redis.rb index ad1b728e7..692e7174c 100644 --- a/lib/redis.rb +++ b/lib/redis.rb @@ -96,31 +96,31 @@ def select(db) # Get information and statistics about the server. def info(cmd = nil) synchronize do - reply = @client.call [:info, cmd].compact - - if reply.kind_of?(String) - reply = Hash[*reply.split(/:|\r\n/).grep(/^[^#]/)] - - if cmd && cmd.to_s == "commandstats" - # Extract nested hashes for INFO COMMANDSTATS - reply = Hash[reply.map do |k, v| - [k[/^cmdstat_(.*)$/, 1], Hash[*v.split(/,|=/)]] - end] + @client.call [:info, cmd].compact do |reply| + if reply.kind_of?(String) + reply = Hash[*reply.split(/:|\r\n/).grep(/^[^#]/)] + + if cmd && cmd.to_s == "commandstats" + # Extract nested hashes for INFO COMMANDSTATS + reply = Hash[reply.map do |k, v| + [k[/^cmdstat_(.*)$/, 1], Hash[*v.split(/,|=/)]] + end] + end end - end - reply + reply + end end end def config(action, *args) synchronize do - reply = @client.call [:config, action, *args] - - if reply.kind_of?(Array) && action == :get - Hash[*reply] - else - reply + @client.call [:config, action, *args] do |reply| + if reply.kind_of?(Array) && action == :get + Hash[*reply] + else + reply + end end end end @@ -189,9 +189,9 @@ def getset(key, value) end # Get the values of all the given keys. - def mget(*keys) + def mget(*keys, &blk) synchronize do - @client.call [:mget, *keys] + @client.call [:mget, *keys], &blk end end @@ -218,16 +218,16 @@ def strlen(key) # Get all the fields and values in a hash. def hgetall(key) synchronize do - reply = @client.call [:hgetall, key] - - if reply.kind_of?(Array) - hash = Hash.new - reply.each_slice(2) do |field, value| - hash[field] = value + @client.call [:hgetall, key] do |reply| + if reply.kind_of?(Array) + hash = Hash.new + reply.each_slice(2) do |field, value| + hash[field] = value + end + hash + else + reply end - hash - else - reply end end end @@ -256,12 +256,12 @@ def hkeys(key) # Find all keys matching the given pattern. def keys(pattern = "*") synchronize do - reply = @client.call [:keys, pattern] - - if reply.kind_of?(String) - reply.split(" ") - else - reply + @client.call [:keys, pattern] do |reply| + if reply.kind_of?(String) + reply.split(" ") + else + reply + end end end end @@ -304,7 +304,7 @@ def dbsize # Determine if a key exists. def exists(key) synchronize do - _bool @client.call [:exists, key] + @client.call [:exists, key], &_boolify end end @@ -438,20 +438,20 @@ def smembers(key) # Determine if a given value is a member of a set. def sismember(key, member) synchronize do - _bool @client.call [:sismember, key, member] + @client.call [:sismember, key, member], &_boolify end end # Add one or more members to a set. def sadd(key, *members) synchronize do - rv = @client.call [:sadd, key, *members] - - # Compatibility: return boolean when 1 member argument was given. - if members.size == 1 - _bool rv - else - rv + @client.call [:sadd, key, *members] do |reply| + # Compatibility: return boolean when 1 member argument was given. + if members.size == 1 + _boolify.call(reply) + else + reply + end end end end @@ -459,13 +459,13 @@ def sadd(key, *members) # Remove one or more members from a set. def srem(key, *members) synchronize do - rv = @client.call [:srem, key, *members] - - # Compatibility: return boolean when 1 member argument was given. - if members.size == 1 - _bool rv - else - rv + @client.call [:srem, key, *members] do |reply| + # Compatibility: return boolean when 1 member argument was given. + if members.size == 1 + _boolify.call(reply) + else + reply + end end end end @@ -473,7 +473,7 @@ def srem(key, *members) # Move a member from one set to another. def smove(source, destination, member) synchronize do - _bool @client.call [:smove, source, destination, member] + @client.call [:smove, source, destination, member], &_boolify end end @@ -545,7 +545,7 @@ def srandmember(key) def zadd(key, *args) synchronize do if args.size == 2 - _bool @client.call [:zadd, key, args[0], args[1]] + @client.call [:zadd, key, args[0], args[1]], &_boolify elsif !args.empty? && args.size % 2 == 0 @client.call [:zadd, key, *args] else @@ -557,13 +557,13 @@ def zadd(key, *args) # Remove one or more members from a sorted set. def zrem(key, *members) synchronize do - rv = @client.call [:zrem, key, *members] - - # Compatibility: return boolean when 1 member argument was given. - if members.size == 1 - _bool rv - else - rv + @client.call [:zrem, key, *members] do |reply| + # Compatibility: return boolean when 1 member argument was given. + if members.size == 1 + _boolify.call(reply) + else + reply + end end end end @@ -705,14 +705,14 @@ def zunionstore(destination, keys, options = {}) # Move a key to another database. def move(key, db) synchronize do - _bool @client.call [:move, key, db] + @client.call [:move, key, db], &_boolify end end # Set the value of a key, only if the key does not exist. def setnx(key, value) synchronize do - _bool @client.call [:setnx, key, value] + @client.call [:setnx, key, value], &_boolify end end @@ -733,21 +733,21 @@ def rename(old_name, new_name) # Rename a key, only if the new key does not exist. def renamenx(old_name, new_name) synchronize do - _bool @client.call [:renamenx, old_name, new_name] + @client.call [:renamenx, old_name, new_name], &_boolify end end # Set a key's time to live in seconds. def expire(key, seconds) synchronize do - _bool @client.call [:expire, key, seconds] + @client.call [:expire, key, seconds], &_boolify end end # Remove the expiration from a key. def persist(key) synchronize do - _bool @client.call [:persist, key] + @client.call [:persist, key], &_boolify end end @@ -761,21 +761,21 @@ def ttl(key) # Set the expiration for a key as a UNIX timestamp. def expireat(key, unix_time) synchronize do - _bool @client.call [:expireat, key, unix_time] + @client.call [:expireat, key, unix_time], &_boolify end end # Set the string value of a hash field. def hset(key, field, value) synchronize do - _bool @client.call [:hset, key, field, value] + @client.call [:hset, key, field, value], &_boolify end end # Set the value of a hash field, only if the field does not exist. def hsetnx(key, field, value) synchronize do - _bool @client.call [:hsetnx, key, field, value] + @client.call [:hsetnx, key, field, value], &_boolify end end @@ -791,23 +791,23 @@ def mapped_hmset(key, hash) end # Get the values of all the given hash fields. - def hmget(key, *fields) + def hmget(key, *fields, &blk) synchronize do - @client.call [:hmget, key, *fields] + @client.call [:hmget, key, *fields], &blk end end def mapped_hmget(key, *fields) - reply = hmget(key, *fields) - - if reply.kind_of?(Array) - hash = Hash.new - fields.zip(reply).each do |field, value| - hash[field] = value + hmget(key, *fields) do |reply| + if reply.kind_of?(Array) + hash = Hash.new + fields.zip(reply).each do |field, value| + hash[field] = value + end + hash + else + reply end - hash - else - reply end end @@ -842,7 +842,7 @@ def discard # Determine if a hash field exists. def hexists(key, field) synchronize do - _bool @client.call [:hexists, key, field] + @client.call [:hexists, key, field], &_boolify end end @@ -931,16 +931,16 @@ def mapped_msetnx(hash) end def mapped_mget(*keys) - reply = mget(*keys) - - if reply.kind_of?(Array) - hash = Hash.new - keys.zip(reply).each do |field, value| - hash[field] = value + mget(*keys) do |reply| + if reply.kind_of?(Array) + hash = Hash.new + keys.zip(reply).each do |field, value| + hash[field] = value + end + hash + else + reply end - hash - else - reply end end @@ -1180,8 +1180,10 @@ def insert(name) # Commands returning 1 for true and 0 for false may be executed in a pipeline # where the method call will return nil. Propagate the nil instead of falsely # returning false. - def _bool(value) - value == 1 if value + def _boolify + lambda { |value| + value == 1 if value + } end def subscription(method, channels, block) diff --git a/test/commands_on_hashes_test.rb b/test/commands_on_hashes_test.rb index eb0b22919..361dd9b57 100644 --- a/test/commands_on_hashes_test.rb +++ b/test/commands_on_hashes_test.rb @@ -8,7 +8,7 @@ load './test/lint/hashes.rb' -test "Mapped HMGET in a pipeline returns plain array" do |r| +test "Mapped HMGET in a pipeline returns hash" do |r| r.hset("foo", "f1", "s1") r.hset("foo", "f2", "s2") @@ -16,5 +16,5 @@ assert nil == r.mapped_hmget("foo", "f1", "f2") end - assert result[0] == ["s1", "s2"] + assert result[0] == { "f1" => "s1", "f2" => "s2" } end diff --git a/test/commands_on_strings_test.rb b/test/commands_on_strings_test.rb index c34884838..d04c02d68 100644 --- a/test/commands_on_strings_test.rb +++ b/test/commands_on_strings_test.rb @@ -32,7 +32,7 @@ assert nil == response["baz"] end -test "Mapped MGET in a pipeline returns plain array" do |r| +test "Mapped MGET in a pipeline returns hash" do |r| r.set("foo", "s1") r.set("bar", "s2") @@ -40,7 +40,7 @@ assert nil == r.mapped_mget("foo", "bar") end - assert result[0] == ["s1", "s2"] + assert result[0] == { "foo" => "s1", "bar" => "s2" } end test "MSET" do |r| diff --git a/test/pipelining_commands_test.rb b/test/pipelining_commands_test.rb index 1156dbc7c..96051be8b 100644 --- a/test/pipelining_commands_test.rb +++ b/test/pipelining_commands_test.rb @@ -95,29 +95,29 @@ assert "s2" == r.get("bar") end -test "INFO in a pipeline" do |r| +test "INFO in a pipeline returns hash" do |r| result = r.pipelined do r.info end - assert result.first.kind_of?(String) + assert result.first.kind_of?(Hash) end -test "CONFIG GET in a pipeline" do |r| +test "CONFIG GET in a pipeline returns hash" do |r| result = r.pipelined do r.config(:get, "*") end - assert result.first.kind_of?(Array) + assert result.first.kind_of?(Hash) end -test "HGETALL in a pipeline should not return hash" do |r| +test "HGETALL in a pipeline returns hash" do |r| r.hmset("hash", "field", "value") result = r.pipelined do r.hgetall("hash") end - assert ["field", "value"] == result.first + assert result.first == { "field" => "value" } end test "KEYS in a pipeline" do |r|