Merge pull request #646 from go-redis/feature/export-cmd-args

Export Cmder.Args
This commit is contained in:
Vladimir Mihailenco 2017-09-26 11:43:33 +03:00 committed by GitHub
commit 34ce349cb7
3 changed files with 50 additions and 30 deletions

View File

@ -535,13 +535,13 @@ func (c *ClusterClient) cmdInfo(name string) *CommandInfo {
func (c *ClusterClient) cmdSlot(cmd Cmder) int { func (c *ClusterClient) cmdSlot(cmd Cmder) int {
cmdInfo := c.cmdInfo(cmd.Name()) cmdInfo := c.cmdInfo(cmd.Name())
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo)) firstKey := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
return hashtag.Slot(firstKey) return hashtag.Slot(firstKey)
} }
func (c *ClusterClient) cmdSlotAndNode(state *clusterState, cmd Cmder) (int, *clusterNode, error) { func (c *ClusterClient) cmdSlotAndNode(state *clusterState, cmd Cmder) (int, *clusterNode, error) {
cmdInfo := c.cmdInfo(cmd.Name()) cmdInfo := c.cmdInfo(cmd.Name())
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo)) firstKey := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
slot := hashtag.Slot(firstKey) slot := hashtag.Slot(firstKey)
if cmdInfo != nil && cmdInfo.ReadOnly && c.opt.ReadOnly { if cmdInfo != nil && cmdInfo.ReadOnly && c.opt.ReadOnly {

View File

@ -12,28 +12,10 @@ import (
"github.com/go-redis/redis/internal/proto" "github.com/go-redis/redis/internal/proto"
) )
var (
_ Cmder = (*Cmd)(nil)
_ Cmder = (*SliceCmd)(nil)
_ Cmder = (*StatusCmd)(nil)
_ Cmder = (*IntCmd)(nil)
_ Cmder = (*DurationCmd)(nil)
_ Cmder = (*BoolCmd)(nil)
_ Cmder = (*StringCmd)(nil)
_ Cmder = (*FloatCmd)(nil)
_ Cmder = (*StringSliceCmd)(nil)
_ Cmder = (*BoolSliceCmd)(nil)
_ Cmder = (*StringStringMapCmd)(nil)
_ Cmder = (*StringIntMapCmd)(nil)
_ Cmder = (*ZSliceCmd)(nil)
_ Cmder = (*ScanCmd)(nil)
_ Cmder = (*ClusterSlotsCmd)(nil)
)
type Cmder interface { type Cmder interface {
args() []interface{}
arg(int) string
Name() string Name() string
Args() []interface{}
stringArg(int) string
readReply(*pool.Conn) error readReply(*pool.Conn) error
setErr(error) setErr(error)
@ -64,7 +46,7 @@ func firstCmdsErr(cmds []Cmder) error {
func writeCmd(cn *pool.Conn, cmds ...Cmder) error { func writeCmd(cn *pool.Conn, cmds ...Cmder) error {
cn.Wb.Reset() cn.Wb.Reset()
for _, cmd := range cmds { for _, cmd := range cmds {
if err := cn.Wb.Append(cmd.args()); err != nil { if err := cn.Wb.Append(cmd.Args()); err != nil {
return err return err
} }
} }
@ -75,7 +57,7 @@ func writeCmd(cn *pool.Conn, cmds ...Cmder) error {
func cmdString(cmd Cmder, val interface{}) string { func cmdString(cmd Cmder, val interface{}) string {
var ss []string var ss []string
for _, arg := range cmd.args() { for _, arg := range cmd.Args() {
ss = append(ss, fmt.Sprint(arg)) ss = append(ss, fmt.Sprint(arg))
} }
s := strings.Join(ss, " ") s := strings.Join(ss, " ")
@ -97,7 +79,7 @@ func cmdString(cmd Cmder, val interface{}) string {
func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int { func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
switch cmd.Name() { switch cmd.Name() {
case "eval", "evalsha": case "eval", "evalsha":
if cmd.arg(2) != "0" { if cmd.stringArg(2) != "0" {
return 3 return 3
} else { } else {
return -1 return -1
@ -120,15 +102,17 @@ type baseCmd struct {
_readTimeout *time.Duration _readTimeout *time.Duration
} }
var _ Cmder = (*Cmd)(nil)
func (cmd *baseCmd) Err() error { func (cmd *baseCmd) Err() error {
return cmd.err return cmd.err
} }
func (cmd *baseCmd) args() []interface{} { func (cmd *baseCmd) Args() []interface{} {
return cmd._args return cmd._args
} }
func (cmd *baseCmd) arg(pos int) string { func (cmd *baseCmd) stringArg(pos int) string {
if pos < 0 || pos >= len(cmd._args) { if pos < 0 || pos >= len(cmd._args) {
return "" return ""
} }
@ -139,7 +123,7 @@ func (cmd *baseCmd) arg(pos int) string {
func (cmd *baseCmd) Name() string { func (cmd *baseCmd) Name() string {
if len(cmd._args) > 0 { if len(cmd._args) > 0 {
// Cmd name must be lower cased. // Cmd name must be lower cased.
s := internal.ToLower(cmd.arg(0)) s := internal.ToLower(cmd.stringArg(0))
cmd._args[0] = s cmd._args[0] = s
return s return s
} }
@ -204,6 +188,8 @@ type SliceCmd struct {
val []interface{} val []interface{}
} }
var _ Cmder = (*SliceCmd)(nil)
func NewSliceCmd(args ...interface{}) *SliceCmd { func NewSliceCmd(args ...interface{}) *SliceCmd {
return &SliceCmd{ return &SliceCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -240,6 +226,8 @@ type StatusCmd struct {
val string val string
} }
var _ Cmder = (*StatusCmd)(nil)
func NewStatusCmd(args ...interface{}) *StatusCmd { func NewStatusCmd(args ...interface{}) *StatusCmd {
return &StatusCmd{ return &StatusCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -271,6 +259,8 @@ type IntCmd struct {
val int64 val int64
} }
var _ Cmder = (*IntCmd)(nil)
func NewIntCmd(args ...interface{}) *IntCmd { func NewIntCmd(args ...interface{}) *IntCmd {
return &IntCmd{ return &IntCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -303,6 +293,8 @@ type DurationCmd struct {
precision time.Duration precision time.Duration
} }
var _ Cmder = (*DurationCmd)(nil)
func NewDurationCmd(precision time.Duration, args ...interface{}) *DurationCmd { func NewDurationCmd(precision time.Duration, args ...interface{}) *DurationCmd {
return &DurationCmd{ return &DurationCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -340,6 +332,8 @@ type TimeCmd struct {
val time.Time val time.Time
} }
var _ Cmder = (*TimeCmd)(nil)
func NewTimeCmd(args ...interface{}) *TimeCmd { func NewTimeCmd(args ...interface{}) *TimeCmd {
return &TimeCmd{ return &TimeCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -376,6 +370,8 @@ type BoolCmd struct {
val bool val bool
} }
var _ Cmder = (*BoolCmd)(nil)
func NewBoolCmd(args ...interface{}) *BoolCmd { func NewBoolCmd(args ...interface{}) *BoolCmd {
return &BoolCmd{ return &BoolCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -431,6 +427,8 @@ type StringCmd struct {
val []byte val []byte
} }
var _ Cmder = (*StringCmd)(nil)
func NewStringCmd(args ...interface{}) *StringCmd { func NewStringCmd(args ...interface{}) *StringCmd {
return &StringCmd{ return &StringCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -494,6 +492,8 @@ type FloatCmd struct {
val float64 val float64
} }
var _ Cmder = (*FloatCmd)(nil)
func NewFloatCmd(args ...interface{}) *FloatCmd { func NewFloatCmd(args ...interface{}) *FloatCmd {
return &FloatCmd{ return &FloatCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -525,6 +525,8 @@ type StringSliceCmd struct {
val []string val []string
} }
var _ Cmder = (*StringSliceCmd)(nil)
func NewStringSliceCmd(args ...interface{}) *StringSliceCmd { func NewStringSliceCmd(args ...interface{}) *StringSliceCmd {
return &StringSliceCmd{ return &StringSliceCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -565,6 +567,8 @@ type BoolSliceCmd struct {
val []bool val []bool
} }
var _ Cmder = (*BoolSliceCmd)(nil)
func NewBoolSliceCmd(args ...interface{}) *BoolSliceCmd { func NewBoolSliceCmd(args ...interface{}) *BoolSliceCmd {
return &BoolSliceCmd{ return &BoolSliceCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -601,6 +605,8 @@ type StringStringMapCmd struct {
val map[string]string val map[string]string
} }
var _ Cmder = (*StringStringMapCmd)(nil)
func NewStringStringMapCmd(args ...interface{}) *StringStringMapCmd { func NewStringStringMapCmd(args ...interface{}) *StringStringMapCmd {
return &StringStringMapCmd{ return &StringStringMapCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -637,6 +643,8 @@ type StringIntMapCmd struct {
val map[string]int64 val map[string]int64
} }
var _ Cmder = (*StringIntMapCmd)(nil)
func NewStringIntMapCmd(args ...interface{}) *StringIntMapCmd { func NewStringIntMapCmd(args ...interface{}) *StringIntMapCmd {
return &StringIntMapCmd{ return &StringIntMapCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -673,6 +681,8 @@ type ZSliceCmd struct {
val []Z val []Z
} }
var _ Cmder = (*ZSliceCmd)(nil)
func NewZSliceCmd(args ...interface{}) *ZSliceCmd { func NewZSliceCmd(args ...interface{}) *ZSliceCmd {
return &ZSliceCmd{ return &ZSliceCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -712,6 +722,8 @@ type ScanCmd struct {
process func(cmd Cmder) error process func(cmd Cmder) error
} }
var _ Cmder = (*ScanCmd)(nil)
func NewScanCmd(process func(cmd Cmder) error, args ...interface{}) *ScanCmd { func NewScanCmd(process func(cmd Cmder) error, args ...interface{}) *ScanCmd {
return &ScanCmd{ return &ScanCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -762,6 +774,8 @@ type ClusterSlotsCmd struct {
val []ClusterSlot val []ClusterSlot
} }
var _ Cmder = (*ClusterSlotsCmd)(nil)
func NewClusterSlotsCmd(args ...interface{}) *ClusterSlotsCmd { func NewClusterSlotsCmd(args ...interface{}) *ClusterSlotsCmd {
return &ClusterSlotsCmd{ return &ClusterSlotsCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -821,6 +835,8 @@ type GeoLocationCmd struct {
locations []GeoLocation locations []GeoLocation
} }
var _ Cmder = (*GeoLocationCmd)(nil)
func NewGeoLocationCmd(q *GeoRadiusQuery, args ...interface{}) *GeoLocationCmd { func NewGeoLocationCmd(q *GeoRadiusQuery, args ...interface{}) *GeoLocationCmd {
args = append(args, q.Radius) args = append(args, q.Radius)
if q.Unit != "" { if q.Unit != "" {
@ -891,6 +907,8 @@ type GeoPosCmd struct {
positions []*GeoPos positions []*GeoPos
} }
var _ Cmder = (*GeoPosCmd)(nil)
func NewGeoPosCmd(args ...interface{}) *GeoPosCmd { func NewGeoPosCmd(args ...interface{}) *GeoPosCmd {
return &GeoPosCmd{ return &GeoPosCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},
@ -937,6 +955,8 @@ type CommandsInfoCmd struct {
val map[string]*CommandInfo val map[string]*CommandInfo
} }
var _ Cmder = (*CommandsInfoCmd)(nil)
func NewCommandsInfoCmd(args ...interface{}) *CommandsInfoCmd { func NewCommandsInfoCmd(args ...interface{}) *CommandsInfoCmd {
return &CommandsInfoCmd{ return &CommandsInfoCmd{
baseCmd: baseCmd{_args: args}, baseCmd: baseCmd{_args: args},

View File

@ -343,7 +343,7 @@ func (c *Ring) shardByName(name string) (*ringShard, error) {
func (c *Ring) cmdShard(cmd Cmder) (*ringShard, error) { func (c *Ring) cmdShard(cmd Cmder) (*ringShard, error) {
cmdInfo := c.cmdInfo(cmd.Name()) cmdInfo := c.cmdInfo(cmd.Name())
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo)) firstKey := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
return c.shardByKey(firstKey) return c.shardByKey(firstKey)
} }
@ -443,7 +443,7 @@ func (c *Ring) pipelineExec(cmds []Cmder) error {
cmdsMap := make(map[string][]Cmder) cmdsMap := make(map[string][]Cmder)
for _, cmd := range cmds { for _, cmd := range cmds {
cmdInfo := c.cmdInfo(cmd.Name()) cmdInfo := c.cmdInfo(cmd.Name())
name := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo)) name := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
if name != "" { if name != "" {
name = c.hash.Get(hashtag.Key(name)) name = c.hash.Get(hashtag.Key(name))
} }