Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Commit

Permalink
Implementing Count functionality in SCAN
Browse files Browse the repository at this point in the history
Based on the implementation for the COUNT parameter in the SSCAN implementation:
alicebob#287
  • Loading branch information
BarakSilverfort committed Sep 27, 2023
1 parent a946a99 commit ba7b20d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
44 changes: 31 additions & 13 deletions cmd_generic.go
Expand Up @@ -552,6 +552,7 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) {

var opts struct {
cursor int
count int
withMatch bool
match string
withType bool
Expand All @@ -566,17 +567,23 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) {
// MATCH, COUNT and TYPE options
for len(args) > 0 {
if strings.ToLower(args[0]) == "count" {
// we do nothing with count
if len(args) < 2 {
setDirty(c)
c.WriteError(msgSyntaxError)
return
}
if _, err := strconv.Atoi(args[1]); err != nil {
count, err := strconv.Atoi(args[1])
if err != nil || count < 0 {
setDirty(c)
c.WriteError(msgInvalidInt)
return
}
if count == 0 {
setDirty(c)
c.WriteError(msgSyntaxError)
return
}
opts.count = count
args = args[2:]
continue
}
Expand Down Expand Up @@ -608,15 +615,6 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) {
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)
// We 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
}

var keys []string

if opts.withType {
Expand All @@ -627,17 +625,37 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) {
keys = append(keys, k)
}
}
sort.Strings(keys) // To make things deterministic.
} else {
keys = db.allKeys()
}

sort.Strings(keys) // To make things deterministic.

if opts.withMatch {
keys, _ = matchKeys(keys, opts.match)
}

low := opts.cursor
high := low + opts.count
// validate high is correct
if high > len(keys) || high == 0 {
high = len(keys)
}
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(keys) {
cursorValue = 0 // no next cursor
}
keys = keys[low:high]

c.WriteLen(2)
c.WriteBulk("0") // no next cursor
c.WriteBulk(fmt.Sprintf("%d", cursorValue))
c.WriteLen(len(keys))
for _, k := range keys {
c.WriteBulk(k)
Expand Down
44 changes: 43 additions & 1 deletion cmd_generic_test.go
Expand Up @@ -655,7 +655,7 @@ func TestScan(t *testing.T) {
)
})

t.Run("count (ignored)", func(t *testing.T) {
t.Run("count", func(t *testing.T) {
mustDo(t, c,
"SCAN", "0", "COUNT", "200",
proto.Array(
Expand All @@ -665,6 +665,40 @@ func TestScan(t *testing.T) {
),
),
)

s.Set("v1", "value")
s.Set("v2", "value")
s.Set("v3", "value")
s.Set("v4", "value")
s.Set("v5", "value")
s.Set("v6", "value")
s.Set("v7", "value")
s.Set("v8", "value")
s.Set("v9", "value")

mustDo(t, c,
"SCAN", "0", "COUNT", "3",
proto.Array(
proto.String("3"),
proto.Array(
proto.String("key"),
proto.String("v1"),
proto.String("v2"),
),
),
)

mustDo(t, c,
"SCAN", "3", "COUNT", "3",
proto.Array(
proto.String("6"),
proto.Array(
proto.String("v3"),
proto.String("v4"),
proto.String("v5"),
),
),
)
})

t.Run("match", func(t *testing.T) {
Expand Down Expand Up @@ -726,6 +760,14 @@ func TestScan(t *testing.T) {
"SCAN", "1", "COUNT", "noint",
proto.Error("ERR value is not an integer or out of range"),
)
mustDo(t, c,
"SCAN", "0", "COUNT", "0",
proto.Error("ERR syntax error"),
)
mustDo(t, c,
"SCAN", "0", "COUNT", "-1",
proto.Error("ERR value is not an integer or out of range"),
)
mustDo(t, c,
"SCAN", "1", "TYPE",
proto.Error("ERR syntax error"),
Expand Down
3 changes: 3 additions & 0 deletions integration/generic_test.go
Expand Up @@ -164,6 +164,8 @@ func TestScan(t *testing.T) {
c.Do("SCAN", "0", "TYPE", "set", "MATCH", "setkey")
c.Do("SCAN", "0", "TYPE", "set", "COUNT", "100")
c.Do("SCAN", "0", "TYPE", "set", "MATCH", "setkey", "COUNT", "100")
c.DoLoosely("SCAN", "0", "COUNT", "1") // cursor differs
c.DoLoosely("SCAN", "0", "COUNT", "2") // cursor differs

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

0 comments on commit ba7b20d

Please sign in to comment.