redis/command.go

1961 lines
37 KiB
Go
Raw Normal View History

2013-09-29 12:06:49 +04:00
package redis
import (
"fmt"
2018-08-02 14:48:46 +03:00
"net"
2013-09-29 12:06:49 +04:00
"strconv"
"strings"
"time"
2017-02-18 17:42:34 +03:00
"github.com/go-redis/redis/internal"
"github.com/go-redis/redis/internal/proto"
2013-09-29 12:06:49 +04:00
)
type Cmder interface {
2017-05-09 12:44:36 +03:00
Name() string
2017-09-26 11:29:22 +03:00
Args() []interface{}
stringArg(int) string
2016-06-17 15:09:38 +03:00
2018-08-17 13:56:37 +03:00
readReply(rd *proto.Reader) error
2013-09-29 12:06:49 +04:00
setErr(error)
readTimeout() *time.Duration
Err() error
}
2013-09-29 13:41:04 +04:00
func setCmdsErr(cmds []Cmder, e error) {
for _, cmd := range cmds {
2017-08-31 15:22:47 +03:00
if cmd.Err() == nil {
cmd.setErr(e)
}
}
}
2018-08-12 11:11:01 +03:00
func cmdsFirstErr(cmds []Cmder) error {
2017-08-31 15:22:47 +03:00
for _, cmd := range cmds {
if err := cmd.Err(); err != nil {
return err
}
2013-09-29 13:41:04 +04:00
}
2017-08-31 15:22:47 +03:00
return nil
2013-09-29 13:41:04 +04:00
}
2018-08-17 13:56:37 +03:00
func writeCmd(wr *proto.Writer, cmds ...Cmder) error {
for _, cmd := range cmds {
2018-08-17 13:56:37 +03:00
err := wr.WriteArgs(cmd.Args())
if err != nil {
return err
}
}
2018-08-15 11:53:15 +03:00
return nil
}
2014-01-09 12:17:38 +04:00
func cmdString(cmd Cmder, val interface{}) string {
var ss []string
2017-09-26 11:29:22 +03:00
for _, arg := range cmd.Args() {
ss = append(ss, fmt.Sprint(arg))
}
s := strings.Join(ss, " ")
2014-01-09 12:17:38 +04:00
if err := cmd.Err(); err != nil {
return s + ": " + err.Error()
}
if val != nil {
switch vv := val.(type) {
case []byte:
return s + ": " + string(vv)
default:
return s + ": " + fmt.Sprint(val)
}
2014-01-09 12:17:38 +04:00
}
return s
}
func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
2017-05-09 12:44:36 +03:00
switch cmd.Name() {
case "eval", "evalsha":
2017-09-26 11:29:22 +03:00
if cmd.stringArg(2) != "0" {
return 3
}
2018-01-24 21:38:47 +03:00
return 0
2017-04-12 13:00:20 +03:00
case "publish":
return 1
}
if info == nil {
return 0
}
return int(info.FirstKeyPos)
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type baseCmd struct {
_args []interface{}
err error
2013-09-29 12:06:49 +04:00
2016-03-08 18:18:52 +03:00
_readTimeout *time.Duration
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*Cmd)(nil)
func (cmd *baseCmd) Name() string {
if len(cmd._args) > 0 {
// Cmd name must be lower cased.
s := internal.ToLower(cmd.stringArg(0))
cmd._args[0] = s
return s
}
return ""
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
func (cmd *baseCmd) Args() []interface{} {
2013-09-29 12:06:49 +04:00
return cmd._args
}
2017-09-26 11:29:22 +03:00
func (cmd *baseCmd) stringArg(pos int) string {
if pos < 0 || pos >= len(cmd._args) {
return ""
}
s, _ := cmd._args[pos].(string)
return s
}
func (cmd *baseCmd) Err() error {
return cmd.err
2017-01-13 14:39:59 +03:00
}
2013-09-29 12:06:49 +04:00
func (cmd *baseCmd) readTimeout() *time.Duration {
return cmd._readTimeout
}
func (cmd *baseCmd) setReadTimeout(d time.Duration) {
cmd._readTimeout = &d
}
2014-01-09 12:17:38 +04:00
func (cmd *baseCmd) setErr(e error) {
cmd.err = e
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type Cmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val interface{}
2013-09-29 12:06:49 +04:00
}
func NewCmd(args ...interface{}) *Cmd {
2016-06-17 15:09:38 +03:00
return &Cmd{
2017-01-13 14:39:59 +03:00
baseCmd: baseCmd{_args: args},
2016-06-17 15:09:38 +03:00
}
2013-09-29 12:06:49 +04:00
}
func (cmd *Cmd) Val() interface{} {
return cmd.val
}
2014-01-09 12:17:38 +04:00
func (cmd *Cmd) Result() (interface{}, error) {
return cmd.val, cmd.err
}
2018-08-12 11:11:01 +03:00
func (cmd *Cmd) String() (string, error) {
if cmd.err != nil {
return "", cmd.err
}
switch val := cmd.val.(type) {
case string:
return val, nil
default:
err := fmt.Errorf("redis: unexpected type=%T for String", val)
return "", err
}
}
2018-08-21 14:11:19 +03:00
func (cmd *Cmd) Int() (int, error) {
if cmd.err != nil {
return 0, cmd.err
}
switch val := cmd.val.(type) {
case int64:
return int(val), nil
case string:
return strconv.Atoi(val)
default:
2018-08-27 08:29:25 +03:00
err := fmt.Errorf("redis: unexpected type=%T for Int", val)
2018-08-21 14:11:19 +03:00
return 0, err
}
}
2018-08-12 11:11:01 +03:00
func (cmd *Cmd) Int64() (int64, error) {
if cmd.err != nil {
return 0, cmd.err
}
switch val := cmd.val.(type) {
case int64:
return val, nil
case string:
return strconv.ParseInt(val, 10, 64)
default:
err := fmt.Errorf("redis: unexpected type=%T for Int64", val)
return 0, err
}
}
func (cmd *Cmd) Uint64() (uint64, error) {
if cmd.err != nil {
return 0, cmd.err
}
switch val := cmd.val.(type) {
case int64:
return uint64(val), nil
case string:
return strconv.ParseUint(val, 10, 64)
default:
err := fmt.Errorf("redis: unexpected type=%T for Uint64", val)
return 0, err
}
}
2019-04-10 15:27:06 +03:00
func (cmd *Cmd) Float32() (float32, error) {
if cmd.err != nil {
return 0, cmd.err
}
switch val := cmd.val.(type) {
case int64:
return float32(val), nil
case string:
f, err := strconv.ParseFloat(val, 32)
if err != nil {
return 0, err
}
return float32(f), nil
default:
err := fmt.Errorf("redis: unexpected type=%T for Float32", val)
return 0, err
}
}
2018-08-12 11:11:01 +03:00
func (cmd *Cmd) Float64() (float64, error) {
if cmd.err != nil {
return 0, cmd.err
}
switch val := cmd.val.(type) {
case int64:
return float64(val), nil
case string:
return strconv.ParseFloat(val, 64)
default:
err := fmt.Errorf("redis: unexpected type=%T for Float64", val)
return 0, err
}
}
func (cmd *Cmd) Bool() (bool, error) {
if cmd.err != nil {
return false, cmd.err
}
switch val := cmd.val.(type) {
case int64:
return val != 0, nil
case string:
return strconv.ParseBool(val)
default:
err := fmt.Errorf("redis: unexpected type=%T for Bool", val)
return false, err
}
2014-01-09 12:17:38 +04:00
}
2018-08-17 13:56:37 +03:00
func (cmd *Cmd) readReply(rd *proto.Reader) error {
2018-08-15 09:38:58 +03:00
cmd.val, cmd.err = rd.ReadReply(sliceParser)
2018-08-16 13:25:19 +03:00
return cmd.err
2014-01-09 12:17:38 +04:00
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func sliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
vals := make([]interface{}, 0, n)
for i := int64(0); i < n; i++ {
v, err := rd.ReadReply(sliceParser)
if err != nil {
if err == Nil {
vals = append(vals, nil)
continue
}
if err, ok := err.(proto.RedisError); ok {
vals = append(vals, err)
continue
}
return nil, err
}
switch v := v.(type) {
2018-08-16 13:25:19 +03:00
case string:
vals = append(vals, v)
2018-08-02 14:48:46 +03:00
default:
vals = append(vals, v)
}
}
return vals, nil
}
2014-01-09 12:17:38 +04:00
//------------------------------------------------------------------------------
type SliceCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val []interface{}
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*SliceCmd)(nil)
func NewSliceCmd(args ...interface{}) *SliceCmd {
2017-01-13 14:39:59 +03:00
return &SliceCmd{
baseCmd: baseCmd{_args: args},
}
2014-01-09 12:17:38 +04:00
}
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)
}
2018-08-17 13:56:37 +03:00
func (cmd *SliceCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(sliceParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2014-01-09 12:17:38 +04:00
}
cmd.val = v.([]interface{})
return nil
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type StatusCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val string
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*StatusCmd)(nil)
func NewStatusCmd(args ...interface{}) *StatusCmd {
2017-01-13 14:39:59 +03:00
return &StatusCmd{
baseCmd: baseCmd{_args: args},
}
2015-01-24 15:12:48 +03:00
}
2013-09-29 12:06:49 +04:00
func (cmd *StatusCmd) Val() string {
2014-01-09 12:17:38 +04:00
return cmd.val
}
func (cmd *StatusCmd) Result() (string, error) {
return cmd.val, cmd.err
}
func (cmd *StatusCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *StatusCmd) readReply(rd *proto.Reader) error {
2018-08-16 13:25:19 +03:00
cmd.val, cmd.err = rd.ReadString()
2015-10-07 17:56:49 +03:00
return cmd.err
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
type IntCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val int64
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*IntCmd)(nil)
func NewIntCmd(args ...interface{}) *IntCmd {
2017-01-13 14:39:59 +03:00
return &IntCmd{
baseCmd: baseCmd{_args: args},
}
2013-09-29 12:06:49 +04:00
}
func (cmd *IntCmd) Val() int64 {
2014-01-09 12:17:38 +04:00
return cmd.val
}
func (cmd *IntCmd) Result() (int64, error) {
return cmd.val, cmd.err
}
func (cmd *IntCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *IntCmd) readReply(rd *proto.Reader) error {
2018-08-15 09:38:58 +03:00
cmd.val, cmd.err = rd.ReadIntReply()
2015-10-07 17:56:49 +03:00
return cmd.err
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
type DurationCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val time.Duration
precision time.Duration
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*DurationCmd)(nil)
func NewDurationCmd(precision time.Duration, args ...interface{}) *DurationCmd {
return &DurationCmd{
2017-01-13 14:39:59 +03:00
baseCmd: baseCmd{_args: args},
precision: precision,
}
}
2014-01-09 12:17:38 +04:00
func (cmd *DurationCmd) Val() time.Duration {
return cmd.val
}
2014-01-09 12:17:38 +04:00
func (cmd *DurationCmd) Result() (time.Duration, error) {
return cmd.val, cmd.err
}
func (cmd *DurationCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *DurationCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var n int64
2018-08-15 09:38:58 +03:00
n, cmd.err = rd.ReadIntReply()
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
}
2015-10-07 17:56:49 +03:00
cmd.val = time.Duration(n) * cmd.precision
2014-01-09 12:17:38 +04:00
return nil
}
//------------------------------------------------------------------------------
2016-10-14 14:39:02 +03:00
type TimeCmd struct {
baseCmd
val time.Time
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*TimeCmd)(nil)
2016-10-14 14:39:02 +03:00
func NewTimeCmd(args ...interface{}) *TimeCmd {
return &TimeCmd{
2017-01-13 14:39:59 +03:00
baseCmd: baseCmd{_args: args},
2016-10-14 14:39:02 +03:00
}
}
func (cmd *TimeCmd) Val() time.Time {
return cmd.val
}
func (cmd *TimeCmd) Result() (time.Time, error) {
return cmd.val, cmd.err
}
func (cmd *TimeCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *TimeCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(timeParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2016-10-14 14:39:02 +03:00
}
cmd.val = v.(time.Time)
return nil
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func timeParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
if n != 2 {
return nil, fmt.Errorf("got %d elements, expected 2", n)
}
sec, err := rd.ReadInt()
if err != nil {
return nil, err
}
microsec, err := rd.ReadInt()
if err != nil {
return nil, err
}
return time.Unix(sec, microsec*1000), nil
}
2016-10-14 14:39:02 +03:00
//------------------------------------------------------------------------------
2013-09-29 12:06:49 +04:00
type BoolCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val bool
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*BoolCmd)(nil)
func NewBoolCmd(args ...interface{}) *BoolCmd {
2017-01-13 14:39:59 +03:00
return &BoolCmd{
baseCmd: baseCmd{_args: args},
}
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
func (cmd *BoolCmd) Val() bool {
return cmd.val
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
func (cmd *BoolCmd) Result() (bool, error) {
return cmd.val, cmd.err
}
func (cmd *BoolCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *BoolCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadReply(nil)
2015-10-07 17:56:49 +03:00
// `SET key value NX` returns nil when key already exists. But
// `SETNX key value` returns bool (0/1). So convert nil to bool.
2015-08-29 13:08:27 +03:00
// TODO: is this okay?
2016-12-13 18:28:39 +03:00
if cmd.err == Nil {
cmd.val = false
2016-12-13 18:28:39 +03:00
cmd.err = nil
return nil
}
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2013-09-29 12:06:49 +04:00
}
2016-12-13 18:28:39 +03:00
switch v := v.(type) {
case int64:
2016-12-13 18:28:39 +03:00
cmd.val = v == 1
return nil
2018-08-16 13:25:19 +03:00
case string:
cmd.val = v == "OK"
return nil
default:
2016-12-13 18:28:39 +03:00
cmd.err = fmt.Errorf("got %T, wanted int64 or string", v)
return cmd.err
}
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
type StringCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
2018-08-16 13:25:19 +03:00
val string
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*StringCmd)(nil)
func NewStringCmd(args ...interface{}) *StringCmd {
2017-01-13 14:39:59 +03:00
return &StringCmd{
baseCmd: baseCmd{_args: args},
}
2013-09-29 12:06:49 +04:00
}
func (cmd *StringCmd) Val() string {
2018-08-16 13:25:19 +03:00
return cmd.val
2014-01-09 12:17:38 +04:00
}
func (cmd *StringCmd) Result() (string, error) {
return cmd.Val(), cmd.err
}
func (cmd *StringCmd) Bytes() ([]byte, error) {
2018-08-16 13:25:19 +03:00
return []byte(cmd.val), cmd.err
2014-01-09 12:17:38 +04:00
}
2018-08-21 14:11:19 +03:00
func (cmd *StringCmd) Int() (int, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.Atoi(cmd.Val())
}
2014-07-31 16:18:23 +04:00
func (cmd *StringCmd) Int64() (int64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseInt(cmd.Val(), 10, 64)
2014-07-31 16:18:23 +04:00
}
func (cmd *StringCmd) Uint64() (uint64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseUint(cmd.Val(), 10, 64)
2014-07-31 16:18:23 +04:00
}
2019-04-10 15:27:06 +03:00
func (cmd *StringCmd) Float32() (float32, error) {
if cmd.err != nil {
return 0, cmd.err
}
f, err := strconv.ParseFloat(cmd.Val(), 32)
if err != nil {
return 0, err
}
return float32(f), nil
}
2014-07-31 16:18:23 +04:00
func (cmd *StringCmd) Float64() (float64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseFloat(cmd.Val(), 64)
}
func (cmd *StringCmd) Scan(val interface{}) error {
if cmd.err != nil {
return cmd.err
}
2018-08-16 13:25:19 +03:00
return proto.Scan([]byte(cmd.val), val)
2014-07-31 16:18:23 +04:00
}
2014-01-09 12:17:38 +04:00
func (cmd *StringCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *StringCmd) readReply(rd *proto.Reader) error {
2018-08-16 13:25:19 +03:00
cmd.val, cmd.err = rd.ReadString()
2016-12-13 18:28:39 +03:00
return cmd.err
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
type FloatCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val float64
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*FloatCmd)(nil)
func NewFloatCmd(args ...interface{}) *FloatCmd {
2017-01-13 14:39:59 +03:00
return &FloatCmd{
baseCmd: baseCmd{_args: args},
}
2013-09-29 12:06:49 +04:00
}
func (cmd *FloatCmd) Val() float64 {
2014-01-09 12:17:38 +04:00
return cmd.val
2013-09-29 12:06:49 +04:00
}
2015-08-29 13:08:27 +03:00
func (cmd *FloatCmd) Result() (float64, error) {
return cmd.Val(), cmd.Err()
}
2014-01-09 12:17:38 +04:00
func (cmd *FloatCmd) String() string {
return cmdString(cmd, cmd.val)
2013-09-29 12:06:49 +04:00
}
2018-08-17 13:56:37 +03:00
func (cmd *FloatCmd) readReply(rd *proto.Reader) error {
2018-08-15 09:38:58 +03:00
cmd.val, cmd.err = rd.ReadFloatReply()
2014-01-09 12:17:38 +04:00
return cmd.err
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
type StringSliceCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val []string
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*StringSliceCmd)(nil)
func NewStringSliceCmd(args ...interface{}) *StringSliceCmd {
2017-01-13 14:39:59 +03:00
return &StringSliceCmd{
baseCmd: baseCmd{_args: args},
}
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
func (cmd *StringSliceCmd) Val() []string {
return cmd.val
2013-09-29 12:06:49 +04:00
}
func (cmd *StringSliceCmd) Result() ([]string, error) {
return cmd.Val(), cmd.Err()
}
2014-01-09 12:17:38 +04:00
func (cmd *StringSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
2017-02-01 11:36:33 +03:00
func (cmd *StringSliceCmd) ScanSlice(container interface{}) error {
return proto.ScanSlice(cmd.Val(), container)
}
2018-08-17 13:56:37 +03:00
func (cmd *StringSliceCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(stringSliceParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
cmd.val = v.([]string)
return nil
2013-09-29 12:06:49 +04:00
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func stringSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
ss := make([]string, 0, n)
for i := int64(0); i < n; i++ {
switch s, err := rd.ReadString(); {
case err == Nil:
2018-08-02 14:48:46 +03:00
ss = append(ss, "")
case err != nil:
2018-08-02 14:48:46 +03:00
return nil, err
default:
2018-08-02 14:48:46 +03:00
ss = append(ss, s)
}
}
return ss, nil
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type BoolSliceCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val []bool
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*BoolSliceCmd)(nil)
func NewBoolSliceCmd(args ...interface{}) *BoolSliceCmd {
2017-01-13 14:39:59 +03:00
return &BoolSliceCmd{
baseCmd: baseCmd{_args: args},
}
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
func (cmd *BoolSliceCmd) Val() []bool {
return cmd.val
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
func (cmd *BoolSliceCmd) Result() ([]bool, error) {
return cmd.val, cmd.err
}
func (cmd *BoolSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *BoolSliceCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(boolSliceParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
cmd.val = v.([]bool)
return nil
2013-09-29 12:06:49 +04:00
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func boolSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
bools := make([]bool, 0, n)
for i := int64(0); i < n; i++ {
n, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
bools = append(bools, n == 1)
}
return bools, nil
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type StringStringMapCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val map[string]string
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*StringStringMapCmd)(nil)
func NewStringStringMapCmd(args ...interface{}) *StringStringMapCmd {
2017-01-13 14:39:59 +03:00
return &StringStringMapCmd{
baseCmd: baseCmd{_args: args},
}
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
func (cmd *StringStringMapCmd) Val() map[string]string {
return cmd.val
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
func (cmd *StringStringMapCmd) Result() (map[string]string, error) {
return cmd.val, cmd.err
}
func (cmd *StringStringMapCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *StringStringMapCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(stringStringMapParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
cmd.val = v.(map[string]string)
return nil
2013-09-29 12:06:49 +04:00
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func stringStringMapParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
m := make(map[string]string, n/2)
for i := int64(0); i < n; i += 2 {
2018-08-16 13:25:19 +03:00
key, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
2018-08-16 13:25:19 +03:00
value, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
m[key] = value
}
return m, nil
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
2015-01-25 15:05:19 +03:00
type StringIntMapCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2015-01-25 15:05:19 +03:00
val map[string]int64
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*StringIntMapCmd)(nil)
func NewStringIntMapCmd(args ...interface{}) *StringIntMapCmd {
2017-01-13 14:39:59 +03:00
return &StringIntMapCmd{
baseCmd: baseCmd{_args: args},
}
2015-01-25 15:05:19 +03:00
}
func (cmd *StringIntMapCmd) Val() map[string]int64 {
return cmd.val
}
func (cmd *StringIntMapCmd) Result() (map[string]int64, error) {
return cmd.val, cmd.err
}
func (cmd *StringIntMapCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *StringIntMapCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(stringIntMapParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2015-01-25 15:05:19 +03:00
}
cmd.val = v.(map[string]int64)
return nil
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func stringIntMapParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
m := make(map[string]int64, n/2)
for i := int64(0); i < n; i += 2 {
2018-08-16 13:25:19 +03:00
key, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
n, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
m[key] = n
}
return m, nil
}
2015-01-25 15:05:19 +03:00
//------------------------------------------------------------------------------
2017-11-19 19:36:23 +03:00
type StringStructMapCmd struct {
baseCmd
val map[string]struct{}
}
var _ Cmder = (*StringStructMapCmd)(nil)
func NewStringStructMapCmd(args ...interface{}) *StringStructMapCmd {
return &StringStructMapCmd{
baseCmd: baseCmd{_args: args},
}
}
func (cmd *StringStructMapCmd) Val() map[string]struct{} {
return cmd.val
}
func (cmd *StringStructMapCmd) Result() (map[string]struct{}, error) {
return cmd.val, cmd.err
}
func (cmd *StringStructMapCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *StringStructMapCmd) readReply(rd *proto.Reader) error {
2017-11-19 19:36:23 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(stringStructMapParser)
2017-11-19 19:36:23 +03:00
if cmd.err != nil {
return cmd.err
}
cmd.val = v.(map[string]struct{})
return nil
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func stringStructMapParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
m := make(map[string]struct{}, n)
for i := int64(0); i < n; i++ {
2018-08-16 13:25:19 +03:00
key, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
2017-11-19 19:36:23 +03:00
2018-08-02 14:48:46 +03:00
m[key] = struct{}{}
}
return m, nil
2017-11-25 05:06:13 +03:00
}
2018-08-02 14:48:46 +03:00
//------------------------------------------------------------------------------
2017-11-25 05:06:13 +03:00
type XMessage struct {
ID string
Values map[string]interface{}
}
2018-08-02 14:48:46 +03:00
type XMessageSliceCmd struct {
2017-11-25 05:06:13 +03:00
baseCmd
2018-08-02 14:48:46 +03:00
val []XMessage
2017-11-25 05:06:13 +03:00
}
2018-08-02 14:48:46 +03:00
var _ Cmder = (*XMessageSliceCmd)(nil)
2017-11-25 05:06:13 +03:00
2018-08-02 14:48:46 +03:00
func NewXMessageSliceCmd(args ...interface{}) *XMessageSliceCmd {
return &XMessageSliceCmd{
2017-11-25 05:06:13 +03:00
baseCmd: baseCmd{_args: args},
}
}
2018-08-02 14:48:46 +03:00
func (cmd *XMessageSliceCmd) Val() []XMessage {
2017-11-25 05:06:13 +03:00
return cmd.val
}
2018-08-02 14:48:46 +03:00
func (cmd *XMessageSliceCmd) Result() ([]XMessage, error) {
2017-11-25 05:06:13 +03:00
return cmd.val, cmd.err
}
2018-08-02 14:48:46 +03:00
func (cmd *XMessageSliceCmd) String() string {
2017-11-25 05:06:13 +03:00
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *XMessageSliceCmd) readReply(rd *proto.Reader) error {
2017-11-25 05:06:13 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(xMessageSliceParser)
2017-11-25 05:06:13 +03:00
if cmd.err != nil {
return cmd.err
}
2018-08-02 14:48:46 +03:00
cmd.val = v.([]XMessage)
2017-11-25 05:06:13 +03:00
return nil
}
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func xMessageSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
msgs := make([]XMessage, 0, n)
2017-11-25 05:06:13 +03:00
for i := int64(0); i < n; i++ {
2018-08-17 13:56:37 +03:00
_, err := rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-16 13:25:19 +03:00
id, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
v, err := rd.ReadArrayReply(stringInterfaceMapParser)
if err != nil {
return nil, err
}
msgs = append(msgs, XMessage{
ID: id,
Values: v.(map[string]interface{}),
})
return nil, nil
})
2017-11-25 05:06:13 +03:00
if err != nil {
return nil, err
}
}
2018-08-02 14:48:46 +03:00
return msgs, nil
2017-11-25 05:06:13 +03:00
}
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func stringInterfaceMapParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
m := make(map[string]interface{}, n/2)
for i := int64(0); i < n; i += 2 {
2018-08-16 13:25:19 +03:00
key, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
2017-11-25 05:06:13 +03:00
2018-08-16 13:25:19 +03:00
value, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
2017-11-25 05:06:13 +03:00
2018-08-02 14:48:46 +03:00
m[key] = value
2017-11-25 05:06:13 +03:00
}
2018-08-02 14:48:46 +03:00
return m, nil
2017-11-25 05:06:13 +03:00
}
//------------------------------------------------------------------------------
2018-08-02 14:48:46 +03:00
type XStream struct {
Stream string
Messages []XMessage
}
type XStreamSliceCmd struct {
2017-11-25 05:06:13 +03:00
baseCmd
2018-08-02 14:48:46 +03:00
val []XStream
2017-11-25 05:06:13 +03:00
}
2018-08-02 14:48:46 +03:00
var _ Cmder = (*XStreamSliceCmd)(nil)
2017-11-25 05:06:13 +03:00
2018-08-02 14:48:46 +03:00
func NewXStreamSliceCmd(args ...interface{}) *XStreamSliceCmd {
return &XStreamSliceCmd{
2017-11-25 05:06:13 +03:00
baseCmd: baseCmd{_args: args},
}
}
2018-08-02 14:48:46 +03:00
func (cmd *XStreamSliceCmd) Val() []XStream {
2017-11-25 05:06:13 +03:00
return cmd.val
}
2018-08-02 14:48:46 +03:00
func (cmd *XStreamSliceCmd) Result() ([]XStream, error) {
2017-11-25 05:06:13 +03:00
return cmd.val, cmd.err
}
2018-08-02 14:48:46 +03:00
func (cmd *XStreamSliceCmd) String() string {
2017-11-25 05:06:13 +03:00
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *XStreamSliceCmd) readReply(rd *proto.Reader) error {
2017-11-25 05:06:13 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(xStreamSliceParser)
2017-11-25 05:06:13 +03:00
if cmd.err != nil {
return cmd.err
}
2018-08-02 14:48:46 +03:00
cmd.val = v.([]XStream)
2017-11-25 05:06:13 +03:00
return nil
}
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func xStreamSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
ret := make([]XStream, 0, n)
2017-11-25 05:06:13 +03:00
for i := int64(0); i < n; i++ {
2018-08-17 13:56:37 +03:00
_, err := rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
if n != 2 {
return nil, fmt.Errorf("got %d, wanted 2", n)
}
2018-08-16 13:25:19 +03:00
stream, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
v, err := rd.ReadArrayReply(xMessageSliceParser)
if err != nil {
return nil, err
}
ret = append(ret, XStream{
Stream: stream,
Messages: v.([]XMessage),
})
return nil, nil
})
2017-11-25 05:06:13 +03:00
if err != nil {
return nil, err
}
}
2018-08-02 14:48:46 +03:00
return ret, nil
2017-11-25 05:06:13 +03:00
}
2018-08-02 14:48:46 +03:00
//------------------------------------------------------------------------------
type XPending struct {
Count int64
Lower string
Higher string
Consumers map[string]int64
}
type XPendingCmd struct {
baseCmd
val *XPending
}
var _ Cmder = (*XPendingCmd)(nil)
func NewXPendingCmd(args ...interface{}) *XPendingCmd {
return &XPendingCmd{
baseCmd: baseCmd{_args: args},
}
}
func (cmd *XPendingCmd) Val() *XPending {
return cmd.val
}
func (cmd *XPendingCmd) Result() (*XPending, error) {
return cmd.val, cmd.err
}
func (cmd *XPendingCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *XPendingCmd) readReply(rd *proto.Reader) error {
2018-08-02 14:48:46 +03:00
var info interface{}
2018-08-15 09:38:58 +03:00
info, cmd.err = rd.ReadArrayReply(xPendingParser)
2018-08-02 14:48:46 +03:00
if cmd.err != nil {
return cmd.err
}
cmd.val = info.(*XPending)
return nil
}
2018-08-17 13:56:37 +03:00
func xPendingParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
if n != 4 {
return nil, fmt.Errorf("got %d, wanted 4", n)
}
count, err := rd.ReadIntReply()
2017-11-25 05:06:13 +03:00
if err != nil {
return nil, err
}
2018-08-16 13:25:19 +03:00
lower, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil && err != Nil {
2017-11-25 05:06:13 +03:00
return nil, err
}
2018-08-16 13:25:19 +03:00
higher, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil && err != Nil {
return nil, err
}
2017-11-25 05:06:13 +03:00
2018-08-02 14:48:46 +03:00
pending := &XPending{
Count: count,
Lower: lower,
Higher: higher,
}
2018-08-17 13:56:37 +03:00
_, err = rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
for i := int64(0); i < n; i++ {
2018-08-17 13:56:37 +03:00
_, err = rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
if n != 2 {
return nil, fmt.Errorf("got %d, wanted 2", n)
}
2018-08-16 13:25:19 +03:00
consumerName, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
2018-08-16 13:25:19 +03:00
consumerPending, err := rd.ReadInt()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
if pending.Consumers == nil {
pending.Consumers = make(map[string]int64)
}
pending.Consumers[consumerName] = consumerPending
return nil, nil
})
if err != nil {
return nil, err
}
2017-11-25 05:06:13 +03:00
}
2018-08-02 14:48:46 +03:00
return nil, nil
})
if err != nil && err != Nil {
return nil, err
}
2017-11-25 05:06:13 +03:00
2018-08-02 14:48:46 +03:00
return pending, nil
}
//------------------------------------------------------------------------------
type XPendingExt struct {
2019-05-16 16:27:19 +03:00
ID string
2018-08-02 14:48:46 +03:00
Consumer string
Idle time.Duration
RetryCount int64
}
type XPendingExtCmd struct {
baseCmd
val []XPendingExt
}
var _ Cmder = (*XPendingExtCmd)(nil)
func NewXPendingExtCmd(args ...interface{}) *XPendingExtCmd {
return &XPendingExtCmd{
baseCmd: baseCmd{_args: args},
}
}
func (cmd *XPendingExtCmd) Val() []XPendingExt {
return cmd.val
}
func (cmd *XPendingExtCmd) Result() ([]XPendingExt, error) {
return cmd.val, cmd.err
}
func (cmd *XPendingExtCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *XPendingExtCmd) readReply(rd *proto.Reader) error {
2018-08-02 14:48:46 +03:00
var info interface{}
2018-08-15 09:38:58 +03:00
info, cmd.err = rd.ReadArrayReply(xPendingExtSliceParser)
2018-08-02 14:48:46 +03:00
if cmd.err != nil {
return cmd.err
}
cmd.val = info.([]XPendingExt)
return nil
}
2018-08-17 13:56:37 +03:00
func xPendingExtSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
ret := make([]XPendingExt, 0, n)
for i := int64(0); i < n; i++ {
2018-08-17 13:56:37 +03:00
_, err := rd.ReadArrayReply(func(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
if n != 4 {
return nil, fmt.Errorf("got %d, wanted 4", n)
}
2018-08-16 13:25:19 +03:00
id, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
2018-08-16 13:25:19 +03:00
consumer, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil && err != Nil {
return nil, err
}
idle, err := rd.ReadIntReply()
if err != nil && err != Nil {
return nil, err
}
retryCount, err := rd.ReadIntReply()
if err != nil && err != Nil {
return nil, err
}
ret = append(ret, XPendingExt{
2019-05-16 16:27:19 +03:00
ID: id,
2018-08-02 14:48:46 +03:00
Consumer: consumer,
Idle: time.Duration(idle) * time.Millisecond,
RetryCount: retryCount,
})
return nil, nil
})
2017-11-25 05:06:13 +03:00
if err != nil {
return nil, err
}
}
2018-08-02 14:48:46 +03:00
return ret, nil
2017-11-25 05:06:13 +03:00
}
//------------------------------------------------------------------------------
2018-08-02 14:48:46 +03:00
//------------------------------------------------------------------------------
2014-07-05 14:46:27 +04:00
type ZSliceCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
2014-07-05 14:46:27 +04:00
val []Z
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*ZSliceCmd)(nil)
func NewZSliceCmd(args ...interface{}) *ZSliceCmd {
2017-01-13 14:39:59 +03:00
return &ZSliceCmd{
baseCmd: baseCmd{_args: args},
}
2013-09-29 12:06:49 +04:00
}
2014-07-05 14:46:27 +04:00
func (cmd *ZSliceCmd) Val() []Z {
2014-01-09 12:17:38 +04:00
return cmd.val
}
2014-07-05 14:46:27 +04:00
func (cmd *ZSliceCmd) Result() ([]Z, error) {
2014-01-09 12:17:38 +04:00
return cmd.val, cmd.err
}
2014-07-05 14:46:27 +04:00
func (cmd *ZSliceCmd) String() string {
2014-01-09 12:17:38 +04:00
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *ZSliceCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(zSliceParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2014-01-09 12:17:38 +04:00
}
2014-07-05 14:46:27 +04:00
cmd.val = v.([]Z)
2014-01-09 12:17:38 +04:00
return nil
2013-09-29 12:06:49 +04:00
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func zSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
zz := make([]Z, n/2)
for i := int64(0); i < n; i += 2 {
var err error
z := &zz[i/2]
2018-08-16 13:25:19 +03:00
z.Member, err = rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
z.Score, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
}
return zz, nil
}
2014-01-09 12:17:38 +04:00
//------------------------------------------------------------------------------
2018-10-31 16:35:23 +03:00
type ZWithKeyCmd struct {
baseCmd
2019-05-16 16:27:19 +03:00
val *ZWithKey
2018-10-31 16:35:23 +03:00
}
var _ Cmder = (*ZWithKeyCmd)(nil)
func NewZWithKeyCmd(args ...interface{}) *ZWithKeyCmd {
return &ZWithKeyCmd{
baseCmd: baseCmd{_args: args},
}
}
2019-05-16 16:27:19 +03:00
func (cmd *ZWithKeyCmd) Val() *ZWithKey {
2018-10-31 16:35:23 +03:00
return cmd.val
}
2019-05-16 16:27:19 +03:00
func (cmd *ZWithKeyCmd) Result() (*ZWithKey, error) {
2018-10-31 16:35:23 +03:00
return cmd.Val(), cmd.Err()
}
func (cmd *ZWithKeyCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *ZWithKeyCmd) readReply(rd *proto.Reader) error {
var v interface{}
v, cmd.err = rd.ReadArrayReply(zWithKeyParser)
if cmd.err != nil {
return cmd.err
}
2019-05-16 16:27:19 +03:00
cmd.val = v.(*ZWithKey)
2018-10-31 16:35:23 +03:00
return nil
}
// Implements proto.MultiBulkParse
func zWithKeyParser(rd *proto.Reader, n int64) (interface{}, error) {
if n != 3 {
return nil, fmt.Errorf("got %d elements, expected 3", n)
}
var z ZWithKey
var err error
z.Key, err = rd.ReadString()
if err != nil {
return nil, err
}
z.Member, err = rd.ReadString()
if err != nil {
return nil, err
}
z.Score, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
2019-05-16 16:27:19 +03:00
return &z, nil
2018-10-31 16:35:23 +03:00
}
//------------------------------------------------------------------------------
2014-01-09 12:17:38 +04:00
type ScanCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
2016-04-13 11:52:47 +03:00
page []string
cursor uint64
2017-01-03 13:44:06 +03:00
process func(cmd Cmder) error
2014-01-09 12:17:38 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*ScanCmd)(nil)
2017-01-03 13:44:06 +03:00
func NewScanCmd(process func(cmd Cmder) error, args ...interface{}) *ScanCmd {
2016-04-13 11:52:47 +03:00
return &ScanCmd{
2017-01-13 14:39:59 +03:00
baseCmd: baseCmd{_args: args},
2017-01-03 13:44:06 +03:00
process: process,
2016-04-13 11:52:47 +03:00
}
2014-01-09 12:17:38 +04:00
}
func (cmd *ScanCmd) Val() (keys []string, cursor uint64) {
return cmd.page, cmd.cursor
2014-01-09 12:17:38 +04:00
}
func (cmd *ScanCmd) Result() (keys []string, cursor uint64, err error) {
return cmd.page, cmd.cursor, cmd.err
2014-01-09 12:17:38 +04:00
}
func (cmd *ScanCmd) String() string {
2016-04-13 11:52:47 +03:00
return cmdString(cmd, cmd.page)
2014-01-09 12:17:38 +04:00
}
2018-08-17 13:56:37 +03:00
func (cmd *ScanCmd) readReply(rd *proto.Reader) error {
2018-08-15 09:38:58 +03:00
cmd.page, cmd.cursor, cmd.err = rd.ReadScanReply()
2016-12-13 18:28:39 +03:00
return cmd.err
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
2017-01-03 13:44:06 +03:00
// Iterator creates a new ScanIterator.
func (cmd *ScanCmd) Iterator() *ScanIterator {
return &ScanIterator{
cmd: cmd,
}
}
2015-01-24 15:12:48 +03:00
//------------------------------------------------------------------------------
type ClusterNode struct {
Id string
Addr string
}
type ClusterSlot struct {
2015-10-07 17:09:20 +03:00
Start int
End int
Nodes []ClusterNode
2015-01-24 15:12:48 +03:00
}
type ClusterSlotsCmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
val []ClusterSlot
2015-01-24 15:12:48 +03:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*ClusterSlotsCmd)(nil)
func NewClusterSlotsCmd(args ...interface{}) *ClusterSlotsCmd {
2017-01-13 14:39:59 +03:00
return &ClusterSlotsCmd{
baseCmd: baseCmd{_args: args},
}
2015-01-24 15:12:48 +03:00
}
func (cmd *ClusterSlotsCmd) Val() []ClusterSlot {
2015-01-24 15:12:48 +03:00
return cmd.val
}
func (cmd *ClusterSlotsCmd) Result() ([]ClusterSlot, error) {
2015-01-24 15:12:48 +03:00
return cmd.Val(), cmd.Err()
}
func (cmd *ClusterSlotsCmd) String() string {
2015-01-24 15:12:48 +03:00
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *ClusterSlotsCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(clusterSlotsParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2015-01-24 15:12:48 +03:00
}
cmd.val = v.([]ClusterSlot)
2015-01-24 15:12:48 +03:00
return nil
}
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func clusterSlotsParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
slots := make([]ClusterSlot, n)
for i := 0; i < len(slots); i++ {
n, err := rd.ReadArrayLen()
if err != nil {
return nil, err
}
if n < 2 {
err := fmt.Errorf("redis: got %d elements in cluster info, expected at least 2", n)
return nil, err
}
start, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
end, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
nodes := make([]ClusterNode, n-2)
for j := 0; j < len(nodes); j++ {
n, err := rd.ReadArrayLen()
if err != nil {
return nil, err
}
if n != 2 && n != 3 {
err := fmt.Errorf("got %d elements in cluster info address, expected 2 or 3", n)
return nil, err
}
2018-08-16 13:25:19 +03:00
ip, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
2018-08-16 13:25:19 +03:00
port, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
2018-08-16 13:25:19 +03:00
nodes[j].Addr = net.JoinHostPort(ip, port)
2018-08-02 14:48:46 +03:00
if n == 3 {
2018-08-16 13:25:19 +03:00
id, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
nodes[j].Id = id
}
}
slots[i] = ClusterSlot{
Start: int(start),
End: int(end),
Nodes: nodes,
}
}
return slots, nil
}
//------------------------------------------------------------------------------
2015-10-07 17:09:20 +03:00
// GeoLocation is used with GeoAdd to add geospatial location.
type GeoLocation struct {
2015-11-14 17:36:21 +03:00
Name string
Longitude, Latitude, Dist float64
GeoHash int64
}
2015-10-07 17:09:20 +03:00
// GeoRadiusQuery is used with GeoRadius to query geospatial index.
type GeoRadiusQuery struct {
Radius float64
2015-10-07 17:09:20 +03:00
// Can be m, km, ft, or mi. Default is km.
Unit string
WithCoord bool
WithDist bool
WithGeoHash bool
Count int
2015-10-07 17:09:20 +03:00
// Can be ASC or DESC. Default is no sort order.
2017-07-19 15:32:50 +03:00
Sort string
Store string
StoreDist string
2015-10-07 17:09:20 +03:00
}
type GeoLocationCmd struct {
baseCmd
2015-11-14 17:36:21 +03:00
q *GeoRadiusQuery
locations []GeoLocation
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*GeoLocationCmd)(nil)
2015-11-14 17:36:21 +03:00
func NewGeoLocationCmd(q *GeoRadiusQuery, args ...interface{}) *GeoLocationCmd {
args = append(args, q.Radius)
if q.Unit != "" {
args = append(args, q.Unit)
} else {
args = append(args, "km")
}
if q.WithCoord {
2017-07-19 15:32:50 +03:00
args = append(args, "withcoord")
2015-11-14 17:36:21 +03:00
}
if q.WithDist {
2017-07-19 15:32:50 +03:00
args = append(args, "withdist")
2015-11-14 17:36:21 +03:00
}
if q.WithGeoHash {
2017-07-19 15:32:50 +03:00
args = append(args, "withhash")
2015-11-14 17:36:21 +03:00
}
if q.Count > 0 {
2017-07-19 15:32:50 +03:00
args = append(args, "count", q.Count)
2015-11-14 17:36:21 +03:00
}
if q.Sort != "" {
args = append(args, q.Sort)
}
2017-07-19 15:32:50 +03:00
if q.Store != "" {
args = append(args, "store")
args = append(args, q.Store)
}
if q.StoreDist != "" {
args = append(args, "storedist")
args = append(args, q.StoreDist)
}
2015-11-14 17:36:21 +03:00
return &GeoLocationCmd{
2017-03-24 14:33:35 +03:00
baseCmd: baseCmd{_args: args},
q: q,
2015-11-14 17:36:21 +03:00
}
}
2015-10-07 17:09:20 +03:00
func (cmd *GeoLocationCmd) Val() []GeoLocation {
return cmd.locations
}
2015-10-07 17:09:20 +03:00
func (cmd *GeoLocationCmd) Result() ([]GeoLocation, error) {
return cmd.locations, cmd.err
}
2015-10-07 17:09:20 +03:00
func (cmd *GeoLocationCmd) String() string {
return cmdString(cmd, cmd.locations)
}
2018-08-17 13:56:37 +03:00
func (cmd *GeoLocationCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(newGeoLocationSliceParser(cmd.q))
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
}
2016-12-13 18:28:39 +03:00
cmd.locations = v.([]GeoLocation)
return nil
}
2018-08-02 14:48:46 +03:00
func newGeoLocationParser(q *GeoRadiusQuery) proto.MultiBulkParse {
2018-08-17 13:56:37 +03:00
return func(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
var loc GeoLocation
var err error
2018-08-16 13:25:19 +03:00
loc.Name, err = rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
if q.WithDist {
loc.Dist, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
}
if q.WithGeoHash {
loc.GeoHash, err = rd.ReadIntReply()
if err != nil {
return nil, err
}
}
if q.WithCoord {
n, err := rd.ReadArrayLen()
if err != nil {
return nil, err
}
if n != 2 {
return nil, fmt.Errorf("got %d coordinates, expected 2", n)
}
loc.Longitude, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
loc.Latitude, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
}
return &loc, nil
}
}
func newGeoLocationSliceParser(q *GeoRadiusQuery) proto.MultiBulkParse {
2018-08-17 13:56:37 +03:00
return func(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
locs := make([]GeoLocation, 0, n)
for i := int64(0); i < n; i++ {
v, err := rd.ReadReply(newGeoLocationParser(q))
if err != nil {
return nil, err
}
switch vv := v.(type) {
2018-08-16 13:25:19 +03:00
case string:
2018-08-02 14:48:46 +03:00
locs = append(locs, GeoLocation{
2018-08-16 13:25:19 +03:00
Name: vv,
2018-08-02 14:48:46 +03:00
})
case *GeoLocation:
locs = append(locs, *vv)
default:
return nil, fmt.Errorf("got %T, expected string or *GeoLocation", v)
}
}
return locs, nil
}
}
//------------------------------------------------------------------------------
type GeoPos struct {
Longitude, Latitude float64
}
2016-08-22 00:32:06 +03:00
type GeoPosCmd struct {
baseCmd
positions []*GeoPos
2016-08-22 00:32:06 +03:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*GeoPosCmd)(nil)
2016-08-22 00:32:06 +03:00
func NewGeoPosCmd(args ...interface{}) *GeoPosCmd {
2017-01-13 14:39:59 +03:00
return &GeoPosCmd{
baseCmd: baseCmd{_args: args},
}
2016-08-22 00:32:06 +03:00
}
func (cmd *GeoPosCmd) Val() []*GeoPos {
2016-08-22 00:32:06 +03:00
return cmd.positions
}
func (cmd *GeoPosCmd) Result() ([]*GeoPos, error) {
2016-08-22 00:32:06 +03:00
return cmd.Val(), cmd.Err()
}
func (cmd *GeoPosCmd) String() string {
return cmdString(cmd, cmd.positions)
}
2018-08-17 13:56:37 +03:00
func (cmd *GeoPosCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(geoPosSliceParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
2016-08-22 00:32:06 +03:00
}
2016-12-13 18:28:39 +03:00
cmd.positions = v.([]*GeoPos)
2016-08-22 00:32:06 +03:00
return nil
}
2018-08-17 13:56:37 +03:00
func geoPosSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
positions := make([]*GeoPos, 0, n)
for i := int64(0); i < n; i++ {
2019-05-16 16:27:19 +03:00
_, err := rd.ReadReply(func(rd *proto.Reader, n int64) (interface{}, error) {
var pos GeoPos
var err error
pos.Longitude, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
pos.Latitude, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
positions = append(positions, &pos)
return nil, nil
})
2018-08-02 14:48:46 +03:00
if err != nil {
if err == Nil {
positions = append(positions, nil)
continue
}
return nil, err
}
}
return positions, nil
}
//------------------------------------------------------------------------------
type CommandInfo struct {
Name string
Arity int8
Flags []string
FirstKeyPos int8
LastKeyPos int8
StepCount int8
ReadOnly bool
}
type CommandsInfoCmd struct {
baseCmd
val map[string]*CommandInfo
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*CommandsInfoCmd)(nil)
func NewCommandsInfoCmd(args ...interface{}) *CommandsInfoCmd {
2017-01-13 14:39:59 +03:00
return &CommandsInfoCmd{
baseCmd: baseCmd{_args: args},
}
}
func (cmd *CommandsInfoCmd) Val() map[string]*CommandInfo {
return cmd.val
}
func (cmd *CommandsInfoCmd) Result() (map[string]*CommandInfo, error) {
return cmd.Val(), cmd.Err()
}
func (cmd *CommandsInfoCmd) String() string {
return cmdString(cmd, cmd.val)
}
2018-08-17 13:56:37 +03:00
func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
2016-12-13 18:28:39 +03:00
var v interface{}
2018-08-15 09:38:58 +03:00
v, cmd.err = rd.ReadArrayReply(commandInfoSliceParser)
2016-12-13 18:28:39 +03:00
if cmd.err != nil {
return cmd.err
}
cmd.val = v.(map[string]*CommandInfo)
return nil
}
2018-03-06 15:50:48 +03:00
2018-08-02 14:48:46 +03:00
// Implements proto.MultiBulkParse
2018-08-17 13:56:37 +03:00
func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
m := make(map[string]*CommandInfo, n)
for i := int64(0); i < n; i++ {
v, err := rd.ReadReply(commandInfoParser)
if err != nil {
return nil, err
}
vv := v.(*CommandInfo)
m[vv.Name] = vv
}
return m, nil
}
2018-08-17 13:56:37 +03:00
func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
2018-08-02 14:48:46 +03:00
var cmd CommandInfo
var err error
if n != 6 {
return nil, fmt.Errorf("redis: got %d elements in COMMAND reply, wanted 6", n)
}
2018-08-16 13:25:19 +03:00
cmd.Name, err = rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return nil, err
}
arity, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
cmd.Arity = int8(arity)
flags, err := rd.ReadReply(stringSliceParser)
if err != nil {
return nil, err
}
cmd.Flags = flags.([]string)
firstKeyPos, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
cmd.FirstKeyPos = int8(firstKeyPos)
lastKeyPos, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
cmd.LastKeyPos = int8(lastKeyPos)
stepCount, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
cmd.StepCount = int8(stepCount)
for _, flag := range cmd.Flags {
if flag == "readonly" {
cmd.ReadOnly = true
break
}
}
return &cmd, nil
}
2018-03-06 15:50:48 +03:00
//------------------------------------------------------------------------------
type cmdsInfoCache struct {
2018-05-17 15:21:51 +03:00
fn func() (map[string]*CommandInfo, error)
2018-03-06 15:50:48 +03:00
once internal.Once
cmds map[string]*CommandInfo
}
2018-05-17 15:21:51 +03:00
func newCmdsInfoCache(fn func() (map[string]*CommandInfo, error)) *cmdsInfoCache {
return &cmdsInfoCache{
fn: fn,
}
2018-03-06 15:50:48 +03:00
}
2018-05-17 15:21:51 +03:00
func (c *cmdsInfoCache) Get() (map[string]*CommandInfo, error) {
2018-03-06 15:50:48 +03:00
err := c.once.Do(func() error {
2018-05-17 15:21:51 +03:00
cmds, err := c.fn()
2018-03-06 15:50:48 +03:00
if err != nil {
return err
}
c.cmds = cmds
return nil
})
return c.cmds, err
}