diff --git a/go/api/glide_cluster_client.go b/go/api/glide_cluster_client.go index eb967bced1..9c72a86562 100644 --- a/go/api/glide_cluster_client.go +++ b/go/api/glide_cluster_client.go @@ -298,3 +298,28 @@ func (client *GlideClusterClient) TimeWithOptions(opts options.RouteOption) (Clu } return handleTimeClusterResponse(result) } + +// Returns the number of keys in the database. +// +// Return value: +// +// The number of keys in the database. +// +// Example: +// +// route := api.SimpleNodeRoute(api.RandomRoute) +// options := options.NewDBOptionsBuilder().SetRoute(route) +// result, err := client.DBSizeWithOption(route) +// if err != nil { +// // handle error +// } +// fmt.Println(result) // Output: 1 +// +// [valkey.io]: https://valkey.io/commands/dbsize/ +func (client *GlideClusterClient) DBSizeWithOptions(opts options.RouteOption) (int64, error) { + result, err := client.executeCommandWithRoute(C.DBSize, []string{}, opts.Route) + if err != nil { + return defaultIntResponse, err + } + return handleIntResponse(result) +} diff --git a/go/api/options/db_size_options.go b/go/api/options/db_size_options.go new file mode 100644 index 0000000000..7ebdb6a6de --- /dev/null +++ b/go/api/options/db_size_options.go @@ -0,0 +1,16 @@ +package options + +import "github.com/valkey-io/valkey-glide/go/glide/api/config" + +type DBSizeOptions struct { + Route config.Route +} + +func NewTimeOptionsBuilder() *DBSizeOptions { + return &DBSizeOptions{} +} + +func (dbSizeOptions *DBSizeOptions) SetRoute(route config.Route) *DBSizeOptions { + dbSizeOptions.Route = route + return dbSizeOptions +} diff --git a/go/api/options/route_options.go b/go/api/options/route_options.go index 1f1cbd2b20..177f470529 100644 --- a/go/api/options/route_options.go +++ b/go/api/options/route_options.go @@ -4,6 +4,9 @@ package options import "github.com/valkey-io/valkey-glide/go/glide/api/config" +// An extension to command option types with Routes type RouteOption struct { + // Specifies the routing configuration for the command. + // The client will route the command to the nodes defined by `route`. Route config.Route } diff --git a/go/api/server_management_cluster_commands.go b/go/api/server_management_cluster_commands.go index b49737c05b..87a04e2813 100644 --- a/go/api/server_management_cluster_commands.go +++ b/go/api/server_management_cluster_commands.go @@ -4,7 +4,7 @@ package api import "github.com/valkey-io/valkey-glide/go/glide/api/options" -// ServerManagementClusterCommands supports commands for the "Server Management Commands" group for cluster client. +// ServerManagementCommands supports commands for the "Server Management" group for a cluster client. // // See [valkey.io] for details. // @@ -15,4 +15,6 @@ type ServerManagementClusterCommands interface { InfoWithOptions(options ClusterInfoOptions) (ClusterValue[string], error) TimeWithOptions(routeOption options.RouteOption) (ClusterValue[[]string], error) + + DBSizeWithOptions(routeOption options.RouteOption) (int64, error) } diff --git a/go/integTest/cluster_commands_test.go b/go/integTest/cluster_commands_test.go index 52bca71abb..19a8167dce 100644 --- a/go/integTest/cluster_commands_test.go +++ b/go/integTest/cluster_commands_test.go @@ -244,3 +244,14 @@ func (suite *GlideTestSuite) TestTimeWithInvalidRoute() { assert.True(suite.T(), result.IsEmpty()) assert.Empty(suite.T(), result.SingleValue()) } + +func (suite *GlideTestSuite) TestDBSizeRandomRoute() { + client := suite.defaultClusterClient() + route := config.Route(config.RandomRoute) + options := options.RouteOption{Route: route} + result, err := client.DBSizeWithOptions(options) + assert.NoError(suite.T(), err) + assert.NotNil(suite.T(), result) + assert.NotEmpty(suite.T(), result) + assert.Greater(suite.T(), result, int64(0)) +}