Merge pull request #153 from jeffpierce/master

Implemented ZRangeByLex and ZRevRangeByLex with tests.
This commit is contained in:
Vladimir Mihailenco 2015-08-26 09:45:15 +03:00
commit fd51850c85
2 changed files with 78 additions and 5 deletions

View File

@ -1089,13 +1089,14 @@ func (c *commandable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
return cmd
}
// TODO: Rename to something more generic in v4
type ZRangeByScore struct {
Min, Max string
Offset, Count int64
}
func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max}
func (c *commandable) zRangeBy(zcmd, key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
args := []interface{}{zcmd, key, opt.Min, opt.Max}
if withScores {
args = append(args, "WITHSCORES")
}
@ -1113,7 +1114,11 @@ func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores bo
}
func (c *commandable) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRangeByScore(key, opt, false)
return c.zRangeBy("ZRANGEBYSCORE", key, opt, false)
}
func (c *commandable) ZRangeByLex(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRangeBy("ZRANGEBYLEX", key, opt, false)
}
func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
@ -1178,8 +1183,8 @@ func (c *commandable) ZRevRangeWithScores(key string, start, stop int64) *ZSlice
return cmd
}
func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
func (c *commandable) zRevRangeBy(zcmd, key string, opt ZRangeByScore) *StringSliceCmd {
args := []interface{}{zcmd, key, opt.Max, opt.Min}
if opt.Offset != 0 || opt.Count != 0 {
args = append(
args,
@ -1193,6 +1198,14 @@ func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSli
return cmd
}
func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRevRangeBy("ZREVRANGEBYSCORE", key, opt)
}
func (c commandable) ZRevRangeByLex(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRevRangeBy("ZREVRANGEBYLEX", key, opt)
}
func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 {

View File

@ -2010,6 +2010,42 @@ var _ = Describe("Commands", func() {
Expect(zRangeByScore.Val()).To(Equal([]string{}))
})
It("should ZRangeByLex", func() {
zAdd := client.ZAdd("zset", redis.Z{0, "a"})
Expect(zAdd.Err()).NotTo(HaveOccurred())
zAdd = client.ZAdd("zset", redis.Z{0, "b"})
Expect(zAdd.Err()).NotTo(HaveOccurred())
zAdd = client.ZAdd("zset", redis.Z{0, "c"})
zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeByScore{
Min: "-",
Max: "+",
})
Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"}))
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{
Min: "[a",
Max: "[b",
})
Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"}))
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{
Min: "(a",
Max: "[b",
})
Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"b"}))
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{
Min: "(a",
Max: "(b",
})
Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{}))
})
It("should ZRangeByScoreWithScoresMap", func() {
zAdd := client.ZAdd("zset", redis.Z{1, "one"})
Expect(zAdd.Err()).NotTo(HaveOccurred())
@ -2181,6 +2217,30 @@ var _ = Describe("Commands", func() {
Expect(vals).To(Equal([]string{}))
})
It("should ZRevRangeByLex", func() {
zadd := client.ZAdd("zset", redis.Z{0, "a"})
Expect(zadd.Err()).NotTo(HaveOccurred())
zadd = client.ZAdd("zset", redis.Z{0, "b"})
Expect(zadd.Err()).NotTo(HaveOccurred())
zadd = client.ZAdd("zset", redis.Z{0, "c"})
Expect(zadd.Err()).NotTo(HaveOccurred())
vals, err := client.ZRevRangeByLex(
"zset", redis.ZRangeByScore{Max: "+", Min: "-"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"c", "b", "a"}))
vals, err = client.ZRevRangeByLex(
"zset", redis.ZRangeByScore{Max: "[b", Min: "(a"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"b"}))
vals, err = client.ZRevRangeByLex(
"zset", redis.ZRangeByScore{Max: "(b", Min: "(a"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{}))
})
It("should ZRevRangeByScoreWithScores", func() {
zadd := client.ZAdd("zset", redis.Z{1, "one"})
Expect(zadd.Err()).NotTo(HaveOccurred())