2012-08-12 22:41:44 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-01-24 15:12:48 +03:00
|
|
|
"net"
|
2012-08-12 22:41:44 +04:00
|
|
|
"strconv"
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-04-09 13:27:16 +03:00
|
|
|
"gopkg.in/redis.v4/internal/pool"
|
2012-08-12 22:41:44 +04:00
|
|
|
)
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
const (
|
|
|
|
errorReply = '-'
|
|
|
|
statusReply = '+'
|
|
|
|
intReply = ':'
|
|
|
|
stringReply = '$'
|
|
|
|
arrayReply = '*'
|
|
|
|
)
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
type multiBulkParser func(cn *pool.Conn, n int64) (interface{}, error)
|
2014-05-11 11:42:40 +04:00
|
|
|
|
2012-08-12 22:41:44 +04:00
|
|
|
var (
|
2014-07-05 14:46:27 +04:00
|
|
|
errReaderTooSmall = errors.New("redis: reader is too small")
|
2012-08-12 22:41:44 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2015-05-28 15:51:19 +03:00
|
|
|
// Copy of encoding.BinaryMarshaler.
|
|
|
|
type binaryMarshaler interface {
|
|
|
|
MarshalBinary() (data []byte, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy of encoding.BinaryUnmarshaler.
|
|
|
|
type binaryUnmarshaler interface {
|
|
|
|
UnmarshalBinary(data []byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendString(b []byte, s string) []byte {
|
|
|
|
b = append(b, '$')
|
|
|
|
b = strconv.AppendUint(b, uint64(len(s)), 10)
|
|
|
|
b = append(b, '\r', '\n')
|
|
|
|
b = append(b, s...)
|
|
|
|
b = append(b, '\r', '\n')
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendBytes(b, bb []byte) []byte {
|
|
|
|
b = append(b, '$')
|
|
|
|
b = strconv.AppendUint(b, uint64(len(bb)), 10)
|
|
|
|
b = append(b, '\r', '\n')
|
|
|
|
b = append(b, bb...)
|
|
|
|
b = append(b, '\r', '\n')
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendArg(b []byte, val interface{}) ([]byte, error) {
|
|
|
|
switch v := val.(type) {
|
|
|
|
case nil:
|
|
|
|
b = appendString(b, "")
|
|
|
|
case string:
|
|
|
|
b = appendString(b, v)
|
|
|
|
case []byte:
|
|
|
|
b = appendBytes(b, v)
|
|
|
|
case int:
|
|
|
|
b = appendString(b, formatInt(int64(v)))
|
|
|
|
case int8:
|
|
|
|
b = appendString(b, formatInt(int64(v)))
|
|
|
|
case int16:
|
|
|
|
b = appendString(b, formatInt(int64(v)))
|
|
|
|
case int32:
|
|
|
|
b = appendString(b, formatInt(int64(v)))
|
|
|
|
case int64:
|
|
|
|
b = appendString(b, formatInt(v))
|
|
|
|
case uint:
|
|
|
|
b = appendString(b, formatUint(uint64(v)))
|
|
|
|
case uint8:
|
|
|
|
b = appendString(b, formatUint(uint64(v)))
|
|
|
|
case uint16:
|
|
|
|
b = appendString(b, formatUint(uint64(v)))
|
|
|
|
case uint32:
|
|
|
|
b = appendString(b, formatUint(uint64(v)))
|
|
|
|
case uint64:
|
|
|
|
b = appendString(b, formatUint(v))
|
|
|
|
case float32:
|
|
|
|
b = appendString(b, formatFloat(float64(v)))
|
|
|
|
case float64:
|
|
|
|
b = appendString(b, formatFloat(v))
|
|
|
|
case bool:
|
|
|
|
if v {
|
|
|
|
b = appendString(b, "1")
|
|
|
|
} else {
|
|
|
|
b = appendString(b, "0")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if bm, ok := val.(binaryMarshaler); ok {
|
|
|
|
bb, err := bm.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b = appendBytes(b, bb)
|
|
|
|
} else {
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"redis: can't marshal %T (consider implementing BinaryMarshaler)", val)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendArgs(b []byte, args []interface{}) ([]byte, error) {
|
2016-02-06 12:45:34 +03:00
|
|
|
b = append(b, arrayReply)
|
2015-05-28 15:51:19 +03:00
|
|
|
b = strconv.AppendUint(b, uint64(len(args)), 10)
|
|
|
|
b = append(b, '\r', '\n')
|
2012-08-12 22:41:44 +04:00
|
|
|
for _, arg := range args {
|
2015-05-28 15:51:19 +03:00
|
|
|
var err error
|
|
|
|
b, err = appendArg(b, arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scan(b []byte, val interface{}) error {
|
|
|
|
switch v := val.(type) {
|
|
|
|
case nil:
|
|
|
|
return errorf("redis: Scan(nil)")
|
|
|
|
case *string:
|
2015-07-18 10:39:03 +03:00
|
|
|
*v = bytesToString(b)
|
2015-05-28 15:51:19 +03:00
|
|
|
return nil
|
|
|
|
case *[]byte:
|
|
|
|
*v = b
|
|
|
|
return nil
|
|
|
|
case *int:
|
|
|
|
var err error
|
2015-07-18 10:39:03 +03:00
|
|
|
*v, err = strconv.Atoi(bytesToString(b))
|
2015-05-28 15:51:19 +03:00
|
|
|
return err
|
|
|
|
case *int8:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseInt(bytesToString(b), 10, 8)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = int8(n)
|
|
|
|
return nil
|
|
|
|
case *int16:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseInt(bytesToString(b), 10, 16)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = int16(n)
|
|
|
|
return nil
|
|
|
|
case *int32:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseInt(bytesToString(b), 10, 16)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = int32(n)
|
|
|
|
return nil
|
|
|
|
case *int64:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseInt(bytesToString(b), 10, 64)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = n
|
|
|
|
return nil
|
|
|
|
case *uint:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseUint(bytesToString(b), 10, 64)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = uint(n)
|
|
|
|
return nil
|
|
|
|
case *uint8:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseUint(bytesToString(b), 10, 8)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = uint8(n)
|
|
|
|
return nil
|
|
|
|
case *uint16:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseUint(bytesToString(b), 10, 16)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = uint16(n)
|
|
|
|
return nil
|
|
|
|
case *uint32:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseUint(bytesToString(b), 10, 32)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = uint32(n)
|
|
|
|
return nil
|
|
|
|
case *uint64:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseUint(bytesToString(b), 10, 64)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = n
|
|
|
|
return nil
|
|
|
|
case *float32:
|
2015-07-18 10:39:03 +03:00
|
|
|
n, err := strconv.ParseFloat(bytesToString(b), 32)
|
2015-05-28 15:51:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*v = float32(n)
|
|
|
|
return err
|
|
|
|
case *float64:
|
|
|
|
var err error
|
2015-07-18 10:39:03 +03:00
|
|
|
*v, err = strconv.ParseFloat(bytesToString(b), 64)
|
2015-05-28 15:51:19 +03:00
|
|
|
return err
|
|
|
|
case *bool:
|
|
|
|
*v = len(b) == 1 && b[0] == '1'
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
if bu, ok := val.(binaryUnmarshaler); ok {
|
|
|
|
return bu.UnmarshalBinary(b)
|
|
|
|
}
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", val)
|
|
|
|
return err
|
2012-08-12 22:41:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func readLine(cn *pool.Conn) ([]byte, error) {
|
|
|
|
line, isPrefix, err := cn.Rd.ReadLine()
|
2012-08-12 22:41:44 +04:00
|
|
|
if err != nil {
|
|
|
|
return line, err
|
|
|
|
}
|
|
|
|
if isPrefix {
|
|
|
|
return line, errReaderTooSmall
|
|
|
|
}
|
2015-12-22 12:02:18 +03:00
|
|
|
if isNilReply(line) {
|
|
|
|
return nil, Nil
|
|
|
|
}
|
2012-08-12 22:41:44 +04:00
|
|
|
return line, nil
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:02:18 +03:00
|
|
|
func isNilReply(b []byte) bool {
|
2016-02-06 12:45:34 +03:00
|
|
|
return len(b) == 3 &&
|
|
|
|
(b[0] == stringReply || b[0] == arrayReply) &&
|
|
|
|
b[1] == '-' && b[2] == '1'
|
2015-12-22 12:02:18 +03:00
|
|
|
}
|
|
|
|
|
2012-08-12 22:41:44 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func parseErrorReply(cn *pool.Conn, line []byte) error {
|
2015-10-07 17:09:20 +03:00
|
|
|
return errorf(string(line[1:]))
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func parseStatusReply(cn *pool.Conn, line []byte) ([]byte, error) {
|
2015-10-07 17:56:49 +03:00
|
|
|
return line[1:], nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func parseIntReply(cn *pool.Conn, line []byte) (int64, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
n, err := strconv.ParseInt(bytesToString(line[1:]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func readIntReply(cn *pool.Conn) (int64, error) {
|
2015-09-03 17:55:31 +03:00
|
|
|
line, err := readLine(cn)
|
2015-10-07 17:09:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
switch line[0] {
|
|
|
|
case errorReply:
|
|
|
|
return 0, parseErrorReply(cn, line)
|
|
|
|
case intReply:
|
|
|
|
return parseIntReply(cn, line)
|
|
|
|
default:
|
2015-10-07 17:56:49 +03:00
|
|
|
return 0, fmt.Errorf("redis: can't parse int reply: %.100q", line)
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func parseBytesReply(cn *pool.Conn, line []byte) ([]byte, error) {
|
2015-10-07 17:56:49 +03:00
|
|
|
if isNilReply(line) {
|
2015-10-07 17:09:20 +03:00
|
|
|
return nil, Nil
|
|
|
|
}
|
|
|
|
|
|
|
|
replyLen, err := strconv.Atoi(bytesToString(line[1:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
b, err := cn.ReadN(replyLen + 2)
|
2012-08-12 22:41:44 +04:00
|
|
|
if err != nil {
|
2014-05-11 11:42:40 +04:00
|
|
|
return nil, err
|
2012-08-12 22:41:44 +04:00
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
return b[:replyLen], nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func readBytesReply(cn *pool.Conn) ([]byte, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
line, err := readLine(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-08-12 22:41:44 +04:00
|
|
|
switch line[0] {
|
2015-10-07 17:09:20 +03:00
|
|
|
case errorReply:
|
|
|
|
return nil, parseErrorReply(cn, line)
|
|
|
|
case stringReply:
|
|
|
|
return parseBytesReply(cn, line)
|
2015-10-07 17:56:49 +03:00
|
|
|
case statusReply:
|
|
|
|
return parseStatusReply(cn, line)
|
2015-10-07 17:09:20 +03:00
|
|
|
default:
|
2015-10-07 17:56:49 +03:00
|
|
|
return nil, fmt.Errorf("redis: can't parse string reply: %.100q", line)
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
|
|
|
}
|
2012-08-12 22:41:44 +04:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func readStringReply(cn *pool.Conn) (string, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
b, err := readBytesReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
2012-08-12 22:41:44 +04:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func readFloatReply(cn *pool.Conn) (float64, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
b, err := readBytesReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return strconv.ParseFloat(bytesToString(b), 64)
|
|
|
|
}
|
2012-08-12 22:41:44 +04:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func parseArrayHeader(cn *pool.Conn, line []byte) (int64, error) {
|
2016-02-06 12:45:34 +03:00
|
|
|
if isNilReply(line) {
|
2015-10-07 17:09:20 +03:00
|
|
|
return 0, Nil
|
|
|
|
}
|
2012-08-26 13:18:42 +04:00
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
n, err := strconv.ParseInt(bytesToString(line[1:]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func parseArrayReply(cn *pool.Conn, p multiBulkParser, line []byte) (interface{}, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
n, err := parseArrayHeader(cn, line)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p(cn, n)
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func readArrayHeader(cn *pool.Conn) (int64, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
line, err := readLine(cn)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
switch line[0] {
|
|
|
|
case errorReply:
|
|
|
|
return 0, parseErrorReply(cn, line)
|
|
|
|
case arrayReply:
|
|
|
|
return parseArrayHeader(cn, line)
|
|
|
|
default:
|
2015-10-07 17:56:49 +03:00
|
|
|
return 0, fmt.Errorf("redis: can't parse array reply: %.100q", line)
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func readArrayReply(cn *pool.Conn, p multiBulkParser) (interface{}, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
line, err := readLine(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch line[0] {
|
|
|
|
case errorReply:
|
|
|
|
return nil, parseErrorReply(cn, line)
|
|
|
|
case arrayReply:
|
|
|
|
return parseArrayReply(cn, p, line)
|
|
|
|
default:
|
2015-10-07 17:56:49 +03:00
|
|
|
return nil, fmt.Errorf("redis: can't parse array reply: %.100q", line)
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func readReply(cn *pool.Conn, p multiBulkParser) (interface{}, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
line, err := readLine(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch line[0] {
|
|
|
|
case errorReply:
|
|
|
|
return nil, parseErrorReply(cn, line)
|
|
|
|
case statusReply:
|
2015-10-07 17:56:49 +03:00
|
|
|
return parseStatusReply(cn, line)
|
2015-10-07 17:09:20 +03:00
|
|
|
case intReply:
|
|
|
|
return parseIntReply(cn, line)
|
|
|
|
case stringReply:
|
|
|
|
return parseBytesReply(cn, line)
|
|
|
|
case arrayReply:
|
|
|
|
return parseArrayReply(cn, p, line)
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-10-07 17:56:49 +03:00
|
|
|
return nil, fmt.Errorf("redis: can't parse %.100q", line)
|
|
|
|
}
|
|
|
|
|
2016-04-09 13:07:42 +03:00
|
|
|
func readScanReply(cn *pool.Conn) ([]string, uint64, error) {
|
2015-10-07 17:56:49 +03:00
|
|
|
n, err := readArrayHeader(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
if n != 2 {
|
2016-03-08 17:00:33 +03:00
|
|
|
return nil, 0, fmt.Errorf("redis: got %d elements in scan reply, expected 2", n)
|
2015-10-07 17:56:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := readBytesReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2016-04-09 13:07:42 +03:00
|
|
|
cursor, err := strconv.ParseUint(bytesToString(b), 10, 64)
|
2015-10-07 17:56:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err = readArrayHeader(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]string, n)
|
|
|
|
for i := int64(0); i < n; i++ {
|
|
|
|
key, err := readStringReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
keys[i] = key
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, cursor, err
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func sliceParser(cn *pool.Conn, n int64) (interface{}, error) {
|
2014-05-11 11:42:40 +04:00
|
|
|
vals := make([]interface{}, 0, n)
|
|
|
|
for i := int64(0); i < n; i++ {
|
2015-10-07 17:09:20 +03:00
|
|
|
v, err := readReply(cn, sliceParser)
|
2014-05-11 11:42:40 +04:00
|
|
|
if err == Nil {
|
|
|
|
vals = append(vals, nil)
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
2015-05-28 15:51:19 +03:00
|
|
|
switch vv := v.(type) {
|
|
|
|
case []byte:
|
|
|
|
vals = append(vals, string(vv))
|
|
|
|
default:
|
|
|
|
vals = append(vals, v)
|
|
|
|
}
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return vals, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func intSliceParser(cn *pool.Conn, n int64) (interface{}, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
ints := make([]int64, 0, n)
|
2014-05-11 11:42:40 +04:00
|
|
|
for i := int64(0); i < n; i++ {
|
2015-10-07 17:09:20 +03:00
|
|
|
n, err := readIntReply(cn)
|
2014-05-11 11:42:40 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
ints = append(ints, n)
|
|
|
|
}
|
|
|
|
return ints, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func boolSliceParser(cn *pool.Conn, n int64) (interface{}, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
bools := make([]bool, 0, n)
|
|
|
|
for i := int64(0); i < n; i++ {
|
|
|
|
n, err := readIntReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
bools = append(bools, n == 1)
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
return bools, nil
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func stringSliceParser(cn *pool.Conn, n int64) (interface{}, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
ss := make([]string, 0, n)
|
2014-05-11 11:42:40 +04:00
|
|
|
for i := int64(0); i < n; i++ {
|
2015-10-07 17:09:20 +03:00
|
|
|
s, err := readStringReply(cn)
|
2016-01-22 13:29:23 +03:00
|
|
|
if err == Nil {
|
|
|
|
ss = append(ss, "")
|
|
|
|
} else if err != nil {
|
2014-05-11 11:42:40 +04:00
|
|
|
return nil, err
|
2016-01-22 13:29:23 +03:00
|
|
|
} else {
|
|
|
|
ss = append(ss, s)
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func floatSliceParser(cn *pool.Conn, n int64) (interface{}, error) {
|
2015-10-07 17:09:20 +03:00
|
|
|
nn := make([]float64, 0, n)
|
|
|
|
for i := int64(0); i < n; i++ {
|
|
|
|
n, err := readFloatReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
nn = append(nn, n)
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
return nn, nil
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func stringStringMapParser(cn *pool.Conn, 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 {
|
2015-10-07 17:09:20 +03:00
|
|
|
key, err := readStringReply(cn)
|
2014-05-11 11:42:40 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
value, err := readStringReply(cn)
|
2014-05-11 11:42:40 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
m[key] = value
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func stringIntMapParser(cn *pool.Conn, n int64) (interface{}, error) {
|
2015-01-25 15:05:19 +03:00
|
|
|
m := make(map[string]int64, n/2)
|
|
|
|
for i := int64(0); i < n; i += 2 {
|
2015-10-07 17:09:20 +03:00
|
|
|
key, err := readStringReply(cn)
|
2015-01-25 15:05:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
n, err := readIntReply(cn)
|
2015-01-25 15:05:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
|
|
|
|
m[key] = n
|
2015-01-25 15:05:19 +03:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func zSliceParser(cn *pool.Conn, n int64) (interface{}, error) {
|
2014-07-05 14:46:27 +04:00
|
|
|
zz := make([]Z, n/2)
|
2014-05-11 11:42:40 +04:00
|
|
|
for i := int64(0); i < n; i += 2 {
|
2015-10-07 17:09:20 +03:00
|
|
|
var err error
|
|
|
|
|
2014-07-05 14:46:27 +04:00
|
|
|
z := &zz[i/2]
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
z.Member, err = readStringReply(cn)
|
2014-05-11 11:42:40 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
z.Score, err = readFloatReply(cn)
|
2014-05-11 11:42:40 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-08-12 22:41:44 +04:00
|
|
|
}
|
2014-07-05 14:46:27 +04:00
|
|
|
return zz, nil
|
2012-08-12 22:41:44 +04:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2016-04-09 12:52:43 +03:00
|
|
|
func clusterSlotsParser(cn *pool.Conn, slotNum int64) (interface{}, error) {
|
|
|
|
slots := make([]ClusterSlot, slotNum)
|
|
|
|
for slotInd := 0; slotInd < len(slots); slotInd++ {
|
2015-10-07 17:09:20 +03:00
|
|
|
n, err := readArrayHeader(cn)
|
2015-01-24 15:12:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
if n < 2 {
|
2015-10-07 17:56:49 +03:00
|
|
|
err := fmt.Errorf("redis: got %d elements in cluster info, expected at least 2", n)
|
|
|
|
return nil, err
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
start, err := readIntReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
end, err := readIntReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
|
2016-04-09 12:52:43 +03:00
|
|
|
nodes := make([]ClusterNode, n-2)
|
|
|
|
for nodeInd := 0; nodeInd < len(nodes); nodeInd++ {
|
2015-10-07 17:09:20 +03:00
|
|
|
n, err := readArrayHeader(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-06 12:45:34 +03:00
|
|
|
if n != 2 && n != 3 {
|
|
|
|
err := fmt.Errorf("got %d elements in cluster info address, expected 2 or 3", n)
|
|
|
|
return nil, err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
ip, err := readStringReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
|
|
|
|
port, err := readIntReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2016-04-09 12:52:43 +03:00
|
|
|
nodes[nodeInd].Addr = net.JoinHostPort(ip, strconv.FormatInt(port, 10))
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2016-02-06 12:45:34 +03:00
|
|
|
if n == 3 {
|
2016-04-09 12:52:43 +03:00
|
|
|
id, err := readStringReply(cn)
|
2016-02-06 12:45:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-09 12:52:43 +03:00
|
|
|
nodes[nodeInd].Id = id
|
2016-02-06 12:45:34 +03:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
|
2016-04-09 12:52:43 +03:00
|
|
|
slots[slotInd] = ClusterSlot{
|
|
|
|
Start: int(start),
|
|
|
|
End: int(end),
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2016-04-09 12:52:43 +03:00
|
|
|
return slots, nil
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
|
2015-11-14 17:36:21 +03:00
|
|
|
func newGeoLocationParser(q *GeoRadiusQuery) multiBulkParser {
|
2016-03-12 11:52:13 +03:00
|
|
|
return func(cn *pool.Conn, n int64) (interface{}, error) {
|
2015-11-14 17:36:21 +03:00
|
|
|
var loc GeoLocation
|
2015-10-07 17:09:20 +03:00
|
|
|
|
2015-11-14 17:36:21 +03:00
|
|
|
var err error
|
|
|
|
loc.Name, err = readStringReply(cn)
|
2015-10-07 17:09:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-14 17:36:21 +03:00
|
|
|
if q.WithDist {
|
|
|
|
loc.Dist, err = readFloatReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
2015-11-14 17:36:21 +03:00
|
|
|
if q.WithGeoHash {
|
|
|
|
loc.GeoHash, err = readIntReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
2015-11-14 17:36:21 +03:00
|
|
|
if q.WithCoord {
|
|
|
|
n, err := readArrayHeader(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != 2 {
|
|
|
|
return nil, fmt.Errorf("got %d coordinates, expected 2", n)
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
|
2015-11-14 17:36:21 +03:00
|
|
|
loc.Longitude, err = readFloatReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
loc.Latitude, err = readFloatReply(cn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
|
|
|
|
2015-11-14 17:36:21 +03:00
|
|
|
return &loc, nil
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
|
|
|
|
2015-11-14 17:36:21 +03:00
|
|
|
func newGeoLocationSliceParser(q *GeoRadiusQuery) multiBulkParser {
|
2016-03-12 11:52:13 +03:00
|
|
|
return func(cn *pool.Conn, n int64) (interface{}, error) {
|
2015-11-14 17:36:21 +03:00
|
|
|
locs := make([]GeoLocation, 0, n)
|
|
|
|
for i := int64(0); i < n; i++ {
|
|
|
|
v, err := readReply(cn, newGeoLocationParser(q))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch vv := v.(type) {
|
|
|
|
case []byte:
|
|
|
|
locs = append(locs, GeoLocation{
|
|
|
|
Name: string(vv),
|
|
|
|
})
|
|
|
|
case *GeoLocation:
|
|
|
|
locs = append(locs, *vv)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("got %T, expected string or *GeoLocation", v)
|
|
|
|
}
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
2015-11-14 17:36:21 +03:00
|
|
|
return locs, nil
|
2015-10-07 17:09:20 +03:00
|
|
|
}
|
|
|
|
}
|