Skip to content

Commit

Permalink
Merge pull request #287 from Abdi-dd/master
Browse files Browse the repository at this point in the history
Implementing Count functionality in SSCAN
  • Loading branch information
alicebob committed Aug 9, 2022
2 parents e4a93ab + 9fc7af8 commit d437a8e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
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

0 comments on commit d437a8e

Please sign in to comment.