redis/internal/proto/reader.go

288 lines
5.4 KiB
Go
Raw Normal View History

2016-07-02 15:52:10 +03:00
package proto
import (
"fmt"
"io"
"strconv"
2018-02-22 15:14:30 +03:00
"github.com/go-redis/redis/internal/util"
2016-07-02 15:52:10 +03:00
)
2016-11-20 10:50:49 +03:00
const (
ErrorReply = '-'
StatusReply = '+'
IntReply = ':'
StringReply = '$'
ArrayReply = '*'
)
2016-07-02 15:52:10 +03:00
2018-02-22 15:14:30 +03:00
//------------------------------------------------------------------------------
const Nil = RedisError("redis: nil")
type RedisError string
func (e RedisError) Error() string { return string(e) }
//------------------------------------------------------------------------------
2018-08-15 09:38:58 +03:00
type MultiBulkParse func(Reader, int64) (interface{}, error)
2016-07-02 15:52:10 +03:00
type Reader struct {
2018-08-06 11:54:47 +03:00
src *ElasticBufReader
2016-07-02 15:52:10 +03:00
}
2018-08-15 09:38:58 +03:00
func NewReader(src *ElasticBufReader) Reader {
return Reader{
src: src,
2016-07-02 15:52:10 +03:00
}
}
2018-08-15 09:38:58 +03:00
func (r Reader) Reset(rd io.Reader) {
r.src.Reset(rd)
}
2018-08-15 09:38:58 +03:00
func (r Reader) Buffer() []byte {
2018-08-06 13:59:15 +03:00
return r.src.Buffer()
}
2018-08-15 09:38:58 +03:00
func (r Reader) ResetBuffer(buf []byte) {
2018-08-06 13:59:15 +03:00
r.src.ResetBuffer(buf)
}
2018-08-15 09:38:58 +03:00
func (r Reader) Bytes() []byte {
return r.src.Bytes()
2016-07-02 15:52:10 +03:00
}
2018-08-15 09:38:58 +03:00
func (r Reader) ReadLine() ([]byte, error) {
2018-08-05 15:07:10 +03:00
line, err := r.src.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, err
}
if len(line) == 0 {
2017-08-31 15:22:47 +03:00
return nil, fmt.Errorf("redis: reply is empty")
2016-07-02 15:52:10 +03:00
}
if isNilReply(line) {
2018-02-22 15:14:30 +03:00
return nil, Nil
2016-07-02 15:52:10 +03:00
}
return line, nil
}
2018-08-15 09:38:58 +03:00
func (r Reader) ReadReply(m MultiBulkParse) (interface{}, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, err
}
switch line[0] {
case ErrorReply:
2016-12-13 18:28:39 +03:00
return nil, ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case StatusReply:
2018-08-16 13:25:19 +03:00
return string(line[1:]), nil
2016-07-02 15:52:10 +03:00
case IntReply:
2018-02-22 15:14:30 +03:00
return util.ParseInt(line[1:], 10, 64)
2016-07-02 15:52:10 +03:00
case StringReply:
2018-08-16 13:25:19 +03:00
return r.readStringReply(line)
2016-07-02 15:52:10 +03:00
case ArrayReply:
n, err := parseArrayLen(line)
if err != nil {
return nil, err
}
2018-01-24 21:38:47 +03:00
return m(r, n)
2016-07-02 15:52:10 +03:00
}
return nil, fmt.Errorf("redis: can't parse %.100q", line)
}
2018-08-15 09:38:58 +03:00
func (r Reader) ReadIntReply() (int64, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return 0, err
}
switch line[0] {
case ErrorReply:
2016-12-13 18:28:39 +03:00
return 0, ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case IntReply:
2018-02-22 15:14:30 +03:00
return util.ParseInt(line[1:], 10, 64)
2016-07-02 15:52:10 +03:00
default:
return 0, fmt.Errorf("redis: can't parse int reply: %.100q", line)
}
}
2018-08-16 13:25:19 +03:00
func (r Reader) ReadString() (string, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return "", err
2016-07-02 15:52:10 +03:00
}
switch line[0] {
case ErrorReply:
2018-08-16 13:25:19 +03:00
return "", ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case StringReply:
2018-08-16 13:25:19 +03:00
return r.readStringReply(line)
2016-07-02 15:52:10 +03:00
case StatusReply:
2018-08-16 13:25:19 +03:00
return string(line[1:]), nil
case IntReply:
return string(line[1:]), nil
2016-07-02 15:52:10 +03:00
default:
2018-08-16 13:25:19 +03:00
return "", fmt.Errorf("redis: can't parse reply=%.100q reading string", line)
2016-07-02 15:52:10 +03:00
}
}
2018-08-16 13:25:19 +03:00
func (r Reader) readStringReply(line []byte) (string, error) {
if isNilReply(line) {
return "", Nil
2017-01-13 14:39:59 +03:00
}
2018-08-16 13:25:19 +03:00
replyLen, err := strconv.Atoi(string(line[1:]))
2016-07-02 15:52:10 +03:00
if err != nil {
return "", err
}
2018-08-16 13:25:19 +03:00
b := make([]byte, replyLen+2)
_, err = io.ReadFull(r.src, b)
2016-07-02 15:52:10 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return "", err
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
return util.BytesToString(b[:replyLen]), nil
2016-07-02 15:52:10 +03:00
}
2018-08-15 09:38:58 +03:00
func (r Reader) ReadArrayReply(m MultiBulkParse) (interface{}, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, err
}
switch line[0] {
case ErrorReply:
2016-12-13 18:28:39 +03:00
return nil, ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case ArrayReply:
n, err := parseArrayLen(line)
if err != nil {
return nil, err
}
2018-01-24 21:38:47 +03:00
return m(r, n)
2016-07-02 15:52:10 +03:00
default:
return nil, fmt.Errorf("redis: can't parse array reply: %.100q", line)
}
}
2018-08-15 09:38:58 +03:00
func (r Reader) ReadArrayLen() (int64, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return 0, err
}
switch line[0] {
case ErrorReply:
2016-12-13 18:28:39 +03:00
return 0, ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case ArrayReply:
return parseArrayLen(line)
default:
return 0, fmt.Errorf("redis: can't parse array reply: %.100q", line)
}
}
2018-08-15 09:38:58 +03:00
func (r Reader) ReadScanReply() ([]string, uint64, error) {
2018-01-24 21:38:47 +03:00
n, err := r.ReadArrayLen()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
if n != 2 {
return nil, 0, fmt.Errorf("redis: got %d elements in scan reply, expected 2", n)
}
2018-01-25 10:15:44 +03:00
cursor, err := r.ReadUint()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
2018-01-24 21:38:47 +03:00
n, err = r.ReadArrayLen()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
keys := make([]string, n)
for i := int64(0); i < n; i++ {
2018-08-16 13:25:19 +03:00
key, err := r.ReadString()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
keys[i] = key
}
return keys, cursor, err
}
2018-08-16 13:25:19 +03:00
func (r Reader) ReadInt() (int64, error) {
b, err := r.readTmpBytesReply()
if err != nil {
return 0, err
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
return util.ParseInt(b, 10, 64)
}
2016-07-02 15:52:10 +03:00
2018-08-16 13:25:19 +03:00
func (r Reader) ReadUint() (uint64, error) {
b, err := r.readTmpBytesReply()
2016-07-02 15:52:10 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return 0, err
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
return util.ParseUint(b, 10, 64)
}
2016-07-02 15:52:10 +03:00
2018-08-16 13:25:19 +03:00
func (r Reader) ReadFloatReply() (float64, error) {
b, err := r.readTmpBytesReply()
2016-07-02 15:52:10 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return 0, err
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
return util.ParseFloat(b, 64)
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
func (r Reader) readTmpBytesReply() ([]byte, error) {
line, err := r.ReadLine()
2017-01-13 14:39:59 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return nil, err
}
switch line[0] {
case ErrorReply:
return nil, ParseErrorReply(line)
case StringReply:
return r._readTmpBytesReply(line)
case StatusReply:
return line[1:], nil
default:
return nil, fmt.Errorf("redis: can't parse string reply: %.100q", line)
2017-01-13 14:39:59 +03:00
}
}
2018-08-16 13:25:19 +03:00
func (r Reader) _readTmpBytesReply(line []byte) ([]byte, error) {
if isNilReply(line) {
return nil, Nil
}
replyLen, err := strconv.Atoi(string(line[1:]))
2017-01-13 14:39:59 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return nil, err
2017-01-13 14:39:59 +03:00
}
2018-08-16 13:25:19 +03:00
b, err := r.src.ReadN(replyLen + 2)
if err != nil {
return nil, err
}
return b[:replyLen], nil
2017-01-13 14:39:59 +03:00
}
2016-07-02 15:52:10 +03:00
func isNilReply(b []byte) bool {
return len(b) == 3 &&
(b[0] == StringReply || b[0] == ArrayReply) &&
b[1] == '-' && b[2] == '1'
}
2016-12-13 18:28:39 +03:00
func ParseErrorReply(line []byte) error {
2018-02-22 15:14:30 +03:00
return RedisError(string(line[1:]))
2016-07-02 15:52:10 +03:00
}
func parseArrayLen(line []byte) (int64, error) {
if isNilReply(line) {
2018-02-22 15:14:30 +03:00
return 0, Nil
2016-07-02 15:52:10 +03:00
}
2018-02-22 15:14:30 +03:00
return util.ParseInt(line[1:], 10, 64)
2016-07-02 15:52:10 +03:00
}