Skip to content

Commit

Permalink
fix; check value existence when applying LT GT option to ZADD
Browse files Browse the repository at this point in the history
  • Loading branch information
sejin-P committed Jun 9, 2023
1 parent df41945 commit b0d3aee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cmd_sorted_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,18 @@ outer:

res := 0
for member, score := range elems {
if opts.nx && db.ssetExists(opts.key, member) {
exists := db.ssetExists(opts.key, member)
if opts.nx && exists {
continue
}
if opts.xx && !db.ssetExists(opts.key, member) {
if opts.xx && !exists {
continue
}
old := db.ssetScore(opts.key, member)
if opts.gt && score <= old {
if opts.gt && exists && score <= old {
continue
}
if opts.lt && score >= old {
if opts.lt && exists && score >= old {
continue
}
if db.ssetAdd(opts.key, score, member) {
Expand Down
7 changes: 7 additions & 0 deletions cmd_sorted_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ func TestSortedSetAdd(t *testing.T) {
)
}

t.Run("LT and GT options", func(t *testing.T) {
must1(t, c, "ZADD", "zlt", "LT", "1", "one")
must0(t, c, "ZADD", "zlt", "LT", "2", "one")
must1(t, c, "ZADD", "zgt", "GT", "-1", "one")
must0(t, c, "ZADD", "zgt", "GT", "-2", "one")
})

t.Run("errors", func(t *testing.T) {
// Wrong type of key
mustOK(t, c, "SET", "str", "value")
Expand Down

0 comments on commit b0d3aee

Please sign in to comment.