diff --git a/cmd_sorted_set.go b/cmd_sorted_set.go index 5b373d47..75b540ba 100644 --- a/cmd_sorted_set.go +++ b/cmd_sorted_set.go @@ -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{} @@ -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:] @@ -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) @@ -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 { diff --git a/cmd_sorted_set_test.go b/cmd_sorted_set_test.go index de0d4ba5..8c81744f 100644 --- a/cmd_sorted_set_test.go +++ b/cmd_sorted_set_test.go @@ -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", ) @@ -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) diff --git a/redis.go b/redis.go index 8dd5a589..795f0503 100644 --- a/redis.go +++ b/redis.go @@ -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"