From fbff0f4eefc523ba6ea0a9a1d09db4204f6db155 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 31 Oct 2016 10:17:30 -0700 Subject: [PATCH] fix #77 NX/XX bug --- controller/crud.go | 11 ++++++++--- controller/token.go | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) 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)