Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Implement FlushAll, FlushDB commands #3117

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions go/api/glide_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,99 @@ func (client *GlideClient) PingWithOptions(pingOptions options.PingOptions) (str
}
return handleStringResponse(result)
}

// FlushAll deletes all the keys of all the existing databases.
//
// See [valkey.io] for details.
//
// Return value:
//
// `"OK"` response on success.
//
// Example:
Copy link
Collaborator

@jbrinkman jbrinkman Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples should not exist in the document block and instead should be placed in the relevant test file. See the Go Developer Guide for Valkey-Glide.

//
// res, err := client.FlushAll()
// fmt.Println(res) // OK
//
// [valkey.io]: https://valkey.io/commands/flushall/
func (client *GlideClient) FlushAll() (string, error) {
result, err := client.executeCommand(C.FlushAll, []string{})
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(result)
}

// Deletes all the keys of all the existing databases.
//
// See [valkey.io] for details.
//
// Parameters:
//
// mode - The flushing mode, could be either [options.SYNC] or [options.ASYNC}.
//
// Return value:
//
// `"OK"` response on success.
//
// Example:
Copy link
Collaborator

@jbrinkman jbrinkman Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples should not exist in the document block and instead should be placed in the relevant test file. See the Go Developer Guide for Valkey-Glide.

//
// res, err := client.FlushAllWithOptions(options.SYNC)
// fmt.Println(res) // OK
//
// [valkey.io]: https://valkey.io/commands/flushall/
func (client *GlideClient) FlushAllWithOptions(mode options.FlushMode) (string, error) {
result, err := client.executeCommand(C.FlushAll, []string{string(mode)})
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(result)
}

// Deletes all the keys of the currently selected database.
//
// See [valkey.io] for details.
//
// Return value:
//
// `"OK"` response on success.
//
// Example:
Copy link
Collaborator

@jbrinkman jbrinkman Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples should not exist in the document block and instead should be placed in the relevant test file. See the Go Developer Guide for Valkey-Glide.

//
// res, err := client.FlushDB()
// fmt.Println(res) // OK
//
// [valkey.io]: https://valkey.io/commands/flushdb/
func (client *GlideClient) FlushDB() (string, error) {
result, err := client.executeCommand(C.FlushDB, []string{})
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(result)
}

// Deletes all the keys of the currently selected database.
//
// See [valkey.io] for details.
//
// Parameters:
//
// mode - The flushing mode, could be either [options.SYNC] or [options.ASYNC}.
//
// Return value:
//
// `"OK"` response on success.
//
// Example:
Copy link
Collaborator

@jbrinkman jbrinkman Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples should not exist in the document block and instead should be placed in the relevant test file. See the Go Developer Guide for Valkey-Glide.

//
// res, err := client.FlushDBWithOptions(options.SYNC)
// fmt.Println(res) // OK
//
// [valkey.io]: https://valkey.io/commands/flushdb/
func (client *GlideClient) FlushDBWithOptions(mode options.FlushMode) (string, error) {
result, err := client.executeCommand(C.FlushDB, []string{string(mode)})
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(result)
}
56 changes: 56 additions & 0 deletions go/api/glide_cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,62 @@ func (client *GlideClusterClient) DBSizeWithOptions(opts options.RouteOption) (i
return handleIntResponse(result)
}

// Deletes all the keys of all the existing databases.
//
// See [valkey.io] for details.
//
// Parameters:
//
// mode - The flushing mode, could be either [options.SYNC] or [options.ASYNC}.
// routeOption - The RouteOption type.
//
// Return value:
//
// `"OK"` response on success.
//
// Example:
Copy link
Collaborator

@jbrinkman jbrinkman Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples should not exist in the document block and instead should be placed in the relevant test file. See the Go Developer Guide for Valkey-Glide.

//
// route := options.RouteOption{Route: config.AllPrimaries}
// res, err := client.FlushAllWithOptions(options.SYNC, route)
// fmt.Println(res) // OK
//
// [valkey.io]: https://valkey.io/commands/flushall/
func (client *GlideClusterClient) FlushAllWithOptions(mode options.FlushMode, opts options.RouteOption) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create

type FlushClusterOptions struct {
    *FlushMode
    *RouteOption
}

In client API check for nil each component. I think we already have this done for another command (probably INFO).

result, err := client.executeCommandWithRoute(C.FlushAll, []string{string(mode)}, opts.Route)
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(result)
}

// Deletes all the keys of the currently selected database.
//
// See [valkey.io] for details.
//
// Parameters:
//
// mode - The flushing mode, could be either [options.SYNC] or [options.ASYNC}.
// routeOption - The RouteOption type.
//
// Return value:
//
// `"OK"` response on success.
//
// Example:
Copy link
Collaborator

@jbrinkman jbrinkman Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples should not exist in the document block and instead should be placed in the relevant test file. See the Go Developer Guide for Valkey-Glide.

//
// route := options.RouteOption{Route: config.AllPrimaries}
// res, err := client.FlushDBWithOptions(options.SYNC, route)
// fmt.Println(res) // OK
//
// [valkey.io]: https://valkey.io/commands/flushdb/
func (client *GlideClusterClient) FlushDBWithOptions(mode options.FlushMode, opts options.RouteOption) (string, error) {
result, err := client.executeCommandWithRoute(C.FlushDB, []string{string(mode)}, opts.Route)
if err != nil {
return defaultStringResponse, err
}
return handleStringResponse(result)
}

// Echo the provided message back.
// The command will be routed a random node, unless `Route` in `echoOptions` is provided.
//
Expand Down
15 changes: 15 additions & 0 deletions go/api/options/flush_mode_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package options

// FlushMode represents the database flush operation mode
type FlushMode string

const (
// SYNC flushes synchronously.
// Since Valkey 6.2 and above.
SYNC FlushMode = "SYNC"

// ASYNC flushes asynchronously.
ASYNC FlushMode = "ASYNC"
)
4 changes: 4 additions & 0 deletions go/api/server_management_cluster_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ type ServerManagementClusterCommands interface {
TimeWithOptions(routeOption options.RouteOption) (ClusterValue[[]string], error)

DBSizeWithOptions(routeOption options.RouteOption) (int64, error)

FlushAllWithOptions(mode options.FlushMode, routeOption options.RouteOption) (string, error)

FlushDBWithOptions(mode options.FlushMode, routeOption options.RouteOption) (string, error)
}
10 changes: 10 additions & 0 deletions go/api/server_management_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package api

import "github.com/valkey-io/valkey-glide/go/api/options"

// ServerManagementCommands supports commands for the "Server Management" group for a standalone client.
//
// See [valkey.io] for details.
Expand All @@ -21,4 +23,12 @@ type ServerManagementCommands interface {
DBSize() (int64, error)

Time() ([]string, error)

FlushAll() (string, error)

FlushAllWithOptions(mode options.FlushMode) (string, error)

FlushDB() (string, error)

FlushDBWithOptions(mode options.FlushMode) (string, error)
}
135 changes: 135 additions & 0 deletions go/integTest/cluster_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package integTest
import (
"strings"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/valkey-io/valkey-glide/go/api"
"github.com/valkey-io/valkey-glide/go/api/config"
Expand Down Expand Up @@ -298,3 +299,137 @@ func (suite *GlideTestSuite) TestEchoCluster() {
assert.Contains(t, strings.ToLower(messages), strings.ToLower("hello"))
}
}

func (suite *GlideTestSuite) TestFlushAllWithOptions_AllNodes() {
client := suite.defaultClusterClient()

key1 := uuid.New().String()
key2 := uuid.New().String()
_, err := client.Set(key1, "value3")
assert.NoError(suite.T(), err)
_, err = client.Set(key2, "value4")
assert.NoError(suite.T(), err)

route := config.Route(config.AllNodes)
result, err := client.FlushAllWithOptions(options.ASYNC, options.RouteOption{Route: route})
assert.Error(suite.T(), err)
assert.Contains(suite.T(), err.Error(), "ReadOnly: You can't write against a read only replica")
assert.Empty(suite.T(), result)
}

func (suite *GlideTestSuite) TestFlushAllWithOptions_AllPrimaries() {
client := suite.defaultClusterClient()

key1 := uuid.New().String()
key2 := uuid.New().String()
_, err := client.Set(key1, "value3")
assert.NoError(suite.T(), err)
_, err = client.Set(key2, "value4")
assert.NoError(suite.T(), err)

route := config.Route(config.AllPrimaries)
result, err := client.FlushAllWithOptions(options.ASYNC, options.RouteOption{Route: route})
assert.NoError(suite.T(), err)
assert.NotEmpty(suite.T(), result)

val1, err := client.Get(key1)
assert.NoError(suite.T(), err)
assert.Empty(suite.T(), val1.Value())

val2, err := client.Get(key2)
assert.NoError(suite.T(), err)
assert.Empty(suite.T(), val2.Value())
}

func (suite *GlideTestSuite) TestFlushAllWithOptions_InvalidRoute() {
client := suite.defaultClusterClient()

invalidRoute := config.Route(config.NewByAddressRoute("invalidHost", 9999))
result, err := client.FlushAllWithOptions(options.SYNC, options.RouteOption{Route: invalidRoute})
assert.Error(suite.T(), err)
assert.Empty(suite.T(), result)
}

func (suite *GlideTestSuite) TestFlushAllWithOptions_AsyncMode() {
client := suite.defaultClusterClient()

key := uuid.New().String()
_, err := client.Set(key, "value5")
assert.NoError(suite.T(), err)

route := config.Route(config.AllPrimaries)
result, err := client.FlushAllWithOptions(options.ASYNC, options.RouteOption{Route: route})
assert.NoError(suite.T(), err)
assert.NotEmpty(suite.T(), result)

val, err := client.Get(key)
assert.NoError(suite.T(), err)
assert.Empty(suite.T(), val.Value())
}

func (suite *GlideTestSuite) TestFlushDBWithOptions_AllNodes() {
client := suite.defaultClusterClient()

key1 := uuid.New().String()
key2 := uuid.New().String()
_, err := client.Set(key1, "value3")
assert.NoError(suite.T(), err)
_, err = client.Set(key2, "value4")
assert.NoError(suite.T(), err)

route := config.Route(config.AllNodes)
result, err := client.FlushDBWithOptions(options.ASYNC, options.RouteOption{Route: route})
assert.Error(suite.T(), err)
assert.Contains(suite.T(), err.Error(), "ReadOnly: You can't write against a read only replica")
assert.Empty(suite.T(), result)
}

func (suite *GlideTestSuite) TestFlushDBWithOptions_AllPrimaries() {
client := suite.defaultClusterClient()

key1 := uuid.New().String()
key2 := uuid.New().String()
_, err := client.Set(key1, "value3")
assert.NoError(suite.T(), err)
_, err = client.Set(key2, "value4")
assert.NoError(suite.T(), err)

route := config.Route(config.AllPrimaries)
result, err := client.FlushDBWithOptions(options.ASYNC, options.RouteOption{Route: route})
assert.NoError(suite.T(), err)
assert.NotEmpty(suite.T(), result)

val1, err := client.Get(key1)
assert.NoError(suite.T(), err)
assert.Empty(suite.T(), val1.Value())

val2, err := client.Get(key2)
assert.NoError(suite.T(), err)
assert.Empty(suite.T(), val2.Value())
}

func (suite *GlideTestSuite) TestFlushDBWithOptions_InvalidRoute() {
client := suite.defaultClusterClient()

invalidRoute := config.Route(config.NewByAddressRoute("invalidHost", 9999))
result, err := client.FlushDBWithOptions(options.SYNC, options.RouteOption{Route: invalidRoute})
assert.Error(suite.T(), err)
assert.Empty(suite.T(), result)
}

func (suite *GlideTestSuite) TestFlushDBWithOptions_AsyncMode() {
client := suite.defaultClusterClient()

key := uuid.New().String()
_, err := client.Set(key, "value5")
assert.NoError(suite.T(), err)

route := config.Route(config.AllPrimaries)
result, err := client.FlushDBWithOptions(options.ASYNC, options.RouteOption{Route: route})
assert.NoError(suite.T(), err)
assert.NotEmpty(suite.T(), result)

val, err := client.Get(key)
assert.NoError(suite.T(), err)
assert.Empty(suite.T(), val.Value())
}
Loading
Loading