...

Source file src/github.com/redis/go-redis/v9/iterator.go

Documentation: github.com/redis/go-redis/v9

     1  package redis
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  // ScanIterator is used to incrementally iterate over a collection of elements.
     8  type ScanIterator struct {
     9  	cmd *ScanCmd
    10  	pos int
    11  }
    12  
    13  // Err returns the last iterator error, if any.
    14  func (it *ScanIterator) Err() error {
    15  	return it.cmd.Err()
    16  }
    17  
    18  // Next advances the cursor and returns true if more values can be read.
    19  func (it *ScanIterator) Next(ctx context.Context) bool {
    20  	// Instantly return on errors.
    21  	if it.cmd.Err() != nil {
    22  		return false
    23  	}
    24  
    25  	// Advance cursor, check if we are still within range.
    26  	if it.pos < len(it.cmd.page) {
    27  		it.pos++
    28  		return true
    29  	}
    30  
    31  	for {
    32  		// Return if there is no more data to fetch.
    33  		if it.cmd.cursor == 0 {
    34  			return false
    35  		}
    36  
    37  		// Fetch next page.
    38  		switch it.cmd.args[0] {
    39  		case "scan", "qscan":
    40  			it.cmd.args[1] = it.cmd.cursor
    41  		default:
    42  			it.cmd.args[2] = it.cmd.cursor
    43  		}
    44  
    45  		err := it.cmd.process(ctx, it.cmd)
    46  		if err != nil {
    47  			return false
    48  		}
    49  
    50  		it.pos = 1
    51  
    52  		// Redis can occasionally return empty page.
    53  		if len(it.cmd.page) > 0 {
    54  			return true
    55  		}
    56  	}
    57  }
    58  
    59  // Val returns the key/field at the current cursor position.
    60  func (it *ScanIterator) Val() string {
    61  	var v string
    62  	if it.cmd.Err() == nil && it.pos > 0 && it.pos <= len(it.cmd.page) {
    63  		v = it.cmd.page[it.pos-1]
    64  	}
    65  	return v
    66  }
    67  

View as plain text