redis/command.go

2923 lines
54 KiB
Go
Raw Normal View History

2013-09-29 12:06:49 +04:00
package redis
import (
2020-03-11 17:26:42 +03:00
"context"
2013-09-29 12:06:49 +04:00
"fmt"
2018-08-02 14:48:46 +03:00
"net"
2013-09-29 12:06:49 +04:00
"strconv"
"time"
2020-03-11 17:29:16 +03:00
"github.com/go-redis/redis/v8/internal"
"github.com/go-redis/redis/v8/internal/hscan"
2020-03-11 17:29:16 +03:00
"github.com/go-redis/redis/v8/internal/proto"
"github.com/go-redis/redis/v8/internal/util"
2013-09-29 12:06:49 +04:00
)
type Cmder interface {
2017-05-09 12:44:36 +03:00
Name() string
2020-05-09 17:34:50 +03:00
FullName() string
2017-09-26 11:29:22 +03:00
Args() []interface{}
2020-02-14 15:30:07 +03:00
String() string
2017-09-26 11:29:22 +03:00
stringArg(int) string
2020-09-23 10:29:13 +03:00
firstKeyPos() int8
setFirstKeyPos(int8)
2016-06-17 15:09:38 +03:00
2013-09-29 12:06:49 +04:00
readTimeout() *time.Duration
2019-08-24 11:55:13 +03:00
readReply(rd *proto.Reader) error
2013-09-29 12:06:49 +04:00
2020-02-03 12:53:47 +03:00
SetErr(error)
2013-09-29 12:06:49 +04:00
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 {
2020-02-03 12:53:47 +03:00
cmd.SetErr(e)
2017-08-31 15:22:47 +03:00
}
}
}
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
}
func writeCmds(wr *proto.Writer, cmds []Cmder) error {
for _, cmd := range cmds {
if err := writeCmd(wr, cmd); err != nil {
return err
}
}
2018-08-15 11:53:15 +03:00
return nil
}
func writeCmd(wr *proto.Writer, cmd Cmder) error {
return wr.WriteArgs(cmd.Args())
}
func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
2020-09-23 10:29:13 +03:00
if pos := cmd.firstKeyPos(); pos != 0 {
return int(pos)
}
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
case "memory":
// https://github.com/redis/redis/issues/7493
if cmd.stringArg(1) == "usage" {
return 2
}
}
2020-09-23 10:29:13 +03:00
if info != nil {
return int(info.FirstKeyPos)
}
2020-09-23 10:29:13 +03:00
return 0
}
2020-05-09 17:30:16 +03:00
func cmdString(cmd Cmder, val interface{}) string {
2020-06-09 16:29:53 +03:00
b := make([]byte, 0, 64)
2020-05-09 17:30:16 +03:00
for i, arg := range cmd.Args() {
if i > 0 {
b = append(b, ' ')
}
2020-06-09 16:29:53 +03:00
b = internal.AppendArg(b, arg)
2020-05-09 17:30:16 +03:00
}
if err := cmd.Err(); err != nil {
b = append(b, ": "...)
b = append(b, err.Error()...)
} else if val != nil {
b = append(b, ": "...)
2020-06-09 16:29:53 +03:00
b = internal.AppendArg(b, val)
2020-05-09 17:30:16 +03:00
}
2020-06-09 16:29:53 +03:00
return internal.String(b)
2020-05-09 17:30:16 +03:00
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type baseCmd struct {
2020-09-23 10:29:13 +03:00
ctx context.Context
args []interface{}
err error
keyPos int8
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 {
2019-08-24 11:55:13 +03:00
if len(cmd.args) == 0 {
2019-08-12 14:53:00 +03:00
return ""
}
2019-08-12 14:53:00 +03:00
// Cmd name must be lower cased.
return internal.ToLower(cmd.stringArg(0))
2013-09-29 12:06:49 +04:00
}
2020-05-09 17:34:50 +03:00
func (cmd *baseCmd) FullName() string {
switch name := cmd.Name(); name {
case "cluster", "command":
if len(cmd.args) == 1 {
return name
}
if s2, ok := cmd.args[1].(string); ok {
return name + " " + s2
}
return name
default:
return name
}
}
2017-09-26 11:29:22 +03:00
func (cmd *baseCmd) Args() []interface{} {
2019-08-24 11:55:13 +03:00
return cmd.args
2013-09-29 12:06:49 +04:00
}
2017-09-26 11:29:22 +03:00
func (cmd *baseCmd) stringArg(pos int) string {
2019-08-24 11:55:13 +03:00
if pos < 0 || pos >= len(cmd.args) {
return ""
}
2019-08-24 11:55:13 +03:00
s, _ := cmd.args[pos].(string)
return s
}
2020-09-23 10:29:13 +03:00
func (cmd *baseCmd) firstKeyPos() int8 {
return cmd.keyPos
}
2020-09-23 10:29:13 +03:00
func (cmd *baseCmd) setFirstKeyPos(keyPos int8) {
cmd.keyPos = keyPos
}
2020-02-03 12:53:47 +03:00
func (cmd *baseCmd) SetErr(e error) {
cmd.err = e
}
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
}
//------------------------------------------------------------------------------
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
}
2020-03-11 17:26:42 +03:00
func NewCmd(ctx context.Context, args ...interface{}) *Cmd {
2016-06-17 15:09:38 +03:00
return &Cmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2016-06-17 15:09:38 +03:00
}
2013-09-29 12:06:49 +04:00
}
2020-02-14 15:30:07 +03:00
func (cmd *Cmd) String() string {
return cmdString(cmd, cmd.val)
}
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
}
2020-02-14 15:30:07 +03:00
func (cmd *Cmd) Text() (string, error) {
2018-08-12 11:11:01 +03:00
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
}
2020-09-17 12:27:16 +03:00
func (cmd *Cmd) readReply(rd *proto.Reader) (err error) {
cmd.val, err = rd.ReadReply()
2020-09-17 12:27:16 +03:00
return err
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)
2020-03-11 17:26:42 +03:00
func NewSliceCmd(ctx context.Context, args ...interface{}) *SliceCmd {
2017-01-13 14:39:59 +03:00
return &SliceCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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)
}
// Scan scans the results from the map into a destination struct. The map keys
// are matched in the Redis struct fields by the `redis:"field"` tag.
func (cmd *SliceCmd) Scan(dst interface{}) error {
if cmd.err != nil {
return cmd.err
}
// Pass the list of keys and values.
// Skip the first two args for: HMGET key
var args []interface{}
if cmd.args[0] == "hmget" {
args = cmd.args[2:]
} else {
// Otherwise, it's: MGET field field ...
args = cmd.args[1:]
}
return hscan.Scan(dst, args, cmd.val)
}
func (cmd *SliceCmd) readReply(rd *proto.Reader) (err error) {
cmd.val, err = rd.ReadSlice()
return err
2014-01-09 12:17:38 +04:00
}
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)
2020-03-11 17:26:42 +03:00
func NewStatusCmd(ctx context.Context, args ...interface{}) *StatusCmd {
2017-01-13 14:39:59 +03:00
return &StatusCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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)
}
2020-09-17 12:27:16 +03:00
func (cmd *StatusCmd) readReply(rd *proto.Reader) (err error) {
cmd.val, err = rd.ReadString()
return 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)
2020-03-11 17:26:42 +03:00
func NewIntCmd(ctx context.Context, args ...interface{}) *IntCmd {
2017-01-13 14:39:59 +03:00
return &IntCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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
}
2019-09-27 14:38:55 +03:00
func (cmd *IntCmd) Uint64() (uint64, error) {
return uint64(cmd.val), cmd.err
}
2014-01-09 12:17:38 +04:00
func (cmd *IntCmd) String() string {
return cmdString(cmd, cmd.val)
}
2020-09-17 12:27:16 +03:00
func (cmd *IntCmd) readReply(rd *proto.Reader) (err error) {
cmd.val, err = rd.ReadInt()
2020-09-17 12:27:16 +03:00
return err
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
2019-06-26 14:45:38 +03:00
type IntSliceCmd struct {
baseCmd
val []int64
}
var _ Cmder = (*IntSliceCmd)(nil)
2020-03-11 17:26:42 +03:00
func NewIntSliceCmd(ctx context.Context, args ...interface{}) *IntSliceCmd {
2019-06-26 14:45:38 +03:00
return &IntSliceCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2019-06-26 14:45:38 +03:00
}
}
func (cmd *IntSliceCmd) Val() []int64 {
return cmd.val
}
func (cmd *IntSliceCmd) Result() ([]int64, error) {
return cmd.val, cmd.err
}
func (cmd *IntSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *IntSliceCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]int64, n)
for i := 0; i < len(cmd.val); i++ {
if cmd.val[i], err = rd.ReadInt(); err != nil {
return err
2019-06-26 14:45:38 +03:00
}
}
return nil
2019-06-26 14:45:38 +03: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)
2020-03-11 17:26:42 +03:00
func NewDurationCmd(ctx context.Context, precision time.Duration, args ...interface{}) *DurationCmd {
return &DurationCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
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 {
n, err := rd.ReadInt()
2020-09-17 12:27:16 +03:00
if err != nil {
return err
}
2019-05-31 13:16:10 +03:00
switch n {
// -2 if the key does not exist
// -1 if the key exists but has no associated expire
case -2, -1:
cmd.val = time.Duration(n)
default:
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)
2020-03-11 17:26:42 +03:00
func NewTimeCmd(ctx context.Context, args ...interface{}) *TimeCmd {
2016-10-14 14:39:02 +03:00
return &TimeCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
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 {
if err := rd.ReadFixedArrayLen(2); err != nil {
return err
}
second, err := rd.ReadInt()
if err != nil {
return err
}
microsecond, err := rd.ReadInt()
if err != nil {
return err
}
cmd.val = time.Unix(second, microsecond*1000)
return nil
2018-08-02 14:48:46 +03:00
}
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)
2020-03-11 17:26:42 +03:00
func NewBoolCmd(ctx context.Context, args ...interface{}) *BoolCmd {
2017-01-13 14:39:59 +03:00
return &BoolCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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)
}
func (cmd *BoolCmd) readReply(rd *proto.Reader) (err error) {
cmd.val, err = rd.ReadBool()
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.
2020-09-17 12:27:16 +03:00
if err == Nil {
cmd.val = false
err = nil
}
return 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)
2020-03-11 17:26:42 +03:00
func NewStringCmd(ctx context.Context, args ...interface{}) *StringCmd {
2017-01-13 14:39:59 +03:00
return &StringCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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) {
return util.StringToBytes(cmd.val), cmd.err
2014-01-09 12:17:38 +04:00
}
2021-03-17 10:50:02 +03:00
func (cmd *StringCmd) Bool() (bool, error) {
if cmd.err != nil {
return false, cmd.err
}
return strconv.ParseBool(cmd.val)
}
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) Time() (time.Time, error) {
if cmd.err != nil {
return time.Time{}, cmd.err
}
return time.Parse(time.RFC3339Nano, cmd.Val())
}
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)
}
2020-09-17 12:27:16 +03:00
func (cmd *StringCmd) readReply(rd *proto.Reader) (err error) {
cmd.val, err = rd.ReadString()
return 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)
2020-03-11 17:26:42 +03:00
func NewFloatCmd(ctx context.Context, args ...interface{}) *FloatCmd {
2017-01-13 14:39:59 +03:00
return &FloatCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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
}
2020-09-17 12:27:16 +03:00
func (cmd *FloatCmd) readReply(rd *proto.Reader) (err error) {
cmd.val, err = rd.ReadFloat()
2020-09-17 12:27:16 +03:00
return err
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
2021-03-05 21:02:53 +03:00
type FloatSliceCmd struct {
baseCmd
val []float64
}
var _ Cmder = (*FloatSliceCmd)(nil)
func NewFloatSliceCmd(ctx context.Context, args ...interface{}) *FloatSliceCmd {
return &FloatSliceCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *FloatSliceCmd) Val() []float64 {
return cmd.val
}
func (cmd *FloatSliceCmd) Result() ([]float64, error) {
return cmd.val, cmd.err
}
func (cmd *FloatSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *FloatSliceCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]float64, n)
for i := 0; i < len(cmd.val); i++ {
switch num, err := rd.ReadFloat(); {
case err == Nil:
cmd.val[i] = 0
case err != nil:
return err
default:
cmd.val[i] = num
2021-03-05 21:02:53 +03:00
}
}
return nil
2021-03-05 21:02:53 +03:00
}
//------------------------------------------------------------------------------
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)
2020-03-11 17:26:42 +03:00
func NewStringSliceCmd(ctx context.Context, args ...interface{}) *StringSliceCmd {
2017-01-13 14:39:59 +03:00
return &StringSliceCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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 {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]string, n)
for i := 0; i < len(cmd.val); i++ {
switch s, err := rd.ReadString(); {
case err == Nil:
cmd.val[i] = ""
case err != nil:
return err
default:
cmd.val[i] = s
}
}
return nil
}
//------------------------------------------------------------------------------
type KeyValue struct {
Key string
Value string
}
type KeyValueSliceCmd struct {
baseCmd
val []KeyValue
}
var _ Cmder = (*KeyValueSliceCmd)(nil)
func NewKeyValueSliceCmd(ctx context.Context, args ...interface{}) *KeyValueSliceCmd {
return &KeyValueSliceCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *KeyValueSliceCmd) Val() []KeyValue {
return cmd.val
}
func (cmd *KeyValueSliceCmd) Result() ([]KeyValue, error) {
return cmd.val, cmd.err
}
func (cmd *KeyValueSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
// Many commands will respond to two formats:
// 1) 1) "one"
// 2) (double) 1
// 2) 1) "two"
// 2) (double) 2
// OR:
// 1) "two"
// 2) (double) 2
// 3) "one"
// 4) (double) 1
func (cmd *KeyValueSliceCmd) readReply(rd *proto.Reader) error { // nolint:dupl
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
// If the n is 0, can't continue reading.
if n == 0 {
cmd.val = make([]KeyValue, 0)
return nil
}
typ, err := rd.PeekReplyType()
if err != nil {
return err
}
array := typ == proto.RespArray
if array {
cmd.val = make([]KeyValue, n)
} else {
cmd.val = make([]KeyValue, n/2)
}
for i := 0; i < len(cmd.val); i++ {
if array {
if err = rd.ReadFixedArrayLen(2); err != nil {
return err
2019-06-26 14:45:38 +03:00
}
2018-08-02 14:48:46 +03:00
}
if cmd.val[i].Key, err = rd.ReadString(); err != nil {
return err
}
if cmd.val[i].Value, err = rd.ReadString(); err != nil {
return err
}
}
return nil
2018-08-02 14:48:46 +03:00
}
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)
2020-03-11 17:26:42 +03:00
func NewBoolSliceCmd(ctx context.Context, args ...interface{}) *BoolSliceCmd {
2017-01-13 14:39:59 +03:00
return &BoolSliceCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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 {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]bool, n)
for i := 0; i < len(cmd.val); i++ {
if cmd.val[i], err = rd.ReadBool(); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
}
return nil
2018-08-02 14:48:46 +03:00
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type MapStringStringCmd 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
}
var _ Cmder = (*MapStringStringCmd)(nil)
2017-09-26 11:29:22 +03:00
func NewMapStringStringCmd(ctx context.Context, args ...interface{}) *MapStringStringCmd {
return &MapStringStringCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
2013-09-29 12:06:49 +04:00
}
func (cmd *MapStringStringCmd) Val() map[string]string {
2014-01-09 12:17:38 +04:00
return cmd.val
2013-09-29 12:06:49 +04:00
}
func (cmd *MapStringStringCmd) Result() (map[string]string, error) {
2014-01-09 12:17:38 +04:00
return cmd.val, cmd.err
}
func (cmd *MapStringStringCmd) String() string {
2014-01-09 12:17:38 +04:00
return cmdString(cmd, cmd.val)
}
// Scan scans the results from the map into a destination struct. The map keys
// are matched in the Redis struct fields by the `redis:"field"` tag.
func (cmd *MapStringStringCmd) Scan(dst interface{}) error {
if cmd.err != nil {
return cmd.err
}
strct, err := hscan.Struct(dst)
if err != nil {
return err
}
for k, v := range cmd.val {
if err := strct.Scan(k, v); err != nil {
return err
}
}
return nil
}
func (cmd *MapStringStringCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadMapLen()
if err != nil {
return err
}
2013-09-29 12:06:49 +04:00
cmd.val = make(map[string]string, n)
for i := 0; i < n; i++ {
key, err := rd.ReadString()
if err != nil {
return err
}
2018-08-02 14:48:46 +03:00
value, err := rd.ReadString()
if err != nil {
return err
2018-08-02 14:48:46 +03:00
}
cmd.val[key] = value
}
return nil
2018-08-02 14:48:46 +03:00
}
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)
2020-03-11 17:26:42 +03:00
func NewStringIntMapCmd(ctx context.Context, args ...interface{}) *StringIntMapCmd {
2017-01-13 14:39:59 +03:00
return &StringIntMapCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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 {
n, err := rd.ReadMapLen()
if err != nil {
return err
}
2015-01-25 15:05:19 +03:00
cmd.val = make(map[string]int64, n)
for i := 0; i < n; i++ {
key, err := rd.ReadString()
if err != nil {
return err
}
2018-08-02 14:48:46 +03:00
nn, err := rd.ReadInt()
if err != nil {
return err
2018-08-02 14:48:46 +03:00
}
cmd.val[key] = nn
}
return nil
2018-08-02 14:48:46 +03:00
}
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)
2020-03-11 17:26:42 +03:00
func NewStringStructMapCmd(ctx context.Context, args ...interface{}) *StringStructMapCmd {
2017-11-19 19:36:23 +03:00
return &StringStructMapCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-11-19 19:36:23 +03:00
}
}
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 {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make(map[string]struct{}, n)
for i := 0; i < n; i++ {
key, err := rd.ReadString()
if err != nil {
return err
2018-08-02 14:48:46 +03:00
}
cmd.val[key] = struct{}{}
}
return 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
2020-03-11 17:26:42 +03:00
func NewXMessageSliceCmd(ctx context.Context, args ...interface{}) *XMessageSliceCmd {
2018-08-02 14:48:46 +03:00
return &XMessageSliceCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-11-25 05:06:13 +03:00
}
}
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)
}
func (cmd *XMessageSliceCmd) readReply(rd *proto.Reader) (err error) {
2020-09-24 09:06:17 +03:00
cmd.val, err = readXMessageSlice(rd)
return err
}
func readXMessageSlice(rd *proto.Reader) ([]XMessage, error) {
n, err := rd.ReadArrayLen()
2020-09-17 12:27:16 +03:00
if err != nil {
2020-09-24 09:06:17 +03:00
return nil, err
2017-11-25 05:06:13 +03:00
}
2020-09-24 09:06:17 +03:00
msgs := make([]XMessage, n)
for i := 0; i < len(msgs); i++ {
if msgs[i], err = readXMessage(rd); err != nil {
2020-09-24 09:06:17 +03:00
return nil, err
}
}
return msgs, nil
2017-11-25 05:06:13 +03:00
}
2020-09-22 20:45:23 +03:00
func readXMessage(rd *proto.Reader) (XMessage, error) {
if err := rd.ReadFixedArrayLen(2); err != nil {
2020-09-22 20:45:23 +03:00
return XMessage{}, err
}
2018-08-02 14:48:46 +03:00
2020-09-22 20:45:23 +03:00
id, err := rd.ReadString()
if err != nil {
return XMessage{}, err
}
2018-08-02 14:48:46 +03:00
v, err := stringInterfaceMapParser(rd)
2020-09-22 20:45:23 +03:00
if err != nil {
if err != proto.Nil {
return XMessage{}, err
}
}
return XMessage{
ID: id,
Values: v,
2020-09-22 20:45:23 +03:00
}, nil
}
func stringInterfaceMapParser(rd *proto.Reader) (map[string]interface{}, error) {
n, err := rd.ReadMapLen()
if err != nil {
return nil, err
}
m := make(map[string]interface{}, n)
for i := 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-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
2020-03-11 17:26:42 +03:00
func NewXStreamSliceCmd(ctx context.Context, args ...interface{}) *XStreamSliceCmd {
2018-08-02 14:48:46 +03:00
return &XStreamSliceCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-11-25 05:06:13 +03:00
}
}
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 {
typ, err := rd.PeekReplyType()
if err != nil {
return err
}
2018-08-02 14:48:46 +03:00
var n int
if typ == proto.RespMap {
n, err = rd.ReadMapLen()
} else {
n, err = rd.ReadArrayLen()
}
if err != nil {
return err
}
cmd.val = make([]XStream, n)
for i := 0; i < len(cmd.val); i++ {
if typ != proto.RespMap {
if err = rd.ReadFixedArrayLen(2); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
2017-11-25 05:06:13 +03:00
}
if cmd.val[i].Stream, err = rd.ReadString(); err != nil {
return err
}
if cmd.val[i].Messages, err = readXMessageSlice(rd); err != nil {
return err
}
}
return 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)
2020-03-11 17:26:42 +03:00
func NewXPendingCmd(ctx context.Context, args ...interface{}) *XPendingCmd {
2018-08-02 14:48:46 +03:00
return &XPendingCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2018-08-02 14:48:46 +03:00
}
}
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 {
var err error
if err = rd.ReadFixedArrayLen(4); err != nil {
return err
}
cmd.val = &XPending{}
2017-11-25 05:06:13 +03:00
if cmd.val.Count, err = rd.ReadInt(); err != nil {
return err
}
2017-11-25 05:06:13 +03:00
if cmd.val.Lower, err = rd.ReadString(); err != nil && err != Nil {
return err
}
2018-08-02 14:48:46 +03:00
if cmd.val.Higher, err = rd.ReadString(); err != nil && err != Nil {
return err
}
2018-08-02 14:48:46 +03:00
n, err := rd.ReadArrayLen()
if err != nil && err != Nil {
return err
}
cmd.val.Consumers = make(map[string]int64, n)
for i := 0; i < n; i++ {
if err = rd.ReadFixedArrayLen(2); err != nil {
return err
2019-06-26 14:45:38 +03:00
}
consumerName, err := rd.ReadString()
if err != nil {
return err
2017-11-25 05:06:13 +03:00
}
consumerPending, err := rd.ReadInt()
if err != nil {
return err
}
cmd.val.Consumers[consumerName] = consumerPending
}
return nil
2018-08-02 14:48:46 +03:00
}
//------------------------------------------------------------------------------
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)
2020-03-11 17:26:42 +03:00
func NewXPendingExtCmd(ctx context.Context, args ...interface{}) *XPendingExtCmd {
2018-08-02 14:48:46 +03:00
return &XPendingExtCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2018-08-02 14:48:46 +03:00
}
}
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 {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]XPendingExt, n)
2018-08-02 14:48:46 +03:00
for i := 0; i < len(cmd.val); i++ {
if err = rd.ReadFixedArrayLen(4); err != nil {
return err
}
2018-08-02 14:48:46 +03:00
if cmd.val[i].ID, err = rd.ReadString(); err != nil {
return err
}
2018-08-02 14:48:46 +03:00
if cmd.val[i].Consumer, err = rd.ReadString(); err != nil && err != Nil {
return err
}
2018-08-02 14:48:46 +03:00
idle, err := rd.ReadInt()
if err != nil && err != Nil {
return err
}
cmd.val[i].Idle = time.Duration(idle) * time.Millisecond
2018-08-02 14:48:46 +03:00
if cmd.val[i].RetryCount, err = rd.ReadInt(); err != nil && err != Nil {
return err
2017-11-25 05:06:13 +03:00
}
}
return nil
2017-11-25 05:06:13 +03:00
}
//------------------------------------------------------------------------------
type XInfoConsumersCmd struct {
baseCmd
val []XInfoConsumer
}
type XInfoConsumer struct {
Name string
Pending int64
Idle int64
}
var _ Cmder = (*XInfoConsumersCmd)(nil)
func NewXInfoConsumersCmd(ctx context.Context, stream string, group string) *XInfoConsumersCmd {
return &XInfoConsumersCmd{
baseCmd: baseCmd{
ctx: ctx,
args: []interface{}{"xinfo", "consumers", stream, group},
},
}
}
func (cmd *XInfoConsumersCmd) Val() []XInfoConsumer {
return cmd.val
}
func (cmd *XInfoConsumersCmd) Result() ([]XInfoConsumer, error) {
return cmd.val, cmd.err
}
func (cmd *XInfoConsumersCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *XInfoConsumersCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]XInfoConsumer, n)
for i := 0; i < len(cmd.val); i++ {
if err = rd.ReadFixedMapLen(3); err != nil {
return err
}
var key string
for f := 0; f < 3; f++ {
key, err = rd.ReadString()
if err != nil {
return err
}
switch key {
case "name":
cmd.val[i].Name, err = rd.ReadString()
case "pending":
cmd.val[i].Pending, err = rd.ReadInt()
case "idle":
cmd.val[i].Idle, err = rd.ReadInt()
default:
return fmt.Errorf("redis: unexpected content %s in XINFO CONSUMERS reply", key)
}
if err != nil {
return err
}
}
}
return nil
}
//------------------------------------------------------------------------------
type XInfoGroupsCmd struct {
baseCmd
2020-09-23 11:00:34 +03:00
val []XInfoGroup
}
2020-09-23 11:00:34 +03:00
type XInfoGroup struct {
Name string
Consumers int64
Pending int64
LastDeliveredID string
}
var _ Cmder = (*XInfoGroupsCmd)(nil)
2020-03-11 17:26:42 +03:00
func NewXInfoGroupsCmd(ctx context.Context, stream string) *XInfoGroupsCmd {
return &XInfoGroupsCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: []interface{}{"xinfo", "groups", stream},
},
}
}
2020-09-23 11:00:34 +03:00
func (cmd *XInfoGroupsCmd) Val() []XInfoGroup {
return cmd.val
}
2020-09-23 11:00:34 +03:00
func (cmd *XInfoGroupsCmd) Result() ([]XInfoGroup, error) {
return cmd.val, cmd.err
}
func (cmd *XInfoGroupsCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *XInfoGroupsCmd) readReply(rd *proto.Reader) error {
2020-09-23 11:00:34 +03:00
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]XInfoGroup, n)
for i := 0; i < len(cmd.val); i++ {
if err = rd.ReadFixedMapLen(4); err != nil {
2020-09-23 11:00:34 +03:00
return err
2020-09-17 12:27:16 +03:00
}
var key string
for f := 0; f < 4; f++ {
key, err = rd.ReadString()
2020-09-23 11:00:34 +03:00
if err != nil {
return err
}
switch key {
case "name":
cmd.val[i].Name, err = rd.ReadString()
case "consumers":
cmd.val[i].Consumers, err = rd.ReadInt()
case "pending":
cmd.val[i].Pending, err = rd.ReadInt()
case "last-delivered-id":
cmd.val[i].LastDeliveredID, err = rd.ReadString()
default:
return fmt.Errorf("redis: unexpected content %s in XINFO GROUPS reply", key)
2020-09-23 11:00:34 +03:00
}
if err != nil {
return err
2020-09-23 11:00:34 +03:00
}
}
}
2020-09-23 11:00:34 +03:00
return nil
}
//------------------------------------------------------------------------------
2020-09-22 20:45:23 +03:00
type XInfoStreamCmd struct {
baseCmd
val *XInfoStream
}
type XInfoStream struct {
Length int64
RadixTreeKeys int64
RadixTreeNodes int64
Groups int64
LastGeneratedID string
FirstEntry XMessage
LastEntry XMessage
}
var _ Cmder = (*XInfoStreamCmd)(nil)
func NewXInfoStreamCmd(ctx context.Context, stream string) *XInfoStreamCmd {
return &XInfoStreamCmd{
baseCmd: baseCmd{
ctx: ctx,
args: []interface{}{"xinfo", "stream", stream},
},
}
}
func (cmd *XInfoStreamCmd) Val() *XInfoStream {
return cmd.val
}
func (cmd *XInfoStreamCmd) Result() (*XInfoStream, error) {
return cmd.val, cmd.err
}
func (cmd *XInfoStreamCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *XInfoStreamCmd) readReply(rd *proto.Reader) error {
if err := rd.ReadFixedMapLen(7); err != nil {
2020-09-22 20:45:23 +03:00
return err
}
cmd.val = &XInfoStream{}
2020-09-22 20:45:23 +03:00
for i := 0; i < 7; i++ {
key, err := rd.ReadString()
if err != nil {
return err
2020-09-22 20:45:23 +03:00
}
switch key {
case "length":
cmd.val.Length, err = rd.ReadInt()
2020-09-22 20:45:23 +03:00
case "radix-tree-keys":
cmd.val.RadixTreeKeys, err = rd.ReadInt()
2020-09-22 20:45:23 +03:00
case "radix-tree-nodes":
cmd.val.RadixTreeNodes, err = rd.ReadInt()
2020-09-22 20:45:23 +03:00
case "groups":
cmd.val.Groups, err = rd.ReadInt()
2020-09-22 20:45:23 +03:00
case "last-generated-id":
cmd.val.LastGeneratedID, err = rd.ReadString()
2020-09-22 20:45:23 +03:00
case "first-entry":
cmd.val.FirstEntry, err = readXMessage(rd)
if err == Nil {
err = nil
}
2020-09-22 20:45:23 +03:00
case "last-entry":
cmd.val.LastEntry, err = readXMessage(rd)
if err == Nil {
err = nil
}
2020-09-22 20:45:23 +03:00
default:
return fmt.Errorf("redis: unexpected content %s "+
2020-09-22 20:45:23 +03:00
"in XINFO STREAM reply", key)
}
if err != nil {
return err
2020-09-22 20:45:23 +03:00
}
}
return nil
2020-09-22 20:45:23 +03:00
}
//------------------------------------------------------------------------------
type XInfoStreamFullCmd struct {
baseCmd
val *XInfoStreamFull
}
type XInfoStreamFull struct {
Length int64
RadixTreeKeys int64
RadixTreeNodes int64
LastGeneratedID string
Entries []XMessage
Groups []XInfoStreamGroup
}
type XInfoStreamGroup struct {
Name string
LastDeliveredID string
PelCount int64
Pending []XInfoStreamGroupPending
Consumers []XInfoStreamConsumer
}
type XInfoStreamGroupPending struct {
ID string
Consumer string
DeliveryTime time.Time
DeliveryCount int64
}
type XInfoStreamConsumer struct {
Name string
SeenTime time.Time
PelCount int64
Pending []XInfoStreamConsumerPending
}
type XInfoStreamConsumerPending struct {
ID string
DeliveryTime time.Time
DeliveryCount int64
}
var _ Cmder = (*XInfoStreamFullCmd)(nil)
func NewXInfoStreamFullCmd(ctx context.Context, args ...interface{}) *XInfoStreamFullCmd {
return &XInfoStreamFullCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *XInfoStreamFullCmd) Val() *XInfoStreamFull {
return cmd.val
}
func (cmd *XInfoStreamFullCmd) Result() (*XInfoStreamFull, error) {
return cmd.val, cmd.err
}
func (cmd *XInfoStreamFullCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *XInfoStreamFullCmd) readReply(rd *proto.Reader) error {
if err := rd.ReadFixedMapLen(6); err != nil {
return err
}
cmd.val = &XInfoStreamFull{}
for i := 0; i < 6; i++ {
key, err := rd.ReadString()
if err != nil {
return err
}
switch key {
case "length":
cmd.val.Length, err = rd.ReadInt()
case "radix-tree-keys":
cmd.val.RadixTreeKeys, err = rd.ReadInt()
case "radix-tree-nodes":
cmd.val.RadixTreeNodes, err = rd.ReadInt()
case "last-generated-id":
cmd.val.LastGeneratedID, err = rd.ReadString()
case "entries":
cmd.val.Entries, err = readXMessageSlice(rd)
case "groups":
cmd.val.Groups, err = readStreamGroups(rd)
default:
return fmt.Errorf("redis: unexpected content %s "+
"in XINFO STREAM FULL reply", key)
}
if err != nil {
return err
}
}
return nil
}
func readStreamGroups(rd *proto.Reader) ([]XInfoStreamGroup, error) {
n, err := rd.ReadArrayLen()
if err != nil {
return nil, err
}
groups := make([]XInfoStreamGroup, 0, n)
for i := 0; i < n; i++ {
if err = rd.ReadFixedMapLen(5); err != nil {
return nil, err
}
group := XInfoStreamGroup{}
for f := 0; f < 5; f++ {
key, err := rd.ReadString()
if err != nil {
return nil, err
}
switch key {
case "name":
group.Name, err = rd.ReadString()
case "last-delivered-id":
group.LastDeliveredID, err = rd.ReadString()
case "pel-count":
group.PelCount, err = rd.ReadInt()
case "pending":
group.Pending, err = readXInfoStreamGroupPending(rd)
case "consumers":
group.Consumers, err = readXInfoStreamConsumers(rd)
default:
return nil, fmt.Errorf("redis: unexpected content %s "+
"in XINFO STREAM FULL reply", key)
}
if err != nil {
return nil, err
}
}
groups = append(groups, group)
}
return groups, nil
}
func readXInfoStreamGroupPending(rd *proto.Reader) ([]XInfoStreamGroupPending, error) {
n, err := rd.ReadArrayLen()
if err != nil {
return nil, err
}
pending := make([]XInfoStreamGroupPending, 0, n)
for i := 0; i < n; i++ {
if err = rd.ReadFixedArrayLen(4); err != nil {
return nil, err
}
p := XInfoStreamGroupPending{}
p.ID, err = rd.ReadString()
if err != nil {
return nil, err
}
p.Consumer, err = rd.ReadString()
if err != nil {
return nil, err
}
delivery, err := rd.ReadInt()
if err != nil {
return nil, err
}
p.DeliveryTime = time.Unix(delivery/1000, delivery%1000*int64(time.Millisecond))
p.DeliveryCount, err = rd.ReadInt()
if err != nil {
return nil, err
}
pending = append(pending, p)
}
return pending, nil
}
func readXInfoStreamConsumers(rd *proto.Reader) ([]XInfoStreamConsumer, error) {
n, err := rd.ReadArrayLen()
if err != nil {
return nil, err
}
consumers := make([]XInfoStreamConsumer, 0, n)
for i := 0; i < n; i++ {
if err = rd.ReadFixedMapLen(4); err != nil {
return nil, err
}
c := XInfoStreamConsumer{}
for f := 0; f < 4; f++ {
cKey, err := rd.ReadString()
if err != nil {
return nil, err
}
switch cKey {
case "name":
c.Name, err = rd.ReadString()
case "seen-time":
seen, err := rd.ReadInt()
if err != nil {
return nil, err
}
c.SeenTime = time.Unix(seen/1000, seen%1000*int64(time.Millisecond))
case "pel-count":
c.PelCount, err = rd.ReadInt()
case "pending":
pendingNumber, err := rd.ReadArrayLen()
if err != nil {
return nil, err
}
c.Pending = make([]XInfoStreamConsumerPending, 0, pendingNumber)
for pn := 0; pn < pendingNumber; pn++ {
if err = rd.ReadFixedArrayLen(3); err != nil {
return nil, err
}
p := XInfoStreamConsumerPending{}
p.ID, err = rd.ReadString()
if err != nil {
return nil, err
}
delivery, err := rd.ReadInt()
if err != nil {
return nil, err
}
p.DeliveryTime = time.Unix(delivery/1000, delivery%1000*int64(time.Millisecond))
p.DeliveryCount, err = rd.ReadInt()
if err != nil {
return nil, err
}
c.Pending = append(c.Pending, p)
}
default:
return nil, fmt.Errorf("redis: unexpected content %s "+
"in XINFO STREAM FULL reply", cKey)
}
if err != nil {
return nil, err
}
}
consumers = append(consumers, c)
}
return consumers, nil
}
//------------------------------------------------------------------------------
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)
2020-03-11 17:26:42 +03:00
func NewZSliceCmd(ctx context.Context, args ...interface{}) *ZSliceCmd {
2017-01-13 14:39:59 +03:00
return &ZSliceCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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)
}
func (cmd *ZSliceCmd) readReply(rd *proto.Reader) error { // nolint:dupl
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
// If the n is 0, can't continue reading.
if n == 0 {
cmd.val = make([]Z, 0)
return nil
}
typ, err := rd.PeekReplyType()
if err != nil {
return err
}
array := typ == proto.RespArray
if array {
cmd.val = make([]Z, n)
} else {
2019-06-26 14:45:38 +03:00
cmd.val = make([]Z, n/2)
}
2018-08-02 14:48:46 +03:00
for i := 0; i < len(cmd.val); i++ {
if array {
if err = rd.ReadFixedArrayLen(2); err != nil {
return err
2019-06-26 14:45:38 +03:00
}
}
2018-08-02 14:48:46 +03:00
if cmd.val[i].Member, err = rd.ReadString(); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
if cmd.val[i].Score, err = rd.ReadFloat(); err != nil {
return err
}
}
return nil
2018-08-02 14:48:46 +03:00
}
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)
2020-03-11 17:26:42 +03:00
func NewZWithKeyCmd(ctx context.Context, args ...interface{}) *ZWithKeyCmd {
2018-10-31 16:35:23 +03:00
return &ZWithKeyCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2018-10-31 16:35:23 +03:00
}
}
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) (err error) {
if err = rd.ReadFixedArrayLen(3); err != nil {
return err
}
cmd.val = &ZWithKey{}
2019-05-16 16:27:19 +03:00
if cmd.val.Key, err = rd.ReadString(); err != nil {
return err
}
if cmd.val.Member, err = rd.ReadString(); err != nil {
return err
}
if cmd.val.Score, err = rd.ReadFloat(); err != nil {
return err
}
2019-06-26 14:45:38 +03:00
return 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
2020-03-11 17:26:42 +03:00
process cmdable
2014-01-09 12:17:38 +04:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*ScanCmd)(nil)
2020-03-11 17:26:42 +03:00
func NewScanCmd(ctx context.Context, process cmdable, args ...interface{}) *ScanCmd {
2016-04-13 11:52:47 +03:00
return &ScanCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
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
}
func (cmd *ScanCmd) readReply(rd *proto.Reader) error {
if err := rd.ReadFixedArrayLen(2); err != nil {
return err
}
cursor, err := rd.ReadInt()
if err != nil {
return err
}
cmd.cursor = uint64(cursor)
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.page = make([]string, n)
for i := 0; i < len(cmd.page); i++ {
if cmd.page[i], err = rd.ReadString(); err != nil {
return err
}
}
return nil
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 {
2019-07-25 13:53:00 +03:00
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)
2020-03-11 17:26:42 +03:00
func NewClusterSlotsCmd(ctx context.Context, args ...interface{}) *ClusterSlotsCmd {
2017-01-13 14:39:59 +03:00
return &ClusterSlotsCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
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 {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]ClusterSlot, n)
for i := 0; i < len(cmd.val); i++ {
n, err = rd.ReadArrayLen()
if err != nil {
return err
}
if n < 2 {
return fmt.Errorf("redis: got %d elements in cluster info, expected at least 2", n)
}
start, err := rd.ReadInt()
if err != nil {
return err
}
end, err := rd.ReadInt()
if err != nil {
return err
}
// subtract start and end.
nodes := make([]ClusterNode, n-2)
for j := 0; j < len(nodes); j++ {
nn, err := rd.ReadArrayLen()
2018-08-02 14:48:46 +03:00
if err != nil {
return err
2018-08-02 14:48:46 +03:00
}
if nn != 2 && nn != 3 {
return fmt.Errorf("got %d elements in cluster info address, expected 2 or 3", nn)
2018-08-02 14:48:46 +03:00
}
ip, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return err
2018-08-02 14:48:46 +03:00
}
port, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return err
2018-08-02 14:48:46 +03:00
}
2018-08-16 13:25:19 +03:00
nodes[j].Addr = net.JoinHostPort(ip, port)
2018-08-02 14:48:46 +03:00
if nn == 3 {
id, err := rd.ReadString()
2018-08-02 14:48:46 +03:00
if err != nil {
return err
2019-06-26 14:45:38 +03:00
}
nodes[j].ID = id
2019-06-26 14:45:38 +03:00
}
2018-08-02 14:48:46 +03:00
}
cmd.val[i] = ClusterSlot{
Start: int(start),
End: int(end),
Nodes: nodes,
}
}
return nil
2018-08-02 14:48:46 +03:00
}
//------------------------------------------------------------------------------
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
// WithCoord+WithDist+WithGeoHash
withLen int
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)
2020-03-11 17:26:42 +03:00
func NewGeoLocationCmd(ctx context.Context, q *GeoRadiusQuery, args ...interface{}) *GeoLocationCmd {
return &GeoLocationCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: geoLocationArgs(q, args...),
},
q: q,
}
}
func geoLocationArgs(q *GeoRadiusQuery, args ...interface{}) []interface{} {
2015-11-14 17:36:21 +03:00
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")
q.withLen++
2015-11-14 17:36:21 +03:00
}
if q.WithDist {
2017-07-19 15:32:50 +03:00
args = append(args, "withdist")
q.withLen++
2015-11-14 17:36:21 +03:00
}
if q.WithGeoHash {
2017-07-19 15:32:50 +03:00
args = append(args, "withhash")
q.withLen++
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)
}
return args
}
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 {
n, err := rd.ReadArrayLen()
2020-09-17 12:27:16 +03:00
if err != nil {
return err
}
cmd.locations = make([]GeoLocation, n)
for i := 0; i < len(cmd.locations); i++ {
// only name
if cmd.q.withLen == 0 {
if cmd.locations[i].Name, err = rd.ReadString(); err != nil {
return err
2019-06-26 14:45:38 +03:00
}
continue
2019-06-26 14:45:38 +03:00
}
// +name
if err = rd.ReadFixedArrayLen(cmd.q.withLen + 1); err != nil {
return err
}
2018-08-02 14:48:46 +03:00
if cmd.locations[i].Name, err = rd.ReadString(); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
if cmd.q.WithDist {
if cmd.locations[i].Dist, err = rd.ReadFloat(); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
}
if cmd.q.WithGeoHash {
if cmd.locations[i].GeoHash, err = rd.ReadInt(); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
}
if cmd.q.WithCoord {
if err = rd.ReadFixedArrayLen(2); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
if cmd.locations[i].Longitude, err = rd.ReadFloat(); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
if cmd.locations[i].Latitude, err = rd.ReadFloat(); err != nil {
return err
2018-08-02 14:48:46 +03:00
}
}
}
return nil
2018-08-02 14:48:46 +03:00
}
//------------------------------------------------------------------------------
type GeoPos struct {
Longitude, Latitude float64
}
2016-08-22 00:32:06 +03:00
type GeoPosCmd struct {
baseCmd
2019-06-26 14:45:38 +03:00
val []*GeoPos
2016-08-22 00:32:06 +03:00
}
2017-09-26 11:29:22 +03:00
var _ Cmder = (*GeoPosCmd)(nil)
2020-03-11 17:26:42 +03:00
func NewGeoPosCmd(ctx context.Context, args ...interface{}) *GeoPosCmd {
2017-01-13 14:39:59 +03:00
return &GeoPosCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
2016-08-22 00:32:06 +03:00
}
func (cmd *GeoPosCmd) Val() []*GeoPos {
2019-06-26 14:45:38 +03:00
return cmd.val
2016-08-22 00:32:06 +03:00
}
func (cmd *GeoPosCmd) Result() ([]*GeoPos, error) {
2016-08-22 00:32:06 +03:00
return cmd.Val(), cmd.Err()
}
func (cmd *GeoPosCmd) String() string {
2019-06-26 14:45:38 +03:00
return cmdString(cmd, cmd.val)
2016-08-22 00:32:06 +03:00
}
2018-08-17 13:56:37 +03:00
func (cmd *GeoPosCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]*GeoPos, n)
2019-05-16 16:27:19 +03:00
for i := 0; i < len(cmd.val); i++ {
err = rd.ReadFixedArrayLen(2)
if err != nil {
if err == Nil {
cmd.val[i] = nil
continue
2019-05-16 16:27:19 +03:00
}
return err
2018-08-02 14:48:46 +03:00
}
longitude, err := rd.ReadFloat()
if err != nil {
return err
}
latitude, err := rd.ReadFloat()
if err != nil {
return err
}
cmd.val[i] = &GeoPos{
Longitude: longitude,
Latitude: latitude,
}
}
return nil
2018-08-02 14:48:46 +03:00
}
//------------------------------------------------------------------------------
type CommandInfo struct {
Name string
Arity int8
Flags []string
ACLFlags []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)
2020-03-11 17:26:42 +03:00
func NewCommandsInfoCmd(ctx context.Context, args ...interface{}) *CommandsInfoCmd {
2017-01-13 14:39:59 +03:00
return &CommandsInfoCmd{
2020-03-11 17:26:42 +03:00
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
2017-01-13 14:39:59 +03:00
}
}
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 {
2020-06-05 09:11:12 +03:00
const numArgRedis5 = 6
const numArgRedis6 = 7
n, err := rd.ReadArrayLen()
if err != nil {
return err
2018-08-02 14:48:46 +03:00
}
cmd.val = make(map[string]*CommandInfo, n)
2018-08-02 14:48:46 +03:00
for i := 0; i < n; i++ {
nn, err := rd.ReadArrayLen()
if err != nil {
return err
}
if nn != numArgRedis5 && nn != numArgRedis6 {
return fmt.Errorf("redis: got %d elements in COMMAND reply, wanted 6/7", nn)
}
2019-06-26 14:45:38 +03:00
cmdInfo := &CommandInfo{}
if cmdInfo.Name, err = rd.ReadString(); err != nil {
return err
}
2018-08-02 14:48:46 +03:00
arity, err := rd.ReadInt()
if err != nil {
return err
}
cmdInfo.Arity = int8(arity)
2018-08-02 14:48:46 +03:00
flagLen, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmdInfo.Flags = make([]string, flagLen)
for f := 0; f < len(cmdInfo.Flags); f++ {
2019-06-26 14:45:38 +03:00
switch s, err := rd.ReadString(); {
case err == Nil:
cmdInfo.Flags[f] = ""
2019-06-26 14:45:38 +03:00
case err != nil:
return err
2019-06-26 14:45:38 +03:00
default:
if !cmdInfo.ReadOnly && s == "readonly" {
cmdInfo.ReadOnly = true
}
cmdInfo.Flags[f] = s
2019-06-26 14:45:38 +03:00
}
}
2018-08-02 14:48:46 +03:00
firstKeyPos, err := rd.ReadInt()
if err != nil {
return err
}
cmdInfo.FirstKeyPos = int8(firstKeyPos)
2018-08-02 14:48:46 +03:00
lastKeyPos, err := rd.ReadInt()
if err != nil {
return err
2020-06-05 09:11:12 +03:00
}
cmdInfo.LastKeyPos = int8(lastKeyPos)
2020-06-05 09:11:12 +03:00
stepCount, err := rd.ReadInt()
if err != nil {
return err
}
cmdInfo.StepCount = int8(stepCount)
2020-06-05 09:11:12 +03:00
if nn == numArgRedis6 {
aclFlagLen, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmdInfo.ACLFlags = make([]string, aclFlagLen)
for f := 0; f < len(cmdInfo.ACLFlags); f++ {
switch s, err := rd.ReadString(); {
case err == Nil:
cmdInfo.ACLFlags[f] = ""
case err != nil:
return err
default:
cmdInfo.ACLFlags[f] = s
}
}
}
cmd.val[cmdInfo.Name] = cmdInfo
}
return nil
2018-08-02 14:48:46 +03:00
}
2018-03-06 15:50:48 +03:00
//------------------------------------------------------------------------------
type cmdsInfoCache struct {
fn func(ctx context.Context) (map[string]*CommandInfo, error)
2018-05-17 15:21:51 +03:00
2018-03-06 15:50:48 +03:00
once internal.Once
cmds map[string]*CommandInfo
}
func newCmdsInfoCache(fn func(ctx context.Context) (map[string]*CommandInfo, error)) *cmdsInfoCache {
2018-05-17 15:21:51 +03:00
return &cmdsInfoCache{
fn: fn,
}
2018-03-06 15:50:48 +03:00
}
func (c *cmdsInfoCache) Get(ctx context.Context) (map[string]*CommandInfo, error) {
2018-03-06 15:50:48 +03:00
err := c.once.Do(func() error {
cmds, err := c.fn(ctx)
2018-03-06 15:50:48 +03:00
if err != nil {
return err
}
2019-11-19 13:37:26 +03:00
// Extensions have cmd names in upper case. Convert them to lower case.
for k, v := range cmds {
lower := internal.ToLower(k)
if lower != k {
cmds[lower] = v
}
}
2019-11-19 13:37:26 +03:00
2018-03-06 15:50:48 +03:00
c.cmds = cmds
return nil
})
return c.cmds, err
}
2020-06-11 10:24:04 +03:00
//------------------------------------------------------------------------------
type SlowLog struct {
2020-09-09 12:49:45 +03:00
ID int64
Time time.Time
Duration time.Duration
Args []string
// These are also optional fields emitted only by Redis 4.0 or greater:
// https://redis.io/commands/slowlog#output-format
ClientAddr string
ClientName string
2020-06-11 10:24:04 +03:00
}
type SlowLogCmd struct {
baseCmd
val []SlowLog
}
var _ Cmder = (*SlowLogCmd)(nil)
func NewSlowLogCmd(ctx context.Context, args ...interface{}) *SlowLogCmd {
return &SlowLogCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *SlowLogCmd) Val() []SlowLog {
return cmd.val
}
func (cmd *SlowLogCmd) Result() ([]SlowLog, error) {
return cmd.Val(), cmd.Err()
}
func (cmd *SlowLogCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *SlowLogCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]SlowLog, n)
for i := 0; i < len(cmd.val); i++ {
nn, err := rd.ReadArrayLen()
if err != nil {
return err
}
if nn < 4 {
return fmt.Errorf("redis: got %d elements in slowlog get, expected at least 4", nn)
}
if cmd.val[i].ID, err = rd.ReadInt(); err != nil {
return err
}
createdAt, err := rd.ReadInt()
if err != nil {
return err
}
cmd.val[i].Time = time.Unix(createdAt, 0)
costs, err := rd.ReadInt()
if err != nil {
return err
}
cmd.val[i].Duration = time.Duration(costs) * time.Microsecond
2020-06-11 10:24:04 +03:00
cmdLen, err := rd.ReadArrayLen()
if err != nil {
return err
}
if cmdLen < 1 {
return fmt.Errorf("redis: got %d elements commands reply in slowlog get, expected at least 1", cmdLen)
}
cmd.val[i].Args = make([]string, cmdLen)
for f := 0; f < len(cmd.val[i].Args); f++ {
cmd.val[i].Args[f], err = rd.ReadString()
2020-06-11 10:24:04 +03:00
if err != nil {
return err
2020-06-11 10:24:04 +03:00
}
}
2020-06-11 10:24:04 +03:00
if nn >= 5 {
if cmd.val[i].ClientAddr, err = rd.ReadString(); err != nil {
return err
2020-06-11 10:24:04 +03:00
}
}
2020-09-09 12:49:45 +03:00
if nn >= 6 {
if cmd.val[i].ClientName, err = rd.ReadString(); err != nil {
return err
2020-06-11 10:24:04 +03:00
}
}
}
2020-09-09 12:49:45 +03:00
return nil
}
//-----------------------------------------------------------------------
type MapStringInterfaceCmd struct {
baseCmd
val map[string]interface{}
}
var _ Cmder = (*MapStringInterfaceCmd)(nil)
func NewMapStringInterfaceCmd(ctx context.Context, args ...interface{}) *MapStringInterfaceCmd {
return &MapStringInterfaceCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *MapStringInterfaceCmd) Val() map[string]interface{} {
return cmd.val
}
func (cmd *MapStringInterfaceCmd) Result() (map[string]interface{}, error) {
return cmd.Val(), cmd.Err()
}
func (cmd *MapStringInterfaceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *MapStringInterfaceCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadMapLen()
if err != nil {
return err
}
cmd.val = make(map[string]interface{}, n)
for i := 0; i < n; i++ {
k, err := rd.ReadString()
if err != nil {
return err
}
v, err := rd.ReadReply()
if err != nil {
if err == Nil {
cmd.val[k] = Nil
continue
2020-06-11 10:24:04 +03:00
}
if err, ok := err.(proto.RedisError); ok {
cmd.val[k] = err
continue
2020-06-11 10:24:04 +03:00
}
return err
}
cmd.val[k] = v
}
return nil
}
2020-09-09 12:49:45 +03:00
//-----------------------------------------------------------------------
2020-09-09 12:49:45 +03:00
type MapStringStringSliceCmd struct {
baseCmd
val []map[string]string
}
var _ Cmder = (*MapStringStringSliceCmd)(nil)
func NewMapStringStringSliceCmd(ctx context.Context, args ...interface{}) *MapStringStringSliceCmd {
return &MapStringStringSliceCmd{
baseCmd: baseCmd{
ctx: ctx,
args: args,
},
}
}
func (cmd *MapStringStringSliceCmd) Val() []map[string]string {
return cmd.val
}
func (cmd *MapStringStringSliceCmd) Result() ([]map[string]string, error) {
return cmd.Val(), cmd.Err()
}
func (cmd *MapStringStringSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *MapStringStringSliceCmd) readReply(rd *proto.Reader) error {
n, err := rd.ReadArrayLen()
if err != nil {
return err
}
cmd.val = make([]map[string]string, n)
for i := 0; i < n; i++ {
nn, err := rd.ReadMapLen()
if err != nil {
return err
}
cmd.val[i] = make(map[string]string, nn)
for f := 0; f < nn; f++ {
k, err := rd.ReadString()
if err != nil {
return err
2020-06-11 10:24:04 +03:00
}
2020-09-09 12:49:45 +03:00
v, err := rd.ReadString()
if err != nil {
return err
2020-06-11 10:24:04 +03:00
}
cmd.val[i][k] = v
2020-06-11 10:24:04 +03:00
}
}
return nil
2020-06-11 10:24:04 +03:00
}