redis/parser.go

384 lines
7.6 KiB
Go
Raw Normal View History

package redis
import (
"fmt"
2015-01-24 15:12:48 +03:00
"net"
"strconv"
2016-07-02 15:52:10 +03:00
"gopkg.in/redis.v4/internal/proto"
)
2016-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func sliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2014-05-11 11:42:40 +04:00
vals := make([]interface{}, 0, n)
for i := int64(0); i < n; i++ {
2016-07-02 15:52:10 +03:00
v, err := rd.ReadReply(sliceParser)
2014-05-11 11:42:40 +04:00
if err == Nil {
vals = append(vals, nil)
} else if err != nil {
return nil, err
} else {
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-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func intSliceParser(rd *proto.Reader, 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++ {
2016-07-02 15:52:10 +03:00
n, err := rd.ReadIntReply()
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-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func boolSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2015-10-07 17:09:20 +03:00
bools := make([]bool, 0, n)
for i := int64(0); i < n; i++ {
2016-07-02 15:52:10 +03:00
n, err := rd.ReadIntReply()
2015-10-07 17:09:20 +03:00
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-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func stringSliceParser(rd *proto.Reader, 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++ {
2016-07-02 15:52:10 +03:00
s, err := rd.ReadStringReply()
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-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func floatSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
2015-10-07 17:09:20 +03:00
nn := make([]float64, 0, n)
for i := int64(0); i < n; i++ {
2016-07-02 15:52:10 +03:00
n, err := rd.ReadFloatReply()
2015-10-07 17:09:20 +03:00
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-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func stringStringMapParser(rd *proto.Reader, 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 {
2016-07-02 15:52:10 +03:00
key, err := rd.ReadStringReply()
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2016-07-02 15:52:10 +03:00
value, err := rd.ReadStringReply()
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-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func stringIntMapParser(rd *proto.Reader, 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 {
2016-07-02 15:52:10 +03:00
key, err := rd.ReadStringReply()
2015-01-25 15:05:19 +03:00
if err != nil {
return nil, err
}
2016-07-02 15:52:10 +03:00
n, err := rd.ReadIntReply()
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-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func zSliceParser(rd *proto.Reader, 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]
2016-07-02 15:52:10 +03:00
z.Member, err = rd.ReadStringReply()
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
2016-07-02 15:52:10 +03:00
z.Score, err = rd.ReadFloatReply()
2014-05-11 11:42:40 +04:00
if err != nil {
return nil, err
}
}
2014-07-05 14:46:27 +04:00
return zz, nil
}
2015-01-24 15:12:48 +03:00
2016-07-02 15:52:10 +03:00
// Implements proto.MultiBulkParse
func clusterSlotsParser(rd *proto.Reader, n int64) (interface{}, error) {
slots := make([]ClusterSlot, n)
for i := 0; i < len(slots); i++ {
n, err := rd.ReadArrayLen()
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
2016-07-02 15:52:10 +03:00
start, err := rd.ReadIntReply()
2015-10-07 17:09:20 +03:00
if err != nil {
return nil, err
2015-01-24 15:12:48 +03:00
}
2016-07-02 15:52:10 +03:00
end, err := rd.ReadIntReply()
2015-10-07 17:09:20 +03:00
if err != nil {
return nil, err
2015-01-24 15:12:48 +03:00
}
2015-10-07 17:09:20 +03:00
nodes := make([]ClusterNode, n-2)
2016-07-02 15:52:10 +03:00
for j := 0; j < len(nodes); j++ {
n, err := rd.ReadArrayLen()
2015-10-07 17:09:20 +03:00
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
}
2016-07-02 15:52:10 +03:00
ip, err := rd.ReadStringReply()
2015-10-07 17:09:20 +03:00
if err != nil {
return nil, err
2015-01-24 15:12:48 +03:00
}
2015-10-07 17:09:20 +03:00
2016-07-02 15:52:10 +03:00
port, err := rd.ReadIntReply()
2015-10-07 17:09:20 +03:00
if err != nil {
return nil, err
2015-01-24 15:12:48 +03:00
}
2016-07-02 15:52:10 +03:00
nodes[j].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-07-02 15:52:10 +03:00
id, err := rd.ReadStringReply()
2016-02-06 12:45:34 +03:00
if err != nil {
return nil, err
}
2016-07-02 15:52:10 +03:00
nodes[j].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-07-02 15:52:10 +03:00
slots[i] = ClusterSlot{
Start: int(start),
End: int(end),
Nodes: nodes,
}
2015-01-24 15:12:48 +03:00
}
return slots, nil
2015-01-24 15:12:48 +03:00
}
2015-10-07 17:09:20 +03:00
2016-07-02 15:52:10 +03:00
func newGeoLocationParser(q *GeoRadiusQuery) proto.MultiBulkParse {
return func(rd *proto.Reader, n int64) (interface{}, error) {
2015-11-14 17:36:21 +03:00
var loc GeoLocation
var err error
2016-07-02 15:52:10 +03:00
loc.Name, err = rd.ReadStringReply()
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 {
2016-07-02 15:52:10 +03:00
loc.Dist, err = rd.ReadFloatReply()
2015-11-14 17:36:21 +03:00
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 {
2016-07-02 15:52:10 +03:00
loc.GeoHash, err = rd.ReadIntReply()
2015-11-14 17:36:21 +03:00
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 {
2016-07-02 15:52:10 +03:00
n, err := rd.ReadArrayLen()
2015-11-14 17:36:21 +03:00
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
2016-07-02 15:52:10 +03:00
loc.Longitude, err = rd.ReadFloatReply()
2015-11-14 17:36:21 +03:00
if err != nil {
return nil, err
}
2016-07-02 15:52:10 +03:00
loc.Latitude, err = rd.ReadFloatReply()
2015-11-14 17:36:21 +03:00
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
}
2016-07-02 15:52:10 +03:00
func newGeoLocationSliceParser(q *GeoRadiusQuery) proto.MultiBulkParse {
return func(rd *proto.Reader, n int64) (interface{}, error) {
2015-11-14 17:36:21 +03:00
locs := make([]GeoLocation, 0, n)
for i := int64(0); i < n; i++ {
2016-07-02 15:52:10 +03:00
v, err := rd.ReadReply(newGeoLocationParser(q))
2015-11-14 17:36:21 +03:00
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
}
}
2016-08-22 00:32:06 +03:00
func newGeoPositionParser() proto.MultiBulkParse {
return func(rd *proto.Reader, n int64) (interface{}, error) {
var pos GeoPosition
var err error
pos.Longitude, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
pos.Latitude, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
return &pos, nil
}
}
func newGeoPositionSliceParser() proto.MultiBulkParse {
return func(rd *proto.Reader, n int64) (interface{}, error) {
positions := make([]*GeoPosition, 0, n)
for i := int64(0); i < n; i++ {
v, err := rd.ReadReply(newGeoPositionParser())
if err != nil {
// response may contain nil results
if err == Nil {
positions = append(positions, nil)
continue
}
return nil, err
}
switch vv := v.(type) {
case *GeoPosition:
positions = append(positions, vv)
default:
return nil, fmt.Errorf("got %T, expected *GeoPosition", v)
}
}
return positions, nil
}
}
2016-07-02 15:52:10 +03:00
func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
var cmd CommandInfo
var err error
if n != 6 {
return nil, fmt.Errorf("redis: got %d elements in COMMAND reply, wanted 6")
}
2016-07-02 15:52:10 +03:00
cmd.Name, err = rd.ReadStringReply()
if err != nil {
return nil, err
}
2016-07-02 15:52:10 +03:00
arity, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
cmd.Arity = int8(arity)
2016-07-02 15:52:10 +03:00
flags, err := rd.ReadReply(stringSliceParser)
if err != nil {
return nil, err
}
cmd.Flags = flags.([]string)
2016-07-02 15:52:10 +03:00
firstKeyPos, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
cmd.FirstKeyPos = int8(firstKeyPos)
2016-07-02 15:52:10 +03:00
lastKeyPos, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
cmd.LastKeyPos = int8(lastKeyPos)
2016-07-02 15:52:10 +03:00
stepCount, err := rd.ReadIntReply()
if err != nil {
return nil, err
}
cmd.StepCount = int8(stepCount)
for _, flag := range cmd.Flags {
if flag == "readonly" {
cmd.ReadOnly = true
break
}
}
return &cmd, nil
}
2016-07-02 15:52:10 +03:00
func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
m := make(map[string]*CommandInfo, n)
for i := int64(0); i < n; i++ {
2016-07-02 15:52:10 +03:00
v, err := rd.ReadReply(commandInfoParser)
if err != nil {
return nil, err
}
vv := v.(*CommandInfo)
m[vv.Name] = vv
}
return m, nil
}