redis/command.go

971 lines
18 KiB
Go
Raw Normal View History

2013-09-29 12:06:49 +04:00
package redis
import (
"bytes"
2013-09-29 12:06:49 +04:00
"fmt"
"strconv"
"strings"
"time"
2017-02-18 17:42:34 +03:00
"github.com/go-redis/redis/internal"
"github.com/go-redis/redis/internal/pool"
"github.com/go-redis/redis/internal/proto"
2013-09-29 12:06:49 +04:00
)
2014-01-09 12:17:38 +04:00
var (
_ Cmder = (*Cmd)(nil)
_ Cmder = (*SliceCmd)(nil)
_ Cmder = (*StatusCmd)(nil)
_ Cmder = (*IntCmd)(nil)
_ Cmder = (*DurationCmd)(nil)
_ Cmder = (*BoolCmd)(nil)
_ Cmder = (*StringCmd)(nil)
_ Cmder = (*FloatCmd)(nil)
_ Cmder = (*StringSliceCmd)(nil)
_ Cmder = (*BoolSliceCmd)(nil)
_ Cmder = (*StringStringMapCmd)(nil)
2015-01-25 15:05:19 +03:00
_ Cmder = (*StringIntMapCmd)(nil)
2014-07-05 14:46:27 +04:00
_ Cmder = (*ZSliceCmd)(nil)
2014-01-09 12:17:38 +04:00
_ Cmder = (*ScanCmd)(nil)
_ Cmder = (*ClusterSlotsCmd)(nil)
2014-01-09 12:17:38 +04:00
)
2013-09-29 12:06:49 +04:00
type Cmder interface {
args() []interface{}
arg(int) string
2017-05-09 12:44:36 +03:00
Name() string
2016-06-17 15:09:38 +03:00
readReply(*pool.Conn) error
2013-09-29 12:06:49 +04:00
readTimeout() *time.Duration
Err() error
2017-09-05 09:14:16 +03:00
SetErr(error)
fmt.Stringer
2013-09-29 12:06:49 +04:00
}
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 {
2017-09-05 09:14:16 +03:00
cmd.SetErr(e)
2017-08-31 15:22:47 +03:00
}
}
}
func firstCmdsErr(cmds []Cmder) error {
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 writeCmd(cn *pool.Conn, cmds ...Cmder) error {
2016-07-02 15:52:10 +03:00
cn.Wb.Reset()
for _, cmd := range cmds {
2016-07-02 15:52:10 +03:00
if err := cn.Wb.Append(cmd.args()); err != nil {
return err
}
}
_, err := cn.Write(cn.Wb.Bytes())
return err
}
2014-01-09 12:17:38 +04:00
func cmdString(cmd Cmder, val interface{}) string {
var ss []string
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":
if cmd.arg(2) != "0" {
return 3
} else {
return -1
}
2017-04-12 13:00:20 +03:00
case "publish":
return 1
}
if info == nil {
return -1
}
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
}
func (cmd *baseCmd) Err() error {
return cmd.err
2013-09-29 12:06:49 +04:00
}
func (cmd *baseCmd) args() []interface{} {
2013-09-29 12:06:49 +04:00
return cmd._args
}
func (cmd *baseCmd) arg(pos int) string {
if pos < 0 || pos >= len(cmd._args) {
return ""
}
s, _ := cmd._args[pos].(string)
return s
}
2017-05-09 12:44:36 +03:00
func (cmd *baseCmd) Name() string {
2017-01-13 14:39:59 +03:00
if len(cmd._args) > 0 {
// Cmd name must be lower cased.
s := internal.ToLower(cmd.arg(0))
cmd._args[0] = s
return s
}
return ""
}
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
}
2017-09-05 09:14:16 +03:00
func (cmd *baseCmd) SetErr(e error) {
2014-01-09 12:17:38 +04:00
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
}
func (cmd *Cmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *Cmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
cmd.val, cmd.err = cn.Rd.ReadReply(sliceParser)
if cmd.err != nil {
2015-10-07 17:09:20 +03:00
return cmd.err
}
2016-12-13 18:28:39 +03:00
if b, ok := cmd.val.([]byte); ok {
2016-04-12 19:41:56 +03:00
// Bytes must be copied, because underlying memory is reused.
cmd.val = string(b)
}
2015-10-07 17:09:20 +03:00
return 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{}
}
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)
}
func (cmd *SliceCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(sliceParser)
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
}
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)
}
func (cmd *StatusCmd) readReply(cn *pool.Conn) error {
2016-07-02 15:52:10 +03:00
cmd.val, cmd.err = cn.Rd.ReadStringReply()
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
}
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)
}
func (cmd *IntCmd) readReply(cn *pool.Conn) error {
2016-07-02 15:52:10 +03:00
cmd.val, cmd.err = cn.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
}
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)
}
func (cmd *DurationCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var n int64
n, cmd.err = cn.Rd.ReadIntReply()
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
}
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)
}
func (cmd *TimeCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(timeParser)
if cmd.err != nil {
return cmd.err
2016-10-14 14:39:02 +03:00
}
cmd.val = v.(time.Time)
return nil
}
//------------------------------------------------------------------------------
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
}
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)
}
var ok = []byte("OK")
func (cmd *BoolCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.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
case []byte:
2016-12-13 18:28:39 +03:00
cmd.val = bytes.Equal(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
2017-01-13 14:39:59 +03:00
val []byte
2013-09-29 12:06:49 +04:00
}
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
}
2017-09-05 09:14:16 +03:00
func (cmd *StringCmd) SetVal(val string) {
cmd.val = []byte(val)
}
2013-09-29 12:06:49 +04:00
func (cmd *StringCmd) Val() string {
2017-01-13 14:39:59 +03:00
return internal.BytesToString(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 cmd.val, cmd.err
2014-01-09 12:17:38 +04:00
}
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
}
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
}
2016-07-02 15:52:10 +03:00
return proto.Scan(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)
}
func (cmd *StringCmd) readReply(cn *pool.Conn) error {
2017-01-13 14:39:59 +03:00
cmd.val, cmd.err = cn.Rd.ReadBytesReply()
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
}
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
}
func (cmd *FloatCmd) readReply(cn *pool.Conn) error {
2016-07-02 15:52:10 +03:00
cmd.val, cmd.err = cn.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
}
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)
}
func (cmd *StringSliceCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(stringSliceParser)
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
}
//------------------------------------------------------------------------------
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
}
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)
}
func (cmd *BoolSliceCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(boolSliceParser)
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
}
//------------------------------------------------------------------------------
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
}
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)
}
func (cmd *StringStringMapCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(stringStringMapParser)
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
}
//------------------------------------------------------------------------------
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
}
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)
}
func (cmd *StringIntMapCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(stringIntMapParser)
if cmd.err != nil {
return cmd.err
2015-01-25 15:05:19 +03:00
}
cmd.val = v.(map[string]int64)
return 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
}
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)
}
func (cmd *ZSliceCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(zSliceParser)
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
}
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-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
}
func (cmd *ScanCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
cmd.page, cmd.cursor, cmd.err = cn.Rd.ReadScanReply()
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
}
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)
}
func (cmd *ClusterSlotsCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(clusterSlotsParser)
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
}
//------------------------------------------------------------------------------
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
}
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)
}
func (cmd *GeoLocationCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(newGeoLocationSliceParser(cmd.q))
if cmd.err != nil {
return cmd.err
}
2016-12-13 18:28:39 +03:00
cmd.locations = v.([]GeoLocation)
return 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
}
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)
}
func (cmd *GeoPosCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(geoPosSliceParser)
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
}
//------------------------------------------------------------------------------
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
}
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)
}
func (cmd *CommandsInfoCmd) readReply(cn *pool.Conn) error {
2016-12-13 18:28:39 +03:00
var v interface{}
v, cmd.err = cn.Rd.ReadArrayReply(commandInfoSliceParser)
if cmd.err != nil {
return cmd.err
}
cmd.val = v.(map[string]*CommandInfo)
return nil
}