Skip to content

Commit b8bfb62

Browse files
committed
Merge pull request #456 from ayrton/zrangebylex
Add support for limit in zrangebylex
2 parents 7db6501 + 54176a0 commit b8bfb62

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/redis.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,38 @@ def zremrangebyrank(key, start, stop)
15881588
end
15891589
end
15901590

1591+
# Return a range of members with the same score in a sorted set, by lexicographical ordering
1592+
#
1593+
# @example Retrieve members matching a
1594+
# redis.zrangebylex("zset", "[a", "[a\xff")
1595+
# # => ["aaren", "aarika", "abagael", "abby"]
1596+
# @example Retrieve the first 2 members matching a
1597+
# redis.zrangebylex("zset", "[a", "[a\xff", :limit => [0, 2])
1598+
# # => ["aaren", "aarika"]
1599+
#
1600+
# @param [String] key
1601+
# @param [String] min
1602+
# - inclusive minimum is specified by prefixing `(`
1603+
# - exclusive minimum is specified by prefixing `[`
1604+
# @param [String] max
1605+
# - inclusive maximum is specified by prefixing `(`
1606+
# - exclusive maximum is specified by prefixing `[`
1607+
# @param [Hash] options
1608+
# - `:limit => [offset, count]`: skip `offset` members, return a maximum of
1609+
# `count` members
1610+
#
1611+
# @return [Array<String>, Array<[String, Float]>]
1612+
def zrangebylex(key, min, max, options = {})
1613+
args = []
1614+
1615+
limit = options[:limit]
1616+
args.concat(["LIMIT"] + limit) if limit
1617+
1618+
synchronize do |client|
1619+
client.call([:zrangebylex, key, min, max] + args)
1620+
end
1621+
end
1622+
15911623
# Return a range of members in a sorted set, by score.
15921624
#
15931625
# @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
@@ -8,6 +8,20 @@ class TestCommandsOnSortedSets < Test::Unit::TestCase
88
include Helper::Client
99
include Lint::SortedSets
1010

11+
def test_zrangebylex
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 ["aaren", "abagael", "abby", "abbygail"], r.zrangebylex("foo", "[a", "[a\xff")
19+
assert_equal ["aaren", "abagael"], r.zrangebylex("foo", "[a", "[a\xff", :limit => [0, 2])
20+
assert_equal ["abby", "abbygail"], r.zrangebylex("foo", "(abb", "(abb\xff")
21+
assert_equal ["abbygail"], r.zrangebylex("foo", "(abby", "(abby\xff")
22+
end
23+
end
24+
1125
def test_zcount
1226
r.zadd "foo", 1, "s1"
1327
r.zadd "foo", 2, "s2"

0 commit comments

Comments
 (0)