redis/request.go

461 lines
8.6 KiB
Go
Raw Normal View History

2012-07-25 17:00:50 +04:00
package redis
import (
"errors"
"fmt"
"strconv"
)
var Nil = errors.New("(nil)")
2012-08-11 19:02:28 +04:00
var ErrValNotSet = errors.New("redis: value is not set")
2012-08-05 16:09:43 +04:00
2012-07-25 17:00:50 +04:00
//------------------------------------------------------------------------------
func isNil(line []byte) bool {
return len(line) == 3 && line[0] == '$' && line[1] == '-' && line[2] == '1'
2012-07-27 18:21:50 +04:00
}
func isEmpty(line []byte) bool {
return len(line) == 2 && line[0] == '$' && line[1] == '0'
}
2012-08-09 18:06:26 +04:00
func isNilReplies(line []byte) bool {
return len(line) == 3 && line[0] == '*' && line[1] == '-' && line[2] == '1'
}
func isNoReplies(line []byte) bool {
2012-08-09 18:06:26 +04:00
return len(line) == 2 && line[1] == '*' && line[1] == '0'
2012-07-27 18:21:50 +04:00
}
//------------------------------------------------------------------------------
type ReadLiner interface {
ReadLine() ([]byte, bool, error)
}
func readLine(rd ReadLiner) ([]byte, error) {
line, isPrefix, err := rd.ReadLine()
if err != nil {
return line, err
}
if isPrefix {
return line, ErrReaderTooSmall
}
return line, nil
}
//------------------------------------------------------------------------------
func ParseReq(rd ReadLiner) ([]string, error) {
2012-08-09 14:12:41 +04:00
line, err := readLine(rd)
2012-07-25 17:00:50 +04:00
if err != nil {
return nil, err
}
2012-07-25 17:00:50 +04:00
if line[0] != '*' {
return []string{string(line)}, nil
}
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return nil, err
}
2012-07-25 17:00:50 +04:00
args := make([]string, 0)
for i := int64(0); i < numReplies; i++ {
2012-08-09 14:12:41 +04:00
line, err = readLine(rd)
2012-07-25 17:00:50 +04:00
if err != nil {
return nil, err
}
if line[0] != '$' {
return nil, fmt.Errorf("Expected '$', but got %q", line)
2012-07-25 17:00:50 +04:00
}
2012-08-09 14:12:41 +04:00
line, err = readLine(rd)
if err != nil {
return nil, err
}
2012-07-25 17:00:50 +04:00
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
ParseReply(ReadLiner) (interface{}, error)
2012-07-25 17:00:50 +04:00
SetErr(error)
2012-08-05 16:09:43 +04:00
Err() error
2012-07-26 22:43:21 +04:00
SetVal(interface{})
2012-08-06 16:09:48 +04:00
InterfaceVal() interface{}
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
type BaseReq struct {
args []string
2012-07-29 13:42:00 +04:00
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)
}
func (r *BaseReq) SetErr(err error) {
2012-07-26 22:43:21 +04:00
if err == nil {
panic("non-nil value expected")
}
2012-07-25 17:00:50 +04:00
r.err = err
}
2012-08-05 16:09:43 +04:00
func (r *BaseReq) Err() error {
2012-08-06 16:09:48 +04:00
if r.err != nil {
return r.err
}
if r.val == nil {
2012-08-11 19:02:28 +04:00
return ErrValNotSet
2012-08-06 16:09:48 +04:00
}
return nil
2012-08-05 16:09:43 +04:00
}
2012-07-26 22:43:21 +04:00
func (r *BaseReq) SetVal(val interface{}) {
if val == nil {
panic("non-nil value expected")
}
r.val = val
}
2012-08-06 16:09:48 +04:00
func (r *BaseReq) InterfaceVal() interface{} {
2012-08-05 16:09:43 +04:00
return r.val
}
func (r *BaseReq) ParseReply(rd ReadLiner) (interface{}, error) {
2012-07-26 22:43:21 +04:00
panic("abstract")
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
type StatusReq struct {
*BaseReq
}
func NewStatusReq(args ...string) *StatusReq {
return &StatusReq{
BaseReq: NewBaseReq(args...),
}
}
func (r *StatusReq) ParseReply(rd ReadLiner) (interface{}, error) {
2012-08-09 14:12:41 +04:00
line, err := readLine(rd)
2012-07-26 22:43:21 +04:00
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] != '+' {
return nil, fmt.Errorf("Expected '+', but got %q", line)
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
}
2012-08-06 16:09:48 +04:00
func (r *StatusReq) Val() string {
if r.val == nil {
return ""
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(string)
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
type IntReq struct {
*BaseReq
}
func NewIntReq(args ...string) *IntReq {
return &IntReq{
BaseReq: NewBaseReq(args...),
}
}
func (r *IntReq) ParseReply(rd ReadLiner) (interface{}, error) {
2012-08-09 14:12:41 +04:00
line, err := readLine(rd)
2012-07-26 22:43:21 +04:00
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] != ':' {
return nil, fmt.Errorf("Expected ':', but got line %q", line)
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
}
2012-08-06 16:09:48 +04:00
func (r *IntReq) Val() int64 {
if r.val == nil {
return 0
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(int64)
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 ReadLiner) (interface{}, error) {
2012-08-09 14:12:41 +04:00
line, err := readLine(rd)
2012-07-27 18:21:50 +04:00
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 line %q", line)
2012-07-27 18:21:50 +04:00
}
2012-08-06 16:09:48 +04:00
func (r *IntNilReq) Val() int64 {
if r.val == nil {
return 0
2012-07-27 18:21:50 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(int64)
2012-07-27 18:21:50 +04:00
}
//------------------------------------------------------------------------------
2012-07-25 17:00:50 +04:00
type BoolReq struct {
*BaseReq
}
func NewBoolReq(args ...string) *BoolReq {
return &BoolReq{
BaseReq: NewBaseReq(args...),
}
}
func (r *BoolReq) ParseReply(rd ReadLiner) (interface{}, error) {
2012-08-09 14:12:41 +04:00
line, err := readLine(rd)
2012-07-26 22:43:21 +04:00
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] != ':' {
return nil, fmt.Errorf("Expected ':', but got line %q", line)
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
}
2012-08-06 16:09:48 +04:00
func (r *BoolReq) Val() bool {
if r.val == nil {
return false
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(bool)
2012-07-25 17:00:50 +04:00
}
//------------------------------------------------------------------------------
type BulkReq struct {
*BaseReq
}
func NewBulkReq(args ...string) *BulkReq {
return &BulkReq{
BaseReq: NewBaseReq(args...),
}
}
func (r *BulkReq) ParseReply(rd ReadLiner) (interface{}, error) {
2012-08-09 14:12:41 +04:00
line, err := readLine(rd)
2012-07-26 22:43:21 +04:00
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] != '$' {
return nil, fmt.Errorf("Expected '$', but got line %q", line)
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-08-09 14:12:41 +04:00
line, err = readLine(rd)
2012-07-26 22:43:21 +04:00
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
}
2012-08-06 16:09:48 +04:00
func (r *BulkReq) Val() string {
if r.val == nil {
return ""
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(string)
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 ReadLiner) (interface{}, error) {
2012-08-09 14:12:41 +04:00
line, err := readLine(rd)
2012-07-27 18:21:50 +04:00
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 line %q", line)
2012-07-27 18:21:50 +04:00
}
if isNil(line) {
return nil, Nil
}
2012-08-09 14:12:41 +04:00
line, err = readLine(rd)
2012-07-27 18:21:50 +04:00
if err != nil {
return nil, err
}
return strconv.ParseFloat(string(line), 64)
}
2012-08-06 16:09:48 +04:00
func (r *FloatReq) Val() float64 {
if r.val == nil {
return 0
2012-07-27 18:21:50 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.(float64)
2012-07-27 18:21:50 +04:00
}
//------------------------------------------------------------------------------
2012-07-25 17:00:50 +04:00
type MultiBulkReq struct {
*BaseReq
}
func NewMultiBulkReq(args ...string) *MultiBulkReq {
return &MultiBulkReq{
BaseReq: NewBaseReq(args...),
}
}
func (r *MultiBulkReq) ParseReply(rd ReadLiner) (interface{}, error) {
2012-08-09 14:12:41 +04:00
line, err := readLine(rd)
2012-07-26 22:43:21 +04:00
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] != '*' {
return nil, fmt.Errorf("Expected '*', but got line %q", line)
2012-08-09 18:06:26 +04:00
} else if isNilReplies(line) {
return nil, Nil
2012-07-25 17:00:50 +04:00
}
val := make([]interface{}, 0)
if isNoReplies(line) {
2012-07-26 22:43:21 +04:00
return val, nil
2012-07-25 17:00:50 +04:00
}
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
2012-07-26 22:43:21 +04:00
if err != nil {
return nil, err
2012-07-25 17:00:50 +04:00
}
for i := int64(0); i < numReplies; i++ {
2012-08-09 14:12:41 +04:00
line, err = readLine(rd)
if err != nil {
return nil, err
}
2012-08-09 18:06:26 +04:00
switch line[0] {
case ':':
2012-07-25 17:00:50 +04:00
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)
2012-08-09 18:06:26 +04:00
case '$':
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-08-09 14:12:41 +04:00
line, err = readLine(rd)
2012-07-26 22:43:21 +04:00
if err != nil {
return nil, err
2012-07-25 17:00:50 +04:00
}
val = append(val, string(line))
}
2012-08-09 18:06:26 +04:00
default:
return nil, fmt.Errorf("Expected '$', but got line %q", line)
2012-07-25 17:00:50 +04:00
}
}
2012-07-26 22:43:21 +04:00
return val, nil
2012-07-25 17:00:50 +04:00
}
2012-08-06 16:09:48 +04:00
func (r *MultiBulkReq) Val() []interface{} {
if r.val == nil {
return nil
2012-07-26 22:43:21 +04:00
}
2012-08-06 16:09:48 +04:00
return r.val.([]interface{})
2012-07-25 17:00:50 +04:00
}