Skip to content

Commit

Permalink
Go Implement Rename and Renamenx Command (valkey-io#2776)
Browse files Browse the repository at this point in the history
* Implement Rename and Renamenx
Signed-off-by: EdricCua <[email protected]>
  • Loading branch information
EdricCua authored Dec 18, 2024
1 parent 2882c81 commit e25eb9c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
16 changes: 16 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,3 +1155,19 @@ func (client *baseClient) Touch(keys []string) (Result[int64], error) {

return handleLongResponse(result)
}

func (client *baseClient) Rename(key string, newKey string) (Result[string], error) {
result, err := client.executeCommand(C.Rename, []string{key, newKey})
if err != nil {
return CreateNilStringResult(), err
}
return handleStringOrNullResponse(result)
}

func (client *baseClient) Renamenx(key string, newKey string) (Result[bool], error) {
result, err := client.executeCommand(C.RenameNX, []string{key, newKey})
if err != nil {
return CreateNilBoolResult(), err
}
return handleBooleanResponse(result)
}
45 changes: 45 additions & 0 deletions go/api/generic_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,49 @@ type GenericBaseCommands interface {
//
// [valkey.io]: Https://valkey.io/commands/type/
Type(key string) (Result[string], error)

// Renames key to new key.
// If new Key already exists it is overwritten.
//
// Note:
// When in cluster mode, both key and newKey must map to the same hash slot.
//
// Parameters:
// key to rename.
// newKey The new name of the key.
//
// Return value:
// If the key was successfully renamed, return "OK". If key does not exist, an error is thrown.
//
// Example:
// result, err := client.Rename([]string{"key","newkey"})
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: OK
//
// [valkey.io]: https://valkey.io/commands/rename/
Rename(key string, newKey string) (Result[string], error)

// Renames key to newkey if newKey does not yet exist.
//
// Note:
// When in cluster mode, both key and newkey must map to the same hash slot.
//
// Parameters:
// key to rename.
// newKey The new name of the key.
//
// Return value:
// true if key was renamed to newKey, false if newKey already exists.
//
// Example:
// result, err := client.Renamenx([]string{"key","newkey"})
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: OK
//
// [valkey.io]: https://valkey.io/commands/renamenx/
Renamenx(key string, newKey string) (Result[bool], error)
}
39 changes: 39 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3504,3 +3504,42 @@ func (suite *GlideTestSuite) TestUnlink() {
assert.Equal(suite.T(), int64(0), resultInvalidKey.Value(), "The unlink should be 0")
})
}

func (suite *GlideTestSuite) Test_Rename() {
suite.runWithDefaultClients(func(client api.BaseClient) {
// Test 1 Check if the command successfully renamed
key := "{keyName}" + uuid.NewString()
initialValueRename := "TestRename_RenameValue"
newRenameKey := "{newkeyName}" + uuid.NewString()
suite.verifyOK(client.Set(key, initialValueRename))
client.Rename(key, newRenameKey)

// Test 2 Check if the rename command return false if the key/newkey is invalid.
key1 := "{keyName}" + uuid.NewString()
res1, err := client.Rename(key1, "invalidKey")
assert.Equal(suite.T(), "", res1.Value())
assert.NotNil(suite.T(), err)
assert.IsType(suite.T(), &api.RequestError{}, err)
})
}

func (suite *GlideTestSuite) TestRenamenx() {
suite.runWithDefaultClients(func(client api.BaseClient) {
// Test 1 Check if the renamenx command return true if key was renamed to newKey
key := "{keyName}" + uuid.NewString()
key2 := "{keyName}" + uuid.NewString()
suite.verifyOK(client.Set(key, initialValue))
res1, err := client.Renamenx(key, key2)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), true, res1.Value())

// Test 2 Check if the renamenx command return false if newKey already exists.
key3 := "{keyName}" + uuid.NewString()
key4 := "{keyName}" + uuid.NewString()
suite.verifyOK(client.Set(key3, initialValue))
suite.verifyOK(client.Set(key4, initialValue))
res2, err := client.Renamenx(key3, key4)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), false, res2.Value())
})
}

0 comments on commit e25eb9c

Please sign in to comment.