Skip to content

Commit 1a990bb

Browse files
authored
un-implent "SCAN COUNT" so cursors work as they should (#418)
This works according to the specs, and avoids having to implement proper cursors.
1 parent 44ac3f3 commit 1a990bb

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

cmd_generic.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,8 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) {
650650

651651
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
652652
db := m.db(ctx.selectedDB)
653-
// We return _all_ (matched) keys every time.
653+
// We return _all_ (matched) keys every time, so that cursors work.
654+
// We ignore "COUNT", which is allowed according to the Redis docs.
654655
var keys []string
655656

656657
if opts.withType {
@@ -671,25 +672,14 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) {
671672
keys, _ = matchKeys(keys, opts.match)
672673
}
673674

674-
low := opts.cursor
675-
high := low + opts.count
676-
// validate high is correct
677-
if high > len(keys) || high == 0 {
678-
high = len(keys)
679-
}
680-
if opts.cursor > high {
681-
// invalid cursor
675+
// we only ever return all at once, so no non-zero cursor can every be valid
676+
if opts.cursor != 0 {
682677
c.WriteLen(2)
683678
c.WriteBulk("0") // no next cursor
684679
c.WriteLen(0) // no elements
685680
return
686681
}
687-
cursorValue := low + opts.count
688-
if cursorValue >= len(keys) {
689-
cursorValue = 0 // no next cursor
690-
}
691-
keys = keys[low:high]
692-
682+
cursorValue := 0 // we don't use cursors
693683
c.WriteLen(2)
694684
c.WriteBulk(fmt.Sprintf("%d", cursorValue))
695685
c.WriteLen(len(keys))

cmd_generic_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -758,27 +758,31 @@ func TestScan(t *testing.T) {
758758
s.Set("v8", "value")
759759
s.Set("v9", "value")
760760

761+
// count is ignored
761762
mustDo(t, c,
762763
"SCAN", "0", "COUNT", "3",
763764
proto.Array(
764-
proto.String("3"),
765+
proto.String("0"),
765766
proto.Array(
766767
proto.String("key"),
767768
proto.String("v1"),
768769
proto.String("v2"),
770+
proto.String("v3"),
771+
proto.String("v4"),
772+
proto.String("v5"),
773+
proto.String("v6"),
774+
proto.String("v7"),
775+
proto.String("v8"),
776+
proto.String("v9"),
769777
),
770778
),
771779
)
772780

773781
mustDo(t, c,
774782
"SCAN", "3", "COUNT", "3",
775783
proto.Array(
776-
proto.String("6"),
777-
proto.Array(
778-
proto.String("v3"),
779-
proto.String("v4"),
780-
proto.String("v5"),
781-
),
784+
proto.String("0"),
785+
proto.Array(),
782786
),
783787
)
784788
})

0 commit comments

Comments
 (0)