Skip to content

Commit

Permalink
Merge pull request #81 from dotslashbit/issue-58
Browse files Browse the repository at this point in the history
feat: Added RENAME command
  • Loading branch information
kelvinmwinuka authored Jun 25, 2024
2 parents 67ea631 + 9b897b4 commit d7bcc3a
Show file tree
Hide file tree
Showing 6 changed files with 7,684 additions and 1,713 deletions.
9,154 changes: 7,446 additions & 1,708 deletions coverage/coverage.out

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions echovault/api_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,25 @@ func (server *EchoVault) DecrBy(key string, value string) (int, error) {
// Parse the integer response
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)
}
55 changes: 55 additions & 0 deletions echovault/api_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,3 +1184,58 @@ func TestEchoVault_DECRBY(t *testing.T) {
})
}
}

func TestEchoVault_Rename(t *testing.T) {
server := createEchoVault()

tests := []struct {
name string
oldKey string
newKey string
presetValues map[string]internal.KeyData
want string
wantErr bool
}{
{
name: "1. Rename existing key",
oldKey: "oldKey1",
newKey: "newKey1",
presetValues: map[string]internal.KeyData{"oldKey1": {Value: "value1"}},
want: "OK",
wantErr: false,
},
{
name: "2. Rename non-existent key",
oldKey: "oldKey2",
newKey: "newKey2",
presetValues: nil,
want: "",
wantErr: true,
},
{
name: "3. Rename to existing key",
oldKey: "oldKey3",
newKey: "newKey4",
presetValues: map[string]internal.KeyData{"oldKey3": {Value: "value3"}, "newKey4": {Value: "value4"}},
want: "OK",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.presetValues != nil {
for k, d := range tt.presetValues {
presetKeyData(server, context.Background(), k, d)
}
}
got, err := server.Rename(tt.oldKey, tt.newKey)
if (err != nil) != tt.wantErr {
t.Errorf("Rename() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Rename() got = %v, want %v", got, tt.want)
}
})
}
}
49 changes: 44 additions & 5 deletions internal/modules/generic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,35 @@ func handleDecrBy(params internal.HandlerFuncParams) ([]byte, error) {
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(constants.WrongArgsResponse)
}

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 {
return []internal.Command{
{
Expand Down Expand Up @@ -778,8 +807,8 @@ This operation is limited to 64 bit signed integers.`,
Command: "incrby",
Module: constants.GenericModule,
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
Description: `(INCRBY key increment)
Increments the number stored at key by increment. If the key does not exist, it is set to 0 before performing the operation.
Description: `(INCRBY key increment)
Increments the number stored at key by increment. If the key does not exist, it is set to 0 before performing the operation.
An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer.`,
Sync: true,
KeyExtractionFunc: incrByKeyFunc,
Expand All @@ -789,13 +818,23 @@ An error is returned if the key contains a value of the wrong type or contains a
Command: "decrby",
Module: constants.GenericModule,
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
Description: `(DECRBY key decrement)
The DECRBY command reduces the value stored at the specified key by the specified decrement.
If the key does not exist, it is initialized with a value of 0 before performing the operation.
Description: `(DECRBY key decrement)
The DECRBY command reduces the value stored at the specified key by the specified decrement.
If the key does not exist, it is initialized with a value of 0 before performing the operation.
If the key's value is not of the correct type or cannot be represented as an integer, an error is returned.`,
Sync: true,
KeyExtractionFunc: decrByKeyFunc,
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,
},
}
}
108 changes: 108 additions & 0 deletions internal/modules/generic/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2393,4 +2393,112 @@ func Test_Generic(t *testing.T) {
})
}
})

t.Run("Test_HandlerRENAME", func(t *testing.T) {
t.Parallel()
conn, err := internal.GetConnection("localhost", port)
if err != nil {
t.Error(err)
return
}
defer func() {
_ = conn.Close()
}()
client := resp.NewConn(conn)

tests := []struct {
name string
oldKey string
newKey string
presetValue interface{}
command []resp.Value
expectedResponse string
expectedError error
}{
{
name: "1. Rename existing key",
oldKey: "oldKey1",
newKey: "newKey1",
presetValue: "value1",
command: []resp.Value{resp.StringValue("RENAME"), resp.StringValue("oldKey1"), resp.StringValue("newKey1")},
expectedResponse: "OK",
expectedError: nil,
},
{
name: "2. Rename non-existent key",
oldKey: "oldKey2",
newKey: "newKey2",
presetValue: nil,
command: []resp.Value{resp.StringValue("RENAME"), resp.StringValue("oldKey2"), resp.StringValue("newKey2")},
expectedResponse: "",
expectedError: errors.New("no such key"),
},
{
name: "3. Rename to existing key",
oldKey: "oldKey3",
newKey: "newKey3",
presetValue: "value3",
command: []resp.Value{resp.StringValue("RENAME"), resp.StringValue("oldKey3"), resp.StringValue("newKey3")},
expectedResponse: "OK",
expectedError: nil,
},
{
name: "4. Command too short",
command: []resp.Value{resp.StringValue("RENAME"), resp.StringValue("key")},
expectedError: errors.New(constants.WrongArgsResponse),
},
{
name: "5. Command too long",
command: []resp.Value{
resp.StringValue("RENAME"),
resp.StringValue("key"),
resp.StringValue("newkey"),
resp.StringValue("extra_arg"),
},
expectedError: errors.New(constants.WrongArgsResponse),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.presetValue != nil {
command := []resp.Value{resp.StringValue("SET"), resp.StringValue(test.oldKey), resp.StringValue(fmt.Sprintf("%v", test.presetValue))}
if err = client.WriteArray(command); err != nil {
t.Error(err)
}
res, _, err := client.ReadValue()
if err != nil {
t.Error(err)
}
if !strings.EqualFold(res.String(), "OK") {
t.Errorf("expected preset response to be OK, got %s", res.String())
}
}

if err = client.WriteArray(test.command); err != nil {
t.Error(err)
}

res, _, err := client.ReadValue()
if err != nil {
t.Error(err)
}

if test.expectedError != nil {
if !strings.Contains(res.Error().Error(), test.expectedError.Error()) {
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedError.Error(), res.Error().Error())
}
return
}

if err != nil {
t.Error(err)
} else {
if res.String() != test.expectedResponse {
t.Errorf("expected response \"%s\", got \"%s\"", test.expectedResponse, res.String())
}
}
})
}
})
}
9 changes: 9 additions & 0 deletions internal/modules/generic/key_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,12 @@ func decrByKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
WriteKeys: []string{cmd[1]},
}, 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
}

0 comments on commit d7bcc3a

Please sign in to comment.