redis/parser.go

263 lines
5.4 KiB
Go
Raw Normal View History

package redis
import (
"errors"
"fmt"
"strconv"
2014-06-18 17:47:21 +04:00
"gopkg.in/bufio.v1"
)
2014-06-18 17:47:21 +04:00
type multiBulkParser func(rd *bufio.Reader, n int64) (interface{}, error)
2014-05-11 11:42:40 +04:00
var (
2014-07-05 14:46:27 +04:00
errReaderTooSmall = errors.New("redis: reader is too small")
)
//------------------------------------------------------------------------------
2014-06-28 15:47:37 +04:00
func appendArgs(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
}
//------------------------------------------------------------------------------
2014-06-18 17:47:21 +04:00
func readLine(rd *bufio.Reader) ([]byte, error) {
line, isPrefix, err := rd.ReadLine()
if err != nil {
return line, err
}
if isPrefix {
return line, errReaderTooSmall
}
return line, nil
}
2014-06-18 17:47:21 +04:00
func readN(rd *bufio.Reader, n int) ([]byte, error) {
2013-03-21 14:28:19 +04:00
b, err := rd.ReadN(n)
if err == bufio.ErrBufferFull {
2014-06-18 17:47:21 +04:00
tmp := make([]byte, n)
r := copy(tmp, b)
b = tmp
for {
2013-03-21 14:28:19 +04:00
nn, err := rd.Read(b[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
}
2013-03-21 14:28:19 +04:00
return b, nil
}
//------------------------------------------------------------------------------
2014-06-18 17:47:21 +04:00
func parseReq(rd *bufio.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
}
//------------------------------------------------------------------------------
2014-06-18 17:47:21 +04:00
func parseReply(rd *bufio.Reader, p multiBulkParser) (interface{}, error) {
line, err := readLine(rd)
if err != nil {
2014-05-11 11:42:40 +04:00
return nil, err
}
switch line[0] {
case '-':
return nil, errorf(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 {
2014-05-11 11:42:40 +04:00
return nil, err
2012-08-26 13:18:42 +04:00
}
return v, nil
case '$':
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
2014-05-11 11:42:40 +04:00
return nil, Nil
}
2014-07-04 16:19:45 +04:00
replyLen, err := strconv.Atoi(string(line[1:]))
if err != nil {
2014-05-11 11:42:40 +04:00
return nil, err
}
2014-07-04 16:19:45 +04:00
b, err := readN(rd, replyLen+2)
if err != nil {
2014-05-11 11:42:40 +04:00
return nil, err
}
2014-07-04 16:19:45 +04:00
return string(b[:replyLen]), 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 {
2014-05-11 11:42:40 +04:00
return nil, err
2012-08-26 13:18:42 +04:00
}
2014-05-11 11:42:40 +04:00
return p(rd, repliesNum)
}
return nil, fmt.Errorf("redis: can't parse %q", line)
}
2014-06-18 17:47:21 +04:00
func parseSlice(rd *bufio.Reader, n int64) (interface{}, error) {
2014-05-11 11:42:40 +04:00
vals := make([]interface{}, 0, n)
for i := int64(0); i < n; i++ {
v, err := parseReply(rd, parseSlice)
if err == Nil {
vals = append(vals, nil)
} else if err != nil {
return nil, err
} else {
vals = append(vals, v)
}
}
return vals, nil
}
2014-06-18 17:47:21 +04:00
func parseStringSlice(rd *bufio.Reader, n int64) (interface{}, error) {
2014-05-11 11:42:40 +04:00
vals := make([]string, 0, n)
for i := int64(0); i < n; i++ {
2014-07-05 14:46:27 +04:00
viface, err := parseReply(rd, nil)
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2014-07-05 14:46:27 +04:00
v, ok := viface.(string)
if !ok {
return nil, fmt.Errorf("got %T, expected string", viface)
2014-05-11 11:42:40 +04:00
}
2014-07-05 14:46:27 +04:00
vals = append(vals, v)
2014-05-11 11:42:40 +04:00
}
return vals, nil
}
2014-06-18 17:47:21 +04:00
func parseBoolSlice(rd *bufio.Reader, n int64) (interface{}, error) {
2014-05-11 11:42:40 +04:00
vals := make([]bool, 0, n)
for i := int64(0); i < n; i++ {
2014-07-05 14:46:27 +04:00
viface, err := parseReply(rd, nil)
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2014-07-05 14:46:27 +04:00
v, ok := viface.(int64)
if !ok {
return nil, fmt.Errorf("got %T, expected int64", viface)
2014-05-11 11:42:40 +04:00
}
2014-07-05 14:46:27 +04:00
vals = append(vals, v == 1)
2014-05-11 11:42:40 +04:00
}
return vals, nil
}
2014-06-18 17:47:21 +04:00
func parseStringStringMap(rd *bufio.Reader, n int64) (interface{}, error) {
2014-05-11 11:42:40 +04:00
m := make(map[string]string, n/2)
for i := int64(0); i < n; i += 2 {
2014-07-05 14:46:27 +04:00
keyiface, err := parseReply(rd, nil)
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2014-07-05 14:46:27 +04:00
key, ok := keyiface.(string)
2014-05-11 11:42:40 +04:00
if !ok {
2014-07-05 14:46:27 +04:00
return nil, fmt.Errorf("got %T, expected string", keyiface)
2014-05-11 11:42:40 +04:00
}
2014-07-05 14:46:27 +04:00
valueiface, err := parseReply(rd, nil)
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2014-07-05 14:46:27 +04:00
value, ok := valueiface.(string)
2014-05-11 11:42:40 +04:00
if !ok {
2014-07-05 14:46:27 +04:00
return nil, fmt.Errorf("got %T, expected string", valueiface)
2014-05-11 11:42:40 +04:00
}
m[key] = value
}
return m, nil
}
2014-07-05 14:46:27 +04:00
func parseZSlice(rd *bufio.Reader, n int64) (interface{}, error) {
zz := make([]Z, n/2)
2014-05-11 11:42:40 +04:00
for i := int64(0); i < n; i += 2 {
2014-07-05 14:46:27 +04:00
z := &zz[i/2]
memberiface, err := parseReply(rd, nil)
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2014-07-05 14:46:27 +04:00
member, ok := memberiface.(string)
2014-05-11 11:42:40 +04:00
if !ok {
2014-07-05 14:46:27 +04:00
return nil, fmt.Errorf("got %T, expected string", memberiface)
2012-08-17 22:36:48 +04:00
}
2014-07-05 14:46:27 +04:00
z.Member = member
2014-05-11 11:42:40 +04:00
2014-07-05 14:46:27 +04:00
scoreiface, err := parseReply(rd, nil)
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2014-07-05 14:46:27 +04:00
scorestr, ok := scoreiface.(string)
2014-05-11 11:42:40 +04:00
if !ok {
2014-07-05 14:46:27 +04:00
return nil, fmt.Errorf("got %T, expected string", scoreiface)
2014-05-11 11:42:40 +04:00
}
2014-07-05 14:46:27 +04:00
score, err := strconv.ParseFloat(scorestr, 64)
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2014-07-05 14:46:27 +04:00
z.Score = score
}
2014-07-05 14:46:27 +04:00
return zz, nil
}