Skip to content

Commit aa81254

Browse files
scan_each commands to wrap *SCAN cursor-based calls
The *scan_each commands can all take a block, and will yield each value to the block. If no block is provided, an Enumerator is returned.
1 parent 6338454 commit aa81254

File tree

2 files changed

+356
-0
lines changed

2 files changed

+356
-0
lines changed

lib/redis.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,31 @@ def scan(cursor, options={})
22762276
_scan(:scan, cursor, [], options)
22772277
end
22782278

2279+
# Scan the keyspace
2280+
#
2281+
# @example Retrieve all of the keys (with possible duplicates)
2282+
# redis.scan_each.to_a
2283+
# # => ["key:21", "key:47", "key:42"]
2284+
# @example Execute block for each key matching a pattern
2285+
# redis.scan_each(:match => "key:1?") {|key| puts key}
2286+
# # => key:13
2287+
# # => key:18
2288+
#
2289+
# @param [Hash] options
2290+
# - `:match => String`: only return keys matching the pattern
2291+
# - `:count => Integer`: return count keys at most per iteration
2292+
#
2293+
# @return [Enumerator] an enumerator for all found keys
2294+
def scan_each(options={})
2295+
return to_enum(:scan_each, options) unless block_given?
2296+
cursor = 0
2297+
loop do
2298+
cursor, keys = scan(cursor, options)
2299+
keys.each {|key| yield key}
2300+
break if cursor == "0"
2301+
end
2302+
end
2303+
22792304
# Scan a hash
22802305
#
22812306
# @example Retrieve the first batch of key/value pairs in a hash
@@ -2293,6 +2318,27 @@ def hscan(key, cursor, options={})
22932318
end
22942319
end
22952320

2321+
# Scan a hash
2322+
#
2323+
# @example Retrieve all of the key/value pairs in a hash
2324+
# redis.hscan_each("hash").to_a
2325+
# # => [["key70", "70"], ["key80", "80"]]
2326+
#
2327+
# @param [Hash] options
2328+
# - `:match => String`: only return keys matching the pattern
2329+
# - `:count => Integer`: return count keys at most per iteration
2330+
#
2331+
# @return [Enumerator] an enumerator for all found keys
2332+
def hscan_each(key, options={})
2333+
return to_enum(:hscan_each, key, options) unless block_given?
2334+
cursor = 0
2335+
loop do
2336+
cursor, key_values = hscan(key, cursor, options)
2337+
key_values.each {|key, val| yield key, val }
2338+
break if cursor == "0"
2339+
end
2340+
end
2341+
22962342
# Scan a sorted set
22972343
#
22982344
# @example Retrieve the first batch of key/value pairs in a hash
@@ -2311,6 +2357,27 @@ def zscan(key, cursor, options={})
23112357
end
23122358
end
23132359

2360+
# Scan a sorted set
2361+
#
2362+
# @example Retrieve all of the members/scores in a sorted set
2363+
# redis.zscan_each("zset").to_a
2364+
# # => [["key70", "70"], ["key80", "80"]]
2365+
#
2366+
# @param [Hash] options
2367+
# - `:match => String`: only return keys matching the pattern
2368+
# - `:count => Integer`: return count keys at most per iteration
2369+
#
2370+
# @return [Enumerator] an enumerator for all found scores and members
2371+
def zscan_each(key, options={})
2372+
return to_enum(:zscan_each, key, options) unless block_given?
2373+
cursor = 0
2374+
loop do
2375+
cursor, member_score = zscan(key, cursor, options)
2376+
member_score.each {|member, score| yield member, score }
2377+
break if cursor == "0"
2378+
end
2379+
end
2380+
23142381
# Scan a set
23152382
#
23162383
# @example Retrieve the first batch of keys in a set
@@ -2326,6 +2393,27 @@ def sscan(key, cursor, options={})
23262393
_scan(:sscan, cursor, [key], options)
23272394
end
23282395

2396+
# Scan a set
2397+
#
2398+
# @example Retrieve all of the keys in a set
2399+
# redis.sscan("set").to_a
2400+
# # => ["key1", "key2", "key3"]
2401+
#
2402+
# @param [Hash] options
2403+
# - `:match => String`: only return keys matching the pattern
2404+
# - `:count => Integer`: return count keys at most per iteration
2405+
#
2406+
# @return [Enumerator] an enumerator for all keys in the set
2407+
def sscan_each(key, options={})
2408+
return to_enum(:sscan_each, key, options) unless block_given?
2409+
cursor = 0
2410+
loop do
2411+
cursor, keys = sscan(key, cursor, options)
2412+
keys.each {|key| yield key}
2413+
break if cursor == "0"
2414+
end
2415+
end
2416+
23292417
def id
23302418
synchronize do |client|
23312419
client.id

0 commit comments

Comments
 (0)