redis/internal/proto/reader.go

295 lines
5.7 KiB
Go
Raw Normal View History

2016-07-02 15:52:10 +03:00
package proto
import (
2018-08-17 13:56:37 +03:00
"bufio"
2016-07-02 15:52:10 +03:00
"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-17 13:56:37 +03:00
type MultiBulkParse func(*Reader, int64) (interface{}, error)
2016-07-02 15:52:10 +03:00
type Reader struct {
2018-08-17 13:56:37 +03:00
rd *bufio.Reader
_buf []byte
2016-07-02 15:52:10 +03:00
}
2018-08-17 13:56:37 +03:00
func NewReader(rd io.Reader) *Reader {
return &Reader{
rd: bufio.NewReader(rd),
_buf: make([]byte, 64),
2016-07-02 15:52:10 +03:00
}
}
2018-08-17 13:56:37 +03:00
func (r *Reader) Reset(rd io.Reader) {
r.rd.Reset(rd)
}
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadLine() ([]byte, error) {
line, isPrefix, err := r.rd.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, err
}
2018-08-17 13:56:37 +03:00
if isPrefix {
return nil, bufio.ErrBufferFull
}
2016-07-02 15:52:10 +03:00
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-17 13:56:37 +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
}
if m == nil {
err := fmt.Errorf("redis: got %.100q, but multi bulk parser is nil", line)
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-17 13:56:37 +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-17 13:56:37 +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-17 13:56:37 +03:00
func (r *Reader) readStringReply(line []byte) (string, error) {
2018-08-16 13:25:19 +03:00
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)
2018-08-17 13:56:37 +03:00
_, err = io.ReadFull(r.rd, 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-17 13:56:37 +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-17 13:56:37 +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-17 13:56:37 +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-17 13:56:37 +03:00
func (r *Reader) ReadInt() (int64, error) {
2018-08-16 13:25:19 +03:00
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-17 13:56:37 +03:00
func (r *Reader) ReadUint() (uint64, error) {
2018-08-16 13:25:19 +03:00
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-17 13:56:37 +03:00
func (r *Reader) ReadFloatReply() (float64, error) {
2018-08-16 13:25:19 +03:00
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-17 13:56:37 +03:00
func (r *Reader) readTmpBytesReply() ([]byte, error) {
2018-08-16 13:25:19 +03:00
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-17 13:56:37 +03:00
func (r *Reader) _readTmpBytesReply(line []byte) ([]byte, error) {
2018-08-16 13:25:19 +03:00
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
2018-08-17 13:56:37 +03:00
buf := r.buf(replyLen + 2)
_, err = io.ReadFull(r.rd, buf)
2018-08-16 13:25:19 +03:00
if err != nil {
return nil, err
}
2018-08-17 13:56:37 +03:00
return buf[:replyLen], nil
}
func (r *Reader) buf(n int) []byte {
if d := n - cap(r._buf); d > 0 {
r._buf = append(r._buf, make([]byte, d)...)
}
return r._buf[:n]
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
}