Refactored zRangeByScore into zRangeBy.

This commit is contained in:
Jeff Pierce 2015-08-24 14:52:00 -07:00
parent 2de07f2493
commit 2abf5c5f14
2 changed files with 22 additions and 28 deletions

View File

@ -1089,19 +1089,13 @@ func (c *commandable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
return cmd return cmd
} }
type ZRangeByScore struct { type ZRangeBy struct {
Min, Max string Min, Max string
Offset, Count int64 Offset, Count int64
} }
func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores, isLex bool) *StringSliceCmd { func (c *commandable) zRangeBy(zRangeType, key string, opt ZRangeBy, withScores bool) *StringSliceCmd {
var zcmd string args := []interface{}{zRangeType, key, opt.Min, opt.Max}
if isLex {
zcmd = "ZRANGEBYLEX"
} else {
zcmd = "ZRANGEBYSCORE"
}
args := []interface{}{zcmd, key, opt.Min, opt.Max}
if withScores { if withScores {
args = append(args, "WITHSCORES") args = append(args, "WITHSCORES")
} }
@ -1118,15 +1112,15 @@ func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores, i
return cmd return cmd
} }
func (c *commandable) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd { func (c *commandable) ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
return c.zRangeByScore(key, opt, false, false) return c.zRangeBy("ZRANGEBYSCORE", key, opt, false)
} }
func (c *commandable) ZRangeByLex(key string, opt ZRangeByScore) *StringSliceCmd { func (c *commandable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
return c.zRangeByScore(key, opt, false, true) return c.zRangeBy("ZRANGEBYLEX", key, opt, false)
} }
func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd { func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"} args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 { if opt.Offset != 0 || opt.Count != 0 {
args = append( args = append(
@ -1188,7 +1182,7 @@ 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) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min} args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
if opt.Offset != 0 || opt.Count != 0 { if opt.Offset != 0 || opt.Count != 0 {
args = append( args = append(
@ -1203,7 +1197,7 @@ func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSli
return cmd return cmd
} }
func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd { func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *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 {
args = append( args = append(

View File

@ -1981,28 +1981,28 @@ var _ = Describe("Commands", func() {
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) zAdd = client.ZAdd("zset", redis.Z{3, "three"})
Expect(zAdd.Err()).NotTo(HaveOccurred()) Expect(zAdd.Err()).NotTo(HaveOccurred())
zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeByScore{ zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeBy{
Min: "-inf", Min: "-inf",
Max: "+inf", Max: "+inf",
}) })
Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"})) Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"}))
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{ zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
Min: "1", Min: "1",
Max: "2", Max: "2",
}) })
Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"})) Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"}))
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{ zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
Min: "(1", Min: "(1",
Max: "2", Max: "2",
}) })
Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"two"})) Expect(zRangeByScore.Val()).To(Equal([]string{"two"}))
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{ zRangeByScore = client.ZRangeByScore("ZRANGEBYSCORE", "zset", redis.ZRangeBy{
Min: "(1", Min: "(1",
Max: "(2", Max: "(2",
}) })
@ -2017,28 +2017,28 @@ var _ = Describe("Commands", func() {
Expect(zAdd.Err()).NotTo(HaveOccurred()) Expect(zAdd.Err()).NotTo(HaveOccurred())
zAdd = client.ZAdd("zset", redis.Z{0, "c"}) zAdd = client.ZAdd("zset", redis.Z{0, "c"})
zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeByScore{ zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeBy{
Min: "-", Min: "-",
Max: "+", Max: "+",
}) })
Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"})) Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"}))
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
Min: "[a", Min: "[a",
Max: "[b", Max: "[b",
}) })
Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"})) Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"}))
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
Min: "(a", Min: "(a",
Max: "[b", Max: "[b",
}) })
Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"b"})) Expect(zRangeByLex.Val()).To(Equal([]string{"b"}))
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
Min: "(a", Min: "(a",
Max: "(b", Max: "(b",
}) })
@ -2054,28 +2054,28 @@ var _ = Describe("Commands", func() {
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) zAdd = client.ZAdd("zset", redis.Z{3, "three"})
Expect(zAdd.Err()).NotTo(HaveOccurred()) Expect(zAdd.Err()).NotTo(HaveOccurred())
val, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ val, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
Min: "-inf", Min: "-inf",
Max: "+inf", Max: "+inf",
}).Result() }).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}, {3, "three"}})) Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}, {3, "three"}}))
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
Min: "1", Min: "1",
Max: "2", Max: "2",
}).Result() }).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}})) Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}}))
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
Min: "(1", Min: "(1",
Max: "2", Max: "2",
}).Result() }).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{2, "two"}})) Expect(val).To(Equal([]redis.Z{{2, "two"}}))
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
Min: "(1", Min: "(1",
Max: "(2", Max: "(2",
}).Result() }).Result()