Skip to content

Commit 06665e6

Browse files
Merge #683
683: Adding support for the ZLEXCOUNT command on sorted set. r=badboy Adding support for `ZLEXCOUNT` command. This command has been available since Redis `2.8.9`, along with the other lexicographical commands on sorted set - `ZRANGEBYLEX`, `ZREVRANGEBYLEX` and `ZREMRANGEBYLEX`
2 parents 0220531 + 4cb1119 commit 06665e6

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/redis.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,30 @@ def zremrangebyrank(key, start, stop)
17081708
end
17091709
end
17101710

1711+
# Count the members, with the same score in a sorted set, within the given lexicographical range.
1712+
#
1713+
# @example Count members matching a
1714+
# redis.zlexcount("zset", "[a", "[a\xff")
1715+
# # => 1
1716+
# @example Count members matching a-z
1717+
# redis.zlexcount("zset", "[a", "[z\xff")
1718+
# # => 26
1719+
#
1720+
# @param [String] key
1721+
# @param [String] min
1722+
# - inclusive minimum is specified by prefixing `(`
1723+
# - exclusive minimum is specified by prefixing `[`
1724+
# @param [String] max
1725+
# - inclusive maximum is specified by prefixing `(`
1726+
# - exclusive maximum is specified by prefixing `[`
1727+
#
1728+
# @return [Fixnum] number of members within the specified lexicographical range
1729+
def zlexcount(key, min, max)
1730+
synchronize do |client|
1731+
client.call([:zlexcount, key, min, max])
1732+
end
1733+
end
1734+
17111735
# Return a range of members with the same score in a sorted set, by lexicographical ordering
17121736
#
17131737
# @example Retrieve members matching a

test/commands_on_sorted_sets_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ class TestCommandsOnSortedSets < Test::Unit::TestCase
88
include Helper::Client
99
include Lint::SortedSets
1010

11+
def test_zlexcount
12+
target_version "2.8.9" do
13+
r.zadd "foo", 0, "aaren"
14+
r.zadd "foo", 0, "abagael"
15+
r.zadd "foo", 0, "abby"
16+
r.zadd "foo", 0, "abbygail"
17+
18+
assert_equal 4, r.zlexcount("foo", "[a", "[a\xff")
19+
assert_equal 4, r.zlexcount("foo", "[aa", "[ab\xff")
20+
assert_equal 3, r.zlexcount("foo", "(aaren", "[ab\xff")
21+
assert_equal 2, r.zlexcount("foo", "[aba", "(abbygail")
22+
assert_equal 1, r.zlexcount("foo", "(aaren", "(abby")
23+
end
24+
end
25+
1126
def test_zrangebylex
1227
target_version "2.8.9" do
1328
r.zadd "foo", 0, "aaren"

0 commit comments

Comments
 (0)