scanner.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package radix
  2. import (
  3. "bufio"
  4. "strconv"
  5. "strings"
  6. "errors"
  7. "github.com/mediocregopher/radix/v3/resp/resp2"
  8. )
  9. // Scanner is used to iterate through the results of a SCAN call (or HSCAN,
  10. // SSCAN, etc...)
  11. //
  12. // Once created, repeatedly call Next() on it to fill the passed in string
  13. // pointer with the next result. Next will return false if there's no more
  14. // results to retrieve or if an error occurred, at which point Close should be
  15. // called to retrieve any error.
  16. type Scanner interface {
  17. Next(*string) bool
  18. Close() error
  19. }
  20. // ScanOpts are various parameters which can be passed into ScanWithOpts. Some
  21. // fields are required depending on which type of scan is being done.
  22. type ScanOpts struct {
  23. // The scan command to do, e.g. "SCAN", "HSCAN", etc...
  24. Command string
  25. // The key to perform the scan on. Only necessary when Command isn't "SCAN"
  26. Key string
  27. // An optional pattern to filter returned keys by
  28. Pattern string
  29. // An optional count hint to send to redis to indicate number of keys to
  30. // return per call. This does not affect the actual results of the scan
  31. // command, but it may be useful for optimizing certain datasets
  32. Count int
  33. // An optional type name to filter for values of the given type.
  34. // The type names are the same as returned by the "TYPE" command.
  35. // This if only available in Redis 6 or newer and only works with "SCAN".
  36. // If used with an older version of Redis or with a Command other than
  37. // "SCAN", scanning will fail.
  38. Type string
  39. }
  40. func (o ScanOpts) cmd(rcv interface{}, cursor string) CmdAction {
  41. cmdStr := strings.ToUpper(o.Command)
  42. args := make([]string, 0, 8)
  43. if cmdStr != "SCAN" {
  44. args = append(args, o.Key)
  45. }
  46. args = append(args, cursor)
  47. if o.Pattern != "" {
  48. args = append(args, "MATCH", o.Pattern)
  49. }
  50. if o.Count > 0 {
  51. args = append(args, "COUNT", strconv.Itoa(o.Count))
  52. }
  53. if o.Type != "" {
  54. args = append(args, "TYPE", o.Type)
  55. }
  56. return Cmd(rcv, cmdStr, args...)
  57. }
  58. // ScanAllKeys is a shortcut ScanOpts which can be used to scan all keys.
  59. var ScanAllKeys = ScanOpts{
  60. Command: "SCAN",
  61. }
  62. type scanner struct {
  63. Client
  64. ScanOpts
  65. res scanResult
  66. resIdx int
  67. err error
  68. }
  69. // NewScanner creates a new Scanner instance which will iterate over the redis
  70. // instance's Client using the ScanOpts.
  71. //
  72. // NOTE if Client is a *Cluster this will not work correctly, use the NewScanner
  73. // method on Cluster instead.
  74. func NewScanner(c Client, o ScanOpts) Scanner {
  75. return &scanner{
  76. Client: c,
  77. ScanOpts: o,
  78. res: scanResult{
  79. cur: "0",
  80. },
  81. }
  82. }
  83. func (s *scanner) Next(res *string) bool {
  84. for {
  85. if s.err != nil {
  86. return false
  87. }
  88. for s.resIdx < len(s.res.keys) {
  89. *res = s.res.keys[s.resIdx]
  90. s.resIdx++
  91. if *res != "" {
  92. return true
  93. }
  94. }
  95. if s.res.cur == "0" && s.res.keys != nil {
  96. return false
  97. }
  98. s.err = s.Client.Do(s.cmd(&s.res, s.res.cur))
  99. s.resIdx = 0
  100. }
  101. }
  102. func (s *scanner) Close() error {
  103. return s.err
  104. }
  105. type scanResult struct {
  106. cur string
  107. keys []string
  108. }
  109. func (s *scanResult) UnmarshalRESP(br *bufio.Reader) error {
  110. var ah resp2.ArrayHeader
  111. if err := ah.UnmarshalRESP(br); err != nil {
  112. return err
  113. } else if ah.N != 2 {
  114. return errors.New("not enough parts returned")
  115. }
  116. var c resp2.BulkString
  117. if err := c.UnmarshalRESP(br); err != nil {
  118. return err
  119. }
  120. s.cur = c.S
  121. s.keys = s.keys[:0]
  122. return (resp2.Any{I: &s.keys}).UnmarshalRESP(br)
  123. }