mirror of https://github.com/go-redis/redis.git
Merge pull request #153 from jeffpierce/master
Implemented ZRangeByLex and ZRevRangeByLex with tests.
This commit is contained in:
commit
fd51850c85
23
commands.go
23
commands.go
|
@ -1089,13 +1089,14 @@ func (c *commandable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Rename to something more generic in v4
|
||||||
type ZRangeByScore struct {
|
type ZRangeByScore struct {
|
||||||
Min, Max string
|
Min, Max string
|
||||||
Offset, Count int64
|
Offset, Count int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
|
func (c *commandable) zRangeBy(zcmd, key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
|
||||||
args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max}
|
args := []interface{}{zcmd, key, opt.Min, opt.Max}
|
||||||
if withScores {
|
if withScores {
|
||||||
args = append(args, "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 {
|
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 {
|
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
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
|
func (c *commandable) zRevRangeBy(zcmd, key string, opt ZRangeByScore) *StringSliceCmd {
|
||||||
args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
|
args := []interface{}{zcmd, key, opt.Max, opt.Min}
|
||||||
if opt.Offset != 0 || opt.Count != 0 {
|
if opt.Offset != 0 || opt.Count != 0 {
|
||||||
args = append(
|
args = append(
|
||||||
args,
|
args,
|
||||||
|
@ -1193,6 +1198,14 @@ func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSli
|
||||||
return cmd
|
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 {
|
func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
|
||||||
args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
|
args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
|
||||||
if opt.Offset != 0 || opt.Count != 0 {
|
if opt.Offset != 0 || opt.Count != 0 {
|
||||||
|
|
|
@ -2010,6 +2010,42 @@ var _ = Describe("Commands", func() {
|
||||||
Expect(zRangeByScore.Val()).To(Equal([]string{}))
|
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() {
|
It("should ZRangeByScoreWithScoresMap", func() {
|
||||||
zAdd := client.ZAdd("zset", redis.Z{1, "one"})
|
zAdd := client.ZAdd("zset", redis.Z{1, "one"})
|
||||||
Expect(zAdd.Err()).NotTo(HaveOccurred())
|
Expect(zAdd.Err()).NotTo(HaveOccurred())
|
||||||
|
@ -2181,6 +2217,30 @@ var _ = Describe("Commands", func() {
|
||||||
Expect(vals).To(Equal([]string{}))
|
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() {
|
It("should ZRevRangeByScoreWithScores", func() {
|
||||||
zadd := client.ZAdd("zset", redis.Z{1, "one"})
|
zadd := client.ZAdd("zset", redis.Z{1, "one"})
|
||||||
Expect(zadd.Err()).NotTo(HaveOccurred())
|
Expect(zadd.Err()).NotTo(HaveOccurred())
|
||||||
|
|
Loading…
Reference in New Issue