Skip to content

Commit

Permalink
Use blocks to post-process replies
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Oct 27, 2011
1 parent ad1b5cd commit 170f6c4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 101 deletions.
184 changes: 93 additions & 91 deletions lib/redis.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -438,42 +438,42 @@ 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

# 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

# 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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/commands_on_hashes_test.rb
Expand Up @@ -8,13 +8,13 @@

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")

result = r.pipelined do
assert nil == r.mapped_hmget("foo", "f1", "f2")
end

assert result[0] == ["s1", "s2"]
assert result[0] == { "f1" => "s1", "f2" => "s2" }
end
4 changes: 2 additions & 2 deletions test/commands_on_strings_test.rb
Expand Up @@ -32,15 +32,15 @@
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")

result = r.pipelined do
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|
Expand Down

0 comments on commit 170f6c4

Please sign in to comment.