Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Implementing Copy command #2480

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behavior of Copy is different for standalone and cluster mode. Can we please keep the definition of commands consistent with other clients? Reference

GenericBaseCommands:
Copy(source, destination)
Copy(source, destination, replace)

GenericCommands:
Copy(source, destination, destinationDB)
Copy(source, destination, destinationDB, replace)

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 @@ -2503,3 +2503,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())
})
}
Loading