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 Oct 14, 2024
1 parent 936b70a commit 12622e2
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 @@ -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
}
20 changes: 20 additions & 0 deletions go/api/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

0 comments on commit 12622e2

Please sign in to comment.