Skip to content

Commit

Permalink
Merge pull request #1033 from fatkodima/zadd-lt-gt
Browse files Browse the repository at this point in the history
[Redis 6.2] Add LT/GT options to ZADD
  • Loading branch information
byroot committed Oct 7, 2021
2 parents eb626f6 + 40398c8 commit c71327a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
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

0 comments on commit c71327a

Please sign in to comment.