Skip to content

Commit

Permalink
Merge pull request redis#153 from jeffpierce/master
Browse files Browse the repository at this point in the history
Implemented ZRangeByLex and ZRevRangeByLex with tests.
  • Loading branch information
vmihailenco committed Aug 26, 2015
2 parents 73e1e9f + 7cafa2f commit fd51850
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
23 changes: 18 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit fd51850

Please sign in to comment.