Refactor to be more consistent with redis responses.

This commit is contained in:
Alex Roitman 2018-12-28 14:46:54 -08:00
parent 01a7dda2a1
commit e1c82e17f7
2 changed files with 18 additions and 15 deletions

View File

@ -470,26 +470,27 @@ func (server *Server) cmdRename(msg *Message, nx bool) (res resp.Value, d comman
err = errInvalidNumberOfArguments err = errInvalidNumberOfArguments
return return
} }
col := server.getCol(d.key)
if col == nil {
err = errKeyNotFound
return
}
for _, h := range server.hooks { for _, h := range server.hooks {
if h.Key == d.key || h.Key == d.newKey { if h.Key == d.key || h.Key == d.newKey {
err = errKeyHasHooksSet err = errKeyHasHooksSet
return return
} }
} }
col := server.getCol(d.key)
if col == nil {
err = errKeyNotFound
return
}
d.command = "rename" d.command = "rename"
d.updated = true
newCol := server.getCol(d.newKey) newCol := server.getCol(d.newKey)
if newCol != nil { if newCol == nil {
if nx { d.updated = true
} else if nx {
d.updated = false d.updated = false
} else { } else {
server.deleteCol(d.newKey) server.deleteCol(d.newKey)
} server.clearKeyExpires(d.newKey)
d.updated = true
} }
if d.updated { if d.updated {
server.deleteCol(d.key) server.deleteCol(d.key)
@ -501,7 +502,9 @@ func (server *Server) cmdRename(msg *Message, nx bool) (res resp.Value, d comman
case JSON: case JSON:
res = resp.StringValue(`{"ok":true,"elapsed":"` + time.Now().Sub(start).String() + "\"}") res = resp.StringValue(`{"ok":true,"elapsed":"` + time.Now().Sub(start).String() + "\"}")
case RESP: case RESP:
if d.updated { if !nx {
res = resp.SimpleStringValue("OK")
} else if d.updated {
res = resp.IntegerValue(1) res = resp.IntegerValue(1)
} else { } else {
res = resp.IntegerValue(0) res = resp.IntegerValue(0)

View File

@ -72,11 +72,11 @@ func keys_RENAME_test(mc *mockServer) error {
{"SET", "mykey", "myid1", "HASH", "9my5xp7"}, {"OK"}, {"SET", "mykey", "myid1", "HASH", "9my5xp7"}, {"OK"},
{"SET", "mykey", "myid2", "HASH", "9my5xp8"}, {"OK"}, {"SET", "mykey", "myid2", "HASH", "9my5xp8"}, {"OK"},
{"SCAN", "mykey", "COUNT"}, {2}, {"SCAN", "mykey", "COUNT"}, {2},
{"RENAME", "mykey", "mynewkey"}, {1}, {"RENAME", "mykey", "mynewkey"}, {"OK"},
{"SCAN", "mykey", "COUNT"}, {0}, {"SCAN", "mykey", "COUNT"}, {0},
{"SCAN", "mynewkey", "COUNT"}, {2}, {"SCAN", "mynewkey", "COUNT"}, {2},
{"SET", "mykey", "myid3", "HASH", "9my5xp7"}, {"OK"}, {"SET", "mykey", "myid3", "HASH", "9my5xp7"}, {"OK"},
{"RENAME", "mykey", "mynewkey"}, {1}, {"RENAME", "mykey", "mynewkey"}, {"OK"},
{"SCAN", "mykey", "COUNT"}, {0}, {"SCAN", "mykey", "COUNT"}, {0},
{"SCAN", "mynewkey", "COUNT"}, {1}, {"SCAN", "mynewkey", "COUNT"}, {1},
{"RENAME", "foo", "mynewkey"}, {"ERR key not found"}, {"RENAME", "foo", "mynewkey"}, {"ERR key not found"},