redis/parser.go

305 lines
6.4 KiB
Go
Raw Normal View History

package redis
import (
"errors"
"fmt"
"strconv"
"github.com/vmihailenco/bufio"
)
2013-02-02 16:17:01 +04:00
type replyType int
const (
ifaceSlice replyType = iota
stringSlice
boolSlice
stringStringMap
stringFloatMap
)
2012-08-25 17:34:13 +04:00
// Represents Redis nil reply.
var Nil = errors.New("(nil)")
var (
errReaderTooSmall = errors.New("redis: reader is too small")
errValNotSet = errors.New("redis: value is not set")
2013-02-02 16:17:01 +04:00
errInvalidType = errors.New("redis: invalid reply type")
)
//------------------------------------------------------------------------------
2012-08-26 13:18:42 +04:00
type parserError struct {
err error
}
func (e *parserError) Error() string {
return e.err.Error()
}
//------------------------------------------------------------------------------
2012-08-17 22:36:48 +04:00
func appendReq(buf []byte, args []string) []byte {
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')
2013-02-02 14:59:42 +04:00
buf = append(buf, arg...)
buf = append(buf, '\r', '\n')
}
return buf
}
//------------------------------------------------------------------------------
2012-08-17 22:36:48 +04:00
type reader interface {
ReadLine() ([]byte, bool, error)
2012-08-13 15:45:32 +04:00
Read([]byte) (int, error)
ReadN(n int) ([]byte, error)
2012-08-17 22:36:48 +04:00
Buffered() int
}
2012-08-17 22:36:48 +04:00
func readLine(rd reader) ([]byte, error) {
line, isPrefix, err := rd.ReadLine()
if err != nil {
return line, err
}
if isPrefix {
return line, errReaderTooSmall
}
return line, nil
}
func readN(rd reader, n int) ([]byte, error) {
buf, err := rd.ReadN(n)
if err == bufio.ErrBufferFull {
newBuf := make([]byte, n)
r := copy(newBuf, buf)
buf = newBuf
for {
nn, err := rd.Read(buf[r:])
r += nn
if r >= n {
// Ignore error if we read enough.
break
}
if err != nil {
return nil, err
}
}
} else if err != nil {
return nil, err
}
return buf, nil
}
//------------------------------------------------------------------------------
func ParseReq(rd reader) ([]string, error) {
line, err := readLine(rd)
if err != nil {
return nil, err
}
if line[0] != '*' {
return []string{string(line)}, nil
}
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return nil, err
}
args := make([]string, 0, numReplies)
for i := int64(0); i < numReplies; i++ {
line, err = readLine(rd)
if err != nil {
return nil, err
}
if line[0] != '$' {
2013-02-02 16:17:01 +04:00
return nil, fmt.Errorf("redis: expected '$', but got %q", line)
}
argLen, err := strconv.ParseInt(string(line[1:]), 10, 32)
if err != nil {
return nil, err
}
arg, err := readN(rd, int(argLen)+2)
if err != nil {
return nil, err
}
args = append(args, string(arg[:argLen]))
}
return args, nil
}
//------------------------------------------------------------------------------
2012-08-17 22:36:48 +04:00
func parseReply(rd reader) (interface{}, error) {
2012-08-20 14:42:33 +04:00
return _parseReply(rd, ifaceSlice)
}
func parseStringSliceReply(rd reader) (interface{}, error) {
return _parseReply(rd, stringSlice)
}
2012-08-20 14:42:33 +04:00
func parseBoolSliceReply(rd reader) (interface{}, error) {
return _parseReply(rd, boolSlice)
2012-08-17 22:36:48 +04:00
}
2013-02-02 16:17:01 +04:00
func parseStringStringMapReply(rd reader) (interface{}, error) {
return _parseReply(rd, stringStringMap)
}
func parseStringFloatMapReply(rd reader) (interface{}, error) {
return _parseReply(rd, stringFloatMap)
}
func _parseReply(rd reader, typ replyType) (interface{}, error) {
line, err := readLine(rd)
if err != nil {
2012-08-26 13:18:42 +04:00
return 0, &parserError{err}
}
switch line[0] {
case '-':
return nil, errors.New(string(line[1:]))
case '+':
return string(line[1:]), nil
case ':':
2012-08-26 13:18:42 +04:00
v, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return 0, &parserError{err}
}
return v, nil
case '$':
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
return "", Nil
}
replyLenInt32, err := strconv.ParseInt(string(line[1:]), 10, 32)
if err != nil {
2012-08-26 13:18:42 +04:00
return "", &parserError{err}
}
replyLen := int(replyLenInt32) + 2
line, err = readN(rd, replyLen)
if err != nil {
2012-08-26 13:18:42 +04:00
return "", &parserError{err}
}
return string(line[:len(line)-2]), nil
case '*':
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
return nil, Nil
}
2013-02-02 16:17:01 +04:00
repliesNum, err := strconv.ParseInt(string(line[1:]), 10, 64)
2012-08-26 13:18:42 +04:00
if err != nil {
return nil, &parserError{err}
}
2013-02-02 16:17:01 +04:00
switch typ {
2012-08-20 14:42:33 +04:00
case stringSlice:
2013-02-02 16:17:01 +04:00
vals := make([]string, 0, repliesNum)
for i := int64(0); i < repliesNum; i++ {
vi, err := parseReply(rd)
2012-08-20 14:42:33 +04:00
if err != nil {
2012-08-17 22:36:48 +04:00
return nil, err
2013-02-02 16:17:01 +04:00
}
if v, ok := vi.(string); ok {
vals = append(vals, v)
2012-08-17 22:36:48 +04:00
} else {
2013-02-02 16:17:01 +04:00
return nil, errInvalidType
2012-08-17 22:36:48 +04:00
}
}
return vals, nil
2012-08-20 14:42:33 +04:00
case boolSlice:
2013-02-02 16:17:01 +04:00
vals := make([]bool, 0, repliesNum)
for i := int64(0); i < repliesNum; i++ {
vi, err := parseReply(rd)
2012-08-17 22:36:48 +04:00
if err != nil {
return nil, err
2013-02-02 16:17:01 +04:00
}
if v, ok := vi.(int64); ok {
vals = append(vals, v == 1)
2012-08-17 22:36:48 +04:00
} else {
2013-02-02 16:17:01 +04:00
return nil, errInvalidType
2012-08-20 14:42:33 +04:00
}
}
return vals, nil
2013-02-02 17:30:58 +04:00
case stringStringMap: // TODO: Consider handling Nil values.
2013-02-02 16:17:01 +04:00
m := make(map[string]string, repliesNum/2)
for i := int64(0); i < repliesNum; i += 2 {
keyI, err := parseReply(rd)
if err != nil {
return nil, err
}
key, ok := keyI.(string)
if !ok {
return nil, errInvalidType
}
valueI, err := parseReply(rd)
if err != nil {
return nil, err
}
value, ok := valueI.(string)
if !ok {
return nil, errInvalidType
}
m[key] = value
}
return m, nil
2013-02-02 17:30:58 +04:00
case stringFloatMap: // TODO: Consider handling Nil values.
2013-02-02 16:17:01 +04:00
m := make(map[string]float64, repliesNum/2)
for i := int64(0); i < repliesNum; i += 2 {
keyI, err := parseReply(rd)
if err != nil {
return nil, err
}
key, ok := keyI.(string)
if !ok {
return nil, errInvalidType
}
valueI, err := parseReply(rd)
if err != nil {
return nil, err
}
valueS, ok := valueI.(string)
if !ok {
return nil, errInvalidType
}
value, err := strconv.ParseFloat(valueS, 64)
if err != nil {
return nil, &parserError{err}
}
m[key] = value
}
return m, nil
2012-08-20 14:42:33 +04:00
default:
2013-02-02 16:17:01 +04:00
vals := make([]interface{}, 0, repliesNum)
for i := int64(0); i < repliesNum; i++ {
2012-08-20 14:42:33 +04:00
v, err := parseReply(rd)
if err == Nil {
vals = append(vals, nil)
} else if err != nil {
return nil, err
} else {
vals = append(vals, v)
2012-08-17 22:36:48 +04:00
}
}
return vals, nil
}
default:
2012-08-26 13:18:42 +04:00
return nil, &parserError{fmt.Errorf("redis: can't parse %q", line)}
}
panic("not reachable")
}