redis/command.go

855 lines
16 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"
)
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)
2015-01-24 15:12:48 +03:00
_ Cmder = (*ClusterSlotCmd)(nil)
2014-01-09 12:17:38 +04:00
)
2013-09-29 12:06:49 +04:00
type Cmder interface {
args() []interface{}
2015-10-07 17:09:20 +03:00
readReply(*conn) error
2013-09-29 12:06:49 +04:00
setErr(error)
2015-01-24 15:12:48 +03:00
reset()
2013-09-29 12:06:49 +04:00
writeTimeout() *time.Duration
readTimeout() *time.Duration
2015-01-24 15:12:48 +03:00
clusterKey() string
2013-09-29 12:06:49 +04:00
Err() 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 {
cmd.setErr(e)
}
}
func resetCmds(cmds []Cmder) {
for _, cmd := range cmds {
cmd.reset()
}
}
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
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type baseCmd struct {
_args []interface{}
2013-09-29 12:06:49 +04:00
err error
2015-01-24 15:12:48 +03:00
_clusterKeyPos int
2013-09-29 12:06:49 +04:00
2015-01-24 15:12:48 +03:00
_writeTimeout, _readTimeout *time.Duration
2013-09-29 12:06:49 +04:00
}
func (cmd *baseCmd) Err() error {
if cmd.err != nil {
return cmd.err
}
return nil
}
func (cmd *baseCmd) args() []interface{} {
2013-09-29 12:06:49 +04:00
return cmd._args
}
func (cmd *baseCmd) readTimeout() *time.Duration {
return cmd._readTimeout
}
func (cmd *baseCmd) setReadTimeout(d time.Duration) {
cmd._readTimeout = &d
}
func (cmd *baseCmd) writeTimeout() *time.Duration {
return cmd._writeTimeout
}
2015-01-24 15:12:48 +03:00
func (cmd *baseCmd) clusterKey() string {
if cmd._clusterKeyPos > 0 && cmd._clusterKeyPos < len(cmd._args) {
return fmt.Sprint(cmd._args[cmd._clusterKeyPos])
2015-01-24 15:12:48 +03:00
}
return ""
}
2013-09-29 12:06:49 +04:00
func (cmd *baseCmd) setWriteTimeout(d time.Duration) {
cmd._writeTimeout = &d
}
2014-01-09 12:17:38 +04:00
func (cmd *baseCmd) setErr(e error) {
cmd.err = e
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type Cmd struct {
2015-01-24 15:12:48 +03:00
baseCmd
2014-01-09 12:17:38 +04:00
val interface{}
2013-09-29 12:06:49 +04:00
}
func NewCmd(args ...interface{}) *Cmd {
2015-01-24 15:12:48 +03:00
return &Cmd{baseCmd: baseCmd{_args: args}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *Cmd) reset() {
cmd.val = nil
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *Cmd) readReply(cn *conn) error {
val, err := readReply(cn, sliceParser)
if err != nil {
cmd.err = err
return cmd.err
}
if v, ok := val.([]byte); ok {
// Convert to string to preserve old behaviour.
// TODO: remove in v4
cmd.val = string(v)
2015-10-07 17:09:20 +03:00
} else {
cmd.val = val
}
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 {
2015-01-24 15:12:48 +03:00
return &SliceCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2014-01-09 12:17:38 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *SliceCmd) reset() {
cmd.val = nil
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *SliceCmd) readReply(cn *conn) error {
v, err := readReply(cn, sliceParser)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return err
}
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 {
2015-01-24 15:12:48 +03:00
return &StatusCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
func newKeylessStatusCmd(args ...interface{}) *StatusCmd {
2015-01-24 15:12:48 +03:00
return &StatusCmd{baseCmd: baseCmd{_args: args}}
}
func (cmd *StatusCmd) reset() {
cmd.val = ""
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *StatusCmd) readReply(cn *conn) error {
v, err := readReply(cn, nil)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return err
2013-09-29 12:06:49 +04:00
}
cmd.val = string(v.([]byte))
2014-01-09 12:17:38 +04:00
return nil
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 {
2015-01-24 15:12:48 +03:00
return &IntCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *IntCmd) reset() {
cmd.val = 0
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *IntCmd) readReply(cn *conn) error {
v, err := readReply(cn, nil)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return err
2013-09-29 12:06:49 +04:00
}
2014-01-09 12:17:38 +04:00
cmd.val = v.(int64)
return nil
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{
precision: precision,
2015-01-24 15:12:48 +03:00
baseCmd: baseCmd{_args: args, _clusterKeyPos: 1},
}
}
2015-01-24 15:12:48 +03:00
func (cmd *DurationCmd) reset() {
cmd.val = 0
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *DurationCmd) readReply(cn *conn) error {
v, err := readReply(cn, nil)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return err
}
2014-01-09 12:17:38 +04:00
cmd.val = time.Duration(v.(int64)) * cmd.precision
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 {
2015-01-24 15:12:48 +03:00
return &BoolCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *BoolCmd) reset() {
cmd.val = false
cmd.err = nil
}
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")
2015-10-07 17:09:20 +03:00
func (cmd *BoolCmd) readReply(cn *conn) error {
v, err := readReply(cn, nil)
2015-08-29 13:08:27 +03:00
// `SET key value NX` returns nil when key already exists, which
// is inconsistent with `SETNX key value`.
// TODO: is this okay?
if err == Nil {
cmd.val = false
return nil
}
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return err
2013-09-29 12:06:49 +04:00
}
switch vv := v.(type) {
case int64:
cmd.val = vv == 1
return nil
case []byte:
cmd.val = bytes.Equal(vv, ok)
return nil
default:
2015-09-01 08:27:19 +03:00
return fmt.Errorf("got %T, wanted int64 or string", v)
}
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
val []byte
2013-09-29 12:06:49 +04:00
}
func NewStringCmd(args ...interface{}) *StringCmd {
2015-01-24 15:12:48 +03:00
return &StringCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *StringCmd) reset() {
cmd.val = nil
cmd.err = nil
}
2013-09-29 12:06:49 +04:00
func (cmd *StringCmd) Val() string {
return 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) {
2014-01-09 12:17:38 +04:00
return cmd.val, cmd.err
}
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
}
return 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)
}
2015-10-07 17:09:20 +03:00
func (cmd *StringCmd) readReply(cn *conn) error {
v, err := readReply(cn, nil)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return err
2013-09-29 12:06:49 +04:00
}
2015-09-03 17:55:31 +03:00
cmd.val = cn.copyBuf(v.([]byte))
2014-01-09 12:17:38 +04:00
return nil
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 {
2015-01-24 15:12:48 +03:00
return &FloatCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *FloatCmd) reset() {
cmd.val = 0
cmd.err = nil
}
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
}
2015-10-07 17:09:20 +03:00
func (cmd *FloatCmd) readReply(cn *conn) error {
v, err := readReply(cn, nil)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return err
2013-09-29 12:06:49 +04:00
}
b := v.([]byte)
cmd.val, cmd.err = strconv.ParseFloat(bytesToString(b), 64)
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 {
2015-01-24 15:12:48 +03:00
return &StringSliceCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *StringSliceCmd) reset() {
cmd.val = nil
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *StringSliceCmd) readReply(cn *conn) error {
v, err := readReply(cn, stringSliceParser)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return 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 {
2015-01-24 15:12:48 +03:00
return &BoolSliceCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *BoolSliceCmd) reset() {
cmd.val = nil
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *BoolSliceCmd) readReply(cn *conn) error {
v, err := readReply(cn, boolSliceParser)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return 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 {
2015-01-24 15:12:48 +03:00
return &StringStringMapCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *StringStringMapCmd) reset() {
cmd.val = nil
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *StringStringMapCmd) readReply(cn *conn) error {
v, err := readReply(cn, stringStringMapParser)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return 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 {
2015-01-24 15:12:48 +03:00
return &StringIntMapCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
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)
}
2015-01-24 15:12:48 +03:00
func (cmd *StringIntMapCmd) reset() {
cmd.val = nil
cmd.err = nil
}
2015-10-07 17:09:20 +03:00
func (cmd *StringIntMapCmd) readReply(cn *conn) error {
v, err := readReply(cn, stringIntMapParser)
2015-01-25 15:05:19 +03:00
if err != nil {
cmd.err = err
return err
}
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 {
2015-01-24 15:12:48 +03:00
return &ZSliceCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *ZSliceCmd) reset() {
cmd.val = nil
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *ZSliceCmd) readReply(cn *conn) error {
v, err := readReply(cn, zSliceParser)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return err
}
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
cursor int64
keys []string
}
func NewScanCmd(args ...interface{}) *ScanCmd {
2015-01-24 15:12:48 +03:00
return &ScanCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
2014-01-09 12:17:38 +04:00
}
2015-01-24 15:12:48 +03:00
func (cmd *ScanCmd) reset() {
cmd.cursor = 0
cmd.keys = nil
cmd.err = nil
}
2014-01-09 12:17:38 +04:00
func (cmd *ScanCmd) Val() (int64, []string) {
return cmd.cursor, cmd.keys
}
func (cmd *ScanCmd) Result() (int64, []string, error) {
return cmd.cursor, cmd.keys, cmd.err
}
func (cmd *ScanCmd) String() string {
return cmdString(cmd, cmd.keys)
}
2015-10-07 17:09:20 +03:00
func (cmd *ScanCmd) readReply(cn *conn) error {
vi, err := readReply(cn, sliceParser)
2014-01-09 12:17:38 +04:00
if err != nil {
cmd.err = err
return cmd.err
}
v := vi.([]interface{})
cmd.cursor, cmd.err = strconv.ParseInt(v[0].(string), 10, 64)
if cmd.err != nil {
return cmd.err
}
keys := v[1].([]interface{})
for _, keyi := range keys {
cmd.keys = append(cmd.keys, keyi.(string))
}
return nil
2013-09-29 12:06:49 +04:00
}
2015-01-24 15:12:48 +03:00
//------------------------------------------------------------------------------
type ClusterSlotInfo struct {
2015-10-07 17:09:20 +03:00
Start int
End int
Addrs []string
2015-01-24 15:12:48 +03:00
}
type ClusterSlotCmd struct {
baseCmd
val []ClusterSlotInfo
}
func NewClusterSlotCmd(args ...interface{}) *ClusterSlotCmd {
2015-01-24 15:12:48 +03:00
return &ClusterSlotCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
func (cmd *ClusterSlotCmd) Val() []ClusterSlotInfo {
return cmd.val
}
func (cmd *ClusterSlotCmd) Result() ([]ClusterSlotInfo, error) {
return cmd.Val(), cmd.Err()
}
func (cmd *ClusterSlotCmd) String() string {
return cmdString(cmd, cmd.val)
}
func (cmd *ClusterSlotCmd) reset() {
cmd.val = nil
cmd.err = nil
}
2015-10-07 17:09:20 +03:00
func (cmd *ClusterSlotCmd) readReply(cn *conn) error {
v, err := readReply(cn, clusterSlotInfoSliceParser)
2015-01-24 15:12:48 +03:00
if err != nil {
cmd.err = err
return err
}
cmd.val = v.([]ClusterSlotInfo)
return nil
}
//------------------------------------------------------------------------------
2015-10-07 17:09:20 +03:00
// GeoLocation is used with GeoAdd to add geospatial location.
type GeoLocation struct {
2015-10-07 17:09:20 +03:00
Name string
Longitude, Latitude, Distance float64
2015-10-07 17:09:20 +03:00
GeoHash int64
}
2015-10-07 17:09:20 +03:00
// GeoRadiusQuery is used with GeoRadius to query geospatial index.
type GeoRadiusQuery struct {
Key string
Longitude float64
Latitude float64
Radius float64
// Can be m, km, ft, or mi. Default is km.
Unit string
WithCoordinates bool
WithDistance bool
WithGeoHash bool
Count int
// Can be ASC or DESC. Default is no sort order.
Sort string
}
type GeoLocationCmd struct {
baseCmd
locations []GeoLocation
}
2015-10-07 17:09:20 +03:00
func NewGeoLocationCmd(args ...interface{}) *GeoLocationCmd {
return &GeoLocationCmd{baseCmd: baseCmd{_args: args, _clusterKeyPos: 1}}
}
2015-10-07 17:09:20 +03:00
func (cmd *GeoLocationCmd) reset() {
cmd.locations = nil
cmd.err = nil
}
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)
}
2015-10-07 17:09:20 +03:00
func (cmd *GeoLocationCmd) readReply(cn *conn) error {
reply, err := readReply(cn, geoLocationSliceParser)
if err != nil {
cmd.err = err
2015-10-07 17:09:20 +03:00
return err
}
2015-10-07 17:09:20 +03:00
cmd.locations = reply.([]GeoLocation)
return nil
}