fix: firstKeyPos cmdSlot (#1502)

* fix(xread):cmdSlot

Co-authored-by: zhangxinjian <zhangxinjian@crop.netease.com>
This commit is contained in:
jamsonzan 2020-09-23 15:22:11 +08:00 committed by GitHub
parent e6caec210d
commit c89b69131d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -18,6 +18,8 @@ type Cmder interface {
Args() []interface{} Args() []interface{}
String() string String() string
stringArg(int) string stringArg(int) string
firstKeyPos() int
addKeyPos(int)
readTimeout() *time.Duration readTimeout() *time.Duration
readReply(rd *proto.Reader) error readReply(rd *proto.Reader) error
@ -73,6 +75,9 @@ func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
} }
} }
if pos := cmd.firstKeyPos(); pos != 0 {
return pos
}
if info == nil { if info == nil {
return 0 return 0
} }
@ -106,6 +111,7 @@ type baseCmd struct {
ctx context.Context ctx context.Context
args []interface{} args []interface{}
err error err error
keyPos int
_readTimeout *time.Duration _readTimeout *time.Duration
} }
@ -147,6 +153,14 @@ func (cmd *baseCmd) stringArg(pos int) string {
return s return s
} }
func (cmd *baseCmd) firstKeyPos() int {
return cmd.keyPos
}
func (cmd *baseCmd) addKeyPos(offset int) {
cmd.keyPos += offset
}
func (cmd *baseCmd) SetErr(e error) { func (cmd *baseCmd) SetErr(e error) {
cmd.err = e cmd.err = e
} }

View File

@ -1504,16 +1504,20 @@ type XReadArgs struct {
func (c cmdable) XRead(ctx context.Context, a *XReadArgs) *XStreamSliceCmd { func (c cmdable) XRead(ctx context.Context, a *XReadArgs) *XStreamSliceCmd {
args := make([]interface{}, 0, 5+len(a.Streams)) args := make([]interface{}, 0, 5+len(a.Streams))
args = append(args, "xread") args = append(args, "xread")
offset := 1
if a.Count > 0 { if a.Count > 0 {
args = append(args, "count") args = append(args, "count")
args = append(args, a.Count) args = append(args, a.Count)
offset += 2
} }
if a.Block >= 0 { if a.Block >= 0 {
args = append(args, "block") args = append(args, "block")
args = append(args, int64(a.Block/time.Millisecond)) args = append(args, int64(a.Block/time.Millisecond))
offset += 2
} }
args = append(args, "streams") args = append(args, "streams")
offset += 1
for _, s := range a.Streams { for _, s := range a.Streams {
args = append(args, s) args = append(args, s)
} }
@ -1522,6 +1526,7 @@ func (c cmdable) XRead(ctx context.Context, a *XReadArgs) *XStreamSliceCmd {
if a.Block >= 0 { if a.Block >= 0 {
cmd.setReadTimeout(a.Block) cmd.setReadTimeout(a.Block)
} }
cmd.addKeyPos(offset)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
} }
@ -1575,16 +1580,22 @@ type XReadGroupArgs struct {
func (c cmdable) XReadGroup(ctx context.Context, a *XReadGroupArgs) *XStreamSliceCmd { func (c cmdable) XReadGroup(ctx context.Context, a *XReadGroupArgs) *XStreamSliceCmd {
args := make([]interface{}, 0, 8+len(a.Streams)) args := make([]interface{}, 0, 8+len(a.Streams))
args = append(args, "xreadgroup", "group", a.Group, a.Consumer) args = append(args, "xreadgroup", "group", a.Group, a.Consumer)
offset := 1
if a.Count > 0 { if a.Count > 0 {
args = append(args, "count", a.Count) args = append(args, "count", a.Count)
offset += 2
} }
if a.Block >= 0 { if a.Block >= 0 {
args = append(args, "block", int64(a.Block/time.Millisecond)) args = append(args, "block", int64(a.Block/time.Millisecond))
offset += 2
} }
if a.NoAck { if a.NoAck {
args = append(args, "noack") args = append(args, "noack")
offset += 1
} }
args = append(args, "streams") args = append(args, "streams")
offset += 1
for _, s := range a.Streams { for _, s := range a.Streams {
args = append(args, s) args = append(args, s)
} }
@ -1593,6 +1604,7 @@ func (c cmdable) XReadGroup(ctx context.Context, a *XReadGroupArgs) *XStreamSlic
if a.Block >= 0 { if a.Block >= 0 {
cmd.setReadTimeout(a.Block) cmd.setReadTimeout(a.Block)
} }
cmd.addKeyPos(offset)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
} }
@ -1871,6 +1883,7 @@ func (c cmdable) ZInterStore(ctx context.Context, destination string, store *ZSt
args = append(args, "aggregate", store.Aggregate) args = append(args, "aggregate", store.Aggregate)
} }
cmd := NewIntCmd(ctx, args...) cmd := NewIntCmd(ctx, args...)
cmd.addKeyPos(3)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
} }
@ -2106,6 +2119,7 @@ func (c cmdable) ZUnionStore(ctx context.Context, dest string, store *ZStore) *I
args = append(args, "aggregate", store.Aggregate) args = append(args, "aggregate", store.Aggregate)
} }
cmd := NewIntCmd(ctx, args...) cmd := NewIntCmd(ctx, args...)
cmd.addKeyPos(3)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
} }