Skip to content

Commit 10c56ce

Browse files
committed
Remove Scanner in favor of ScanCmd.
1 parent 67acf6e commit 10c56ce

File tree

3 files changed

+44
-58
lines changed

3 files changed

+44
-58
lines changed

command.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,14 @@ type ScanCmd struct {
683683

684684
page []string
685685
cursor uint64
686+
687+
process func(cmd Cmder) error
686688
}
687689

688-
func NewScanCmd(args ...interface{}) *ScanCmd {
689-
cmd := newBaseCmd(args)
690+
func NewScanCmd(process func(cmd Cmder) error, args ...interface{}) *ScanCmd {
690691
return &ScanCmd{
691-
baseCmd: cmd,
692+
baseCmd: newBaseCmd(args),
693+
process: process,
692694
}
693695
}
694696

@@ -709,6 +711,13 @@ func (cmd *ScanCmd) readReply(cn *pool.Conn) error {
709711
return cmd.err
710712
}
711713

714+
// Iterator creates a new ScanIterator.
715+
func (cmd *ScanCmd) Iterator() *ScanIterator {
716+
return &ScanIterator{
717+
cmd: cmd,
718+
}
719+
}
720+
712721
//------------------------------------------------------------------------------
713722

714723
type ClusterNode struct {

commands.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ type Cmdable interface {
7171
SortInterfaces(key string, sort Sort) *SliceCmd
7272
TTL(key string) *DurationCmd
7373
Type(key string) *StatusCmd
74-
Scan(cursor uint64, match string, count int64) Scanner
75-
SScan(key string, cursor uint64, match string, count int64) Scanner
76-
HScan(key string, cursor uint64, match string, count int64) Scanner
77-
ZScan(key string, cursor uint64, match string, count int64) Scanner
74+
Scan(cursor uint64, match string, count int64) *ScanCmd
75+
SScan(key string, cursor uint64, match string, count int64) *ScanCmd
76+
HScan(key string, cursor uint64, match string, count int64) *ScanCmd
77+
ZScan(key string, cursor uint64, match string, count int64) *ScanCmd
7878
Append(key, value string) *IntCmd
7979
BitCount(key string, bitCount *BitCount) *IntCmd
8080
BitOpAnd(destKey string, keys ...string) *IntCmd
@@ -515,68 +515,56 @@ func (c *cmdable) Type(key string) *StatusCmd {
515515
return cmd
516516
}
517517

518-
func (c *cmdable) Scan(cursor uint64, match string, count int64) Scanner {
518+
func (c *cmdable) Scan(cursor uint64, match string, count int64) *ScanCmd {
519519
args := []interface{}{"scan", cursor}
520520
if match != "" {
521521
args = append(args, "match", match)
522522
}
523523
if count > 0 {
524524
args = append(args, "count", count)
525525
}
526-
cmd := NewScanCmd(args...)
526+
cmd := NewScanCmd(c.process, args...)
527527
c.process(cmd)
528-
return Scanner{
529-
client: c,
530-
ScanCmd: cmd,
531-
}
528+
return cmd
532529
}
533530

534-
func (c *cmdable) SScan(key string, cursor uint64, match string, count int64) Scanner {
531+
func (c *cmdable) SScan(key string, cursor uint64, match string, count int64) *ScanCmd {
535532
args := []interface{}{"sscan", key, cursor}
536533
if match != "" {
537534
args = append(args, "match", match)
538535
}
539536
if count > 0 {
540537
args = append(args, "count", count)
541538
}
542-
cmd := NewScanCmd(args...)
539+
cmd := NewScanCmd(c.process, args...)
543540
c.process(cmd)
544-
return Scanner{
545-
client: c,
546-
ScanCmd: cmd,
547-
}
541+
return cmd
548542
}
549543

550-
func (c *cmdable) HScan(key string, cursor uint64, match string, count int64) Scanner {
544+
func (c *cmdable) HScan(key string, cursor uint64, match string, count int64) *ScanCmd {
551545
args := []interface{}{"hscan", key, cursor}
552546
if match != "" {
553547
args = append(args, "match", match)
554548
}
555549
if count > 0 {
556550
args = append(args, "count", count)
557551
}
558-
cmd := NewScanCmd(args...)
552+
cmd := NewScanCmd(c.process, args...)
559553
c.process(cmd)
560-
return Scanner{
561-
client: c,
562-
ScanCmd: cmd,
563-
}
554+
return cmd
564555
}
565556

566-
func (c *cmdable) ZScan(key string, cursor uint64, match string, count int64) Scanner {
557+
func (c *cmdable) ZScan(key string, cursor uint64, match string, count int64) *ScanCmd {
567558
args := []interface{}{"zscan", key, cursor}
568559
if match != "" {
569560
args = append(args, "match", match)
570561
}
571562
if count > 0 {
572563
args = append(args, "count", count)
573564
}
574-
cmd := NewScanCmd(args...)
565+
cmd := NewScanCmd(c.process, args...)
575566
c.process(cmd)
576-
return Scanner{
577-
client: c,
578-
ScanCmd: cmd,
579-
}
567+
return cmd
580568
}
581569

582570
//------------------------------------------------------------------------------

iterator.go

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,18 @@ package redis
22

33
import "sync"
44

5-
type Scanner struct {
6-
client *cmdable
7-
*ScanCmd
8-
}
9-
10-
// Iterator creates a new ScanIterator.
11-
func (s Scanner) Iterator() *ScanIterator {
12-
return &ScanIterator{
13-
Scanner: s,
14-
}
15-
}
16-
175
// ScanIterator is used to incrementally iterate over a collection of elements.
186
// It's safe for concurrent use by multiple goroutines.
197
type ScanIterator struct {
20-
mu sync.Mutex // protects Scanner and pos
21-
Scanner
8+
mu sync.Mutex // protects Scanner and pos
9+
cmd *ScanCmd
2210
pos int
2311
}
2412

2513
// Err returns the last iterator error, if any.
2614
func (it *ScanIterator) Err() error {
2715
it.mu.Lock()
28-
err := it.ScanCmd.Err()
16+
err := it.cmd.Err()
2917
it.mu.Unlock()
3018
return err
3119
}
@@ -36,37 +24,38 @@ func (it *ScanIterator) Next() bool {
3624
defer it.mu.Unlock()
3725

3826
// Instantly return on errors.
39-
if it.ScanCmd.Err() != nil {
27+
if it.cmd.Err() != nil {
4028
return false
4129
}
4230

4331
// Advance cursor, check if we are still within range.
44-
if it.pos < len(it.ScanCmd.page) {
32+
if it.pos < len(it.cmd.page) {
4533
it.pos++
4634
return true
4735
}
4836

4937
for {
5038
// Return if there is no more data to fetch.
51-
if it.ScanCmd.cursor == 0 {
39+
if it.cmd.cursor == 0 {
5240
return false
5341
}
5442

5543
// Fetch next page.
56-
if it.ScanCmd._args[0] == "scan" {
57-
it.ScanCmd._args[1] = it.ScanCmd.cursor
44+
if it.cmd._args[0] == "scan" {
45+
it.cmd._args[1] = it.cmd.cursor
5846
} else {
59-
it.ScanCmd._args[2] = it.ScanCmd.cursor
47+
it.cmd._args[2] = it.cmd.cursor
6048
}
61-
it.client.process(it.ScanCmd)
62-
if it.ScanCmd.Err() != nil {
49+
50+
err := it.cmd.process(it.cmd)
51+
if err != nil {
6352
return false
6453
}
6554

6655
it.pos = 1
6756

68-
// Redis can occasionally return empty page
69-
if len(it.ScanCmd.page) > 0 {
57+
// Redis can occasionally return empty page.
58+
if len(it.cmd.page) > 0 {
7059
return true
7160
}
7261
}
@@ -76,8 +65,8 @@ func (it *ScanIterator) Next() bool {
7665
func (it *ScanIterator) Val() string {
7766
var v string
7867
it.mu.Lock()
79-
if it.ScanCmd.Err() == nil && it.pos > 0 && it.pos <= len(it.ScanCmd.page) {
80-
v = it.ScanCmd.page[it.pos-1]
68+
if it.cmd.Err() == nil && it.pos > 0 && it.pos <= len(it.cmd.page) {
69+
v = it.cmd.page[it.pos-1]
8170
}
8271
it.mu.Unlock()
8372
return v

0 commit comments

Comments
 (0)