Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Count functionality in SSCAN #287

Merged
merged 3 commits into from Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 30 additions & 14 deletions cmd_set.go
Expand Up @@ -3,6 +3,7 @@
package miniredis

import (
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -613,6 +614,7 @@ func (m *Miniredis) cmdSscan(c *server.Peer, cmd string, args []string) {
key string
value int
cursor int
count int
withMatch bool
match string
}
Expand All @@ -631,13 +633,18 @@ func (m *Miniredis) cmdSscan(c *server.Peer, cmd string, args []string) {
c.WriteError(msgSyntaxError)
return
}
_, err := strconv.Atoi(args[1])
if err != nil {
count, err := strconv.Atoi(args[1])
if err != nil || count < 0 {
setDirty(c)
c.WriteError(msgInvalidInt)
return
}
// We do nothing with count.
if count == 0 {
setDirty(c)
c.WriteError(msgSyntaxError)
return
}
opts.count = count
args = args[2:]
continue
}
Expand All @@ -660,29 +667,38 @@ func (m *Miniredis) cmdSscan(c *server.Peer, cmd string, args []string) {
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)
// return _all_ (matched) keys every time

if opts.cursor != 0 {
// invalid cursor
c.WriteLen(2)
c.WriteBulk("0") // no next cursor
c.WriteLen(0) // no elements
return
}
if db.exists(opts.key) && db.t(opts.key) != "set" {
c.WriteError(ErrWrongType.Error())
return
}

members := db.setMembers(opts.key)
if opts.withMatch {
members, _ = matchKeys(members, opts.match)
}

low := opts.cursor
high := low + opts.count
// validate high is correct
if high > len(members) || high == 0 {
high = len(members)
}
if opts.cursor > high {
// invalid cursor
c.WriteLen(2)
c.WriteBulk("0") // no next cursor
c.WriteLen(0) // no elements
return
}
cursorValue := low + opts.count
if cursorValue > len(members) {
cursorValue = 0 // no next cursor
}
members = members[low:high]
c.WriteLen(2)
c.WriteBulk("0") // no next cursor
c.WriteBulk(fmt.Sprintf("%d", cursorValue))
c.WriteLen(len(members))
for _, k := range members {
c.WriteBulk(k)
}

})
}
43 changes: 42 additions & 1 deletion cmd_set_test.go
Expand Up @@ -750,7 +750,6 @@ func TestSscan(t *testing.T) {
// We cheat with sscan. It always returns everything.

s.SetAdd("set", "value1", "value2")

// No problem
mustDo(t, c,
"SSCAN", "set", "0",
Expand Down Expand Up @@ -817,14 +816,56 @@ func TestSscan(t *testing.T) {
"SSCAN", "set", "0", "COUNT",
proto.Error(msgSyntaxError),
)
mustDo(t, c,
"SSCAN", "set", "0", "COUNT", "0",
proto.Error(msgSyntaxError),
)
mustDo(t, c,
"SSCAN", "set", "0", "COUNT", "noint",
proto.Error(msgInvalidInt),
)
mustDo(t, c,
"SSCAN", "set", "0", "COUNT", "-3",
proto.Error(msgInvalidInt),
)
s.Set("str", "value")
mustDo(t, c,
"SSCAN", "str", "0",
proto.Error(msgWrongType),
)
})

s.SetAdd("largeset", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8")
mustDo(t, c,
"SSCAN", "largeset", "0", "COUNT", "3",
proto.Array(
proto.String("3"),
proto.Array(
proto.String("v1"),
proto.String("v2"),
proto.String("v3"),
),
),
)
mustDo(t, c,
"SSCAN", "largeset", "3", "COUNT", "3",
proto.Array(
proto.String("6"),
proto.Array(
proto.String("v4"),
proto.String("v5"),
proto.String("v6"),
),
),
)
mustDo(t, c,
"SSCAN", "largeset", "6", "COUNT", "3",
proto.Array(
proto.String("0"),
proto.Array(
proto.String("v7"),
proto.String("v8"),
),
),
)
}
3 changes: 3 additions & 0 deletions integration/set_test.go
Expand Up @@ -314,6 +314,8 @@ func TestSscan(t *testing.T) {
c.Do("SSCAN", "set", "0", "MATCH", "anoth*")
c.Do("SSCAN", "set", "0", "MATCH", "anoth*", "COUNT", "100")
c.Do("SSCAN", "set", "0", "COUNT", "100", "MATCH", "anoth*")
c.DoLoosely("SSCAN", "set", "0", "COUNT", "1") // cursor differs
c.DoLoosely("SSCAN", "set", "0", "COUNT", "2") // cursor differs

// Can't really test multiple keys.
// c.Do("SET", "key2", "value2")
Expand All @@ -323,6 +325,7 @@ func TestSscan(t *testing.T) {
c.Error("wrong number", "SSCAN")
c.Error("wrong number", "SSCAN", "noint")
c.Error("not an integer", "SSCAN", "set", "0", "COUNT", "noint")
c.Error("syntax error", "SSCAN", "set", "0", "COUNT", "0")
c.Error("syntax error", "SSCAN", "set", "0", "COUNT")
c.Error("syntax error", "SSCAN", "set", "0", "MATCH")
c.Error("syntax error", "SSCAN", "set", "0", "garbage")
Expand Down