Skip to content

Commit

Permalink
chore: Implement Copy command
Browse files Browse the repository at this point in the history
Signed-off-by: MikeMwita <[email protected]>
  • Loading branch information
MikeMwita committed Oct 27, 2024
1 parent bcc6b00 commit 62ebad6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
19 changes: 19 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,22 @@ func (client *baseClient) Del(keys []string) (Result[int64], error) {

return handleLongResponse(result)
}

func (client *baseClient) Copy(sourceKey, destinationKey string, destinationDB *int, replace bool) (Result[bool], error) {
args := []string{sourceKey, destinationKey}

if destinationDB != nil {
args = append(args, "DB", strconv.Itoa(*destinationDB))
}

if replace {
args = append(args, "REPLACE")
}

result, err := client.executeCommand(C.Copy, args)
if err != nil {
return CreateNilBoolResult(), err
}

return handleBooleanResponse(result)
}
30 changes: 30 additions & 0 deletions go/api/generic_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,34 @@ type GenericBaseCommands interface {
//
// [valkey.io]: https://valkey.io/commands/del/
Del(keys []string) (Result[int64], error)

// Copy duplicates the value stored at `sourceKey` into `destinationKey`.
//
// By default, the destination key is created in the same logical database as the connection.
// The `destinationDB` parameter allows specifying an alternative logical database index for the destination key.
// If the `replace` option is true, the destination key will be removed if it already exists.
//
// Note:
// - In cluster mode, both `sourceKey` and `destinationKey` must be managed by the same node for this command to succeed.
//
// Parameters:
// - sourceKey (string): The key from which to copy the value.
// - destinationKey (string): The key to copy the value to.
// - destinationDB (*int): An optional integer specifying the database index for the destination key.
// If nil, the current database is used.
// - replace (bool): If true, removes `destinationKey` before copying the value if it already exists.
//
// Return value:
// - Result[bool]: Returns true if the copy was successful; returns false if the destination key exists and `replace` is
// not specified.
//
// Example:
// result, err := client.Copy("dolly", "clone", nil, false)
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: true or false
//
// [valkey.io]: https://valkey.io/commands/copy/
Copy(sourceKey string, destinationKey string, destinationDB *int, replace bool) (Result[bool], error)
}
25 changes: 25 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1981,3 +1981,28 @@ func (suite *GlideTestSuite) TestDel_MultipleKeys() {
assert.True(suite.T(), result3.IsNil())
})
}

func (suite *GlideTestSuite) TestCopyCommand() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key1 := "{dolly}_" + uuid.New().String()
key2 := "{dolly}_" + uuid.New().String()

suite.verifyOK(client.Set(key1, "sheep"))

result, err := client.Copy(key1, key2, nil, false)
assert.Nil(suite.T(), err)
assert.True(suite.T(), result.Value())

resultClone, err := client.Get(key2)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "sheep", resultClone.Value())

resultReplace, err := client.Copy(key1, key2, nil, true)
assert.Nil(suite.T(), err)
assert.True(suite.T(), resultReplace.Value())

resultReplacedClone, err := client.Get(key2)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "sheep", resultReplacedClone.Value())
})
}

0 comments on commit 62ebad6

Please sign in to comment.