Better ZRange API.

This commit is contained in:
Vladimir Mihailenco 2013-10-15 11:18:16 +03:00
parent 6485188460
commit 8fd6b4ad9f
2 changed files with 52 additions and 28 deletions

View File

@ -815,22 +815,23 @@ func (c *Client) ZRangeWithScoresMap(key string, start, stop int64) *StringFloat
return req return req
} }
func (c *Client) zRangeByScore( type ZRangeByScore struct {
key string, Min, Max string
min, max string,
withScores bool, Offset, Count int64
offset, count int64, }
) *StringSliceCmd {
args := []string{"ZRANGEBYSCORE", key, min, max} func (c *Client) zRangeByScore(key string, opts ZRangeByScore, withScores bool) *StringSliceCmd {
args := []string{"ZRANGEBYSCORE", key, opts.Min, opts.Max}
if withScores { if withScores {
args = append(args, "WITHSCORES") args = append(args, "WITHSCORES")
} }
if offset != 0 || count != 0 { if opts.Offset != 0 || opts.Count != 0 {
args = append( args = append(
args, args,
"LIMIT", "LIMIT",
strconv.FormatInt(offset, 10), strconv.FormatInt(opts.Offset, 10),
strconv.FormatInt(count, 10), strconv.FormatInt(opts.Count, 10),
) )
} }
req := NewStringSliceCmd(args...) req := NewStringSliceCmd(args...)
@ -838,23 +839,22 @@ func (c *Client) zRangeByScore(
return req return req
} }
func (c *Client) ZRangeByScore(key string, min, max string, offset, count int64) *StringSliceCmd { func (c *Client) ZRangeByScore(key string, opts ZRangeByScore) *StringSliceCmd {
return c.zRangeByScore(key, min, max, false, offset, count) return c.zRangeByScore(key, opts, false)
} }
func (c *Client) ZRangeByScoreWithScores(key, min, max string, offset, count int64) *StringSliceCmd { func (c *Client) ZRangeByScoreWithScores(key string, opts ZRangeByScore) *StringSliceCmd {
return c.zRangeByScore(key, min, max, true, offset, count) return c.zRangeByScore(key, opts, true)
} }
func (c *Client) ZRangeByScoreWithScoresMap( func (c *Client) ZRangeByScoreWithScoresMap(key string, opts ZRangeByScore) *StringFloatMapCmd {
key string, min, max string, offset, count int64) *StringFloatMapCmd { args := []string{"ZRANGEBYSCORE", key, opts.Min, opts.Max, "WITHSCORES"}
args := []string{"ZRANGEBYSCORE", key, min, max, "WITHSCORES"} if opts.Offset != 0 || opts.Count != 0 {
if offset != 0 || count != 0 {
args = append( args = append(
args, args,
"LIMIT", "LIMIT",
strconv.FormatInt(offset, 10), strconv.FormatInt(opts.Offset, 10),
strconv.FormatInt(count, 10), strconv.FormatInt(opts.Count, 10),
) )
} }
req := NewStringFloatMapCmd(args...) req := NewStringFloatMapCmd(args...)

View File

@ -2016,19 +2016,31 @@ func (t *RedisTest) TestZRangeByScore(c *C) {
zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) zAdd = t.client.ZAdd("zset", redis.Z{3, "three"})
c.Assert(zAdd.Err(), IsNil) c.Assert(zAdd.Err(), IsNil)
zRangeByScore := t.client.ZRangeByScore("zset", "-inf", "+inf", 0, 0) zRangeByScore := t.client.ZRangeByScore("zset", redis.ZRangeByScore{
Min: "-inf",
Max: "+inf",
})
c.Assert(zRangeByScore.Err(), IsNil) c.Assert(zRangeByScore.Err(), IsNil)
c.Assert(zRangeByScore.Val(), DeepEquals, []string{"one", "two", "three"}) c.Assert(zRangeByScore.Val(), DeepEquals, []string{"one", "two", "three"})
zRangeByScore = t.client.ZRangeByScore("zset", "1", "2", 0, 0) zRangeByScore = t.client.ZRangeByScore("zset", redis.ZRangeByScore{
Min: "1",
Max: "2",
})
c.Assert(zRangeByScore.Err(), IsNil) c.Assert(zRangeByScore.Err(), IsNil)
c.Assert(zRangeByScore.Val(), DeepEquals, []string{"one", "two"}) c.Assert(zRangeByScore.Val(), DeepEquals, []string{"one", "two"})
zRangeByScore = t.client.ZRangeByScore("zset", "(1", "2", 0, 0) zRangeByScore = t.client.ZRangeByScore("zset", redis.ZRangeByScore{
Min: "(1",
Max: "2",
})
c.Assert(zRangeByScore.Err(), IsNil) c.Assert(zRangeByScore.Err(), IsNil)
c.Assert(zRangeByScore.Val(), DeepEquals, []string{"two"}) c.Assert(zRangeByScore.Val(), DeepEquals, []string{"two"})
zRangeByScore = t.client.ZRangeByScore("zset", "(1", "(2", 0, 0) zRangeByScore = t.client.ZRangeByScore("zset", redis.ZRangeByScore{
Min: "(1",
Max: "(2",
})
c.Assert(zRangeByScore.Err(), IsNil) c.Assert(zRangeByScore.Err(), IsNil)
c.Assert(zRangeByScore.Val(), DeepEquals, []string{}) c.Assert(zRangeByScore.Val(), DeepEquals, []string{})
} }
@ -2041,19 +2053,31 @@ func (t *RedisTest) TestZRangeByScoreWithScoresMap(c *C) {
zAdd = t.client.ZAdd("zset", redis.Z{3, "three"}) zAdd = t.client.ZAdd("zset", redis.Z{3, "three"})
c.Assert(zAdd.Err(), IsNil) c.Assert(zAdd.Err(), IsNil)
zRangeByScore := t.client.ZRangeByScoreWithScoresMap("zset", "-inf", "+inf", 0, 0) zRangeByScore := t.client.ZRangeByScoreWithScoresMap("zset", redis.ZRangeByScore{
Min: "-inf",
Max: "+inf",
})
c.Assert(zRangeByScore.Err(), IsNil) c.Assert(zRangeByScore.Err(), IsNil)
c.Assert(zRangeByScore.Val(), DeepEquals, map[string]float64{"one": 1, "two": 2, "three": 3}) c.Assert(zRangeByScore.Val(), DeepEquals, map[string]float64{"one": 1, "two": 2, "three": 3})
zRangeByScore = t.client.ZRangeByScoreWithScoresMap("zset", "1", "2", 0, 0) zRangeByScore = t.client.ZRangeByScoreWithScoresMap("zset", redis.ZRangeByScore{
Min: "1",
Max: "2",
})
c.Assert(zRangeByScore.Err(), IsNil) c.Assert(zRangeByScore.Err(), IsNil)
c.Assert(zRangeByScore.Val(), DeepEquals, map[string]float64{"one": 1, "two": 2}) c.Assert(zRangeByScore.Val(), DeepEquals, map[string]float64{"one": 1, "two": 2})
zRangeByScore = t.client.ZRangeByScoreWithScoresMap("zset", "(1", "2", 0, 0) zRangeByScore = t.client.ZRangeByScoreWithScoresMap("zset", redis.ZRangeByScore{
Min: "(1",
Max: "2",
})
c.Assert(zRangeByScore.Err(), IsNil) c.Assert(zRangeByScore.Err(), IsNil)
c.Assert(zRangeByScore.Val(), DeepEquals, map[string]float64{"two": 2}) c.Assert(zRangeByScore.Val(), DeepEquals, map[string]float64{"two": 2})
zRangeByScore = t.client.ZRangeByScoreWithScoresMap("zset", "(1", "(2", 0, 0) zRangeByScore = t.client.ZRangeByScoreWithScoresMap("zset", redis.ZRangeByScore{
Min: "(1",
Max: "(2",
})
c.Assert(zRangeByScore.Err(), IsNil) c.Assert(zRangeByScore.Err(), IsNil)
c.Assert(zRangeByScore.Val(), DeepEquals, map[string]float64{}) c.Assert(zRangeByScore.Val(), DeepEquals, map[string]float64{})
} }