Skip to content

Commit

Permalink
Fixed review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Niharika Bhavaraju <[email protected]>
  • Loading branch information
niharikabhavaraju committed Jan 6, 2025
1 parent 6a1ed9c commit eaf901b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 150 deletions.
6 changes: 3 additions & 3 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ func (client *baseClient) Sort(key string) ([]Result[string], error) {
return handleStringArrayResponse(result)
}

func (client *baseClient) SortWithOptions(key string, options *SortOptions) ([]Result[string], error) {
func (client *baseClient) SortWithOptions(key string, options *options.SortOptions) ([]Result[string], error) {
optionArgs := options.ToArgs()
result, err := client.executeCommand(C.Sort, append([]string{key}, optionArgs...))
if err != nil {
Expand All @@ -1467,7 +1467,7 @@ func (client *baseClient) SortReadOnly(key string) ([]Result[string], error) {
return handleStringArrayResponse(result)
}

func (client *baseClient) SortReadOnlyWithOptions(key string, options *SortOptions) ([]Result[string], error) {
func (client *baseClient) SortReadOnlyWithOptions(key string, options *options.SortOptions) ([]Result[string], error) {
optionArgs := options.ToArgs()
result, err := client.executeCommand(C.SortReadOnly, append([]string{key}, optionArgs...))
if err != nil {
Expand All @@ -1484,7 +1484,7 @@ func (client *baseClient) SortStore(key string, destination string) (Result[int6
return handleLongResponse(result)
}

func (client *baseClient) SortStoreWithOptions(key string, destination string, options *SortOptions) (Result[int64], error) {
func (client *baseClient) SortStoreWithOptions(key string, destination string, options *options.SortOptions) (Result[int64], error) {
optionArgs := options.ToArgs()
result, err := client.executeCommand(C.Sort, append([]string{key, "STORE", destination}, optionArgs...))
if err != nil {
Expand Down
123 changes: 0 additions & 123 deletions go/api/command_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package api

import (
"fmt"
"strconv"

"github.com/valkey-io/valkey-glide/go/glide/utils"
Expand Down Expand Up @@ -280,128 +279,6 @@ func (listDirection ListDirection) toString() (string, error) {
}
}

const (
// LIMIT subcommand string to include in the SORT and SORT_RO commands.
LIMIT_COMMAND_STRING = "LIMIT"
// ALPHA subcommand string to include in the SORT and SORT_RO commands.
ALPHA_COMMAND_STRING = "ALPHA"
// BY subcommand string to include in the SORT and SORT_RO commands.
// Supported in cluster mode since Valkey version 8.0 and above.
BY_COMMAND_STRING = "BY"
// GET subcommand string to include in the SORT and SORT_RO commands.
GET_COMMAND_STRING = "GET"
)

// Limit struct represents the range of elements to retrieve
// The LIMIT argument is commonly used to specify a subset of results from the matching elements, similar to the
// LIMIT clause in SQL (e.g., `SELECT LIMIT offset, count`).
type Limit struct {
// The starting position of the range, zero based.
Offset int64
// The maximum number of elements to include in the range. A negative count returns all elementsnfrom the offset.
Count int64
}

// OrderBy specifies the order to sort the elements. Can be ASC (ascending) or DESC(descending).
type OrderBy string

const (
ASC OrderBy = "ASC"
DESC OrderBy = "DESC"
)

// SortOptions struct combines both the base options and additional sorting options
type SortOptions struct {
// Limit Limits the range of elements
Limit *Limit

// OrderBy sets the order to sort by (ASC or DESC)
OrderBy OrderBy

// IsAlpha determines whether to sort lexicographically (true) or numerically (false)
IsAlpha bool

// ByPattern - a pattern to sort by external keys instead of by the elements stored at the key themselves. The
// pattern should contain an asterisk (*) as a placeholder for the element values, where the value
// from the key replaces the asterisk to create the key name. For example, if key
// contains IDs of objects, byPattern can be used to sort these IDs based on an
// attribute of the objects, like their weights or timestamps. Supported in cluster mode since
// Valkey version 8.0 and above.
ByPattern string

// A pattern used to retrieve external keys' values, instead of the elements at key.
// The pattern should contain an asterisk (*) as a placeholder for the element values, where the
// value from key replaces the asterisk to create the key name. This
// allows the sorted elements to be transformed based on the related keys values. For example, if
// key< contains IDs of users, getPatterns can be used to retrieve
// specific attributes of these users, such as their names or email addresses. E.g., if
// getPatterns is name_*, the command will return the values of the keys
// name_&lt;element&gt; for each sorted element. Multiple getPatterns
// arguments can be provided to retrieve multiple attributes. The special value # can
// be used to include the actual element from key being sorted. If not provided, only
// the sorted elements themselves are returned.
// Supported in cluster mode since Valkey version 8.0 and above.
GetPatterns []string // List of patterns to retrieve external keys' values
}

func NewSortOptions() *SortOptions {
return &SortOptions{
OrderBy: ASC, // Default order is ascending
IsAlpha: false, // Default is numeric sorting
}
}

func (opts *SortOptions) SetLimit(offset, count int64) *SortOptions {
opts.Limit = &Limit{Offset: offset, Count: count}
return opts
}

func (opts *SortOptions) SetOrderBy(order OrderBy) *SortOptions {
opts.OrderBy = order
return opts
}

func (opts *SortOptions) SetIsAlpha(isAlpha bool) *SortOptions {
opts.IsAlpha = isAlpha
return opts
}

func (opts *SortOptions) SetByPattern(byPattern string) *SortOptions {
opts.ByPattern = byPattern
return opts
}

func (opts *SortOptions) AddGetPattern(getPattern string) *SortOptions {
opts.GetPatterns = append(opts.GetPatterns, getPattern)
return opts
}

// ToArgs creates the arguments to be used in SORT and SORT_RO commands.
func (opts *SortOptions) ToArgs() []string {
var args []string

if opts.Limit != nil {
args = append(args, LIMIT_COMMAND_STRING, fmt.Sprintf("%d", opts.Limit.Offset), fmt.Sprintf("%d", opts.Limit.Count))
}

if opts.OrderBy != "" {
args = append(args, string(opts.OrderBy))
}

if opts.IsAlpha {
args = append(args, ALPHA_COMMAND_STRING)
}

if opts.ByPattern != "" {
args = append(args, BY_COMMAND_STRING, opts.ByPattern)
}

for _, getPattern := range opts.GetPatterns {
args = append(args, GET_COMMAND_STRING, getPattern)
}
return args
}

// This base option struct represents the common set of optional arguments for the SCAN family of commands.
// Concrete implementations of this class are tied to specific SCAN commands (`SCAN`, `SSCAN`).
type BaseScanOptions struct {
Expand Down
22 changes: 11 additions & 11 deletions go/api/generic_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/glide/api/options"

// Supports commands and transactions for the "Generic" group of commands for standalone and cluster clients.
//
// See [valkey.io] for details.
Expand All @@ -11,7 +13,7 @@ type GenericBaseCommands interface {
// Del removes the specified keys from the database. A key is ignored if it does not exist.
//
// Note:
// In cluster mode, if keys in `keyValueMap` map to different hash slots, the command
// In cluster mode, if `key` and `destination` map to different hash slots, the command
// will be split across these slots and executed separately for each. This means the command
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
// call will return the first encountered error, even though some requests may have succeeded
Expand All @@ -37,7 +39,7 @@ type GenericBaseCommands interface {
// Exists returns the number of keys that exist in the database
//
// Note:
// In cluster mode, if keys in `keyValueMap` map to different hash slots, the command
// In cluster mode, if `key` and `destination` map to different hash slots, the command
// will be split across these slots and executed separately for each. This means the command
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
// call will return the first encountered error, even though some requests may have succeeded
Expand Down Expand Up @@ -338,7 +340,7 @@ type GenericBaseCommands interface {
// To store the result into a new key, see {@link #sortStore(string, string)}.
//
// Note:
// In cluster mode, if keys in `keyValueMap` map to different hash slots, the command
// In cluster mode, if `key` and `destination` map to different hash slots, the command
// will be split across these slots and executed separately for each. This means the command
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
// call will return the first encountered error, even though some requests may have succeeded
Expand All @@ -362,7 +364,7 @@ type GenericBaseCommands interface {
// result.IsNil(): false
//
// [valkey.io]: https://valkey.io/commands/sort/
SortWithOptions(key string, sortOptions *SortOptions) ([]Result[string], error)
SortWithOptions(key string, sortOptions *options.SortOptions) ([]Result[string], error)

// Sorts the elements in the list, set, or sorted set at key and stores the result in
// destination. The sort command can be used to sort elements based on
Expand All @@ -372,7 +374,7 @@ type GenericBaseCommands interface {
// To get the sort result without storing it into a key, see {@link #sort(String)} or {@link #sortReadOnly(String)}.
//
// Note:
// In cluster mode, if keys in `keyValueMap` map to different hash slots, the command
// In cluster mode, if `key` and `destination` map to different hash slots, the command
// will be split across these slots and executed separately for each. This means the command
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
// call will return the first encountered error, even though some requests may have succeeded
Expand Down Expand Up @@ -403,7 +405,7 @@ type GenericBaseCommands interface {
// To get the sort result without storing it into a key, see {@link #sort(String)} or {@link #sortReadOnly(String)}.
//
// Note:
// In cluster mode, if keys in `keyValueMap` map to different hash slots, the command
// In cluster mode, if `key` and `destination` map to different hash slots, the command
// will be split across these slots and executed separately for each. This means the command
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
// call will return the first encountered error, even though some requests may have succeeded
Expand All @@ -428,7 +430,7 @@ type GenericBaseCommands interface {
// result.IsNil(): false
//
// [valkey.io]: https://valkey.io/commands/sort/
SortStoreWithOptions(key string, destination string, sortOptions *SortOptions) (Result[int64], error)
SortStoreWithOptions(key string, destination string, sortOptions *options.SortOptions) (Result[int64], error)

// Sorts the elements in the list, set, or sorted set at key and returns the result.
// The sortReadOnly command can be used to sort elements based on different criteria and apply
Expand Down Expand Up @@ -456,16 +458,14 @@ type GenericBaseCommands interface {
// This command is routed depending on the client's {@link ReadFrom} strategy.
//
// Note:
// In cluster mode, if keys in `keyValueMap` map to different hash slots, the command
// In cluster mode, if `key` and `destination` map to different hash slots, the command
// will be split across these slots and executed separately for each. This means the command
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
// call will return the first encountered error, even though some requests may have succeeded
// while others did not. If this behavior impacts your application logic, consider splitting
// the request into sub-requests per slot to ensure atomicity.
// The use of {@link SortOptions#byPattern} and {@link SortOptions#getPatterns} in cluster mode is
// supported since Valkey version 8.0.
// The use of {@link SortOptions#byPattern} and {@link SortOptions#getPatterns} in cluster mode is
// supported since Valkey version 8.0.
//
// Parameters:
// key - The key of the list, set, or sorted set to be sorted.
Expand All @@ -482,7 +482,7 @@ type GenericBaseCommands interface {
// result.IsNil(): false
//
// [valkey.io]: https://valkey.io/commands/sort/
SortReadOnlyWithOptions(key string, sortOptions *SortOptions) ([]Result[string], error)
SortReadOnlyWithOptions(key string, sortOptions *options.SortOptions) ([]Result[string], error)

// Unlink (delete) multiple keys from the database. A key is ignored if it does not exist.
// This command, similar to Del However, this command does not block the server
Expand Down
129 changes: 129 additions & 0 deletions go/api/options/sort_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package options

import (
"github.com/valkey-io/valkey-glide/go/glide/utils"
)

const (
// LIMIT subcommand string to include in the SORT and SORT_RO commands.
LIMIT_COMMAND_STRING = "LIMIT"
// ALPHA subcommand string to include in the SORT and SORT_RO commands.
ALPHA_COMMAND_STRING = "ALPHA"
// BY subcommand string to include in the SORT and SORT_RO commands.
// Supported in cluster mode since Valkey version 8.0 and above.
BY_COMMAND_STRING = "BY"
// GET subcommand string to include in the SORT and SORT_RO commands.
GET_COMMAND_STRING = "GET"
)

// Limit struct represents the range of elements to retrieve
// The LIMIT argument is commonly used to specify a subset of results from the matching elements, similar to the
// LIMIT clause in SQL (e.g., `SELECT LIMIT offset, count`).
type Limit struct {
// The starting position of the range, zero based.
Offset int64
// The maximum number of elements to include in the range. A negative count returns all elementsnfrom the offset.
Count int64
}

// OrderBy specifies the order to sort the elements. Can be ASC (ascending) or DESC(descending).
type OrderBy string

const (
ASC OrderBy = "ASC"
DESC OrderBy = "DESC"
)

// SortOptions struct combines both the base options and additional sorting options
type SortOptions struct {
// Limit Limits the range of elements
Limit *Limit

// OrderBy sets the order to sort by (ASC or DESC)
OrderBy OrderBy

// IsAlpha determines whether to sort lexicographically (true) or numerically (false)
IsAlpha bool

// ByPattern - a pattern to sort by external keys instead of by the elements stored at the key themselves. The
// pattern should contain an asterisk (*) as a placeholder for the element values, where the value
// from the key replaces the asterisk to create the key name. For example, if key
// contains IDs of objects, byPattern can be used to sort these IDs based on an
// attribute of the objects, like their weights or timestamps.
// Supported in cluster mode since Valkey version 8.0 and above.
ByPattern string

// A pattern used to retrieve external keys' values, instead of the elements at key.
// The pattern should contain an asterisk (*) as a placeholder for the element values, where the
// value from key replaces the asterisk to create the key name. This
// allows the sorted elements to be transformed based on the related keys values. For example, if
// key< contains IDs of users, getPatterns can be used to retrieve
// specific attributes of these users, such as their names or email addresses. E.g., if
// getPatterns is name_*, the command will return the values of the keys
// name_&lt;element&gt; for each sorted element. Multiple getPatterns
// arguments can be provided to retrieve multiple attributes. The special value # can
// be used to include the actual element from key being sorted. If not provided, only
// the sorted elements themselves are returned.
// Supported in cluster mode since Valkey version 8.0 and above.
GetPatterns []string // List of patterns to retrieve external keys' values
}

func NewSortOptions() *SortOptions {
return &SortOptions{
OrderBy: ASC, // Default order is ascending
IsAlpha: false, // Default is numeric sorting
}
}

func (opts *SortOptions) SetLimit(offset, count int64) *SortOptions {
opts.Limit = &Limit{Offset: offset, Count: count}
return opts
}

func (opts *SortOptions) SetOrderBy(order OrderBy) *SortOptions {
opts.OrderBy = order
return opts
}

func (opts *SortOptions) SetIsAlpha(isAlpha bool) *SortOptions {
opts.IsAlpha = isAlpha
return opts
}

func (opts *SortOptions) SetByPattern(byPattern string) *SortOptions {
opts.ByPattern = byPattern
return opts
}

func (opts *SortOptions) AddGetPattern(getPattern string) *SortOptions {
opts.GetPatterns = append(opts.GetPatterns, getPattern)
return opts
}

// ToArgs creates the arguments to be used in SORT and SORT_RO commands.
func (opts *SortOptions) ToArgs() []string {
var args []string

if opts.Limit != nil {
args = append(args, LIMIT_COMMAND_STRING, utils.IntToString(opts.Limit.Offset), utils.IntToString(opts.Limit.Count))
}

if opts.OrderBy != "" {
args = append(args, string(opts.OrderBy))
}

if opts.IsAlpha {
args = append(args, ALPHA_COMMAND_STRING)
}

if opts.ByPattern != "" {
args = append(args, BY_COMMAND_STRING, opts.ByPattern)
}

for _, getPattern := range opts.GetPatterns {
args = append(args, GET_COMMAND_STRING, getPattern)
}
return args
}
Loading

0 comments on commit eaf901b

Please sign in to comment.