Skip to content

Commit

Permalink
Reverted change to struct ZRangeByScore, implemented ZRevRangeByLex.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Pierce committed Aug 25, 2015
1 parent 15c887f commit 5498ba4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
27 changes: 18 additions & 9 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
}

type ZRangeBy struct {
// TODO: Rename to something more generic in v4
type ZRangeByScore struct {
Min, Max string
Offset, Count int64
}

func (c *commandable) zRangeBy(zRangeType, key string, opt ZRangeBy, withScores bool) *StringSliceCmd {
args := []interface{}{zRangeType, 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 @@ -1112,15 +1113,15 @@ func (c *commandable) zRangeBy(zRangeType, key string, opt ZRangeBy, withScores
return cmd
}

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

func (c *commandable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
func (c *commandable) ZRangeByLex(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRangeBy("ZRANGEBYLEX", key, opt, false)
}

func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 {
args = append(
Expand Down Expand Up @@ -1182,8 +1183,8 @@ func (c *commandable) ZRevRangeWithScores(key string, start, stop int64) *ZSlice
return cmd
}

func (c *commandable) ZRevRangeByScore(key string, opt ZRangeBy) *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 @@ -1197,7 +1198,15 @@ func (c *commandable) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
return cmd
}

func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
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 {
args = append(
Expand Down
62 changes: 43 additions & 19 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1981,28 +1981,28 @@ var _ = Describe("Commands", func() {
zAdd = client.ZAdd("zset", redis.Z{3, "three"})
Expect(zAdd.Err()).NotTo(HaveOccurred())

zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeBy{
zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeByScore{
Min: "-inf",
Max: "+inf",
})
Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"}))

zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{
Min: "1",
Max: "2",
})
Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"}))

zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{
Min: "(1",
Max: "2",
})
Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"two"}))

zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{
Min: "(1",
Max: "(2",
})
Expand All @@ -2017,28 +2017,28 @@ var _ = Describe("Commands", func() {
Expect(zAdd.Err()).NotTo(HaveOccurred())
zAdd = client.ZAdd("zset", redis.Z{0, "c"})

zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeBy{
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.ZRangeBy{
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.ZRangeBy{
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.ZRangeBy{
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{
Min: "(a",
Max: "(b",
})
Expand All @@ -2054,28 +2054,28 @@ var _ = Describe("Commands", func() {
zAdd = client.ZAdd("zset", redis.Z{3, "three"})
Expect(zAdd.Err()).NotTo(HaveOccurred())

val, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
val, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{
Min: "-inf",
Max: "+inf",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}, {3, "three"}}))

val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{
Min: "1",
Max: "2",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}}))

val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{
Min: "(1",
Max: "2",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{2, "two"}}))

val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{
Min: "(1",
Max: "(2",
}).Result()
Expand Down Expand Up @@ -2202,17 +2202,41 @@ var _ = Describe("Commands", func() {
Expect(zadd.Err()).NotTo(HaveOccurred())

vals, err := client.ZRevRangeByScore(
"zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
"zset", redis.ZRangeByScore{Max: "+inf", Min: "-inf"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"three", "two", "one"}))

vals, err = client.ZRevRangeByScore(
"zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
"zset", redis.ZRangeByScore{Max: "2", Min: "(1"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"two"}))

vals, err = client.ZRevRangeByScore(
"zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
"zset", redis.ZRangeByScore{Max: "(2", Min: "(1"}).Result()
Expect(err).NotTo(HaveOccurred())
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{}))
})
Expand All @@ -2226,7 +2250,7 @@ var _ = Describe("Commands", func() {
Expect(zadd.Err()).NotTo(HaveOccurred())

vals, err := client.ZRevRangeByScoreWithScores(
"zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
"zset", redis.ZRangeByScore{Max: "+inf", Min: "-inf"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}))
})
Expand All @@ -2240,17 +2264,17 @@ var _ = Describe("Commands", func() {
Expect(zAdd.Err()).NotTo(HaveOccurred())

val, err := client.ZRevRangeByScoreWithScores(
"zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
"zset", redis.ZRangeByScore{Max: "+inf", Min: "-inf"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}))

val, err = client.ZRevRangeByScoreWithScores(
"zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
"zset", redis.ZRangeByScore{Max: "2", Min: "(1"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{2, "two"}}))

val, err = client.ZRevRangeByScoreWithScores(
"zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
"zset", redis.ZRangeByScore{Max: "(2", Min: "(1"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{}))
})
Expand Down

0 comments on commit 5498ba4

Please sign in to comment.