redis/command.go

598 lines
10 KiB
Go
Raw Normal View History

2013-09-29 12:06:49 +04:00
package redis
import (
"fmt"
"strconv"
"strings"
"time"
2014-06-18 17:47:21 +04:00
"gopkg.in/bufio.v1"
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)
2014-07-05 14:46:27 +04:00
_ Cmder = (*ZSliceCmd)(nil)
2014-01-09 12:17:38 +04:00
_ Cmder = (*ScanCmd)(nil)
)
2013-09-29 12:06:49 +04:00
type Cmder interface {
args() []string
2014-06-18 17:47:21 +04:00
parseReply(*bufio.Reader) error
2013-09-29 12:06:49 +04:00
setErr(error)
writeTimeout() *time.Duration
readTimeout() *time.Duration
Err() error
2014-01-09 12:17:38 +04:00
String() string
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)
}
}
2014-01-09 12:17:38 +04:00
func cmdString(cmd Cmder, val interface{}) string {
s := strings.Join(cmd.args(), " ")
if err := cmd.Err(); err != nil {
return s + ": " + err.Error()
}
if val != nil {
return s + ": " + fmt.Sprint(val)
}
return s
}
2013-09-29 12:06:49 +04:00
//------------------------------------------------------------------------------
type baseCmd struct {
_args []string
err error
_writeTimeout, _readTimeout *time.Duration
}
func newBaseCmd(args ...string) *baseCmd {
return &baseCmd{
_args: args,
}
}
func (cmd *baseCmd) Err() error {
if cmd.err != nil {
return cmd.err
}
return nil
}
func (cmd *baseCmd) args() []string {
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
}
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 {
*baseCmd
2014-01-09 12:17:38 +04:00
val interface{}
2013-09-29 12:06:49 +04:00
}
func NewCmd(args ...string) *Cmd {
return &Cmd{
baseCmd: newBaseCmd(args...),
}
}
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)
}
2014-06-18 17:47:21 +04:00
func (cmd *Cmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
cmd.val, cmd.err = parseReply(rd, parseSlice)
return cmd.err
}
//------------------------------------------------------------------------------
type SliceCmd struct {
*baseCmd
val []interface{}
}
func NewSliceCmd(args ...string) *SliceCmd {
return &SliceCmd{
baseCmd: newBaseCmd(args...),
}
}
func (cmd *SliceCmd) Val() []interface{} {
return cmd.val
}
func (cmd *SliceCmd) Result() ([]interface{}, error) {
return cmd.val, cmd.err
}
func (cmd *SliceCmd) String() string {
return cmdString(cmd, cmd.val)
}
2014-06-18 17:47:21 +04:00
func (cmd *SliceCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, parseSlice)
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 {
*baseCmd
2014-01-09 12:17:38 +04:00
val string
2013-09-29 12:06:49 +04:00
}
func NewStatusCmd(args ...string) *StatusCmd {
return &StatusCmd{
baseCmd: newBaseCmd(args...),
}
}
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)
}
2014-06-18 17:47:21 +04:00
func (cmd *StatusCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, nil)
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 IntCmd struct {
*baseCmd
2014-01-09 12:17:38 +04:00
val int64
2013-09-29 12:06:49 +04:00
}
func NewIntCmd(args ...string) *IntCmd {
return &IntCmd{
baseCmd: newBaseCmd(args...),
}
}
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)
}
2014-06-18 17:47:21 +04:00
func (cmd *IntCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, nil)
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 {
*baseCmd
2014-01-09 12:17:38 +04:00
val time.Duration
precision time.Duration
}
func NewDurationCmd(precision time.Duration, args ...string) *DurationCmd {
return &DurationCmd{
baseCmd: newBaseCmd(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)
}
2014-06-18 17:47:21 +04:00
func (cmd *DurationCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, nil)
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 {
*baseCmd
2014-01-09 12:17:38 +04:00
val bool
2013-09-29 12:06:49 +04:00
}
func NewBoolCmd(args ...string) *BoolCmd {
return &BoolCmd{
baseCmd: newBaseCmd(args...),
}
}
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)
}
2014-06-18 17:47:21 +04:00
func (cmd *BoolCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, nil)
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) == 1
return nil
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
type StringCmd struct {
*baseCmd
2014-01-09 12:17:38 +04:00
val string
2013-09-29 12:06:49 +04:00
}
func NewStringCmd(args ...string) *StringCmd {
return &StringCmd{
baseCmd: newBaseCmd(args...),
}
}
func (cmd *StringCmd) Val() string {
2014-01-09 12:17:38 +04:00
return cmd.val
}
func (cmd *StringCmd) Result() (string, error) {
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)
}
func (cmd *StringCmd) Uint64() (uint64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseUint(cmd.val, 10, 64)
}
func (cmd *StringCmd) Float64() (float64, error) {
if cmd.err != nil {
return 0, cmd.err
}
return strconv.ParseFloat(cmd.val, 64)
}
2014-01-09 12:17:38 +04:00
func (cmd *StringCmd) String() string {
return cmdString(cmd, cmd.val)
}
2014-06-18 17:47:21 +04:00
func (cmd *StringCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, nil)
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 FloatCmd struct {
*baseCmd
2014-01-09 12:17:38 +04:00
val float64
2013-09-29 12:06:49 +04:00
}
func NewFloatCmd(args ...string) *FloatCmd {
return &FloatCmd{
baseCmd: newBaseCmd(args...),
}
}
func (cmd *FloatCmd) Val() float64 {
2014-01-09 12:17:38 +04:00
return cmd.val
2013-09-29 12:06:49 +04:00
}
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
}
2014-06-18 17:47:21 +04:00
func (cmd *FloatCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, nil)
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, cmd.err = strconv.ParseFloat(v.(string), 64)
return cmd.err
2013-09-29 12:06:49 +04:00
}
//------------------------------------------------------------------------------
type StringSliceCmd struct {
*baseCmd
2014-01-09 12:17:38 +04:00
val []string
2013-09-29 12:06:49 +04:00
}
func NewStringSliceCmd(args ...string) *StringSliceCmd {
return &StringSliceCmd{
baseCmd: newBaseCmd(args...),
}
}
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)
}
2014-06-18 17:47:21 +04:00
func (cmd *StringSliceCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, parseStringSlice)
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 {
*baseCmd
2014-01-09 12:17:38 +04:00
val []bool
2013-09-29 12:06:49 +04:00
}
func NewBoolSliceCmd(args ...string) *BoolSliceCmd {
return &BoolSliceCmd{
baseCmd: newBaseCmd(args...),
}
}
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)
}
2014-06-18 17:47:21 +04:00
func (cmd *BoolSliceCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, parseBoolSlice)
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 {
*baseCmd
2014-01-09 12:17:38 +04:00
val map[string]string
2013-09-29 12:06:49 +04:00
}
func NewStringStringMapCmd(args ...string) *StringStringMapCmd {
return &StringStringMapCmd{
baseCmd: newBaseCmd(args...),
}
}
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)
}
2014-06-18 17:47:21 +04:00
func (cmd *StringStringMapCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
v, err := parseReply(rd, parseStringStringMap)
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
}
//------------------------------------------------------------------------------
2014-07-05 14:46:27 +04:00
type ZSliceCmd struct {
2013-09-29 12:06:49 +04: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
}
2014-07-05 14:46:27 +04:00
func NewZSliceCmd(args ...string) *ZSliceCmd {
return &ZSliceCmd{
2013-09-29 12:06:49 +04:00
baseCmd: newBaseCmd(args...),
}
}
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)
}
2014-07-05 14:46:27 +04:00
func (cmd *ZSliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseZSlice)
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 {
*baseCmd
cursor int64
keys []string
}
func NewScanCmd(args ...string) *ScanCmd {
return &ScanCmd{
baseCmd: newBaseCmd(args...),
2013-09-29 12:06:49 +04:00
}
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)
}
2014-06-18 17:47:21 +04:00
func (cmd *ScanCmd) parseReply(rd *bufio.Reader) error {
2014-01-09 12:17:38 +04:00
vi, err := parseReply(rd, parseSlice)
if err != nil {
cmd.err = err
return cmd.err
}
v := vi.([]interface{})
cmd.cursor, cmd.err = strconv.ParseInt(v[0].(string), 10, 64)
if cmd.err != nil {
return cmd.err
}
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
}