Permalink
Browse files

Slice result for SORT with multiple GET

  • Loading branch information...
pietern committed Mar 9, 2012
1 parent e046f93 commit 284e29c25426bc73d8f1c6a4cfcc36268f5459b8
Showing with 21 additions and 5 deletions.
  1. +4 −0 CHANGELOG.md
  2. +14 −3 lib/redis.rb
  3. +3 −2 test/sorting_test.rb
View
@@ -1,5 +1,9 @@
# 3.0 (unreleased)
+* When `SORT` is passed multiple key patterns to get via the `:get`
+ option, it now returns an array per result element, holding all `GET`
+ substitutions.
+
* The `ZRANGE`, `ZREVRANGE`, `ZRANGEBYSCORE` and `ZREVRANGEBYSCORE` commands
now return an array containing `[String, Float]` pairs when
`:with_scores => true` is passed.
View
@@ -1616,8 +1616,11 @@ def mapped_mget(*keys)
# - `:order => String`: combination of `ASC`, `DESC` and optionally `ALPHA`
# - `:store => String`: key to store the result at
#
- # @return [Array<String>, Fixnum]
- # - when `:store` is not specified, an array of elements
+ # @return [Array<String>, Array<Array<String>>, Fixnum]
+ # - when `:get` is not specified, or holds a single element, an array of elements
+ # - when `:get` is specified, and holds more than one element, an array of
+ # elements where every element is an array with the result for every
+ # element specified in `:get`
# - when `:store` is specified, the number of elements in the stored result
def sort(key, options = {})
args = []
@@ -1638,7 +1641,15 @@ def sort(key, options = {})
args.concat ["STORE", store] if store
synchronize do
- @client.call [:sort, key, *args]
+ @client.call [:sort, key, *args] do |reply|
+ if get.size > 1
+ if reply
+ reply.each_slice(get.size).to_a
+ end
+ else
+ reply
+ end
+ end
end
end
View
@@ -27,8 +27,9 @@
r.rpush("bar", "1")
r.rpush("bar", "2")
- assert ["s1a", "s1b"] == r.sort("bar", :get => ["foo:*:a", "foo:*:b"], :limit => [0, 1])
- assert ["s2a", "s2b"] == r.sort("bar", :get => ["foo:*:a", "foo:*:b"], :limit => [0, 1], :order => "desc alpha")
+ assert [["s1a", "s1b"]] == r.sort("bar", :get => ["foo:*:a", "foo:*:b"], :limit => [0, 1])
+ assert [["s2a", "s2b"]] == r.sort("bar", :get => ["foo:*:a", "foo:*:b"], :limit => [0, 1], :order => "desc alpha")
+ assert [["s1a", "s1b"], ["s2a", "s2b"]] == r.sort("bar", :get => ["foo:*:a", "foo:*:b"])
end
test "SORT with STORE" do |r|

0 comments on commit 284e29c

Please sign in to comment.