RENAMENX Command Implementation (#149)

* Implemented of RENAMENX command - @DMcP89 
---------
Co-authored-by: Kelvin Clement Mwinuka <kelvinmwinuka@hotmail.co.uk>
This commit is contained in:
Dave McPherson
2024-11-21 18:56:05 -05:00
committed by GitHub
parent 09640082c4
commit 3ddbf1c00b
7 changed files with 9580 additions and 9618 deletions

View File

@@ -677,6 +677,21 @@ func handleRename(params internal.HandlerFuncParams) ([]byte, error) {
return []byte("+OK\r\n"), nil
}
func handleRenamenx(params internal.HandlerFuncParams) ([]byte, error) {
if len(params.Command) != 3 {
return nil, errors.New(constants.WrongArgsResponse)
}
newKey := params.Command[2]
keyExistsCheck := params.KeysExist(params.Context, []string{newKey})
if keyExistsCheck[newKey] {
return nil, errors.New("Key already exists!")
}
return handleRename(params)
}
func handleFlush(params internal.HandlerFuncParams) ([]byte, error) {
if len(params.Command) != 1 {
return nil, errors.New(constants.WrongArgsResponse)
@@ -1325,5 +1340,14 @@ The REPLACE option removes the destination key before copying the value to it.`,
KeyExtractionFunc: moveKeyFunc,
HandlerFunc: handleMove,
},
{
Command: "renamenx",
Module: constants.GenericModule,
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
Description: "(RENAMENX key newkey) Renames the specified key with the new name only if the new name does not already exist.",
Sync: true,
KeyExtractionFunc: renamenxKeyFunc,
HandlerFunc: handleRenamenx,
},
}
}