mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-11-03 02:13:32 +08:00
Implementation of Copy command (#141)
* Added COPY command - @zenc0derr --------- Co-authored-by: Tejesh Kumar S <zenc0derr> Co-authored-by: Kelvin Clement Mwinuka <kelvinmwinuka@hotmail.co.uk>
This commit is contained in:
@@ -595,6 +595,7 @@ func handleIncrByFloat(params internal.HandlerFuncParams) ([]byte, error) {
|
||||
response := fmt.Sprintf("$%d\r\n%g\r\n", len(fmt.Sprintf("%g", newValue)), newValue)
|
||||
return []byte(response), nil
|
||||
}
|
||||
|
||||
func handleDecrBy(params internal.HandlerFuncParams) ([]byte, error) {
|
||||
// Extract key from command
|
||||
keys, err := decrByKeyFunc(params.Command)
|
||||
@@ -870,6 +871,50 @@ func handleObjIdleTime(params internal.HandlerFuncParams) ([]byte, error) {
|
||||
return []byte(fmt.Sprintf("+%v\r\n", idletime)), nil
|
||||
}
|
||||
|
||||
func handleCopy(params internal.HandlerFuncParams) ([]byte, error) {
|
||||
keys, err := copyKeyFunc(params.Command)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
options, err := getCopyCommandOptions(params.Command[3:], CopyOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sourceKey := keys.ReadKeys[0]
|
||||
destinationKey := keys.WriteKeys[0]
|
||||
sourceKeyExists := params.KeysExist(params.Context, []string{sourceKey})[sourceKey]
|
||||
|
||||
if !sourceKeyExists {
|
||||
return []byte(":0\r\n"), nil
|
||||
}
|
||||
|
||||
if !options.replace {
|
||||
destinationKeyExists := params.KeysExist(params.Context, []string{destinationKey})[destinationKey]
|
||||
|
||||
if destinationKeyExists {
|
||||
return []byte(":0\r\n"), nil
|
||||
}
|
||||
}
|
||||
|
||||
value := params.GetValues(params.Context, []string{sourceKey})[sourceKey]
|
||||
|
||||
ctx := context.WithoutCancel(params.Context)
|
||||
|
||||
if options.database != "" {
|
||||
database, _ := strconv.Atoi(options.database)
|
||||
ctx = context.WithValue(ctx, "Database", database)
|
||||
}
|
||||
|
||||
if err = params.SetValues(ctx, map[string]interface{}{
|
||||
destinationKey: value,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []byte(":1\r\n"), nil
|
||||
}
|
||||
|
||||
func handleMove(params internal.HandlerFuncParams) ([]byte, error) {
|
||||
keys, err := moveKeyFunc(params.Command)
|
||||
if err != nil {
|
||||
@@ -1255,6 +1300,18 @@ The command is only available when the maxmemory-policy configuration directive
|
||||
KeyExtractionFunc: objIdleTimeKeyFunc,
|
||||
HandlerFunc: handleObjIdleTime,
|
||||
},
|
||||
{
|
||||
Command: "copy",
|
||||
Module: constants.GenericModule,
|
||||
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.SlowCategory},
|
||||
Description: `(COPY source destination [DB destination-db] [REPLACE])
|
||||
Copies the value stored at the source key to the destination key.
|
||||
The command returns zero when the destination key already exists.
|
||||
The REPLACE option removes the destination key before copying the value to it.`,
|
||||
Sync: false,
|
||||
KeyExtractionFunc: copyKeyFunc,
|
||||
HandlerFunc: handleCopy,
|
||||
},
|
||||
{
|
||||
Command: "move",
|
||||
Module: constants.GenericModule,
|
||||
|
||||
Reference in New Issue
Block a user