mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-08 09:20:07 +08:00
feat: added RENAME command
This commit is contained in:
@@ -507,3 +507,25 @@ func (server *EchoVault) DecrBy(key string, value string) (int, error) {
|
|||||||
// Parse the integer response
|
// Parse the integer response
|
||||||
return internal.ParseIntegerResponse(b)
|
return internal.ParseIntegerResponse(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rename renames the key from oldKey to newKey.
|
||||||
|
// If the oldKey does not exist, an error is returned.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
//
|
||||||
|
// `oldKey` - string - The key to be renamed.
|
||||||
|
//
|
||||||
|
// `newKey` - string - The new name for the key.
|
||||||
|
//
|
||||||
|
// Returns: A string indicating the success of the operation.
|
||||||
|
func (server *EchoVault) Rename(oldKey string, newKey string) (string, error) {
|
||||||
|
// Construct the command
|
||||||
|
cmd := []string{"RENAME", oldKey, newKey}
|
||||||
|
// Execute the command
|
||||||
|
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
// Parse the simple string response
|
||||||
|
return internal.ParseStringResponse(b)
|
||||||
|
}
|
||||||
|
@@ -581,6 +581,35 @@ func handleDecrBy(params internal.HandlerFuncParams) ([]byte, error) {
|
|||||||
return []byte(fmt.Sprintf(":%d\r\n", newValue)), nil
|
return []byte(fmt.Sprintf(":%d\r\n", newValue)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleRename(params internal.HandlerFuncParams) ([]byte, error) {
|
||||||
|
if len(params.Command) != 3 {
|
||||||
|
return nil, errors.New("wrong number of arguments for RENAME")
|
||||||
|
}
|
||||||
|
|
||||||
|
oldKey := params.Command[1]
|
||||||
|
newKey := params.Command[2]
|
||||||
|
|
||||||
|
// Get the current value for the old key
|
||||||
|
values := params.GetValues(params.Context, []string{oldKey})
|
||||||
|
oldValue, ok := values[oldKey]
|
||||||
|
|
||||||
|
if !ok || oldValue == nil {
|
||||||
|
return nil, errors.New("no such key")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the new key with the old value
|
||||||
|
if err := params.SetValues(params.Context, map[string]interface{}{newKey: oldValue}); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the old key
|
||||||
|
if err := params.DeleteKey(oldKey); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte("+OK\r\n"), nil
|
||||||
|
}
|
||||||
|
|
||||||
func Commands() []internal.Command {
|
func Commands() []internal.Command {
|
||||||
return []internal.Command{
|
return []internal.Command{
|
||||||
{
|
{
|
||||||
@@ -797,5 +826,14 @@ If the key's value is not of the correct type or cannot be represented as an int
|
|||||||
KeyExtractionFunc: decrByKeyFunc,
|
KeyExtractionFunc: decrByKeyFunc,
|
||||||
HandlerFunc: handleDecrBy,
|
HandlerFunc: handleDecrBy,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Command: "rename",
|
||||||
|
Module: constants.GenericModule,
|
||||||
|
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
|
||||||
|
Description: `(RENAME key newkey) Renames key to newkey. If newkey already exists, it is overwritten. If key does not exist, an error is returned.`,
|
||||||
|
Sync: true,
|
||||||
|
KeyExtractionFunc: renameKeyFunc,
|
||||||
|
HandlerFunc: handleRename,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -172,3 +172,12 @@ func decrByKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
|
|||||||
WriteKeys: []string{cmd[1]},
|
WriteKeys: []string{cmd[1]},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func renameKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
|
||||||
|
if len(cmd) != 3 {
|
||||||
|
return internal.KeyExtractionFuncResult{}, errors.New(constants.WrongArgsResponse)
|
||||||
|
}
|
||||||
|
return internal.KeyExtractionFuncResult{
|
||||||
|
WriteKeys: cmd[1:3],
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user