Skip to content

Commit 1324657

Browse files
authored
Merge pull request #347 from flisky/master
fix iterator for hscan/sscan/zscan
2 parents bc66ed0 + 9199312 commit 1324657

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

iterator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ func (it *ScanIterator) Next() bool {
5252
}
5353

5454
// Fetch next page.
55-
it.ScanCmd._args[1] = it.ScanCmd.cursor
55+
if it.ScanCmd._args[0] == "scan" {
56+
it.ScanCmd._args[1] = it.ScanCmd.cursor
57+
} else {
58+
it.ScanCmd._args[2] = it.ScanCmd.cursor
59+
}
5660
it.ScanCmd.reset()
5761
it.client.process(it.ScanCmd)
5862
if it.ScanCmd.Err() != nil {

iterator_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ var _ = Describe("ScanIterator", func() {
2121
return err
2222
}
2323

24+
var hashKey = "K_HASHTEST"
25+
var hashSeed = func(n int) error {
26+
pipe := client.Pipeline()
27+
for i := 1; i <= n; i++ {
28+
pipe.HSet(hashKey, fmt.Sprintf("K%02d", i), "x").Err()
29+
}
30+
_, err := pipe.Exec()
31+
return err
32+
}
33+
2434
BeforeEach(func() {
2535
client = redis.NewClient(redisOptions())
2636
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
@@ -62,6 +72,20 @@ var _ = Describe("ScanIterator", func() {
6272
Expect(vals).To(ContainElement("K71"))
6373
})
6474

75+
It("should hscan across multiple pages", func() {
76+
Expect(hashSeed(71)).NotTo(HaveOccurred())
77+
78+
var vals []string
79+
iter := client.HScan(hashKey, 0, "", 10).Iterator()
80+
for iter.Next() {
81+
vals = append(vals, iter.Val())
82+
}
83+
Expect(iter.Err()).NotTo(HaveOccurred())
84+
Expect(vals).To(HaveLen(71 * 2))
85+
Expect(vals).To(ContainElement("K01"))
86+
Expect(vals).To(ContainElement("K71"))
87+
})
88+
6589
It("should scan to page borders", func() {
6690
Expect(seed(20)).NotTo(HaveOccurred())
6791

0 commit comments

Comments
 (0)