Skip to content

Commit

Permalink
Rework ZRangeWithScores.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jul 5, 2014
1 parent 00a131e commit dc9bffa
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 134 deletions.
22 changes: 11 additions & 11 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
_ Cmder = (*StringSliceCmd)(nil)
_ Cmder = (*BoolSliceCmd)(nil)
_ Cmder = (*StringStringMapCmd)(nil)
_ Cmder = (*StringFloatMapCmd)(nil)
_ Cmder = (*ZSliceCmd)(nil)
_ Cmder = (*ScanCmd)(nil)
)

Expand Down Expand Up @@ -493,37 +493,37 @@ func (cmd *StringStringMapCmd) parseReply(rd *bufio.Reader) error {

//------------------------------------------------------------------------------

type StringFloatMapCmd struct {
type ZSliceCmd struct {
*baseCmd

val map[string]float64
val []Z
}

func NewStringFloatMapCmd(args ...string) *StringFloatMapCmd {
return &StringFloatMapCmd{
func NewZSliceCmd(args ...string) *ZSliceCmd {
return &ZSliceCmd{
baseCmd: newBaseCmd(args...),
}
}

func (cmd *StringFloatMapCmd) Val() map[string]float64 {
func (cmd *ZSliceCmd) Val() []Z {
return cmd.val
}

func (cmd *StringFloatMapCmd) Result() (map[string]float64, error) {
func (cmd *ZSliceCmd) Result() ([]Z, error) {
return cmd.val, cmd.err
}

func (cmd *StringFloatMapCmd) String() string {
func (cmd *ZSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringFloatMapCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseStringFloatMap)
func (cmd *ZSliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseZSlice)
if err != nil {
cmd.err = err
return err
}
cmd.val = v.(map[string]float64)
cmd.val = v.([]Z)
return nil
}

Expand Down
32 changes: 8 additions & 24 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,19 +854,15 @@ func (c *Client) ZRange(key string, start, stop int64) *StringSliceCmd {
return c.zRange(key, start, stop, false)
}

func (c *Client) ZRangeWithScores(key string, start, stop int64) *StringSliceCmd {
return c.zRange(key, start, stop, true)
}

func (c *Client) ZRangeWithScoresMap(key string, start, stop int64) *StringFloatMapCmd {
func (c *Client) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
args := []string{
"ZRANGE",
key,
strconv.FormatInt(start, 10),
strconv.FormatInt(stop, 10),
"WITHSCORES",
}
cmd := NewStringFloatMapCmd(args...)
cmd := NewZSliceCmd(args...)
c.Process(cmd)
return cmd
}
Expand Down Expand Up @@ -899,11 +895,7 @@ func (c *Client) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRangeByScore(key, opt, false)
}

func (c *Client) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRangeByScore(key, opt, true)
}

func (c *Client) ZRangeByScoreWithScoresMap(key string, opt ZRangeByScore) *StringFloatMapCmd {
func (c *Client) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 {
args = append(
Expand All @@ -913,7 +905,7 @@ func (c *Client) ZRangeByScoreWithScoresMap(key string, opt ZRangeByScore) *Stri
strconv.FormatInt(opt.Count, 10),
)
}
cmd := NewStringFloatMapCmd(args...)
cmd := NewZSliceCmd(args...)
c.Process(cmd)
return cmd
}
Expand Down Expand Up @@ -962,13 +954,9 @@ func (c *Client) ZRevRange(key, start, stop string) *StringSliceCmd {
return c.zRevRange(key, start, stop, false)
}

func (c *Client) ZRevRangeWithScores(key, start, stop string) *StringSliceCmd {
return c.zRevRange(key, start, stop, true)
}

func (c *Client) ZRevRangeWithScoresMap(key, start, stop string) *StringFloatMapCmd {
func (c *Client) ZRevRangeWithScores(key, start, stop string) *ZSliceCmd {
args := []string{"ZREVRANGE", key, start, stop, "WITHSCORES"}
cmd := NewStringFloatMapCmd(args...)
cmd := NewZSliceCmd(args...)
c.Process(cmd)
return cmd
}
Expand All @@ -995,11 +983,7 @@ func (c *Client) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd
return c.zRevRangeByScore(key, opt, false)
}

func (c *Client) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRevRangeByScore(key, opt, true)
}

func (c *Client) ZRevRangeByScoreWithScoresMap(key string, opt ZRangeByScore) *StringFloatMapCmd {
func (c *Client) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 {
args = append(
Expand All @@ -1009,7 +993,7 @@ func (c *Client) ZRevRangeByScoreWithScoresMap(key string, opt ZRangeByScore) *S
strconv.FormatInt(opt.Count, 10),
)
}
cmd := NewStringFloatMapCmd(args...)
cmd := NewZSliceCmd(args...)
c.Process(cmd)
return cmd
}
Expand Down
61 changes: 31 additions & 30 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
type multiBulkParser func(rd *bufio.Reader, n int64) (interface{}, error)

var (
errReaderTooSmall = errors.New("redis: reader is too small")
errInvalidReplyType = errors.New("redis: invalid reply type")
errReaderTooSmall = errors.New("redis: reader is too small")
)

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -175,87 +174,89 @@ func parseSlice(rd *bufio.Reader, n int64) (interface{}, error) {
func parseStringSlice(rd *bufio.Reader, n int64) (interface{}, error) {
vals := make([]string, 0, n)
for i := int64(0); i < n; i++ {
vi, err := parseReply(rd, nil)
viface, err := parseReply(rd, nil)
if err != nil {
return nil, err
}
if v, ok := vi.(string); ok {
vals = append(vals, v)
} else {
return nil, errInvalidReplyType
v, ok := viface.(string)
if !ok {
return nil, fmt.Errorf("got %T, expected string", viface)
}
vals = append(vals, v)
}
return vals, nil
}

func parseBoolSlice(rd *bufio.Reader, n int64) (interface{}, error) {
vals := make([]bool, 0, n)
for i := int64(0); i < n; i++ {
vi, err := parseReply(rd, nil)
viface, err := parseReply(rd, nil)
if err != nil {
return nil, err
}
if v, ok := vi.(int64); ok {
vals = append(vals, v == 1)
} else {
return nil, errInvalidReplyType
v, ok := viface.(int64)
if !ok {
return nil, fmt.Errorf("got %T, expected int64", viface)
}
vals = append(vals, v == 1)
}
return vals, nil
}

func parseStringStringMap(rd *bufio.Reader, n int64) (interface{}, error) {
m := make(map[string]string, n/2)
for i := int64(0); i < n; i += 2 {
keyI, err := parseReply(rd, nil)
keyiface, err := parseReply(rd, nil)
if err != nil {
return nil, err
}
key, ok := keyI.(string)
key, ok := keyiface.(string)
if !ok {
return nil, errInvalidReplyType
return nil, fmt.Errorf("got %T, expected string", keyiface)
}

valueI, err := parseReply(rd, nil)
valueiface, err := parseReply(rd, nil)
if err != nil {
return nil, err
}
value, ok := valueI.(string)
value, ok := valueiface.(string)
if !ok {
return nil, errInvalidReplyType
return nil, fmt.Errorf("got %T, expected string", valueiface)
}

m[key] = value
}
return m, nil
}

func parseStringFloatMap(rd *bufio.Reader, n int64) (interface{}, error) {
m := make(map[string]float64, n/2)
func parseZSlice(rd *bufio.Reader, n int64) (interface{}, error) {
zz := make([]Z, n/2)
for i := int64(0); i < n; i += 2 {
keyI, err := parseReply(rd, nil)
z := &zz[i/2]

memberiface, err := parseReply(rd, nil)
if err != nil {
return nil, err
}
key, ok := keyI.(string)
member, ok := memberiface.(string)
if !ok {
return nil, errInvalidReplyType
return nil, fmt.Errorf("got %T, expected string", memberiface)
}
z.Member = member

valueI, err := parseReply(rd, nil)
scoreiface, err := parseReply(rd, nil)
if err != nil {
return nil, err
}
valueS, ok := valueI.(string)
scorestr, ok := scoreiface.(string)
if !ok {
return nil, errInvalidReplyType
return nil, fmt.Errorf("got %T, expected string", scoreiface)
}
value, err := strconv.ParseFloat(valueS, 64)
score, err := strconv.ParseFloat(scorestr, 64)
if err != nil {
return nil, err
}

m[key] = value
z.Score = score
}
return m, nil
return zz, nil
}
Loading

0 comments on commit dc9bffa

Please sign in to comment.