Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into go/zrandmember
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Jan 21, 2025
2 parents 4daa25a + 918162b commit 4c4acfe
Show file tree
Hide file tree
Showing 10 changed files with 926 additions and 26 deletions.
380 changes: 376 additions & 4 deletions go/api/base_client.go

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions go/api/bitmap_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api

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

// Supports commands and transactions for the "Bitmap" group of commands for standalone and cluster clients.
//
// See [valkey.io] for details.
//
// [valkey.io]: https://valkey.io/commands/#bitmap
type BitmapCommands interface {
SetBit(key string, offset int64, value int64) (int64, error)

GetBit(key string, offset int64) (int64, error)

BitCount(key string) (int64, error)

BitCountWithOptions(key string, options *options.BitCountOptions) (int64, error)
}
2 changes: 2 additions & 0 deletions go/api/generic_base_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,4 +703,6 @@ type GenericBaseCommands interface {
//
// [valkey.io]: https://valkey.io/commands/sort/
SortReadOnlyWithOptions(key string, sortOptions *options.SortOptions) ([]Result[string], error)

Wait(numberOfReplicas int64, timeout int64) (int64, error)
}
6 changes: 6 additions & 0 deletions go/api/hash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,11 @@ type HashCommands interface {

HScan(key string, cursor string) (string, []string, error)

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

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

HRandFieldWithCountWithValues(key string, count int64) ([][]string, error)

HScanWithOptions(key string, cursor string, options *options.HashScanOptions) (string, []string, error)
}
63 changes: 63 additions & 0 deletions go/api/options/bitcount_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package options

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

type BitmapIndexType string

const (
BYTE BitmapIndexType = "BYTE"
BIT BitmapIndexType = "BIT"
)

// Optional arguments to `BitCount` in [BitMapCommands]
type BitCountOptions struct {
start *int64
end *int64
bitMapIndexType BitmapIndexType
}

func NewBitCountOptionsBuilder() *BitCountOptions {
return &BitCountOptions{}
}

// SetStart defines start byte to calculate bitcount in bitcount command.
func (options *BitCountOptions) SetStart(start int64) *BitCountOptions {
options.start = &start
return options
}

// SetEnd defines start byte to calculate bitcount in bitcount command.
func (options *BitCountOptions) SetEnd(end int64) *BitCountOptions {
options.end = &end
return options
}

// SetBitmapIndexType to specify start and end are in BYTE or BIT
func (options *BitCountOptions) SetBitmapIndexType(bitMapIndexType BitmapIndexType) *BitCountOptions {
options.bitMapIndexType = bitMapIndexType
return options
}

// ToArgs converts the options to a list of arguments.
func (opts *BitCountOptions) ToArgs() ([]string, error) {
args := []string{}
var err error

if opts.start != nil {
args = append(args, utils.IntToString(*opts.start))
if opts.end != nil {
args = append(args, utils.IntToString(*opts.end))
if opts.bitMapIndexType != "" {
if opts.bitMapIndexType == BIT || opts.bitMapIndexType == BYTE {
args = append(args, string(opts.bitMapIndexType))
}
}
}
}

return args, err
}
1 change: 1 addition & 0 deletions go/api/options/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ const (
WithScore string = "WITHSCORE" // Valkey API keyword for the with score option for zrank and zrevrank commands.
WithScores string = "WITHSCORES" // Valkey API keyword for ZRandMember command to return scores along with members.
NoScores string = "NOSCORES" // Valkey API keyword for the no scores option for zscan command.
WithValues string = "WITHVALUES" // Valkey API keyword to query hash values along their names in `HRANDFIELD`.
)
32 changes: 28 additions & 4 deletions go/api/options/stream_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ func (xpo *XPendingOptions) SetConsumer(consumer string) *XPendingOptions {
func (xpo *XPendingOptions) ToArgs() ([]string, error) {
args := []string{}

// if minIdleTime is set, we need to add an `IDLE` argument along with the minIdleTime
if xpo.minIdleTime > 0 {
args = append(args, "IDLE")
args = append(args, utils.IntToString(xpo.minIdleTime))
Expand Down Expand Up @@ -280,9 +279,6 @@ func (xgco *XGroupCreateOptions) SetMakeStream() *XGroupCreateOptions {
return xgco
}

// A value representing the number of stream entries already read by the group.
//
// Since Valkey version 7.0.0.
func (xgco *XGroupCreateOptions) SetEntriesRead(entriesRead int64) *XGroupCreateOptions {
xgco.entriesRead = entriesRead
return xgco
Expand All @@ -302,3 +298,31 @@ func (xgco *XGroupCreateOptions) ToArgs() ([]string, error) {

return args, nil
}

// Optional arguments for `XGroupSetId` in [StreamCommands]
type XGroupSetIdOptions struct {
entriesRead int64
}

// Create new empty `XGroupSetIdOptions`
func NewXGroupSetIdOptionsOptions() *XGroupSetIdOptions {
return &XGroupSetIdOptions{-1}
}

// A value representing the number of stream entries already read by the group.
//
// Since Valkey version 7.0.0.
func (xgsio *XGroupSetIdOptions) SetEntriesRead(entriesRead int64) *XGroupSetIdOptions {
xgsio.entriesRead = entriesRead
return xgsio
}

func (xgsio *XGroupSetIdOptions) ToArgs() ([]string, error) {
var args []string

if xgsio.entriesRead > -1 {
args = append(args, "ENTRIESREAD", utils.IntToString(xgsio.entriesRead))
}

return args, nil
}
50 changes: 50 additions & 0 deletions go/api/response_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,56 @@ func convertStringOrNilArray(response *C.struct_CommandResponse) ([]Result[strin
return slice, nil
}

func handle2DStringArrayResponse(response *C.struct_CommandResponse) ([][]string, error) {
defer C.free_command_response(response)
typeErr := checkResponseType(response, C.Array, false)
if typeErr != nil {
return nil, typeErr
}
array, err := parseArray(response)
if err != nil {
return nil, err
}
converted, err := arrayConverter[[]string]{
arrayConverter[string]{
nil,
false,
},
false,
}.convert(array)
if err != nil {
return nil, err
}
res, ok := converted.([][]string)
if !ok {
return nil, &RequestError{fmt.Sprintf("unexpected type: %T", converted)}
}
return res, nil
}

func handleStringArrayOrNullResponse(response *C.struct_CommandResponse) ([]Result[string], error) {
defer C.free_command_response(response)

typeErr := checkResponseType(response, C.Array, true)
if typeErr != nil {
return nil, typeErr
}

if response.response_type == C.Null {
return nil, nil
}

slice := make([]Result[string], 0, response.array_value_len)
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
res, err := convertCharArrayToString(&v, true)
if err != nil {
return nil, err
}
slice = append(slice, res)
}
return slice, nil
}

// array could be nillable, but strings - aren't
func convertStringArray(response *C.struct_CommandResponse, isNilable bool) ([]string, error) {
typeErr := checkResponseType(response, C.Array, isNilable)
Expand Down
8 changes: 8 additions & 0 deletions go/api/stream_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,19 @@ type StreamCommands interface {

XPendingWithOptions(key string, group string, options *options.XPendingOptions) ([]XPendingDetail, error)

XGroupSetId(key string, group string, id string) (string, error)

XGroupSetIdWithOptions(key string, group string, id string, opts *options.XGroupSetIdOptions) (string, error)

XGroupCreate(key string, group string, id string) (string, error)

XGroupCreateWithOptions(key string, group string, id string, opts *options.XGroupCreateOptions) (string, error)

XGroupDestroy(key string, group string) (bool, error)

XGroupCreateConsumer(key string, group string, consumer string) (bool, error)

XGroupDelConsumer(key string, group string, consumer string) (int64, error)

XAck(key string, group string, ids []string) (int64, error)
}
Loading

0 comments on commit 4c4acfe

Please sign in to comment.