Skip to content

Commit 998d6f0

Browse files
committed
Merge pull request #381 from joshuaflanagan/scan_return_vals
Improve return values for zscan and hscan
2 parents 7da5f65 + ff4a3f7 commit 998d6f0

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

lib/redis.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ def evalsha(*args)
22712271
_eval(:evalsha, args)
22722272
end
22732273

2274-
def _scan(command, cursor, options = {})
2274+
def _scan(command, cursor, options = {}, &block)
22752275
# SSCAN/ZSCAN/HSCAN need a key to work on
22762276
args = []
22772277

@@ -2289,7 +2289,7 @@ def _scan(command, cursor, options = {})
22892289
args.concat(["COUNT", count]) if count
22902290

22912291
synchronize do |client|
2292-
client.call([command] + args)
2292+
client.call([command] + args, &block)
22932293
end
22942294
end
22952295

@@ -2322,9 +2322,11 @@ def scan(cursor, options={})
23222322
# - `:match => String`: only return keys matching the pattern
23232323
# - `:count => Integer`: return count keys at most per iteration
23242324
#
2325-
# @return [String, Array<String>] the next cursor and all found keys
2325+
# @return [String, Array<[String, String]>] the next cursor and all found keys
23262326
def hscan(key, cursor, options={})
2327-
_scan(:hscan, cursor, options.merge(:key => key))
2327+
_scan(:hscan, cursor, options.merge(:key => key)) do |reply|
2328+
[reply[0], reply[1].each_slice(2).to_a]
2329+
end
23282330
end
23292331

23302332
# Scan a sorted set
@@ -2337,9 +2339,12 @@ def hscan(key, cursor, options={})
23372339
# - `:match => String`: only return keys matching the pattern
23382340
# - `:count => Integer`: return count keys at most per iteration
23392341
#
2340-
# @return [String, Array<String>] the next cursor and all found scores and members
2342+
# @return [String, Array<[String, Float]>] the next cursor and all found
2343+
# members and scores
23412344
def zscan(key, cursor, options={})
2342-
_scan(:zscan, cursor, options.merge(:key => key))
2345+
_scan(:zscan, cursor, options.merge(:key => key)) do |reply|
2346+
[reply[0], reply[1].each_slice(2).map{|k,s| [k, _floatify(s)]}]
2347+
end
23432348
end
23442349

23452350
# Scan a set

test/scanning_test.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ def test_hscan_with_encoding
9898
assert_equal enc.to_s, r.object("encoding", "hash")
9999

100100
cursor = 0
101-
all_keys = []
101+
all_key_values = []
102102
loop {
103-
cursor, keys = r.hscan "hash", cursor
104-
all_keys += keys
103+
cursor, key_values = r.hscan "hash", cursor
104+
all_key_values.concat key_values
105105
break if cursor == "0"
106106
}
107107

108108
keys2 = []
109-
all_keys.each_slice(2) do |k, v|
109+
all_key_values.each do |k, v|
110110
assert_equal "key:#{v}", k
111111
keys2 << k
112112
end
@@ -132,16 +132,17 @@ def test_zscan_with_encoding
132132
assert_equal enc.to_s, r.object("encoding", "zset")
133133

134134
cursor = 0
135-
all_keys = []
135+
all_key_scores = []
136136
loop {
137-
cursor, keys = r.zscan "zset", cursor
138-
all_keys += keys
137+
cursor, key_scores = r.zscan "zset", cursor
138+
all_key_scores.concat key_scores
139139
break if cursor == "0"
140140
}
141141

142142
keys2 = []
143-
all_keys.each_slice(2) do |k, v|
144-
assert_equal "key:#{v}", k
143+
all_key_scores.each do |k, v|
144+
assert_equal true, v.is_a?(Float)
145+
assert_equal "key:#{Integer(v)}", k
145146
keys2 << k
146147
end
147148

0 commit comments

Comments
 (0)