From 12622e20af47dd0426965d2802affb474cdec694 Mon Sep 17 00:00:00 2001 From: MikeMwita Date: Sun, 15 Sep 2024 15:36:29 +0300 Subject: [PATCH] Go: Implementing DEL command Signed-off-by: MikeMwita --- go/api/base_client.go | 14 +++++++++++++ go/api/commands.go | 20 +++++++++++++++++++ go/integTest/shared_commands_test.go | 30 ++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/go/api/base_client.go b/go/api/base_client.go index 39584164ce..96ed5fc73d 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -622,3 +622,17 @@ func (client *baseClient) PingWithMessage(message string) (string, error) { } return response.Value(), nil } + +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.Value(), nil +} diff --git a/go/api/commands.go b/go/api/commands.go index f48239c524..8bc524166e 100644 --- a/go/api/commands.go +++ b/go/api/commands.go @@ -442,6 +442,26 @@ type StringCommands interface { // //[valkey.io]: https://valkey.io/commands/getdel/ GetDel(key string) (Result[string], error) + // Del removes the specified keys from the database. A key is ignored if it does not exist. + // + // Note: + //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 diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index a25c9cbe54..2298ffe8ed 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -1515,3 +1515,33 @@ func (suite *GlideTestSuite) TestLInsert() { assert.IsType(suite.T(), &api.RequestError{}, err) }) } + +func (suite *GlideTestSuite) TestDel_MultipleKeys() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key1 := "testKey1_" + uuid.New().String() + key2 := "testKey2_" + uuid.New().String() + key3 := "testKey3_" + uuid.New().String() + + suite.verifyOK(client.Set(key1, initialValue)) + suite.verifyOK(client.Set(key2, initialValue)) + suite.verifyOK(client.Set(key3, initialValue)) + + deletedCount, err := client.Del([]string{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.True(suite.T(), result1.IsNil()) + + assert.Nil(suite.T(), err2) + assert.True(suite.T(), result2.IsNil()) + + assert.Nil(suite.T(), err3) + assert.True(suite.T(), result3.IsNil()) + }) +}