depager.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. */
  6. package depager
  7. import (
  8. "fmt"
  9. )
  10. /*
  11. The `Page` interface must wrap server responses. This
  12. allows pagers to calculate page sizes and iterate over
  13. response aggregates.
  14. If the underlying value of this interface is `nil` (e.g. a
  15. nil pointer to a struct or a nil slice), `Elems()` will
  16. panic.
  17. */
  18. type Page[T any] interface {
  19. // Elems must return the items from the current page
  20. Elems() []T
  21. }
  22. // Exposes the part of the client that depager understands.
  23. type Client[T any] interface {
  24. // NextPage returns the next page or it returns an error
  25. NextPage(
  26. page Page[T],
  27. offset uint64, // item offset at which to start page
  28. ) (
  29. count uint64, // total count of all items being paged
  30. err error,
  31. )
  32. }
  33. type Pager[T any] interface {
  34. // Iter is intended to be used in a for-range loop
  35. Iter() <-chan T
  36. // LastErr must return the first error encountered, if any
  37. LastErr() error
  38. }
  39. func NewPager[T any](
  40. c Client[T],
  41. pagePool chan Page[T],
  42. ) Pager[T] {
  43. if len(pagePool) == 0 {
  44. panic("new pager: provided page pool is empty")
  45. }
  46. var pageSize uint64
  47. pg := <-pagePool
  48. pageSize = uint64(cap(pg.Elems()))
  49. pagePool <- pg
  50. return &pager[T]{
  51. client: c,
  52. n: pageSize,
  53. pagePool: pagePool,
  54. }
  55. }
  56. /*
  57. Retrieve n items in the range [m*n, m*n + n - 1], inclusive.
  58. We keep len(pagePool) pages buffered.
  59. */
  60. type pager[T any] struct {
  61. client Client[T]
  62. m uint64
  63. n uint64
  64. err error
  65. pagePool chan Page[T]
  66. cnt uint64
  67. }
  68. func (p *pager[T]) iteratePages() <-chan Page[T] {
  69. ch := make(chan Page[T], len(p.pagePool))
  70. go func() {
  71. defer close(ch)
  72. var page Page[T]
  73. for {
  74. page = <-p.pagePool
  75. cnt, err := p.client.NextPage(page, p.m*p.n)
  76. if err != nil {
  77. p.err = err
  78. return
  79. }
  80. if p.cnt == 0 {
  81. p.cnt = cnt
  82. }
  83. ch <- page
  84. if (p.m*p.n + p.n) >= p.cnt {
  85. return
  86. }
  87. p.m++
  88. }
  89. }()
  90. return ch
  91. }
  92. func (p *pager[T]) Iter() <-chan T {
  93. ch := make(chan T, p.n)
  94. go func() {
  95. defer close(ch)
  96. for page := range p.iteratePages() {
  97. for _, i := range page.Elems() {
  98. ch <- i
  99. }
  100. p.pagePool <- page
  101. if p.err != nil {
  102. p.err = fmt.Errorf("pager: iterate items: %s", p.err)
  103. return
  104. }
  105. }
  106. }()
  107. return ch
  108. }
  109. func (p *pager[T]) LastErr() error {
  110. return p.err
  111. }