Skip to content

Commit

Permalink
Added support for GT and LT options in ZADD command
Browse files Browse the repository at this point in the history
  • Loading branch information
lsgndln committed May 2, 2022
1 parent 71881f2 commit 199ed21
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cmd_sorted_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (m *Miniredis) cmdZadd(c *server.Peer, cmd string, args []string) {
var (
nx = false
xx = false
gt = false
lt = false
ch = false
incr = false
elems = map[string]float64{}
Expand All @@ -75,6 +77,14 @@ outer:
xx = true
args = args[1:]
continue
case "GT":
gt = true
args = args[1:]
continue
case "LT":
lt = true
args = args[1:]
continue
case "CH":
ch = true
args = args[1:]
Expand Down Expand Up @@ -110,6 +120,14 @@ outer:
return
}

if gt && lt ||
gt && nx ||
lt && nx {
setDirty(c)
c.WriteError(msgGTLTandNX)
return
}

if incr && len(elems) > 1 {
setDirty(c)
c.WriteError(msgSingleElementPair)
Expand Down Expand Up @@ -149,6 +167,12 @@ outer:
continue
}
old := db.ssetScore(key, member)
if gt && score <= old {
continue
}
if lt && score >= old {
continue
}
if db.ssetAdd(key, score, member) {
res++
} else {
Expand Down
12 changes: 12 additions & 0 deletions cmd_sorted_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ func TestSortedSetAdd(t *testing.T) {
"ZADD", "z", "NX", "1", "one", "2.2", "two", "3", "three",
)

must0(t, c,
"ZADD", "z", "GT", "1", "one",
)

must0(t, c,
"ZADD", "z", "GT", "2", "one",
)

must1(t, c,
"ZADD", "z", "NX", "1", "one", "4", "four",
)
Expand Down Expand Up @@ -271,6 +279,10 @@ func TestSortedSetAdd(t *testing.T) {
"ZADD", "set", "INCR", "1.0", "foo", "2.3", "bar",
proto.Error("ERR INCR option supports a single increment-element pair"),
)
mustDo(t, c,
"ZADD", "set", "GT", "LT", "1.0", "foo",
proto.Error(msgGTLTandNX),
)
})

useRESP3(t, c)
Expand Down
1 change: 1 addition & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
msgFPubsubUsage = "ERR Unknown subcommand or wrong number of arguments for '%s'. Try PUBSUB HELP."
msgScriptFlush = "ERR SCRIPT FLUSH only support SYNC|ASYNC option"
msgSingleElementPair = "ERR INCR option supports a single increment-element pair"
msgGTLTandNX = "ERR GT, LT and NX options are mutually exclusive"
msgInvalidStreamID = "ERR Invalid stream ID specified as stream command argument"
msgStreamIDTooSmall = "ERR The ID specified in XADD is equal or smaller than the target stream top item"
msgStreamIDZero = "ERR The ID specified in XADD must be greater than 0-0"
Expand Down

0 comments on commit 199ed21

Please sign in to comment.