Skip to content

Commit

Permalink
Merge pull request #330 from sejin-P/ZADD-LT-default-value
Browse files Browse the repository at this point in the history
fix; check key existence when applying LT GT option to ZADD
  • Loading branch information
alicebob committed Jun 13, 2023
2 parents df41945 + b0d3aee commit f507fa8
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 f507fa8

Please sign in to comment.