Skip to content

Commit

Permalink
Go: Implementing DEL command
Browse files Browse the repository at this point in the history
Signed-off-by: MikeMwita <[email protected]>
  • Loading branch information
MikeMwita committed Sep 23, 2024
1 parent 4f7c72c commit dfa827b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
14 changes: 14 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,17 @@ func (client *baseClient) HStrLen(key string, field string) (int64, error) {

return handleLongResponse(result)
}

func (client *baseClient) Del(keys []string) (int64, error) {
result, err := client.executeCommand(C.Del, keys)
if err != nil {
return 0, err
}

deletedCount, handleErr := handleLongResponse(result)
if handleErr != nil {
return 0, handleErr
}

return deletedCount, nil
}
21 changes: 21 additions & 0 deletions go/api/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,28 @@ type StringCommands interface {
// result, err := client.GetDel("key")
//
//[valkey.io]: https://valkey.io/commands/getdel/

GetDel(key string) (string, error)

// Del removes the specified keys from the database. A key is ignored if it does not exist.
//
// @apiNote When in cluster mode, the command may route to multiple nodes when `keys` map to different hash slots.
//
// Parameters:
// keys - One or more keys to delete.
//
// Return value:
// Returns the number of keys that were removed.
//
// Example:
// result, err := client.Del([]string{"key1", "key2", "key3"})
// if err != nil {
// // handle error
// }
// fmt.Println(result) // Output: 2
//
// [valkey.io]: https://valkey.io/commands/del/
Del(keys ...string) (int64, error)
}

// HashCommands supports commands and transactions for the "Hash Commands" group for standalone and cluster
Expand Down
29 changes: 29 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,3 +927,32 @@ func (suite *GlideTestSuite) TestHStrLen_WithNotExistingField() {
assert.Equal(suite.T(), int64(0), res2)
})
}

func (suite *GlideTestSuite) TestDel_MultipleKeys() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key1 := "testKey1"
key2 := "testKey2"
key3 := "testKey3"
suite.verifyOK(client.Set(key1, initialValue))
suite.verifyOK(client.Set(key2, initialValue))
suite.verifyOK(client.Set(key3, initialValue))

deletedCount, err := client.Del(key1, key2, key3)

assert.Nil(suite.T(), err)
assert.Equal(suite.T(), int64(3), deletedCount)

result1, err1 := client.Get(key1)
result2, err2 := client.Get(key2)
result3, err3 := client.Get(key3)

assert.Nil(suite.T(), err1)
assert.Equal(suite.T(), "", result1)

assert.Nil(suite.T(), err2)
assert.Equal(suite.T(), "", result2)

assert.Nil(suite.T(), err3)
assert.Equal(suite.T(), "", result3)
})
}

0 comments on commit dfa827b

Please sign in to comment.