Skip to content

Commit

Permalink
added support for ZRank and ZRevRank with score
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Howell authored and alicebob committed Nov 27, 2024
1 parent 08e664a commit 5056952
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cmd_sorted_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,12 @@ func (m *Miniredis) makeCmdZrank(reverse bool) server.Cmd {
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)

withScore := false
if len(args) > 0 && strings.ToUpper(args[len(args)-1]) == "WITHSCORE" {
withScore = true
args = args[:len(args)-1]
}

if len(args) > 2 {
setDirty(c)
c.WriteError(msgSyntaxError)
Expand All @@ -850,7 +856,16 @@ func (m *Miniredis) makeCmdZrank(reverse bool) server.Cmd {
c.WriteNull()
return
}
c.WriteInt(rank)

if withScore {
c.WriteLen(2)
c.WriteInt(rank)
c.WriteFloat(db.ssetScore(key, member))
} else {
c.WriteInt(rank)
}


})
}
}
Expand Down
17 changes: 17 additions & 0 deletions cmd_sorted_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,27 @@ func TestSortedSet(t *testing.T) {
proto.Int(2),
)

mustDo(t, c,
"ZRANK", "z", "three", "withscore",
proto.Array(
proto.Int(2),
proto.String("3"),
),
)

mustDo(t, c,
"ZREVRANK", "z", "one",
proto.Int(2),
)

mustDo(t, c,
"ZREVRANK", "z", "one", "withscore",
proto.Array(
proto.Int(2),
proto.String("1"),
),
)

must0(t, c,
"ZREVRANK", "z", "three",
)
Expand Down

0 comments on commit 5056952

Please sign in to comment.