mirror of
https://github.com/redis/go-redis.git
synced 2025-12-03 18:31:14 +03:00
Get rid of custom bufio package.
This commit is contained in:
133
parser.go
133
parser.go
@@ -3,13 +3,12 @@ package redis
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/bufio.v1"
|
||||
)
|
||||
|
||||
type multiBulkParser func(rd *bufio.Reader, n int64) (interface{}, error)
|
||||
type multiBulkParser func(cn *conn, n int64) (interface{}, error)
|
||||
|
||||
var (
|
||||
errReaderTooSmall = errors.New("redis: reader is too small")
|
||||
@@ -216,8 +215,8 @@ func scan(b []byte, val interface{}) error {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func readLine(rd *bufio.Reader) ([]byte, error) {
|
||||
line, isPrefix, err := rd.ReadLine()
|
||||
func readLine(cn *conn) ([]byte, error) {
|
||||
line, isPrefix, err := cn.rd.ReadLine()
|
||||
if err != nil {
|
||||
return line, err
|
||||
}
|
||||
@@ -227,74 +226,21 @@ func readLine(rd *bufio.Reader) ([]byte, error) {
|
||||
return line, nil
|
||||
}
|
||||
|
||||
func readN(rd *bufio.Reader, n int) ([]byte, error) {
|
||||
b, err := rd.ReadN(n)
|
||||
if err == bufio.ErrBufferFull {
|
||||
tmp := make([]byte, n)
|
||||
r := copy(tmp, b)
|
||||
b = tmp
|
||||
|
||||
for {
|
||||
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
|
||||
func readN(cn *conn, n int) ([]byte, error) {
|
||||
var b []byte
|
||||
if cap(cn.buf) < n {
|
||||
b = make([]byte, n)
|
||||
} else {
|
||||
b = cn.buf[:n]
|
||||
}
|
||||
return b, nil
|
||||
_, err := io.ReadFull(cn.rd, b)
|
||||
return b, err
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
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] != '$' {
|
||||
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
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func parseReply(rd *bufio.Reader, p multiBulkParser) (interface{}, error) {
|
||||
line, err := readLine(rd)
|
||||
func parseReply(cn *conn, p multiBulkParser) (interface{}, error) {
|
||||
line, err := readLine(cn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -315,12 +261,12 @@ func parseReply(rd *bufio.Reader, p multiBulkParser) (interface{}, error) {
|
||||
return nil, Nil
|
||||
}
|
||||
|
||||
replyLen, err := strconv.Atoi(string(line[1:]))
|
||||
replyLen, err := strconv.Atoi(bytesToString(line[1:]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b, err := readN(rd, replyLen+2)
|
||||
b, err := readN(cn, replyLen+2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -335,15 +281,15 @@ func parseReply(rd *bufio.Reader, p multiBulkParser) (interface{}, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return p(rd, repliesNum)
|
||||
return p(cn, repliesNum)
|
||||
}
|
||||
return nil, fmt.Errorf("redis: can't parse %q", line)
|
||||
}
|
||||
|
||||
func parseSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
func parseSlice(cn *conn, n int64) (interface{}, error) {
|
||||
vals := make([]interface{}, 0, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
v, err := parseReply(rd, parseSlice)
|
||||
v, err := parseReply(cn, parseSlice)
|
||||
if err == Nil {
|
||||
vals = append(vals, nil)
|
||||
} else if err != nil {
|
||||
@@ -360,10 +306,10 @@ func parseSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
return vals, nil
|
||||
}
|
||||
|
||||
func parseStringSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
func parseStringSlice(cn *conn, n int64) (interface{}, error) {
|
||||
vals := make([]string, 0, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
viface, err := parseReply(rd, nil)
|
||||
viface, err := parseReply(cn, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -376,10 +322,10 @@ func parseStringSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
return vals, nil
|
||||
}
|
||||
|
||||
func parseBoolSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
func parseBoolSlice(cn *conn, n int64) (interface{}, error) {
|
||||
vals := make([]bool, 0, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
viface, err := parseReply(rd, nil)
|
||||
viface, err := parseReply(cn, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -392,36 +338,37 @@ func parseBoolSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
return vals, nil
|
||||
}
|
||||
|
||||
func parseStringStringMap(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
func parseStringStringMap(cn *conn, n int64) (interface{}, error) {
|
||||
m := make(map[string]string, n/2)
|
||||
for i := int64(0); i < n; i += 2 {
|
||||
keyiface, err := parseReply(rd, nil)
|
||||
keyIface, err := parseReply(cn, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key, ok := keyiface.([]byte)
|
||||
keyBytes, ok := keyIface.([]byte)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("got %T, expected string", keyiface)
|
||||
return nil, fmt.Errorf("got %T, expected []byte", keyIface)
|
||||
}
|
||||
key := string(keyBytes)
|
||||
|
||||
valueiface, err := parseReply(rd, nil)
|
||||
valueIface, err := parseReply(cn, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value, ok := valueiface.([]byte)
|
||||
valueBytes, ok := valueIface.([]byte)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("got %T, expected string", valueiface)
|
||||
return nil, fmt.Errorf("got %T, expected []byte", valueIface)
|
||||
}
|
||||
|
||||
m[string(key)] = string(value)
|
||||
m[key] = string(valueBytes)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func parseStringIntMap(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
func parseStringIntMap(cn *conn, n int64) (interface{}, error) {
|
||||
m := make(map[string]int64, n/2)
|
||||
for i := int64(0); i < n; i += 2 {
|
||||
keyiface, err := parseReply(rd, nil)
|
||||
keyiface, err := parseReply(cn, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -430,7 +377,7 @@ func parseStringIntMap(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
return nil, fmt.Errorf("got %T, expected string", keyiface)
|
||||
}
|
||||
|
||||
valueiface, err := parseReply(rd, nil)
|
||||
valueiface, err := parseReply(cn, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -449,12 +396,12 @@ func parseStringIntMap(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func parseZSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
func parseZSlice(cn *conn, n int64) (interface{}, error) {
|
||||
zz := make([]Z, n/2)
|
||||
for i := int64(0); i < n; i += 2 {
|
||||
z := &zz[i/2]
|
||||
|
||||
memberiface, err := parseReply(rd, nil)
|
||||
memberiface, err := parseReply(cn, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -464,7 +411,7 @@ func parseZSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
}
|
||||
z.Member = string(member)
|
||||
|
||||
scoreiface, err := parseReply(rd, nil)
|
||||
scoreiface, err := parseReply(cn, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -481,10 +428,10 @@ func parseZSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
return zz, nil
|
||||
}
|
||||
|
||||
func parseClusterSlotInfoSlice(rd *bufio.Reader, n int64) (interface{}, error) {
|
||||
func parseClusterSlotInfoSlice(cn *conn, n int64) (interface{}, error) {
|
||||
infos := make([]ClusterSlotInfo, 0, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
viface, err := parseReply(rd, parseSlice)
|
||||
viface, err := parseReply(cn, parseSlice)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user