Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Redis 6.2] Add LT/GT options to ZADD #1033

Merged
merged 1 commit into from Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/redis.rb
Expand Up @@ -1641,6 +1641,10 @@ def zcard(key)
# add elements)
# - `:nx => true`: Don't update already existing elements (always
# add new elements)
# - `:lt => true`: Only update existing elements if the new score
# is less than the current score
# - `:gt => true`: Only update existing elements if the new score
# is greater than the current score
# - `:ch => true`: Modify the return value from the number of new
# elements added, to the total number of elements changed (CH is an
# abbreviation of changed); changed elements are new elements added
Expand All @@ -1655,10 +1659,12 @@ def zcard(key)
# pairs that were **added** to the sorted set.
# - `Float` when option :incr is specified, holding the score of the member
# after incrementing it.
def zadd(key, *args, nx: nil, xx: nil, ch: nil, incr: nil)
def zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil)
command = [:zadd, key]
command << "NX" if nx
command << "XX" if xx
command << "LT" if lt
command << "GT" if gt
command << "CH" if ch
command << "INCR" if incr

Expand Down
51 changes: 51 additions & 0 deletions test/lint/sorted_sets.rb
Expand Up @@ -45,6 +45,35 @@ def test_zadd
# Incompatible options combination
assert_raises(Redis::CommandError) { r.zadd("foo", 1, "s1", xx: true, nx: true) }
end

target_version "6.2" do
# LT option
r.zadd("foo", 2, "s1")

r.zadd("foo", 3, "s1", lt: true)
assert_equal 2.0, r.zscore("foo", "s1")

r.zadd("foo", 1, "s1", lt: true)
assert_equal 1.0, r.zscore("foo", "s1")

assert_equal true, r.zadd("foo", 3, "s2", lt: true) # adds new member
r.del "foo"

# GT option
r.zadd("foo", 2, "s1")

r.zadd("foo", 1, "s1", gt: true)
assert_equal 2.0, r.zscore("foo", "s1")

r.zadd("foo", 3, "s1", gt: true)
assert_equal 3.0, r.zscore("foo", "s1")

assert_equal true, r.zadd("foo", 1, "s2", gt: true) # adds new member
r.del "foo"

# Incompatible options combination
assert_raises(Redis::CommandError) { r.zadd("foo", 1, "s1", nx: true, gt: true) }
end
end

def test_variadic_zadd
Expand Down Expand Up @@ -109,6 +138,28 @@ def test_variadic_zadd
# Incompatible options combination
assert_raises(Redis::CommandError) { r.zadd("foo", [1, "s1"], xx: true, nx: true) }
end

target_version "6.2" do
# LT option
r.zadd("foo", 2, "s1")

assert_equal 1, r.zadd("foo", [3, "s1", 2, "s2"], lt: true, ch: true)
assert_equal 2.0, r.zscore("foo", "s1")

assert_equal 1, r.zadd("foo", [1, "s1"], lt: true, ch: true)

r.del "foo"

# GT option
r.zadd("foo", 2, "s1")

assert_equal 1, r.zadd("foo", [1, "s1", 2, "s2"], gt: true, ch: true)
assert_equal 2.0, r.zscore("foo", "s1")

assert_equal 1, r.zadd("foo", [3, "s1"], gt: true, ch: true)

r.del "foo"
end
end

def test_zrem
Expand Down