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: XINFO STREAM. #2994

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
119 changes: 119 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3492,3 +3492,122 @@ func (client *baseClient) XClaimJustIdWithOptions(
}
return handleStringArrayResponse(result)
}

// Returns information about the stream stored at `key`.
//
// See [valkey.io] for details.
//
// Parameters:
//
// key - The key of the stream.
//
// Return value:
//
// A stream information for the given `key`. See the example for a sample response.
//
// Example:
//
// infoBreef, err := client.XInfoStream(key)
// infoBreef:
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
// // map[string]any {
// // "entries-added" : 1,
// // "first-entry" : []any{
// // "1719877599564-0", []any{"some_field", "some_value", ...},
// // },
// // "groups" : 1,
// // "last-entry" : []any{
// // "1719877599564-1", []any{"some_field", "some_value", ...},
// // },
// // "last-generated-id" : "1719877599564-1",
// // "length" : 1,
// // "max-deleted-entry-id" : "0-0",
// // "radix-tree-keys" : 1,
// // "radix-tree-nodes" : 2,
// // "recorded-first-entry-id" : "1719877599564-1",
// // }
//
// [valkey.io]: https://valkey.io/commands/xinfo-stream/
func (client *baseClient) XInfoStream(key string) (map[string]any, error) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
result, err := client.executeCommand(C.XInfoStream, []string{key})
if err != nil {
return nil, err
}
return handleStringToAnyMapResponse(result)
}

// Returns detailed information about the stream stored at `key`.
//
// See [valkey.io] for details.
//
// Parameters:
//
// key - The key of the stream.
// options - Stream info options.
//
// Return value:
//
// A detailed stream information for the given `key`. See the example for a sample response.
//
// Example:
//
// options := options.NewXInfoStreamOptionsOptions().SetCount(5)
// infoFull, err := client.XInfoStreamWithOptions(key, options)
// infoFull:
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
// // map[string]any {
// // "entries" : []any{
// // "1719877599564-0", []any{"some_field", "some_value", ...},
// // ...
// // },
// // "entries-added" : 2,
// // "groups" : []any{
// // map[string]any {
// // "consumers" : []any{
// // map[string]any {
// // "active-time" : 1737592821596,
// // "name" : "consumer1",
// // "pel-count" : 1,
// // "pending" : []any{
// // []any{ "1719877599564-0", 1737592821596, 1 },
// // ...
// // },
// // "seen-time" : 1737592821596,
// // },
// // },
// // "entries-read" : 1,
// // "lag" : 1,
// // "last-delivered-id" : "1719877599564-0",
// // "name" : "group1"
// // "pel-count" : 1,
// // "pending" : []any{
// // []any{ "1719877599564-0", "consumer1", 1737592821596, 1 },
// // ...
// // },
// // },
// // },
// // "last-generated-id" : "1719877599564-1",
// // "length" : 2,
// // "max-deleted-entry-id" : "0-0",
// // "radix-tree-keys" : 1,
// // "radix-tree-nodes" : 2,
// // "recorded-first-entry-id" : "1719877599564-1",
// // }
//
// // get info for the first consumer of the first group
// consumer := infoFull["groups"].([]any)[0].(map[string]any)["consumers"].([]any)[0]
//
// [valkey.io]: https://valkey.io/commands/xinfo-stream/
func (client *baseClient) XInfoStreamWithOptions(key string, options *options.XInfoStreamOptions) (map[string]any, error) {
args := []string{key, "FULL"}
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
if options != nil {
optionArgs, err := options.ToArgs()
if err != nil {
return nil, err
}
args = append(args, optionArgs...)
}
result, err := client.executeCommand(C.XInfoStream, args)
if err != nil {
return nil, err
}
return handleStringToAnyMapResponse(result)
}
27 changes: 27 additions & 0 deletions go/api/options/stream_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,30 @@ func (sco *StreamClaimOptions) ToArgs() ([]string, error) {

return optionArgs, nil
}

// Optional arguments for `XInfoStream` in [StreamCommands]
type XInfoStreamOptions struct {
count int64
}

// Create new empty `XInfoStreamOptions`
func NewXInfoStreamOptionsOptions() *XInfoStreamOptions {
return &XInfoStreamOptions{-1}
}

// Request verbose information limiting the returned PEL entries.
// If `0` is specified, returns verbose information with no limit.
func (xiso *XInfoStreamOptions) SetCount(count int64) *XInfoStreamOptions {
xiso.count = count
return xiso
}

func (xiso *XInfoStreamOptions) ToArgs() ([]string, error) {
var args []string

if xiso.count > -1 {
args = append(args, "COUNT", utils.IntToString(xiso.count))
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
}

return args, nil
}
15 changes: 15 additions & 0 deletions go/api/response_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,18 @@ func handleXPendingDetailResponse(response *C.struct_CommandResponse) ([]XPendin

return pendingDetails, nil
}

func handleStringToAnyMapResponse(response *C.struct_CommandResponse) (map[string]any, error) {
defer C.free_command_response(response)

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

result, err := parseMap(response)
if err != nil {
return nil, err
}
return result.(map[string]any), nil
}
4 changes: 4 additions & 0 deletions go/api/stream_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,8 @@ type StreamCommands interface {
ids []string,
options *options.StreamClaimOptions,
) ([]string, error)

XInfoStream(key string) (map[string]any, error)
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved

XInfoStreamWithOptions(key string, options *options.XInfoStreamOptions) (map[string]any, error)
}
51 changes: 51 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6711,6 +6711,57 @@ func (suite *GlideTestSuite) TestXGroupStreamCommands() {
})
}

func (suite *GlideTestSuite) TestXInfoStream() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.NewString()
group := uuid.NewString()
consumer := uuid.NewString()

xadd, err := client.XAddWithOptions(key, [][]string{{"a", "b"}, {"c", "d"}}, options.NewXAddOptions().SetId("1-0"))
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "1-0", xadd.Value())

suite.verifyOK(client.XGroupCreate(key, group, "0-0"))

_, err = client.XReadGroup(group, consumer, map[string]string{key: ">"})
assert.NoError(suite.T(), err)

infoSmall, err := client.XInfoStream(key)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), int64(1), infoSmall["length"])
assert.Equal(suite.T(), int64(1), infoSmall["groups"])
expectedEntry := []any{"1-0", []any{"a", "b", "c", "d"}}
assert.Equal(suite.T(), expectedEntry, infoSmall["first-entry"])
assert.Equal(suite.T(), expectedEntry, infoSmall["last-entry"])

xadd, err = client.XAddWithOptions(key, [][]string{{"e", "f"}}, options.NewXAddOptions().SetId("1-1"))
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "1-1", xadd.Value())

infoFull, err := client.XInfoStreamWithOptions(key, options.NewXInfoStreamOptionsOptions().SetCount(1))
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), int64(2), infoFull["length"])

if suite.serverVersion >= "7.0.0" {
assert.Equal(suite.T(), "1-0", infoFull["recorded-first-entry-id"])
} else {
assert.NotContains(suite.T(), infoFull, "recorded-first-entry-id")
assert.NotContains(suite.T(), infoFull, "max-deleted-entry-id")
assert.NotContains(suite.T(), infoFull, "entries-added")
assert.NotContains(suite.T(), infoFull["groups"].([]any)[0], "entries-read")
assert.NotContains(suite.T(), infoFull["groups"].([]any)[0], "lag")
}
// first consumer of first group
cns := infoFull["groups"].([]any)[0].(map[string]any)["consumers"].([]any)[0]
assert.Contains(suite.T(), cns, "seen-time")
if suite.serverVersion >= "7.2.0" {
assert.Contains(suite.T(), cns, "active-time")
} else {
assert.NotContains(suite.T(), cns, "active-time")
}
})
}

func (suite *GlideTestSuite) TestSetBit_SetSingleBit() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
Expand Down
Loading