diff --git a/controller/crud.go b/controller/crud.go index aecb7fa9..ecfbbfb6 100644 --- a/controller/crud.go +++ b/controller/crud.go @@ -662,9 +662,9 @@ func (c *Controller) cmdSet(msg *server.Message) (res string, d commandDetailsT, col = collection.New() c.setCol(d.key, col) } - if nx { + if xx || nx { _, _, ok := col.Get(d.id) - if ok { + if (nx && ok) || (xx && !ok) { goto notok } } @@ -691,7 +691,12 @@ func (c *Controller) cmdSet(msg *server.Message) (res string, d commandDetailsT, notok: switch msg.OutputType { case server.JSON: - res = `{"ok":false,"elapsed":"` + time.Now().Sub(start).String() + "\"}" + if nx { + err = errIDAlreadyExists + } else { + err = errIDNotFound + } + return case server.RESP: res = "$-1\r\n" } diff --git a/controller/token.go b/controller/token.go index f24201fa..37e50356 100644 --- a/controller/token.go +++ b/controller/token.go @@ -15,6 +15,7 @@ const defaultSearchOutput = outputObjects var errInvalidNumberOfArguments = errors.New("invalid number of arguments") var errKeyNotFound = errors.New("key not found") var errIDNotFound = errors.New("id not found") +var errIDAlreadyExists = errors.New("id already exists") func errInvalidArgument(arg string) error { return fmt.Errorf("invalid argument '%s'", arg)