Support SCAN and friends.

This commit is contained in:
Vladimir Mihailenco 2014-01-09 10:17:38 +02:00
parent 4252b31cd9
commit 62e78627c1
4 changed files with 416 additions and 129 deletions

View File

@ -7,16 +7,32 @@ import (
"time" "time"
) )
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 = (*StringFloatMapCmd)(nil)
_ Cmder = (*ScanCmd)(nil)
)
type Cmder interface { type Cmder interface {
args() []string args() []string
parseReply(reader) error parseReply(reader) error
setErr(error) setErr(error)
setVal(interface{})
writeTimeout() *time.Duration writeTimeout() *time.Duration
readTimeout() *time.Duration readTimeout() *time.Duration
Err() error Err() error
String() string
} }
func setCmdsErr(cmds []Cmder, e error) { func setCmdsErr(cmds []Cmder, e error) {
@ -25,12 +41,23 @@ func setCmdsErr(cmds []Cmder, e error) {
} }
} }
func cmdString(cmd Cmder, val interface{}) string {
s := strings.Join(cmd.args(), " ")
if err := cmd.Err(); err != nil {
return s + ": " + err.Error()
}
if val != nil {
return s + ": " + fmt.Sprint(val)
}
return s
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type baseCmd struct { type baseCmd struct {
_args []string _args []string
val interface{}
err error err error
_writeTimeout, _readTimeout *time.Duration _writeTimeout, _readTimeout *time.Duration
@ -42,23 +69,10 @@ func newBaseCmd(args ...string) *baseCmd {
} }
} }
func (cmd *baseCmd) String() string {
args := strings.Join(cmd._args, " ")
if cmd.err != nil {
return args + ": " + cmd.err.Error()
} else if cmd.val != nil {
return args + ": " + fmt.Sprint(cmd.val)
}
return args
}
func (cmd *baseCmd) Err() error { func (cmd *baseCmd) Err() error {
if cmd.err != nil { if cmd.err != nil {
return cmd.err return cmd.err
} }
if cmd.val == nil {
return errValNotSet
}
return nil return nil
} }
@ -66,25 +80,6 @@ func (cmd *baseCmd) args() []string {
return cmd._args return cmd._args
} }
func (cmd *baseCmd) setErr(err error) {
if err == nil {
panic("non-nil value expected")
}
cmd.err = err
}
func (cmd *baseCmd) setVal(val interface{}) {
if val == nil {
panic("non-nil value expected")
}
cmd.val = val
}
func (cmd *baseCmd) parseReply(rd reader) error {
cmd.val, cmd.err = parseReply(rd, parseSlice)
return cmd.err
}
func (cmd *baseCmd) readTimeout() *time.Duration { func (cmd *baseCmd) readTimeout() *time.Duration {
return cmd._readTimeout return cmd._readTimeout
} }
@ -101,10 +96,16 @@ func (cmd *baseCmd) setWriteTimeout(d time.Duration) {
cmd._writeTimeout = &d cmd._writeTimeout = &d
} }
func (cmd *baseCmd) setErr(e error) {
cmd.err = e
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type Cmd struct { type Cmd struct {
*baseCmd *baseCmd
val interface{}
} }
func NewCmd(args ...string) *Cmd { func NewCmd(args ...string) *Cmd {
@ -117,10 +118,61 @@ func (cmd *Cmd) Val() interface{} {
return cmd.val return cmd.val
} }
func (cmd *Cmd) Result() (interface{}, error) {
return cmd.val, cmd.err
}
func (cmd *Cmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *Cmd) parseReply(rd reader) error {
cmd.val, cmd.err = parseReply(rd, parseSlice)
return cmd.err
}
//------------------------------------------------------------------------------
type SliceCmd struct {
*baseCmd
val []interface{}
}
func NewSliceCmd(args ...string) *SliceCmd {
return &SliceCmd{
baseCmd: newBaseCmd(args...),
}
}
func (cmd *SliceCmd) Val() []interface{} {
return cmd.val
}
func (cmd *SliceCmd) Result() ([]interface{}, error) {
return cmd.val, cmd.err
}
func (cmd *SliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *SliceCmd) parseReply(rd reader) error {
v, err := parseReply(rd, parseSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]interface{})
return nil
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type StatusCmd struct { type StatusCmd struct {
*baseCmd *baseCmd
val string
} }
func NewStatusCmd(args ...string) *StatusCmd { func NewStatusCmd(args ...string) *StatusCmd {
@ -130,16 +182,33 @@ func NewStatusCmd(args ...string) *StatusCmd {
} }
func (cmd *StatusCmd) Val() string { func (cmd *StatusCmd) Val() string {
if cmd.val == nil { return cmd.val
return ""
} }
return cmd.val.(string)
func (cmd *StatusCmd) Result() (string, error) {
return cmd.val, cmd.err
}
func (cmd *StatusCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StatusCmd) parseReply(rd reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(string)
return nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type IntCmd struct { type IntCmd struct {
*baseCmd *baseCmd
val int64
} }
func NewIntCmd(args ...string) *IntCmd { func NewIntCmd(args ...string) *IntCmd {
@ -149,16 +218,33 @@ func NewIntCmd(args ...string) *IntCmd {
} }
func (cmd *IntCmd) Val() int64 { func (cmd *IntCmd) Val() int64 {
if cmd.val == nil { return cmd.val
return 0
} }
return cmd.val.(int64)
func (cmd *IntCmd) Result() (int64, error) {
return cmd.val, cmd.err
}
func (cmd *IntCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *IntCmd) parseReply(rd reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(int64)
return nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type DurationCmd struct { type DurationCmd struct {
*baseCmd *baseCmd
val time.Duration
precision time.Duration precision time.Duration
} }
@ -169,23 +255,34 @@ func NewDurationCmd(precision time.Duration, args ...string) *DurationCmd {
} }
} }
func (cmd *DurationCmd) parseReply(rd reader) error { func (cmd *DurationCmd) Val() time.Duration {
cmd.val, cmd.err = parseReply(rd, nil) return cmd.val
cmd.val = time.Duration(cmd.val.(int64)) * cmd.precision
return cmd.err
} }
func (cmd *DurationCmd) Val() time.Duration { func (cmd *DurationCmd) Result() (time.Duration, error) {
if cmd.val == nil { return cmd.val, cmd.err
return 0
} }
return cmd.val.(time.Duration)
func (cmd *DurationCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *DurationCmd) parseReply(rd reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val = time.Duration(v.(int64)) * cmd.precision
return nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type BoolCmd struct { type BoolCmd struct {
*baseCmd *baseCmd
val bool
} }
func NewBoolCmd(args ...string) *BoolCmd { func NewBoolCmd(args ...string) *BoolCmd {
@ -194,23 +291,34 @@ func NewBoolCmd(args ...string) *BoolCmd {
} }
} }
func (cmd *BoolCmd) parseReply(rd reader) error { func (cmd *BoolCmd) Val() bool {
cmd.val, cmd.err = parseReply(rd, nil) return cmd.val
cmd.val = cmd.val.(int64) == 1
return cmd.err
} }
func (cmd *BoolCmd) Val() bool { func (cmd *BoolCmd) Result() (bool, error) {
if cmd.val == nil { return cmd.val, cmd.err
return false
} }
return cmd.val.(bool)
func (cmd *BoolCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *BoolCmd) parseReply(rd reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(int64) == 1
return nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type StringCmd struct { type StringCmd struct {
*baseCmd *baseCmd
val string
} }
func NewStringCmd(args ...string) *StringCmd { func NewStringCmd(args ...string) *StringCmd {
@ -220,16 +328,33 @@ func NewStringCmd(args ...string) *StringCmd {
} }
func (cmd *StringCmd) Val() string { func (cmd *StringCmd) Val() string {
if cmd.val == nil { return cmd.val
return ""
} }
return cmd.val.(string)
func (cmd *StringCmd) Result() (string, error) {
return cmd.val, cmd.err
}
func (cmd *StringCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StringCmd) parseReply(rd reader) error {
v, err := parseReply(rd, nil)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(string)
return nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type FloatCmd struct { type FloatCmd struct {
*baseCmd *baseCmd
val float64
} }
func NewFloatCmd(args ...string) *FloatCmd { func NewFloatCmd(args ...string) *FloatCmd {
@ -238,45 +363,30 @@ func NewFloatCmd(args ...string) *FloatCmd {
} }
} }
func (cmd *FloatCmd) parseReply(rd reader) error {
cmd.val, cmd.err = parseReply(rd, nil)
if cmd.err != nil {
return cmd.err
}
cmd.val, cmd.err = strconv.ParseFloat(cmd.val.(string), 64)
return cmd.err
}
func (cmd *FloatCmd) Val() float64 { func (cmd *FloatCmd) Val() float64 {
if cmd.val == nil { return cmd.val
return 0
}
return cmd.val.(float64)
} }
//------------------------------------------------------------------------------ func (cmd *FloatCmd) String() string {
return cmdString(cmd, cmd.val)
type SliceCmd struct {
*baseCmd
} }
func NewSliceCmd(args ...string) *SliceCmd { func (cmd *FloatCmd) parseReply(rd reader) error {
return &SliceCmd{ v, err := parseReply(rd, nil)
baseCmd: newBaseCmd(args...), if err != nil {
cmd.err = err
return err
} }
} cmd.val, cmd.err = strconv.ParseFloat(v.(string), 64)
return cmd.err
func (cmd *SliceCmd) Val() []interface{} {
if cmd.val == nil {
return nil
}
return cmd.val.([]interface{})
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type StringSliceCmd struct { type StringSliceCmd struct {
*baseCmd *baseCmd
val []string
} }
func NewStringSliceCmd(args ...string) *StringSliceCmd { func NewStringSliceCmd(args ...string) *StringSliceCmd {
@ -285,22 +395,30 @@ func NewStringSliceCmd(args ...string) *StringSliceCmd {
} }
} }
func (cmd *StringSliceCmd) parseReply(rd reader) error { func (cmd *StringSliceCmd) Val() []string {
cmd.val, cmd.err = parseReply(rd, parseStringSlice) return cmd.val
return cmd.err
} }
func (cmd *StringSliceCmd) Val() []string { func (cmd *StringSliceCmd) String() string {
if cmd.val == nil { return cmdString(cmd, cmd.val)
return nil
} }
return cmd.val.([]string)
func (cmd *StringSliceCmd) parseReply(rd reader) error {
v, err := parseReply(rd, parseStringSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]string)
return nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type BoolSliceCmd struct { type BoolSliceCmd struct {
*baseCmd *baseCmd
val []bool
} }
func NewBoolSliceCmd(args ...string) *BoolSliceCmd { func NewBoolSliceCmd(args ...string) *BoolSliceCmd {
@ -309,22 +427,34 @@ func NewBoolSliceCmd(args ...string) *BoolSliceCmd {
} }
} }
func (cmd *BoolSliceCmd) parseReply(rd reader) error { func (cmd *BoolSliceCmd) Val() []bool {
cmd.val, cmd.err = parseReply(rd, parseBoolSlice) return cmd.val
return cmd.err
} }
func (cmd *BoolSliceCmd) Val() []bool { func (cmd *BoolSliceCmd) Result() ([]bool, error) {
if cmd.val == nil { return cmd.val, cmd.err
return nil
} }
return cmd.val.([]bool)
func (cmd *BoolSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *BoolSliceCmd) parseReply(rd reader) error {
v, err := parseReply(rd, parseBoolSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]bool)
return nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type StringStringMapCmd struct { type StringStringMapCmd struct {
*baseCmd *baseCmd
val map[string]string
} }
func NewStringStringMapCmd(args ...string) *StringStringMapCmd { func NewStringStringMapCmd(args ...string) *StringStringMapCmd {
@ -333,22 +463,34 @@ func NewStringStringMapCmd(args ...string) *StringStringMapCmd {
} }
} }
func (cmd *StringStringMapCmd) parseReply(rd reader) error { func (cmd *StringStringMapCmd) Val() map[string]string {
cmd.val, cmd.err = parseReply(rd, parseStringStringMap) return cmd.val
return cmd.err
} }
func (cmd *StringStringMapCmd) Val() map[string]string { func (cmd *StringStringMapCmd) Result() (map[string]string, error) {
if cmd.val == nil { return cmd.val, cmd.err
return nil
} }
return cmd.val.(map[string]string)
func (cmd *StringStringMapCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StringStringMapCmd) parseReply(rd reader) error {
v, err := parseReply(rd, parseStringStringMap)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(map[string]string)
return nil
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type StringFloatMapCmd struct { type StringFloatMapCmd struct {
*baseCmd *baseCmd
val map[string]float64
} }
func NewStringFloatMapCmd(args ...string) *StringFloatMapCmd { func NewStringFloatMapCmd(args ...string) *StringFloatMapCmd {
@ -357,14 +499,72 @@ func NewStringFloatMapCmd(args ...string) *StringFloatMapCmd {
} }
} }
func (cmd *StringFloatMapCmd) Val() map[string]float64 {
return cmd.val
}
func (cmd *StringFloatMapCmd) Result() (map[string]float64, error) {
return cmd.val, cmd.err
}
func (cmd *StringFloatMapCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *StringFloatMapCmd) parseReply(rd reader) error { func (cmd *StringFloatMapCmd) parseReply(rd reader) error {
cmd.val, cmd.err = parseReply(rd, parseStringFloatMap) v, err := parseReply(rd, parseStringFloatMap)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(map[string]float64)
return nil
}
//------------------------------------------------------------------------------
type ScanCmd struct {
*baseCmd
cursor int64
keys []string
}
func NewScanCmd(args ...string) *ScanCmd {
return &ScanCmd{
baseCmd: newBaseCmd(args...),
}
}
func (cmd *ScanCmd) Val() (int64, []string) {
return cmd.cursor, cmd.keys
}
func (cmd *ScanCmd) Result() (int64, []string, error) {
return cmd.cursor, cmd.keys, cmd.err
}
func (cmd *ScanCmd) String() string {
return cmdString(cmd, cmd.keys)
}
func (cmd *ScanCmd) parseReply(rd reader) error {
vi, err := parseReply(rd, parseSlice)
if err != nil {
cmd.err = err
return cmd.err
}
v := vi.([]interface{})
cmd.cursor, cmd.err = strconv.ParseInt(v[0].(string), 10, 64)
if cmd.err != nil {
return cmd.err return cmd.err
} }
func (cmd *StringFloatMapCmd) Val() map[string]float64 { keys := v[1].([]interface{})
if cmd.val == nil { for _, keyi := range keys {
cmd.keys = append(cmd.keys, keyi.(string))
}
return nil return nil
} }
return cmd.val.(map[string]float64)
}

View File

@ -229,18 +229,57 @@ func (c *Client) Type(key string) *StatusCmd {
return req return req
} }
// func (c *Client) Scan(cursor, match string, count int64) *ScanCmd { func (c *Client) Scan(cursor int64, match string, count int64) *ScanCmd {
// args := []string{"SCAN", cursor} args := []string{"SCAN", strconv.FormatInt(cursor, 10)}
// if match != "" { if match != "" {
// args = append(args, "MATCH", match) args = append(args, "MATCH", match)
// } }
// if count > 0 { if count > 0 {
// args = append(args, "COUNT", strconv.FormatInt(count, 10)) args = append(args, "COUNT", strconv.FormatInt(count, 10))
// } }
// req := NewScanCmd(args...) req := NewScanCmd(args...)
// c.Process(req) c.Process(req)
// return req return req
// } }
func (c *Client) SScan(key string, cursor int64, match string, count int64) *ScanCmd {
args := []string{"SSCAN", key, strconv.FormatInt(cursor, 10)}
if match != "" {
args = append(args, "MATCH", match)
}
if count > 0 {
args = append(args, "COUNT", strconv.FormatInt(count, 10))
}
req := NewScanCmd(args...)
c.Process(req)
return req
}
func (c *Client) HScan(key string, cursor int64, match string, count int64) *ScanCmd {
args := []string{"HSCAN", key, strconv.FormatInt(cursor, 10)}
if match != "" {
args = append(args, "MATCH", match)
}
if count > 0 {
args = append(args, "COUNT", strconv.FormatInt(count, 10))
}
req := NewScanCmd(args...)
c.Process(req)
return req
}
func (c *Client) ZScan(key string, cursor int64, match string, count int64) *ScanCmd {
args := []string{"ZSCAN", key, strconv.FormatInt(cursor, 10)}
if match != "" {
args = append(args, "MATCH", match)
}
if count > 0 {
args = append(args, "COUNT", strconv.FormatInt(count, 10))
}
req := NewScanCmd(args...)
c.Process(req)
return req
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -18,7 +18,6 @@ var TxFailedErr = errors.New("redis: transaction failed")
var ( var (
errReaderTooSmall = errors.New("redis: reader is too small") errReaderTooSmall = errors.New("redis: reader is too small")
errValNotSet = errors.New("redis: value is not set")
errInvalidReplyType = errors.New("redis: invalid reply type") errInvalidReplyType = errors.New("redis: invalid reply type")
) )

View File

@ -760,6 +760,54 @@ func (t *RedisTest) TestCmdKeysType(c *C) {
c.Assert(type_.Val(), Equals, "string") c.Assert(type_.Val(), Equals, "string")
} }
func (t *RedisTest) TestCmdScan(c *C) {
for i := 0; i < 1000; i++ {
set := t.client.Set(fmt.Sprintf("key%d", i), "hello")
c.Assert(set.Err(), IsNil)
}
cursor, keys, err := t.client.Scan(0, "", 0).Result()
c.Assert(err, IsNil)
c.Assert(cursor > 0, Equals, true)
c.Assert(len(keys) > 0, Equals, true)
}
func (t *RedisTest) TestCmdSScan(c *C) {
for i := 0; i < 1000; i++ {
sadd := t.client.SAdd("myset", fmt.Sprintf("member%d", i))
c.Assert(sadd.Err(), IsNil)
}
cursor, keys, err := t.client.SScan("myset", 0, "", 0).Result()
c.Assert(err, IsNil)
c.Assert(cursor > 0, Equals, true)
c.Assert(len(keys) > 0, Equals, true)
}
func (t *RedisTest) TestCmdHScan(c *C) {
for i := 0; i < 1000; i++ {
sadd := t.client.HSet("myhash", fmt.Sprintf("key%d", i), "hello")
c.Assert(sadd.Err(), IsNil)
}
cursor, keys, err := t.client.HScan("myhash", 0, "", 0).Result()
c.Assert(err, IsNil)
c.Assert(cursor > 0, Equals, true)
c.Assert(len(keys) > 0, Equals, true)
}
func (t *RedisTest) TestCmdZScan(c *C) {
for i := 0; i < 1000; i++ {
sadd := t.client.ZAdd("myset", redis.Z{float64(i), fmt.Sprintf("member%d", i)})
c.Assert(sadd.Err(), IsNil)
}
cursor, keys, err := t.client.ZScan("myset", 0, "", 0).Result()
c.Assert(err, IsNil)
c.Assert(cursor > 0, Equals, true)
c.Assert(len(keys) > 0, Equals, true)
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
func (t *RedisTest) TestStringsAppend(c *C) { func (t *RedisTest) TestStringsAppend(c *C) {
@ -2508,7 +2556,8 @@ func (t *RedisTest) TestPipelineErrValNotSet(c *C) {
}() }()
get := pipeline.Get("key") get := pipeline.Get("key")
c.Assert(get.Err(), ErrorMatches, "redis: value is not set") c.Assert(get.Err(), IsNil)
c.Assert(get.Val(), Equals, "")
} }
func (t *RedisTest) TestPipelineRunQueuedOnEmptyQueue(c *C) { func (t *RedisTest) TestPipelineRunQueuedOnEmptyQueue(c *C) {