Skip to content

Commit 76c33da

Browse files
committed
fix iterator across empty pages
1 parent 49f197e commit 76c33da

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

iterator.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,30 @@ func (it *ScanIterator) Next() bool {
4646
return true
4747
}
4848

49-
// Return if there is more data to fetch.
50-
if it.ScanCmd.cursor == 0 {
51-
return false
52-
}
49+
for {
50+
// Return if there is more data to fetch.
51+
if it.ScanCmd.cursor == 0 {
52+
return false
53+
}
5354

54-
// Fetch next page.
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-
}
60-
it.ScanCmd.reset()
61-
it.client.process(it.ScanCmd)
62-
if it.ScanCmd.Err() != nil {
63-
return false
64-
}
55+
// Fetch next page.
56+
if it.ScanCmd._args[0] == "scan" {
57+
it.ScanCmd._args[1] = it.ScanCmd.cursor
58+
} else {
59+
it.ScanCmd._args[2] = it.ScanCmd.cursor
60+
}
61+
it.ScanCmd.reset()
62+
it.client.process(it.ScanCmd)
63+
if it.ScanCmd.Err() != nil {
64+
return false
65+
}
6566

66-
it.pos = 1
67-
return len(it.ScanCmd.page) > 0
67+
it.pos = 1
68+
if len(it.ScanCmd.page) > 0 {
69+
return true
70+
}
71+
}
72+
return false
6873
}
6974

7075
// Val returns the key/field at the current cursor position.

iterator_test.go

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

24+
var extraSeed = func(n int, m int) error {
25+
pipe := client.Pipeline()
26+
for i := 1; i <= m; i++ {
27+
pipe.Set(fmt.Sprintf("A%02d", i), "x", 0).Err()
28+
}
29+
for i := 1; i <= n; i++ {
30+
pipe.Set(fmt.Sprintf("K%02d", i), "x", 0).Err()
31+
}
32+
_, err := pipe.Exec()
33+
return err
34+
}
35+
2436
var hashKey = "K_HASHTEST"
2537
var hashSeed = func(n int) error {
2638
pipe := client.Pipeline()
@@ -110,4 +122,15 @@ var _ = Describe("ScanIterator", func() {
110122
Expect(vals).To(HaveLen(13))
111123
})
112124

125+
It("should scan with match across empty pages", func() {
126+
Expect(extraSeed(2, 10)).NotTo(HaveOccurred())
127+
128+
var vals []string
129+
iter := client.Scan(0, "K*", 1).Iterator()
130+
for iter.Next() {
131+
vals = append(vals, iter.Val())
132+
}
133+
Expect(iter.Err()).NotTo(HaveOccurred())
134+
Expect(vals).To(HaveLen(2))
135+
})
113136
})

0 commit comments

Comments
 (0)