Skip to content

Commit 97125d9

Browse files
Merge remote-tracking branch 'upstream/main' into go/hrandfield
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
2 parents b60dbdc + a2e90f6 commit 97125d9

File tree

10 files changed

+1241
-243
lines changed

10 files changed

+1241
-243
lines changed

go/api/base_client.go

Lines changed: 299 additions & 25 deletions
Large diffs are not rendered by default.

go/api/generic_base_commands.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package api
44

5+
import "github.com/valkey-io/valkey-glide/go/glide/api/options"
6+
57
// Supports commands and transactions for the "Generic Commands" group for standalone and cluster clients.
68
//
79
// See [valkey.io] for details.
@@ -527,4 +529,180 @@ type GenericBaseCommands interface {
527529
//
528530
// [valkey.io]: https://valkey.io/commands/dump/
529531
Dump(key string) (Result[string], error)
532+
533+
ObjectFreq(key string) (Result[int64], error)
534+
535+
ObjectIdleTime(key string) (Result[int64], error)
536+
537+
ObjectRefCount(key string) (Result[int64], error)
538+
539+
// Sorts the elements in the list, set, or sorted set at key and returns the result.
540+
// The sort command can be used to sort elements based on different criteria and apply
541+
// transformations on sorted elements.
542+
// To store the result into a new key, see the sortStore function.
543+
//
544+
// Parameters:
545+
// key - The key of the list, set, or sorted set to be sorted.
546+
//
547+
// Return value:
548+
// An Array of sorted elements.
549+
//
550+
// Example:
551+
//
552+
// result, err := client.Sort("key")
553+
// result.Value(): [{1 false} {2 false} {3 false}]
554+
// result.IsNil(): false
555+
//
556+
// [valkey.io]: https://valkey.io/commands/sort/
557+
Sort(key string) ([]Result[string], error)
558+
559+
// Sorts the elements in the list, set, or sorted set at key and returns the result.
560+
// The sort command can be used to sort elements based on different criteria and apply
561+
// transformations on sorted elements.
562+
// To store the result into a new key, see the sortStore function.
563+
//
564+
// Note:
565+
// In cluster mode, if `key` map to different hash slots, the command
566+
// will be split across these slots and executed separately for each. This means the command
567+
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
568+
// call will return the first encountered error, even though some requests may have succeeded
569+
// while others did not. If this behavior impacts your application logic, consider splitting
570+
// the request into sub-requests per slot to ensure atomicity.
571+
// The use of SortOptions.byPattern and SortOptions.getPatterns in cluster mode is
572+
// supported since Valkey version 8.0.
573+
//
574+
// Parameters:
575+
// key - The key of the list, set, or sorted set to be sorted.
576+
// sortOptions - The SortOptions type.
577+
//
578+
// Return value:
579+
// An Array of sorted elements.
580+
//
581+
// Example:
582+
//
583+
// options := api.NewSortOptions().SetByPattern("weight_*").SetIsAlpha(false).AddGetPattern("object_*").AddGetPattern("#")
584+
// result, err := client.Sort("key", options)
585+
// result.Value(): [{Object_3 false} {c false} {Object_1 false} {a false} {Object_2 false} {b false}]
586+
// result.IsNil(): false
587+
//
588+
// [valkey.io]: https://valkey.io/commands/sort/
589+
SortWithOptions(key string, sortOptions *options.SortOptions) ([]Result[string], error)
590+
591+
// Sorts the elements in the list, set, or sorted set at key and stores the result in
592+
// destination. The sort command can be used to sort elements based on
593+
// different criteria, apply transformations on sorted elements, and store the result in a new key.
594+
// The sort command can be used to sort elements based on different criteria and apply
595+
// transformations on sorted elements.
596+
// To get the sort result without storing it into a key, see the sort or sortReadOnly function.
597+
//
598+
// Note:
599+
// In cluster mode, if `key` and `destination` map to different hash slots, the command
600+
// will be split across these slots and executed separately for each. This means the command
601+
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
602+
// call will return the first encountered error, even though some requests may have succeeded
603+
// while others did not. If this behavior impacts your application logic, consider splitting
604+
// the request into sub-requests per slot to ensure atomicity.
605+
//
606+
// Parameters:
607+
// key - The key of the list, set, or sorted set to be sorted.
608+
// destination - The key where the sorted result will be stored.
609+
//
610+
// Return value:
611+
// The number of elements in the sorted key stored at destination.
612+
//
613+
// Example:
614+
//
615+
// result, err := client.SortStore("key","destkey")
616+
// result.Value(): 1
617+
// result.IsNil(): false
618+
//
619+
// [valkey.io]: https://valkey.io/commands/sort/
620+
SortStore(key string, destination string) (Result[int64], error)
621+
622+
// Sorts the elements in the list, set, or sorted set at key and stores the result in
623+
// destination. The sort command can be used to sort elements based on
624+
// different criteria, apply transformations on sorted elements, and store the result in a new key.
625+
// The sort command can be used to sort elements based on different criteria and apply
626+
// transformations on sorted elements.
627+
// To get the sort result without storing it into a key, see the sort or sortReadOnly function.
628+
//
629+
// Note:
630+
// In cluster mode, if `key` and `destination` map to different hash slots, the command
631+
// will be split across these slots and executed separately for each. This means the command
632+
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
633+
// call will return the first encountered error, even though some requests may have succeeded
634+
// while others did not. If this behavior impacts your application logic, consider splitting
635+
// the request into sub-requests per slot to ensure atomicity.
636+
// The use of SortOptions.byPattern and SortOptions.getPatterns
637+
// in cluster mode is supported since Valkey version 8.0.
638+
//
639+
// Parameters:
640+
// key - The key of the list, set, or sorted set to be sorted.
641+
// destination - The key where the sorted result will be stored.
642+
// sortOptions - The SortOptions type.
643+
//
644+
// Return value:
645+
// The number of elements in the sorted key stored at destination.
646+
//
647+
// Example:
648+
//
649+
// options := api.NewSortOptions().SetByPattern("weight_*").SetIsAlpha(false).AddGetPattern("object_*").AddGetPattern("#")
650+
// result, err := client.SortStore("key","destkey",options)
651+
// result.Value(): 1
652+
// result.IsNil(): false
653+
//
654+
// [valkey.io]: https://valkey.io/commands/sort/
655+
SortStoreWithOptions(key string, destination string, sortOptions *options.SortOptions) (Result[int64], error)
656+
657+
// Sorts the elements in the list, set, or sorted set at key and returns the result.
658+
// The sortReadOnly command can be used to sort elements based on different criteria and apply
659+
// transformations on sorted elements.
660+
// This command is routed depending on the client's ReadFrom strategy.
661+
//
662+
// Parameters:
663+
// key - The key of the list, set, or sorted set to be sorted.
664+
//
665+
// Return value:
666+
// An Array of sorted elements.
667+
//
668+
// Example:
669+
//
670+
// result, err := client.SortReadOnly("key")
671+
// result.Value(): [{1 false} {2 false} {3 false}]
672+
// result.IsNil(): false
673+
//
674+
// [valkey.io]: https://valkey.io/commands/sort/
675+
SortReadOnly(key string) ([]Result[string], error)
676+
677+
// Sorts the elements in the list, set, or sorted set at key and returns the result.
678+
// The sort command can be used to sort elements based on different criteria and apply
679+
// transformations on sorted elements.
680+
// This command is routed depending on the client's ReadFrom strategy.
681+
//
682+
// Note:
683+
// In cluster mode, if `key` map to different hash slots, the command
684+
// will be split across these slots and executed separately for each. This means the command
685+
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
686+
// call will return the first encountered error, even though some requests may have succeeded
687+
// while others did not. If this behavior impacts your application logic, consider splitting
688+
// the request into sub-requests per slot to ensure atomicity.
689+
// The use of SortOptions.byPattern and SortOptions.getPatterns in cluster mode is
690+
// supported since Valkey version 8.0.
691+
//
692+
// Parameters:
693+
// key - The key of the list, set, or sorted set to be sorted.
694+
// sortOptions - The SortOptions type.
695+
//
696+
// Return value:
697+
// An Array of sorted elements.
698+
//
699+
// Example:
700+
//
701+
// options := api.NewSortOptions().SetByPattern("weight_*").SetIsAlpha(false).AddGetPattern("object_*").AddGetPattern("#")
702+
// result, err := client.SortReadOnly("key", options)
703+
// result.Value(): [{Object_3 false} {c false} {Object_1 false} {a false} {Object_2 false} {b false}]
704+
// result.IsNil(): false
705+
//
706+
// [valkey.io]: https://valkey.io/commands/sort/
707+
SortReadOnlyWithOptions(key string, sortOptions *options.SortOptions) ([]Result[string], error)
530708
}

go/api/hash_commands.go

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -290,63 +290,13 @@ type HashCommands interface {
290290
// [valkey.io]: https://valkey.io/commands/hincrbyfloat/
291291
HIncrByFloat(key string, field string, increment float64) (float64, error)
292292

293-
// Iterates fields of Hash types and their associated values. This definition of HSCAN command does not include the
294-
// optional arguments of the command.
295-
//
296-
// See [valkey.io] for details.
297-
//
298-
// Parameters:
299-
// key - The key of the hash.
300-
// cursor - The cursor that points to the next iteration of results. A value of "0" indicates the start of the search.
301-
//
302-
// Return value:
303-
// An array of the cursor and the subset of the hash held by `key`. The first element is always the `cursor`
304-
// for the next iteration of results. The `cursor` will be `"0"` on the last iteration of the subset.
305-
// The second element is always an array of the subset of the set held in `key`. The array in the
306-
// second element is always a flattened series of String pairs, where the key is at even indices
307-
// and the value is at odd indices.
308-
//
309-
// Example:
310-
// // Assume key contains a hash {{"a": "1"}, {"b", "2"}}
311-
// resCursor, resCollection, err = client.HScan(key, initialCursor)
312-
// // resCursor = {0 false}
313-
// // resCollection = [{a false} {1 false} {b false} {2 false}]
314-
//
315-
// [valkey.io]: https://valkey.io/commands/hscan/
316-
HScan(key string, cursor string) (Result[string], []Result[string], error)
317-
318-
// Iterates fields of Hash types and their associated values. This definition of HSCAN includes optional arguments of the
319-
// command.
320-
//
321-
// See [valkey.io] for details.
322-
//
323-
// Parameters:
324-
// key - The key of the hash.
325-
// cursor - The cursor that points to the next iteration of results. A value of "0" indicates the start of the search.
326-
// options - The [api.HashScanOptions].
327-
//
328-
// Return value:
329-
// An array of the cursor and the subset of the hash held by `key`. The first element is always the `cursor`
330-
// for the next iteration of results. The `cursor` will be `"0"` on the last iteration of the subset.
331-
// The second element is always an array of the subset of the set held in `key`. The array in the
332-
// second element is always a flattened series of String pairs, where the key is at even indices
333-
// and the value is at odd indices.
334-
//
335-
// Example:
336-
// // Assume key contains a hash {{"a": "1"}, {"b", "2"}}
337-
// opts := options.NewHashScanOptionsBuilder().SetMatch("a")
338-
// resCursor, resCollection, err = client.HScan(key, initialCursor, opts)
339-
// // resCursor = {0 false}
340-
// // resCollection = [{a false} {1 false}]
341-
// // The resCollection only contains the hash map entry that matches with the match option provided with the command
342-
// // input.
343-
//
344-
// [valkey.io]: https://valkey.io/commands/hscan/
345-
HScanWithOptions(key string, cursor string, options *options.HashScanOptions) (Result[string], []Result[string], error)
293+
HScan(key string, cursor string) (string, []string, error)
346294

347295
HRandField(key string) (Result[string], error)
348296

349297
HRandFieldWithCount(key string, count int64) ([]string, error)
350298

351299
HRandFieldWithCountWithValues(key string, count int64) ([][]string, error)
300+
301+
HScanWithOptions(key string, cursor string, options *options.HashScanOptions) (string, []string, error)
352302
}

go/api/options/sort_options.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
2+
3+
package options
4+
5+
import (
6+
"github.com/valkey-io/valkey-glide/go/glide/utils"
7+
)
8+
9+
const (
10+
// LIMIT subcommand string to include in the SORT and SORT_RO commands.
11+
LIMIT_COMMAND_STRING = "LIMIT"
12+
// ALPHA subcommand string to include in the SORT and SORT_RO commands.
13+
ALPHA_COMMAND_STRING = "ALPHA"
14+
// BY subcommand string to include in the SORT and SORT_RO commands.
15+
// Supported in cluster mode since Valkey version 8.0 and above.
16+
BY_COMMAND_STRING = "BY"
17+
// GET subcommand string to include in the SORT and SORT_RO commands.
18+
GET_COMMAND_STRING = "GET"
19+
)
20+
21+
// SortLimit struct represents the range of elements to retrieve
22+
// The LIMIT argument is commonly used to specify a subset of results from the matching elements, similar to the
23+
// LIMIT clause in SQL (e.g., `SELECT LIMIT offset, count`).
24+
type SortLimit struct {
25+
Offset int64
26+
Count int64
27+
}
28+
29+
// OrderBy specifies the order to sort the elements. Can be ASC (ascending) or DESC(descending).
30+
type OrderBy string
31+
32+
const (
33+
ASC OrderBy = "ASC"
34+
DESC OrderBy = "DESC"
35+
)
36+
37+
// SortOptions struct combines both the base options and additional sorting options
38+
type SortOptions struct {
39+
SortLimit *SortLimit
40+
OrderBy OrderBy
41+
IsAlpha bool
42+
ByPattern string
43+
GetPatterns []string
44+
}
45+
46+
func NewSortOptions() *SortOptions {
47+
return &SortOptions{
48+
OrderBy: ASC, // Default order is ascending
49+
IsAlpha: false, // Default is numeric sorting
50+
}
51+
}
52+
53+
// SortLimit Limits the range of elements
54+
// Offset is the starting position of the range, zero based.
55+
// Count is the maximum number of elements to include in the range.
56+
// A negative count returns all elements from the offset.
57+
func (opts *SortOptions) SetSortLimit(offset, count int64) *SortOptions {
58+
opts.SortLimit = &SortLimit{Offset: offset, Count: count}
59+
return opts
60+
}
61+
62+
// OrderBy sets the order to sort by (ASC or DESC)
63+
func (opts *SortOptions) SetOrderBy(order OrderBy) *SortOptions {
64+
opts.OrderBy = order
65+
return opts
66+
}
67+
68+
// IsAlpha determines whether to sort lexicographically (true) or numerically (false)
69+
func (opts *SortOptions) SetIsAlpha(isAlpha bool) *SortOptions {
70+
opts.IsAlpha = isAlpha
71+
return opts
72+
}
73+
74+
// ByPattern - a pattern to sort by external keys instead of by the elements stored at the key themselves. The
75+
// pattern should contain an asterisk (*) as a placeholder for the element values, where the value
76+
// from the key replaces the asterisk to create the key name. For example, if key
77+
// contains IDs of objects, byPattern can be used to sort these IDs based on an
78+
// attribute of the objects, like their weights or timestamps.
79+
// Supported in cluster mode since Valkey version 8.0 and above.
80+
func (opts *SortOptions) SetByPattern(byPattern string) *SortOptions {
81+
opts.ByPattern = byPattern
82+
return opts
83+
}
84+
85+
// A pattern used to retrieve external keys' values, instead of the elements at key.
86+
// The pattern should contain an asterisk (*) as a placeholder for the element values, where the
87+
// value from key replaces the asterisk to create the key name. This
88+
// allows the sorted elements to be transformed based on the related keys values. For example, if
89+
// key< contains IDs of users, getPatterns can be used to retrieve
90+
// specific attributes of these users, such as their names or email addresses. E.g., if
91+
// getPatterns is name_*, the command will return the values of the keys
92+
// name_&lt;element&gt; for each sorted element. Multiple getPatterns
93+
// arguments can be provided to retrieve multiple attributes. The special value # can
94+
// be used to include the actual element from key being sorted. If not provided, only
95+
// the sorted elements themselves are returned.
96+
// Supported in cluster mode since Valkey version 8.0 and above.
97+
func (opts *SortOptions) AddGetPattern(getPattern string) *SortOptions {
98+
opts.GetPatterns = append(opts.GetPatterns, getPattern)
99+
return opts
100+
}
101+
102+
// ToArgs creates the arguments to be used in SORT and SORT_RO commands.
103+
func (opts *SortOptions) ToArgs() []string {
104+
var args []string
105+
106+
if opts.SortLimit != nil {
107+
args = append(
108+
args,
109+
LIMIT_COMMAND_STRING,
110+
utils.IntToString(opts.SortLimit.Offset),
111+
utils.IntToString(opts.SortLimit.Count),
112+
)
113+
}
114+
115+
if opts.OrderBy != "" {
116+
args = append(args, string(opts.OrderBy))
117+
}
118+
119+
if opts.IsAlpha {
120+
args = append(args, ALPHA_COMMAND_STRING)
121+
}
122+
123+
if opts.ByPattern != "" {
124+
args = append(args, BY_COMMAND_STRING, opts.ByPattern)
125+
}
126+
127+
for _, getPattern := range opts.GetPatterns {
128+
args = append(args, GET_COMMAND_STRING, getPattern)
129+
}
130+
return args
131+
}

0 commit comments

Comments
 (0)