From a32502b1d9e34364e3139569d38b77d8192738c1 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Wed, 23 Sep 2020 10:29:13 +0300 Subject: [PATCH] Cleanup --- command.go | 31 ++++++++++++++++--------------- commands.go | 27 ++++++++++++++------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/command.go b/command.go index f21e2db..7f49b78 100644 --- a/command.go +++ b/command.go @@ -18,8 +18,8 @@ type Cmder interface { Args() []interface{} String() string stringArg(int) string - firstKeyPos() int - addKeyPos(int) + firstKeyPos() int8 + setFirstKeyPos(int8) readTimeout() *time.Duration readReply(rd *proto.Reader) error @@ -59,6 +59,10 @@ func writeCmd(wr *proto.Writer, cmd Cmder) error { } func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int { + if pos := cmd.firstKeyPos(); pos != 0 { + return int(pos) + } + switch cmd.Name() { case "eval", "evalsha": if cmd.stringArg(2) != "0" { @@ -75,13 +79,10 @@ func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int { } } - if pos := cmd.firstKeyPos(); pos != 0 { - return pos + if info != nil { + return int(info.FirstKeyPos) } - if info == nil { - return 0 - } - return int(info.FirstKeyPos) + return 0 } func cmdString(cmd Cmder, val interface{}) string { @@ -108,10 +109,10 @@ func cmdString(cmd Cmder, val interface{}) string { //------------------------------------------------------------------------------ type baseCmd struct { - ctx context.Context - args []interface{} - err error - keyPos int + ctx context.Context + args []interface{} + err error + keyPos int8 _readTimeout *time.Duration } @@ -153,12 +154,12 @@ func (cmd *baseCmd) stringArg(pos int) string { return s } -func (cmd *baseCmd) firstKeyPos() int { +func (cmd *baseCmd) firstKeyPos() int8 { return cmd.keyPos } -func (cmd *baseCmd) addKeyPos(offset int) { - cmd.keyPos += offset +func (cmd *baseCmd) setFirstKeyPos(keyPos int8) { + cmd.keyPos = keyPos } func (cmd *baseCmd) SetErr(e error) { diff --git a/commands.go b/commands.go index e70eac4..a6c9eee 100644 --- a/commands.go +++ b/commands.go @@ -1505,19 +1505,19 @@ func (c cmdable) XRead(ctx context.Context, a *XReadArgs) *XStreamSliceCmd { args := make([]interface{}, 0, 5+len(a.Streams)) args = append(args, "xread") - offset := 1 + keyPos := int8(1) if a.Count > 0 { args = append(args, "count") args = append(args, a.Count) - offset += 2 + keyPos += 2 } if a.Block >= 0 { args = append(args, "block") args = append(args, int64(a.Block/time.Millisecond)) - offset += 2 + keyPos += 2 } args = append(args, "streams") - offset += 1 + keyPos += 1 for _, s := range a.Streams { args = append(args, s) } @@ -1526,7 +1526,7 @@ func (c cmdable) XRead(ctx context.Context, a *XReadArgs) *XStreamSliceCmd { if a.Block >= 0 { cmd.setReadTimeout(a.Block) } - cmd.addKeyPos(offset) + cmd.setFirstKeyPos(keyPos) _ = c(ctx, cmd) return cmd } @@ -1581,21 +1581,21 @@ func (c cmdable) XReadGroup(ctx context.Context, a *XReadGroupArgs) *XStreamSlic args := make([]interface{}, 0, 8+len(a.Streams)) args = append(args, "xreadgroup", "group", a.Group, a.Consumer) - offset := 1 + keyPos := int8(1) if a.Count > 0 { args = append(args, "count", a.Count) - offset += 2 + keyPos += 2 } if a.Block >= 0 { args = append(args, "block", int64(a.Block/time.Millisecond)) - offset += 2 + keyPos += 2 } if a.NoAck { args = append(args, "noack") - offset += 1 + keyPos += 1 } args = append(args, "streams") - offset += 1 + keyPos += 1 for _, s := range a.Streams { args = append(args, s) } @@ -1604,7 +1604,7 @@ func (c cmdable) XReadGroup(ctx context.Context, a *XReadGroupArgs) *XStreamSlic if a.Block >= 0 { cmd.setReadTimeout(a.Block) } - cmd.addKeyPos(offset) + cmd.setFirstKeyPos(keyPos) _ = c(ctx, cmd) return cmd } @@ -1883,7 +1883,7 @@ func (c cmdable) ZInterStore(ctx context.Context, destination string, store *ZSt args = append(args, "aggregate", store.Aggregate) } cmd := NewIntCmd(ctx, args...) - cmd.addKeyPos(3) + cmd.setFirstKeyPos(3) _ = c(ctx, cmd) return cmd } @@ -2118,8 +2118,9 @@ func (c cmdable) ZUnionStore(ctx context.Context, dest string, store *ZStore) *I if store.Aggregate != "" { args = append(args, "aggregate", store.Aggregate) } + cmd := NewIntCmd(ctx, args...) - cmd.addKeyPos(3) + cmd.setFirstKeyPos(3) _ = c(ctx, cmd) return cmd }