Skip to content

Commit 3edcc90

Browse files
caulfielddjanowski
authored andcommitted
Add limit options support to zrevrangebylex
1 parent b07cc27 commit 3edcc90

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/redis.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,28 @@ def zrangebylex(key, min, max, options = {})
16201620
end
16211621
end
16221622

1623+
# Return a range of members with the same score in a sorted set, by reversed lexicographical ordering.
1624+
# Apart from the reversed ordering, #zrevrangebylex is similar to #zrangebylex.
1625+
#
1626+
# @example Retrieve members matching a
1627+
# redis.zrevrangebylex("zset", "[a", "[a\xff")
1628+
# # => ["abbygail", "abby", "abagael", "aaren"]
1629+
# @example Retrieve the last 2 members matching a
1630+
# redis.zrevrangebylex("zset", "[a", "[a\xff", :limit => [0, 2])
1631+
# # => ["abbygail", "abby"]
1632+
#
1633+
# @see #zrangebylex
1634+
def zrevrangebylex(key, max, min, options = {})
1635+
args = []
1636+
1637+
limit = options[:limit]
1638+
args.concat(["LIMIT"] + limit) if limit
1639+
1640+
synchronize do |client|
1641+
client.call([:zrevrangebylex, key, max, min] + args)
1642+
end
1643+
end
1644+
16231645
# Return a range of members in a sorted set, by score.
16241646
#
16251647
# @example Retrieve members with score `>= 5` and `< 100`

test/commands_on_sorted_sets_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ def test_zrangebylex
2222
end
2323
end
2424

25+
def test_zrevrangebylex
26+
target_version "2.9.9" do
27+
r.zadd "foo", 0, "aaren"
28+
r.zadd "foo", 0, "abagael"
29+
r.zadd "foo", 0, "abby"
30+
r.zadd "foo", 0, "abbygail"
31+
32+
assert_equal ["abbygail", "abby", "abagael", "aaren"], r.zrevrangebylex("foo", "[a\xff", "[a")
33+
assert_equal ["abbygail", "abby"], r.zrevrangebylex("foo", "[a\xff", "[a", :limit => [0, 2])
34+
assert_equal ["abbygail", "abby"], r.zrevrangebylex("foo", "(abb\xff", "(abb")
35+
assert_equal ["abbygail"], r.zrevrangebylex("foo", "(abby\xff", "(abby")
36+
end
37+
end
38+
2539
def test_zcount
2640
r.zadd "foo", 1, "s1"
2741
r.zadd "foo", 2, "s2"

0 commit comments

Comments
 (0)