You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

228 lines
6.1 KiB

  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package iterator_test
  15. import (
  16. "bytes"
  17. "fmt"
  18. "html/template"
  19. "log"
  20. "math"
  21. "net/http"
  22. "sort"
  23. "strconv"
  24. "golang.org/x/net/context"
  25. "google.golang.org/api/iterator"
  26. )
  27. var (
  28. client *Client
  29. ctx = context.Background()
  30. )
  31. var pageTemplate = template.Must(template.New("").Parse(`
  32. <table>
  33. {{range .Entries}}
  34. <tr><td>{{.}}</td></tr>
  35. {{end}}
  36. </table>
  37. {{with .Next}}
  38. <a href="/entries?pageToken={{.}}">Next Page</a>
  39. {{end}}
  40. `))
  41. // This example demonstrates how to use Pager to support
  42. // pagination on a web site.
  43. func Example_webHandler(w http.ResponseWriter, r *http.Request) {
  44. const pageSize = 25
  45. it := client.Items(ctx)
  46. var items []int
  47. pageToken, err := iterator.NewPager(it, pageSize, r.URL.Query().Get("pageToken")).NextPage(&items)
  48. if err != nil {
  49. http.Error(w, fmt.Sprintf("getting next page: %v", err), http.StatusInternalServerError)
  50. }
  51. data := struct {
  52. Items []int
  53. Next string
  54. }{
  55. items,
  56. pageToken,
  57. }
  58. var buf bytes.Buffer
  59. if err := pageTemplate.Execute(&buf, data); err != nil {
  60. http.Error(w, fmt.Sprintf("executing page template: %v", err), http.StatusInternalServerError)
  61. }
  62. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  63. if _, err := buf.WriteTo(w); err != nil {
  64. log.Printf("writing response: %v", err)
  65. }
  66. }
  67. // This example demonstrates how to use a Pager to page through an iterator in a loop.
  68. func Example_pageLoop() {
  69. // Find all primes up to 42, in pages of size 5.
  70. const max = 42
  71. const pageSize = 5
  72. p := iterator.NewPager(Primes(max), pageSize, "" /* start from the beginning */)
  73. for page := 0; ; page++ {
  74. var items []int
  75. pageToken, err := p.NextPage(&items)
  76. if err != nil {
  77. log.Fatalf("Iterator paging failed: %v", err)
  78. }
  79. fmt.Printf("Page %d: %v\n", page, items)
  80. if pageToken == "" {
  81. break
  82. }
  83. }
  84. // Output:
  85. // Page 0: [2 3 5 7 11]
  86. // Page 1: [13 17 19 23 29]
  87. // Page 2: [31 37 41]
  88. }
  89. // The example demonstrates how to use a Pager to request a page from a given token.
  90. func Example_pageToken() {
  91. const pageSize = 5
  92. const pageToken = "1337"
  93. p := iterator.NewPager(Primes(0), pageSize, pageToken)
  94. var items []int
  95. nextPage, err := p.NextPage(&items)
  96. if err != nil {
  97. log.Fatalf("Iterator paging failed: %v", err)
  98. }
  99. fmt.Printf("Primes: %v\nToken: %q\n", items, nextPage)
  100. // Output:
  101. // Primes: [1361 1367 1373 1381 1399]
  102. // Token: "1400"
  103. }
  104. // This example demonstrates how to get exactly the items in the buffer, without
  105. // triggering an extra RPC.
  106. func Example_serverPages() {
  107. // The iterator returned by Primes has a default page size of 20, which means
  108. // it will return all the primes in the range [2, 21).
  109. it := Primes(0)
  110. var items []int
  111. for {
  112. item, err := it.Next()
  113. if err != nil && err != iterator.Done {
  114. log.Fatal(err)
  115. }
  116. if err == iterator.Done {
  117. break
  118. }
  119. items = append(items, item)
  120. if it.PageInfo().Remaining() == 0 {
  121. break
  122. }
  123. }
  124. fmt.Println(items)
  125. // Output:
  126. // [2 3 5 7 11 13 17 19]
  127. }
  128. // Primes returns a iterator which returns a sequence of prime numbers.
  129. // If non-zero, max specifies the maximum number which could possibly be
  130. // returned.
  131. func Primes(max int) *SieveIterator {
  132. it := &SieveIterator{pos: 2, max: max}
  133. it.pageInfo, it.nextFunc = iterator.NewPageInfo(
  134. it.fetch,
  135. func() int { return len(it.items) },
  136. func() interface{} { b := it.items; it.items = nil; return b })
  137. return it
  138. }
  139. // SieveIterator is an iterator that returns primes using the sieve of
  140. // Eratosthenes. It is a demonstration of how an iterator might work.
  141. // Internally, it uses "page size" as the number of ints to consider,
  142. // and "page token" as the first number to consider (defaults to 2).
  143. type SieveIterator struct {
  144. pageInfo *iterator.PageInfo
  145. nextFunc func() error
  146. max int // The largest number to consider.
  147. p []int // Primes in the range [2, pos).
  148. pos int // Next number to consider when generating p.
  149. items []int
  150. }
  151. // PageInfo returns a PageInfo, which supports pagination.
  152. func (it *SieveIterator) PageInfo() *iterator.PageInfo { return it.pageInfo }
  153. func (it *SieveIterator) fetch(pageSize int, pageToken string) (string, error) {
  154. start := 2
  155. if pageToken != "" {
  156. s, err := strconv.Atoi(pageToken)
  157. if err != nil || s < 2 {
  158. return "", fmt.Errorf("invalid token %q", pageToken)
  159. }
  160. start = s
  161. }
  162. if pageSize == 0 {
  163. pageSize = 20 // Default page size.
  164. }
  165. // Make sure sufficient primes have been calculated.
  166. it.calc(start + pageSize)
  167. // Find the subslice of primes which match this page.
  168. // Note that PageInfo requires that fetch does not remove any existing items,
  169. // so we cannot assume that items is empty at this call.
  170. items := it.p[sort.SearchInts(it.p, start):]
  171. items = items[:sort.SearchInts(items, start+pageSize)]
  172. it.items = append(it.items, items...)
  173. if it.max > 0 && start+pageSize > it.max {
  174. return "", nil // No more possible numbers to return.
  175. }
  176. return strconv.Itoa(start + pageSize), nil
  177. }
  178. // calc populates p with all primes up to, but not including, max.
  179. func (it *SieveIterator) calc(max int) {
  180. if it.max > 0 && max > it.max+1 { // it.max is an inclusive bounds, max is exclusive.
  181. max = it.max + 1
  182. }
  183. outer:
  184. for x := it.pos; x < max; x++ {
  185. sqrt := int(math.Sqrt(float64(x)))
  186. for _, p := range it.p {
  187. switch {
  188. case x%p == 0:
  189. // Not a prime.
  190. continue outer
  191. case p > sqrt:
  192. // Only need to check up to sqrt.
  193. break
  194. }
  195. }
  196. it.p = append(it.p, x)
  197. }
  198. it.pos = max
  199. }
  200. func (it *SieveIterator) Next() (int, error) {
  201. if err := it.nextFunc(); err != nil {
  202. return 0, err
  203. }
  204. item := it.items[0]
  205. it.items = it.items[1:]
  206. return item, nil
  207. }