add xinfo groups command (#1166)

Add xinfo groups command
This commit is contained in:
saito 2019-10-08 17:48:24 +08:00 committed by Vladimir Mihailenco
parent 4afa84c2c4
commit d5b2eccbe2
2 changed files with 97 additions and 0 deletions

View File

@ -1289,6 +1289,96 @@ func (cmd *XPendingExtCmd) readReply(rd *proto.Reader) error {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type XInfoGroupsCmd struct {
baseCmd
val []XGroup
}
type XGroup struct {
Name string
Consumers int64
Pending int64
LastDeliveredID string
}
var _ Cmder = (*XInfoGroupsCmd)(nil)
func NewXInfoGroupsCmd(stream string) *XInfoGroupsCmd {
return &XInfoGroupsCmd{
baseCmd: baseCmd{args: []interface{}{"xinfo", "groups", stream}},
}
}
func (cmd *XInfoGroupsCmd) Val() []XGroup {
return cmd.val
}
func (cmd *XInfoGroupsCmd) Result() ([]XGroup, error) {
return cmd.val, cmd.err
}
func (cmd *XInfoGroupsCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *XInfoGroupsCmd) readReply(rd *proto.Reader) error {
_, cmd.err = rd.ReadArrayReply(
func(rd *proto.Reader, n int64) (interface{}, error) {
for i := int64(0); i < n; i++ {
v, err := rd.ReadReply(xGroupInfoParser)
if err != nil {
return nil, err
}
cmd.val = append(cmd.val, v.(XGroup))
}
return nil, nil
})
return nil
}
func xGroupInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
if n != 8 {
return nil, fmt.Errorf("redis: got %d elements in XINFO GROUPS reply,"+
"wanted 8", n)
}
var (
err error
grp XGroup
key string
val string
)
for i := 0; i < 4; i++ {
key, err = rd.ReadString()
if err != nil {
return nil, err
}
val, err = rd.ReadString()
if err != nil {
return nil, err
}
switch key {
case "name":
grp.Name = val
case "consumers":
grp.Consumers, err = strconv.ParseInt(val, 0, 64)
case "pending":
grp.Pending, err = strconv.ParseInt(val, 0, 64)
case "last-delivered-id":
grp.LastDeliveredID = val
default:
return nil, fmt.Errorf("redis: unexpected content %s "+
"in XINFO GROUPS reply", key)
}
if err != nil {
return nil, err
}
}
return grp, err
}
//------------------------------------------------------------------------------
type ZSliceCmd struct { type ZSliceCmd struct {
baseCmd baseCmd

View File

@ -186,6 +186,7 @@ type Cmdable interface {
XClaimJustID(a *XClaimArgs) *StringSliceCmd XClaimJustID(a *XClaimArgs) *StringSliceCmd
XTrim(key string, maxLen int64) *IntCmd XTrim(key string, maxLen int64) *IntCmd
XTrimApprox(key string, maxLen int64) *IntCmd XTrimApprox(key string, maxLen int64) *IntCmd
XInfoGroups(key string) *XInfoGroupsCmd
BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd
BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd
ZAdd(key string, members ...*Z) *IntCmd ZAdd(key string, members ...*Z) *IntCmd
@ -1571,6 +1572,12 @@ func (c cmdable) XTrimApprox(key string, maxLen int64) *IntCmd {
return cmd return cmd
} }
func (c cmdable) XInfoGroups(key string) *XInfoGroupsCmd {
cmd := NewXInfoGroupsCmd(key)
_ = c(cmd)
return cmd
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Z represents sorted set member. // Z represents sorted set member.