Skip to content

Commit

Permalink
split up zrange and zrevrange
Browse files Browse the repository at this point in the history
Zrevrange is "legacy" and shouldn't get the updates for zrange.
  • Loading branch information
alicebob committed Mar 8, 2022
1 parent baa6b88 commit a2b33f0
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 109 deletions.
187 changes: 130 additions & 57 deletions cmd_sorted_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ func commandsSortedSet(m *Miniredis) {
m.srv.Register("ZINCRBY", m.cmdZincrby)
m.srv.Register("ZINTERSTORE", m.cmdZinterstore)
m.srv.Register("ZLEXCOUNT", m.cmdZlexcount)
m.srv.Register("ZRANGE", m.makeCmdZrange(false))
m.srv.Register("ZRANGE", m.cmdZrange)
m.srv.Register("ZRANGEBYLEX", m.makeCmdZrangebylex(false))
m.srv.Register("ZRANGEBYSCORE", m.makeCmdZrangebyscore(false))
m.srv.Register("ZRANK", m.makeCmdZrank(false))
m.srv.Register("ZREM", m.cmdZrem)
m.srv.Register("ZREMRANGEBYLEX", m.cmdZremrangebylex)
m.srv.Register("ZREMRANGEBYRANK", m.cmdZremrangebyrank)
m.srv.Register("ZREMRANGEBYSCORE", m.cmdZremrangebyscore)
m.srv.Register("ZREVRANGE", m.makeCmdZrange(true))
m.srv.Register("ZREVRANGE", m.cmdZrevrange)
m.srv.Register("ZREVRANGEBYLEX", m.makeCmdZrangebylex(true))
m.srv.Register("ZREVRANGEBYSCORE", m.makeCmdZrangebyscore(true))
m.srv.Register("ZREVRANK", m.makeCmdZrank(true))
Expand Down Expand Up @@ -469,79 +469,152 @@ func (m *Miniredis) cmdZlexcount(c *server.Peer, cmd string, args []string) {
})
}

// ZRANGE and ZREVRANGE
func (m *Miniredis) makeCmdZrange(reverse bool) server.Cmd {
return func(c *server.Peer, cmd string, args []string) {
if len(args) < 3 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
// ZRANGE
func (m *Miniredis) cmdZrange(c *server.Peer, cmd string, args []string) {
if len(args) < 3 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}

var opts struct {
Key string
Start int
End int
WithScores bool
Reverse bool
}

opts.Key = args[0]
if ok := optInt(c, args[1], &opts.Start); !ok {
return
}
if ok := optInt(c, args[2], &opts.End); !ok {
return
}
args = args[3:]

for len(args) > 0 {
switch strings.ToLower(args[0]) {
case "withscores":
opts.WithScores = true
args = args[1:]
default:
c.WriteError(msgSyntaxError)
return
}
if !m.handleAuth(c) {
}

withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)

if !db.exists(opts.Key) {
c.WriteLen(0)
return
}
if m.checkPubsub(c, cmd) {

if db.t(opts.Key) != "zset" {
c.WriteError(ErrWrongType.Error())
return
}

var opts struct {
Key string
Start int
End int
WithScores bool
members := db.ssetMembers(opts.Key)
if opts.Reverse {
reverseSlice(members)
}

opts.Key = args[0]
if ok := optInt(c, args[1], &opts.Start); !ok {
return
rs, re := redisRange(len(members), opts.Start, opts.End, false)
if opts.WithScores {
c.WriteLen((re - rs) * 2)
} else {
c.WriteLen(re - rs)
}
if ok := optInt(c, args[2], &opts.End); !ok {
return
for _, el := range members[rs:re] {
c.WriteBulk(el)
if opts.WithScores {
c.WriteFloat(db.ssetScore(opts.Key, el))
}
}
args = args[3:]
})
}

for len(args) > 0 {
switch strings.ToLower(args[0]) {
case "withscores":
opts.WithScores = true
args = args[1:]
default:
c.WriteError(msgSyntaxError)
return
}
// ZREVRANGE
func (m *Miniredis) cmdZrevrange(c *server.Peer, cmd string, args []string) {
reverse := true
if len(args) < 3 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}

var opts struct {
Key string
Start int
End int
WithScores bool
}

opts.Key = args[0]
if ok := optInt(c, args[1], &opts.Start); !ok {
return
}
if ok := optInt(c, args[2], &opts.End); !ok {
return
}
args = args[3:]

for len(args) > 0 {
switch strings.ToLower(args[0]) {
case "withscores":
opts.WithScores = true
args = args[1:]
default:
c.WriteError(msgSyntaxError)
return
}
}

withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)

if !db.exists(opts.Key) {
c.WriteLen(0)
return
}
if !db.exists(opts.Key) {
c.WriteLen(0)
return
}

if db.t(opts.Key) != "zset" {
c.WriteError(ErrWrongType.Error())
return
}
if db.t(opts.Key) != "zset" {
c.WriteError(ErrWrongType.Error())
return
}

members := db.ssetMembers(opts.Key)
if reverse {
reverseSlice(members)
}
rs, re := redisRange(len(members), opts.Start, opts.End, false)
members := db.ssetMembers(opts.Key)
if reverse {
reverseSlice(members)
}
rs, re := redisRange(len(members), opts.Start, opts.End, false)
if opts.WithScores {
c.WriteLen((re - rs) * 2)
} else {
c.WriteLen(re - rs)
}
for _, el := range members[rs:re] {
c.WriteBulk(el)
if opts.WithScores {
c.WriteLen((re - rs) * 2)
} else {
c.WriteLen(re - rs)
c.WriteFloat(db.ssetScore(opts.Key, el))
}
for _, el := range members[rs:re] {
c.WriteBulk(el)
if opts.WithScores {
c.WriteFloat(db.ssetScore(opts.Key, el))
}
}
})
}
}
})
}

// ZRANGEBYLEX and ZREVRANGEBYLEX
Expand Down
Loading

0 comments on commit a2b33f0

Please sign in to comment.