forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
889409d
commit 7456a0e
Showing
6 changed files
with
216 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package redis | ||
|
||
import "sync" | ||
|
||
type Scanner struct { | ||
client *commandable | ||
*ScanCmd | ||
} | ||
|
||
// Iterator creates a new ScanIterator. | ||
func (s Scanner) Iterator() *ScanIterator { | ||
return &ScanIterator{ | ||
Scanner: s, | ||
} | ||
} | ||
|
||
// ScanIterator is used to incrementally iterate over a collection of elements. | ||
// It's safe for concurrent use by multiple goroutines. | ||
type ScanIterator struct { | ||
mu sync.Mutex // protects Scanner and pos | ||
Scanner | ||
pos int | ||
} | ||
|
||
// Err returns the last iterator error, if any. | ||
func (it *ScanIterator) Err() error { | ||
it.mu.Lock() | ||
err := it.ScanCmd.Err() | ||
it.mu.Unlock() | ||
return err | ||
} | ||
|
||
// Next advances the cursor and returns true if more values can be read. | ||
func (it *ScanIterator) Next() bool { | ||
it.mu.Lock() | ||
defer it.mu.Unlock() | ||
|
||
// Instantly return on errors. | ||
if it.ScanCmd.Err() != nil { | ||
return false | ||
} | ||
|
||
// Advance cursor, check if we are still within range. | ||
if it.pos < len(it.ScanCmd.page) { | ||
it.pos++ | ||
return true | ||
} | ||
|
||
// Return if there is more data to fetch. | ||
if it.ScanCmd.cursor == 0 { | ||
return false | ||
} | ||
|
||
// Fetch next page. | ||
it.ScanCmd._args[1] = it.ScanCmd.cursor | ||
it.ScanCmd.reset() | ||
it.client.Process(it.ScanCmd) | ||
if it.ScanCmd.Err() != nil { | ||
return false | ||
} | ||
|
||
it.pos = 1 | ||
return len(it.ScanCmd.page) > 0 | ||
} | ||
|
||
// Val returns the key/field at the current cursor position. | ||
func (it *ScanIterator) Val() string { | ||
var v string | ||
it.mu.Lock() | ||
if it.ScanCmd.Err() == nil && it.pos > 0 && it.pos <= len(it.ScanCmd.page) { | ||
v = it.ScanCmd.page[it.pos-1] | ||
} | ||
it.mu.Unlock() | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package redis_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"gopkg.in/redis.v4" | ||
) | ||
|
||
var _ = Describe("ScanIterator", func() { | ||
var client *redis.Client | ||
|
||
var seed = func(n int) error { | ||
pipe := client.Pipeline() | ||
for i := 1; i <= n; i++ { | ||
pipe.Set(fmt.Sprintf("K%02d", i), "x", 0).Err() | ||
} | ||
_, err := pipe.Exec() | ||
return err | ||
} | ||
|
||
BeforeEach(func() { | ||
client = redis.NewClient(redisOptions()) | ||
Expect(client.FlushDb().Err()).NotTo(HaveOccurred()) | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(client.Close()).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should scan across empty DBs", func() { | ||
iter := client.Scan(0, "", 10).Iterator() | ||
Expect(iter.Next()).To(BeFalse()) | ||
Expect(iter.Err()).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should scan across one page", func() { | ||
Expect(seed(7)).NotTo(HaveOccurred()) | ||
|
||
var vals []string | ||
iter := client.Scan(0, "", 0).Iterator() | ||
for iter.Next() { | ||
vals = append(vals, iter.Val()) | ||
} | ||
Expect(iter.Err()).NotTo(HaveOccurred()) | ||
Expect(vals).To(ConsistOf([]string{"K01", "K02", "K03", "K04", "K05", "K06", "K07"})) | ||
}) | ||
|
||
It("should scan across multiple pages", func() { | ||
Expect(seed(71)).NotTo(HaveOccurred()) | ||
|
||
var vals []string | ||
iter := client.Scan(0, "", 10).Iterator() | ||
for iter.Next() { | ||
vals = append(vals, iter.Val()) | ||
} | ||
Expect(iter.Err()).NotTo(HaveOccurred()) | ||
Expect(vals).To(HaveLen(71)) | ||
Expect(vals).To(ContainElement("K01")) | ||
Expect(vals).To(ContainElement("K71")) | ||
}) | ||
|
||
It("should scan to page borders", func() { | ||
Expect(seed(20)).NotTo(HaveOccurred()) | ||
|
||
var vals []string | ||
iter := client.Scan(0, "", 10).Iterator() | ||
for iter.Next() { | ||
vals = append(vals, iter.Val()) | ||
} | ||
Expect(iter.Err()).NotTo(HaveOccurred()) | ||
Expect(vals).To(HaveLen(20)) | ||
}) | ||
|
||
It("should scan with match", func() { | ||
Expect(seed(33)).NotTo(HaveOccurred()) | ||
|
||
var vals []string | ||
iter := client.Scan(0, "K*2*", 10).Iterator() | ||
for iter.Next() { | ||
vals = append(vals, iter.Val()) | ||
} | ||
Expect(iter.Err()).NotTo(HaveOccurred()) | ||
Expect(vals).To(HaveLen(13)) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters