Skip to content

Commit

Permalink
Pass pointers where it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed May 31, 2019
1 parent 458982a commit 685d892
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 223 deletions.
2 changes: 1 addition & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func BenchmarkZAdd(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := client.ZAdd("key", redis.Z{
err := client.ZAdd("key", &redis.Z{
Score: float64(1),
Member: "hello",
}).Err()
Expand Down
56 changes: 25 additions & 31 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ func xPendingParser(rd *proto.Reader, n int64) (interface{}, error) {
//------------------------------------------------------------------------------

type XPendingExt struct {
Id string
ID string
Consumer string
Idle time.Duration
RetryCount int64
Expand Down Expand Up @@ -1290,7 +1290,7 @@ func xPendingExtSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
}

ret = append(ret, XPendingExt{
Id: id,
ID: id,
Consumer: consumer,
Idle: time.Duration(idle) * time.Millisecond,
RetryCount: retryCount,
Expand Down Expand Up @@ -1370,7 +1370,7 @@ func zSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
type ZWithKeyCmd struct {
baseCmd

val ZWithKey
val *ZWithKey
}

var _ Cmder = (*ZWithKeyCmd)(nil)
Expand All @@ -1381,11 +1381,11 @@ func NewZWithKeyCmd(args ...interface{}) *ZWithKeyCmd {
}
}

func (cmd *ZWithKeyCmd) Val() ZWithKey {
func (cmd *ZWithKeyCmd) Val() *ZWithKey {
return cmd.val
}

func (cmd *ZWithKeyCmd) Result() (ZWithKey, error) {
func (cmd *ZWithKeyCmd) Result() (*ZWithKey, error) {
return cmd.Val(), cmd.Err()
}

Expand All @@ -1399,7 +1399,7 @@ func (cmd *ZWithKeyCmd) readReply(rd *proto.Reader) error {
if cmd.err != nil {
return cmd.err
}
cmd.val = v.(ZWithKey)
cmd.val = v.(*ZWithKey)
return nil
}

Expand All @@ -1424,7 +1424,8 @@ func zWithKeyParser(rd *proto.Reader, n int64) (interface{}, error) {
if err != nil {
return nil, err
}
return z, nil

return &z, nil
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -1787,41 +1788,34 @@ func (cmd *GeoPosCmd) readReply(rd *proto.Reader) error {
func geoPosSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
positions := make([]*GeoPos, 0, n)
for i := int64(0); i < n; i++ {
v, err := rd.ReadReply(geoPosParser)
_, err := rd.ReadReply(func(rd *proto.Reader, n int64) (interface{}, error) {
var pos GeoPos
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
}

positions = append(positions, &pos)
return nil, nil
})
if err != nil {
if err == Nil {
positions = append(positions, nil)
continue
}
return nil, err
}
switch v := v.(type) {
case *GeoPos:
positions = append(positions, v)
default:
return nil, fmt.Errorf("got %T, expected *GeoPos", v)
}
}
return positions, nil
}

func geoPosParser(rd *proto.Reader, n int64) (interface{}, error) {
var pos GeoPos
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
}

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

type CommandInfo struct {
Expand Down
86 changes: 43 additions & 43 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,40 +189,40 @@ type Cmdable interface {
XTrimApprox(key string, maxLen int64) *IntCmd
BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd
BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd
ZAdd(key string, members ...Z) *IntCmd
ZAddNX(key string, members ...Z) *IntCmd
ZAddXX(key string, members ...Z) *IntCmd
ZAddCh(key string, members ...Z) *IntCmd
ZAddNXCh(key string, members ...Z) *IntCmd
ZAddXXCh(key string, members ...Z) *IntCmd
ZIncr(key string, member Z) *FloatCmd
ZIncrNX(key string, member Z) *FloatCmd
ZIncrXX(key string, member Z) *FloatCmd
ZAdd(key string, members ...*Z) *IntCmd
ZAddNX(key string, members ...*Z) *IntCmd
ZAddXX(key string, members ...*Z) *IntCmd
ZAddCh(key string, members ...*Z) *IntCmd
ZAddNXCh(key string, members ...*Z) *IntCmd
ZAddXXCh(key string, members ...*Z) *IntCmd
ZIncr(key string, member *Z) *FloatCmd
ZIncrNX(key string, member *Z) *FloatCmd
ZIncrXX(key string, member *Z) *FloatCmd
ZCard(key string) *IntCmd
ZCount(key, min, max string) *IntCmd
ZLexCount(key, min, max string) *IntCmd
ZIncrBy(key string, increment float64, member string) *FloatCmd
ZInterStore(destination string, store ZStore, keys ...string) *IntCmd
ZInterStore(destination string, store *ZStore, keys ...string) *IntCmd
ZPopMax(key string, count ...int64) *ZSliceCmd
ZPopMin(key string, count ...int64) *ZSliceCmd
ZRange(key string, start, stop int64) *StringSliceCmd
ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
ZRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd
ZRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd
ZRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd
ZRank(key, member string) *IntCmd
ZRem(key string, members ...interface{}) *IntCmd
ZRemRangeByRank(key string, start, stop int64) *IntCmd
ZRemRangeByScore(key, min, max string) *IntCmd
ZRemRangeByLex(key, min, max string) *IntCmd
ZRevRange(key string, start, stop int64) *StringSliceCmd
ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd
ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
ZRevRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd
ZRevRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd
ZRevRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd
ZRevRank(key, member string) *IntCmd
ZScore(key, member string) *FloatCmd
ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd
ZUnionStore(dest string, store *ZStore, keys ...string) *IntCmd
PFAdd(key string, els ...interface{}) *IntCmd
PFCount(keys ...string) *IntCmd
PFMerge(dest string, keys ...string) *StatusCmd
Expand Down Expand Up @@ -1453,11 +1453,10 @@ func (c *cmdable) XGroupDelConsumer(stream, group, consumer string) *IntCmd {
type XReadGroupArgs struct {
Group string
Consumer string
// List of streams and ids.
Streams []string
Count int64
Block time.Duration
NoAck bool
Streams []string // list of streams and ids, e.g. stream1 stream2 id1 id2
Count int64
Block time.Duration
NoAck bool
}

func (c *cmdable) XReadGroup(a *XReadGroupArgs) *XStreamSliceCmd {
Expand Down Expand Up @@ -1618,7 +1617,7 @@ func (c *cmdable) BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd {
return cmd
}

func (c *cmdable) zAdd(a []interface{}, n int, members ...Z) *IntCmd {
func (c *cmdable) zAdd(a []interface{}, n int, members ...*Z) *IntCmd {
for i, m := range members {
a[n+2*i] = m.Score
a[n+2*i+1] = m.Member
Expand All @@ -1629,54 +1628,54 @@ func (c *cmdable) zAdd(a []interface{}, n int, members ...Z) *IntCmd {
}

// Redis `ZADD key score member [score member ...]` command.
func (c *cmdable) ZAdd(key string, members ...Z) *IntCmd {
func (c *cmdable) ZAdd(key string, members ...*Z) *IntCmd {
const n = 2
a := make([]interface{}, n+2*len(members))
a[0], a[1] = "zadd", key
return c.zAdd(a, n, members...)
}

// Redis `ZADD key NX score member [score member ...]` command.
func (c *cmdable) ZAddNX(key string, members ...Z) *IntCmd {
func (c *cmdable) ZAddNX(key string, members ...*Z) *IntCmd {
const n = 3
a := make([]interface{}, n+2*len(members))
a[0], a[1], a[2] = "zadd", key, "nx"
return c.zAdd(a, n, members...)
}

// Redis `ZADD key XX score member [score member ...]` command.
func (c *cmdable) ZAddXX(key string, members ...Z) *IntCmd {
func (c *cmdable) ZAddXX(key string, members ...*Z) *IntCmd {
const n = 3
a := make([]interface{}, n+2*len(members))
a[0], a[1], a[2] = "zadd", key, "xx"
return c.zAdd(a, n, members...)
}

// Redis `ZADD key CH score member [score member ...]` command.
func (c *cmdable) ZAddCh(key string, members ...Z) *IntCmd {
func (c *cmdable) ZAddCh(key string, members ...*Z) *IntCmd {
const n = 3
a := make([]interface{}, n+2*len(members))
a[0], a[1], a[2] = "zadd", key, "ch"
return c.zAdd(a, n, members...)
}

// Redis `ZADD key NX CH score member [score member ...]` command.
func (c *cmdable) ZAddNXCh(key string, members ...Z) *IntCmd {
func (c *cmdable) ZAddNXCh(key string, members ...*Z) *IntCmd {
const n = 4
a := make([]interface{}, n+2*len(members))
a[0], a[1], a[2], a[3] = "zadd", key, "nx", "ch"
return c.zAdd(a, n, members...)
}

// Redis `ZADD key XX CH score member [score member ...]` command.
func (c *cmdable) ZAddXXCh(key string, members ...Z) *IntCmd {
func (c *cmdable) ZAddXXCh(key string, members ...*Z) *IntCmd {
const n = 4
a := make([]interface{}, n+2*len(members))
a[0], a[1], a[2], a[3] = "zadd", key, "xx", "ch"
return c.zAdd(a, n, members...)
}

func (c *cmdable) zIncr(a []interface{}, n int, members ...Z) *FloatCmd {
func (c *cmdable) zIncr(a []interface{}, n int, members ...*Z) *FloatCmd {
for i, m := range members {
a[n+2*i] = m.Score
a[n+2*i+1] = m.Member
Expand All @@ -1687,23 +1686,23 @@ func (c *cmdable) zIncr(a []interface{}, n int, members ...Z) *FloatCmd {
}

// Redis `ZADD key INCR score member` command.
func (c *cmdable) ZIncr(key string, member Z) *FloatCmd {
func (c *cmdable) ZIncr(key string, member *Z) *FloatCmd {
const n = 3
a := make([]interface{}, n+2)
a[0], a[1], a[2] = "zadd", key, "incr"
return c.zIncr(a, n, member)
}

// Redis `ZADD key NX INCR score member` command.
func (c *cmdable) ZIncrNX(key string, member Z) *FloatCmd {
func (c *cmdable) ZIncrNX(key string, member *Z) *FloatCmd {
const n = 4
a := make([]interface{}, n+2)
a[0], a[1], a[2], a[3] = "zadd", key, "incr", "nx"
return c.zIncr(a, n, member)
}

// Redis `ZADD key XX INCR score member` command.
func (c *cmdable) ZIncrXX(key string, member Z) *FloatCmd {
func (c *cmdable) ZIncrXX(key string, member *Z) *FloatCmd {
const n = 4
a := make([]interface{}, n+2)
a[0], a[1], a[2], a[3] = "zadd", key, "incr", "xx"
Expand Down Expand Up @@ -1734,7 +1733,7 @@ func (c *cmdable) ZIncrBy(key string, increment float64, member string) *FloatCm
return cmd
}

func (c *cmdable) ZInterStore(destination string, store ZStore, keys ...string) *IntCmd {
func (c *cmdable) ZInterStore(destination string, store *ZStore, keys ...string) *IntCmd {
args := make([]interface{}, 3+len(keys))
args[0] = "zinterstore"
args[1] = destination
Expand Down Expand Up @@ -1826,7 +1825,7 @@ type ZRangeBy struct {
Offset, Count int64
}

func (c *cmdable) zRangeBy(zcmd, key string, opt ZRangeBy, withScores bool) *StringSliceCmd {
func (c *cmdable) zRangeBy(zcmd, key string, opt *ZRangeBy, withScores bool) *StringSliceCmd {
args := []interface{}{zcmd, key, opt.Min, opt.Max}
if withScores {
args = append(args, "withscores")
Expand All @@ -1844,15 +1843,15 @@ func (c *cmdable) zRangeBy(zcmd, key string, opt ZRangeBy, withScores bool) *Str
return cmd
}

func (c *cmdable) ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
func (c *cmdable) ZRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd {
return c.zRangeBy("zrangebyscore", key, opt, false)
}

func (c *cmdable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
func (c *cmdable) ZRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd {
return c.zRangeBy("zrangebylex", key, opt, false)
}

func (c *cmdable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
func (c *cmdable) ZRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd {
args := []interface{}{"zrangebyscore", key, opt.Min, opt.Max, "withscores"}
if opt.Offset != 0 || opt.Count != 0 {
args = append(
Expand Down Expand Up @@ -1918,7 +1917,7 @@ func (c *cmdable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd
return cmd
}

func (c *cmdable) zRevRangeBy(zcmd, key string, opt ZRangeBy) *StringSliceCmd {
func (c *cmdable) zRevRangeBy(zcmd, key string, opt *ZRangeBy) *StringSliceCmd {
args := []interface{}{zcmd, key, opt.Max, opt.Min}
if opt.Offset != 0 || opt.Count != 0 {
args = append(
Expand All @@ -1933,15 +1932,15 @@ func (c *cmdable) zRevRangeBy(zcmd, key string, opt ZRangeBy) *StringSliceCmd {
return cmd
}

func (c *cmdable) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
func (c *cmdable) ZRevRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd {
return c.zRevRangeBy("zrevrangebyscore", key, opt)
}

func (c *cmdable) ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
func (c *cmdable) ZRevRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd {
return c.zRevRangeBy("zrevrangebylex", key, opt)
}

func (c *cmdable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
func (c *cmdable) ZRevRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd {
args := []interface{}{"zrevrangebyscore", key, opt.Max, opt.Min, "withscores"}
if opt.Offset != 0 || opt.Count != 0 {
args = append(
Expand All @@ -1968,7 +1967,8 @@ func (c *cmdable) ZScore(key, member string) *FloatCmd {
return cmd
}

func (c *cmdable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
// TODO: move keys to ZStore?
func (c *cmdable) ZUnionStore(dest string, store *ZStore, keys ...string) *IntCmd {
args := make([]interface{}, 3+len(keys))
args[0] = "zunionstore"
args[1] = dest
Expand Down
Loading

0 comments on commit 685d892

Please sign in to comment.