2012-07-25 17:00:50 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
|
2012-07-27 15:43:30 +04:00
|
|
|
"github.com/vmihailenco/bufreader"
|
2012-07-25 17:00:50 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
var Nil = errors.New("(nil)")
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-27 18:21:50 +04:00
|
|
|
func isNil(buf []byte) bool {
|
|
|
|
return len(buf) == 3 && buf[0] == '$' && buf[1] == '-' && buf[2] == '1'
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEmpty(buf []byte) bool {
|
|
|
|
return len(buf) == 2 && buf[0] == '$' && buf[1] == '0'
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-25 17:00:50 +04:00
|
|
|
func ParseReq(rd *bufreader.Reader) ([]string, error) {
|
|
|
|
line, err := rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if line[0] != '*' {
|
|
|
|
return []string{string(line)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := make([]string, 0)
|
|
|
|
for {
|
|
|
|
line, err = rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if line[0] != '$' {
|
|
|
|
return nil, fmt.Errorf("Expected '$', but got %q of %q.", line, rd.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
line, err = rd.ReadLine('\n')
|
|
|
|
args = append(args, string(line))
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func PackReq(args []string) []byte {
|
|
|
|
buf := make([]byte, 0, 1024)
|
|
|
|
buf = append(buf, '*')
|
|
|
|
buf = strconv.AppendUint(buf, uint64(len(args)), 10)
|
|
|
|
buf = append(buf, '\r', '\n')
|
|
|
|
for _, arg := range args {
|
|
|
|
buf = append(buf, '$')
|
|
|
|
buf = strconv.AppendUint(buf, uint64(len(arg)), 10)
|
|
|
|
buf = append(buf, '\r', '\n')
|
|
|
|
buf = append(buf, []byte(arg)...)
|
|
|
|
buf = append(buf, '\r', '\n')
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Req interface {
|
|
|
|
Req() []byte
|
2012-07-26 22:43:21 +04:00
|
|
|
ParseReply(*bufreader.Reader) (interface{}, error)
|
2012-07-29 13:42:00 +04:00
|
|
|
SetClient(*Client)
|
2012-07-25 17:00:50 +04:00
|
|
|
SetErr(error)
|
2012-07-26 22:43:21 +04:00
|
|
|
SetVal(interface{})
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type BaseReq struct {
|
|
|
|
args []string
|
2012-07-29 13:42:00 +04:00
|
|
|
|
|
|
|
client *Client
|
|
|
|
// TODO: use int32 and atomic access?
|
|
|
|
done bool
|
|
|
|
|
|
|
|
val interface{}
|
|
|
|
err error
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseReq(args ...string) *BaseReq {
|
|
|
|
return &BaseReq{
|
|
|
|
args: args,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *BaseReq) Req() []byte {
|
|
|
|
return PackReq(r.args)
|
|
|
|
}
|
|
|
|
|
2012-07-29 13:42:00 +04:00
|
|
|
func (r *BaseReq) SetClient(c *Client) {
|
|
|
|
r.client = c
|
|
|
|
}
|
|
|
|
|
2012-07-25 17:00:50 +04:00
|
|
|
func (r *BaseReq) SetErr(err error) {
|
2012-07-26 22:43:21 +04:00
|
|
|
if err == nil {
|
|
|
|
panic("non-nil value expected")
|
|
|
|
}
|
2012-07-29 13:42:00 +04:00
|
|
|
r.done = true
|
2012-07-25 17:00:50 +04:00
|
|
|
r.err = err
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
func (r *BaseReq) SetVal(val interface{}) {
|
|
|
|
if val == nil {
|
|
|
|
panic("non-nil value expected")
|
|
|
|
}
|
2012-07-29 13:42:00 +04:00
|
|
|
r.done = true
|
2012-07-26 22:43:21 +04:00
|
|
|
r.val = val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *BaseReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
|
|
|
|
panic("abstract")
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type StatusReq struct {
|
|
|
|
*BaseReq
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStatusReq(args ...string) *StatusReq {
|
|
|
|
return &StatusReq{
|
|
|
|
BaseReq: NewBaseReq(args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
func (r *StatusReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
|
|
|
|
line, err := rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '-' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, errors.New(string(line[1:]))
|
2012-07-25 17:00:50 +04:00
|
|
|
} else if line[0] != '+' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, fmt.Errorf("Expected '+', but got %q of %q.", line, rd.Bytes())
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
return string(line[1:]), nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *StatusReq) Reply() (string, error) {
|
2012-07-29 13:42:00 +04:00
|
|
|
if !r.done {
|
|
|
|
_, err := r.client.RunQueued()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !r.done {
|
|
|
|
panic("req is not ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
if r.err != nil {
|
|
|
|
return "", r.err
|
|
|
|
}
|
|
|
|
return r.val.(string), nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type IntReq struct {
|
|
|
|
*BaseReq
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIntReq(args ...string) *IntReq {
|
|
|
|
return &IntReq{
|
|
|
|
BaseReq: NewBaseReq(args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
func (r *IntReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
|
|
|
|
line, err := rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '-' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, errors.New(string(line[1:]))
|
2012-07-25 17:00:50 +04:00
|
|
|
} else if line[0] != ':' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, fmt.Errorf("Expected ':', but got %q of %q.", line, rd.Bytes())
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
return strconv.ParseInt(string(line[1:]), 10, 64)
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IntReq) Reply() (int64, error) {
|
2012-07-29 13:42:00 +04:00
|
|
|
if !r.done {
|
|
|
|
_, err := r.client.RunQueued()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if !r.done {
|
|
|
|
panic("req is not ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
if r.err != nil {
|
|
|
|
return 0, r.err
|
|
|
|
}
|
|
|
|
return r.val.(int64), nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-27 18:21:50 +04:00
|
|
|
type IntNilReq struct {
|
|
|
|
*BaseReq
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIntNilReq(args ...string) *IntNilReq {
|
|
|
|
return &IntNilReq{
|
|
|
|
BaseReq: NewBaseReq(args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IntNilReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
|
|
|
|
line, err := rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '-' {
|
|
|
|
return nil, errors.New(string(line[1:]))
|
|
|
|
} else if line[0] == ':' {
|
|
|
|
return strconv.ParseInt(string(line[1:]), 10, 64)
|
|
|
|
} else if isNil(line) {
|
|
|
|
return nil, Nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Expected ':', but got %q of %q.", line, rd.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IntNilReq) Reply() (int64, error) {
|
2012-07-29 13:42:00 +04:00
|
|
|
if !r.done {
|
|
|
|
_, err := r.client.RunQueued()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if !r.done {
|
|
|
|
panic("req is not ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 18:21:50 +04:00
|
|
|
if r.err != nil {
|
|
|
|
return 0, r.err
|
|
|
|
}
|
|
|
|
return r.val.(int64), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-25 17:00:50 +04:00
|
|
|
type BoolReq struct {
|
|
|
|
*BaseReq
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBoolReq(args ...string) *BoolReq {
|
|
|
|
return &BoolReq{
|
|
|
|
BaseReq: NewBaseReq(args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
func (r *BoolReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
|
|
|
|
line, err := rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '-' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, errors.New(string(line[1:]))
|
2012-07-25 17:00:50 +04:00
|
|
|
} else if line[0] != ':' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, fmt.Errorf("Expected ':', but got %q of %q.", line, rd.Bytes())
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
return line[1] == '1', nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *BoolReq) Reply() (bool, error) {
|
2012-07-29 13:42:00 +04:00
|
|
|
if !r.done {
|
|
|
|
_, err := r.client.RunQueued()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !r.done {
|
|
|
|
panic("req is not ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
if r.err != nil {
|
|
|
|
return false, r.err
|
|
|
|
}
|
|
|
|
return r.val.(bool), nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type BulkReq struct {
|
|
|
|
*BaseReq
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBulkReq(args ...string) *BulkReq {
|
|
|
|
return &BulkReq{
|
|
|
|
BaseReq: NewBaseReq(args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
func (r *BulkReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
|
|
|
|
line, err := rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '-' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, errors.New(string(line[1:]))
|
2012-07-25 17:00:50 +04:00
|
|
|
} else if line[0] != '$' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, fmt.Errorf("Expected '$', but got %q of %q.", line, rd.Bytes())
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-27 18:21:50 +04:00
|
|
|
if isNil(line) {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, Nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
line, err = rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
return string(line), nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *BulkReq) Reply() (string, error) {
|
2012-07-29 13:42:00 +04:00
|
|
|
if !r.done {
|
|
|
|
_, err := r.client.RunQueued()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !r.done {
|
|
|
|
panic("req is not ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
if r.err != nil {
|
|
|
|
return "", r.err
|
|
|
|
}
|
|
|
|
return r.val.(string), nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-27 18:21:50 +04:00
|
|
|
type FloatReq struct {
|
|
|
|
*BaseReq
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFloatReq(args ...string) *FloatReq {
|
|
|
|
return &FloatReq{
|
|
|
|
BaseReq: NewBaseReq(args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *FloatReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
|
|
|
|
line, err := rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '-' {
|
|
|
|
return nil, errors.New(string(line[1:]))
|
|
|
|
} else if line[0] != '$' {
|
|
|
|
return nil, fmt.Errorf("Expected '$', but got %q of %q.", line, rd.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if isNil(line) {
|
|
|
|
return nil, Nil
|
|
|
|
}
|
|
|
|
|
|
|
|
line, err = rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.ParseFloat(string(line), 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *FloatReq) Reply() (float64, error) {
|
2012-07-29 13:42:00 +04:00
|
|
|
if !r.done {
|
|
|
|
_, err := r.client.RunQueued()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if !r.done {
|
|
|
|
panic("req is not ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 18:21:50 +04:00
|
|
|
if r.err != nil {
|
|
|
|
return 0, r.err
|
|
|
|
}
|
|
|
|
return r.val.(float64), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-25 17:00:50 +04:00
|
|
|
type MultiBulkReq struct {
|
|
|
|
*BaseReq
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMultiBulkReq(args ...string) *MultiBulkReq {
|
|
|
|
return &MultiBulkReq{
|
|
|
|
BaseReq: NewBaseReq(args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
func (r *MultiBulkReq) ParseReply(rd *bufreader.Reader) (interface{}, error) {
|
|
|
|
line, err := rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '-' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, errors.New(string(line[1:]))
|
2012-07-25 17:00:50 +04:00
|
|
|
} else if line[0] != '*' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, fmt.Errorf("Expected '*', but got line %q of %q.", line, rd.Bytes())
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
val := make([]interface{}, 0)
|
|
|
|
|
|
|
|
if len(line) >= 2 && line[1] == '0' {
|
2012-07-26 22:43:21 +04:00
|
|
|
return val, nil
|
2012-07-27 18:21:50 +04:00
|
|
|
} else if isNil(line) {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, Nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
line, err = rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
for {
|
|
|
|
if line[0] == ':' {
|
|
|
|
var n int64
|
2012-07-26 22:43:21 +04:00
|
|
|
n, err = strconv.ParseInt(string(line[1:]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
val = append(val, n)
|
|
|
|
} else if line[0] == '$' {
|
2012-07-27 18:21:50 +04:00
|
|
|
if isEmpty(line) {
|
2012-07-25 17:00:50 +04:00
|
|
|
val = append(val, "")
|
2012-07-27 18:21:50 +04:00
|
|
|
} else if isNil(line) {
|
2012-07-25 17:00:50 +04:00
|
|
|
val = append(val, nil)
|
|
|
|
} else {
|
2012-07-26 22:43:21 +04:00
|
|
|
line, err = rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
val = append(val, string(line))
|
|
|
|
}
|
|
|
|
} else {
|
2012-07-26 22:43:21 +04:00
|
|
|
return nil, fmt.Errorf("Expected '$', but got line %q of %q.", line, rd.Bytes())
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
line, err = rd.ReadLine('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
2012-07-26 22:43:21 +04:00
|
|
|
|
|
|
|
// Check for the header of another reply.
|
2012-07-25 17:00:50 +04:00
|
|
|
if line[0] == '*' {
|
|
|
|
rd.UnreadLine('\n')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
return val, nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MultiBulkReq) Reply() ([]interface{}, error) {
|
2012-07-29 13:42:00 +04:00
|
|
|
if !r.done {
|
|
|
|
_, err := r.client.RunQueued()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !r.done {
|
|
|
|
panic("req is not ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:43:21 +04:00
|
|
|
if r.err != nil {
|
|
|
|
return nil, r.err
|
|
|
|
}
|
|
|
|
return r.val.([]interface{}), nil
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|