redis/internal/proto/reader.go

278 lines
5.2 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-09 11:04:37 +03:00
const bytesAllocLimit = 1024 * 1024 // 1mb
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) }
//------------------------------------------------------------------------------
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-06 11:54:47 +03:00
func NewReader(src *ElasticBufReader) *Reader {
2016-07-02 15:52:10 +03:00
return &Reader{
src: src,
2016-07-02 15:52:10 +03:00
}
}
func (r *Reader) Reset(rd io.Reader) {
r.src.Reset(rd)
}
2018-08-06 13:59:15 +03:00
func (r *Reader) Buffer() []byte {
return r.src.Buffer()
}
func (r *Reader) ResetBuffer(buf []byte) {
r.src.ResetBuffer(buf)
}
func (r *Reader) Bytes() []byte {
return r.src.Bytes()
2016-07-02 15:52:10 +03:00
}
2018-01-24 21:38:47 +03:00
func (r *Reader) ReadN(n int) ([]byte, error) {
return r.src.ReadN(n)
2016-07-02 15:52:10 +03:00
}
2018-01-24 21:38:47 +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-01-24 21:38:47 +03:00
func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) {
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-07-22 09:27:36 +03:00
return parseTmpStatusReply(line), 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-07-22 09:27:36 +03:00
return r.readTmpBytesReply(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-01-24 21:38:47 +03:00
func (r *Reader) ReadIntReply() (int64, error) {
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-01-24 21:38:47 +03:00
func (r *Reader) ReadTmpBytesReply() ([]byte, error) {
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 StringReply:
2018-07-22 09:27:36 +03:00
return r.readTmpBytesReply(line)
2016-07-02 15:52:10 +03:00
case StatusReply:
2018-07-22 09:27:36 +03:00
return parseTmpStatusReply(line), nil
2016-07-02 15:52:10 +03:00
default:
return nil, fmt.Errorf("redis: can't parse string reply: %.100q", line)
}
}
2017-01-13 14:39:59 +03:00
func (r *Reader) ReadBytesReply() ([]byte, error) {
b, err := r.ReadTmpBytesReply()
if err != nil {
return nil, err
}
cp := make([]byte, len(b))
copy(cp, b)
return cp, nil
}
2018-01-24 21:38:47 +03:00
func (r *Reader) ReadStringReply() (string, error) {
b, err := r.ReadTmpBytesReply()
2016-07-02 15:52:10 +03:00
if err != nil {
return "", err
}
return string(b), nil
}
2018-01-24 21:38:47 +03:00
func (r *Reader) ReadFloatReply() (float64, error) {
b, err := r.ReadTmpBytesReply()
2016-07-02 15:52:10 +03:00
if err != nil {
return 0, err
}
2018-02-22 15:14:30 +03:00
return util.ParseFloat(b, 64)
2016-07-02 15:52:10 +03:00
}
2018-01-24 21:38:47 +03:00
func (r *Reader) ReadArrayReply(m MultiBulkParse) (interface{}, error) {
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-01-24 21:38:47 +03:00
func (r *Reader) ReadArrayLen() (int64, error) {
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-01-24 21:38:47 +03:00
func (r *Reader) ReadScanReply() ([]string, uint64, error) {
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-01-24 21:38:47 +03:00
key, err := r.ReadStringReply()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
keys[i] = key
}
return keys, cursor, err
}
2018-07-22 09:27:36 +03:00
func (r *Reader) readTmpBytesReply(line []byte) ([]byte, error) {
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
}
replyLen, err := strconv.Atoi(string(line[1:]))
if err != nil {
return nil, err
}
2018-01-24 21:38:47 +03:00
b, err := r.ReadN(replyLen + 2)
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, err
}
return b[:replyLen], nil
}
2017-01-13 14:39:59 +03:00
func (r *Reader) ReadInt() (int64, error) {
b, err := r.ReadTmpBytesReply()
if err != nil {
return 0, err
}
2018-02-22 15:14:30 +03:00
return util.ParseInt(b, 10, 64)
2017-01-13 14:39:59 +03:00
}
func (r *Reader) ReadUint() (uint64, error) {
b, err := r.ReadTmpBytesReply()
if err != nil {
return 0, err
}
2018-02-22 15:14:30 +03:00
return util.ParseUint(b, 10, 64)
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
}
2018-07-22 09:27:36 +03:00
func parseTmpStatusReply(line []byte) []byte {
return 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
}