fix #77 NX/XX bug

This commit is contained in:
Josh Baker 2016-10-31 10:17:30 -07:00
parent 0df21b998a
commit fbff0f4eef
2 changed files with 9 additions and 3 deletions

View File

@ -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"
}

View File

@ -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)