1 // Copyright 2016 Google LLC
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Package iterator provides support for standard Google API iterators.
16 // See https://github.com/GoogleCloudPlatform/gcloud-golang/wiki/Iterator-Guidelines.
25 // Done is returned by an iterator's Next method when the iteration is
26 // complete; when there are no more items to return.
27 var Done = errors.New("no more items in iterator")
29 // We don't support mixed calls to Next and NextPage because they play
30 // with the paging state in incompatible ways.
31 var errMixed = errors.New("iterator: Next and NextPage called on same iterator")
33 // PageInfo contains information about an iterator's paging state.
34 type PageInfo struct {
35 // Token is the token used to retrieve the next page of items from the
36 // API. You may set Token immediately after creating an iterator to
37 // begin iteration at a particular point. If Token is the empty string,
38 // the iterator will begin with the first eligible item.
40 // The result of setting Token after the first call to Next is undefined.
42 // After the underlying API method is called to retrieve a page of items,
43 // Token is set to the next-page token in the response.
46 // MaxSize is the maximum number of items returned by a call to the API.
47 // Set MaxSize as a hint to optimize the buffering behavior of the iterator.
48 // If zero, the page size is determined by the underlying service.
50 // Use Pager to retrieve a page of a specific, exact size.
53 // The error state of the iterator. Manipulated by PageInfo.next and Pager.
54 // This is a latch: it starts as nil, and once set should never change.
57 // If true, no more calls to fetch should be made. Set to true when fetch
58 // returns an empty page token. The iterator is Done when this is true AND
59 // the buffer is empty.
62 // Function that fetches a page from the underlying service. It should pass
63 // the pageSize and pageToken arguments to the service, fill the buffer
64 // with the results from the call, and return the next-page token returned
65 // by the service. The function must not remove any existing items from the
66 // buffer. If the underlying RPC takes an int32 page size, pageSize should
67 // be silently truncated.
68 fetch func(pageSize int, pageToken string) (nextPageToken string, err error)
70 // Function that returns the number of currently buffered items.
73 // Function that returns the buffer, after setting the buffer variable to nil.
74 takeBuf func() interface{}
76 // Set to true on first call to PageInfo.next or Pager.NextPage. Used to check
77 // for calls to both Next and NextPage with the same iterator.
78 nextCalled, nextPageCalled bool
81 // NewPageInfo exposes internals for iterator implementations.
82 // It is not a stable interface.
83 var NewPageInfo = newPageInfo
85 // If an iterator can support paging, its iterator-creating method should call
86 // this (via the NewPageInfo variable above).
88 // The fetch, bufLen and takeBuf arguments provide access to the
89 // iterator's internal slice of buffered items. They behave as described in
92 // The return value is the PageInfo.next method bound to the returned PageInfo value.
93 // (Returning it avoids exporting PageInfo.next.)
94 func newPageInfo(fetch func(int, string) (string, error), bufLen func() int, takeBuf func() interface{}) (*PageInfo, func() error) {
103 // Remaining returns the number of items available before the iterator makes another API call.
104 func (pi *PageInfo) Remaining() int { return pi.bufLen() }
106 // next provides support for an iterator's Next function. An iterator's Next
107 // should return the error returned by next if non-nil; else it can assume
108 // there is at least one item in its buffer, and it should return that item and
109 // remove it from the buffer.
110 func (pi *PageInfo) next() error {
112 if pi.err != nil { // Once we get an error, always return it.
113 // TODO(jba): fix so users can retry on transient errors? Probably not worth it.
116 if pi.nextPageCalled {
120 // Loop until we get some items or reach the end.
121 for pi.bufLen() == 0 && !pi.atEnd {
122 if err := pi.fill(pi.MaxSize); err != nil {
130 // Either the buffer is non-empty or pi.atEnd is true (or both).
131 if pi.bufLen() == 0 {
132 // The buffer is empty and pi.atEnd is true, i.e. the service has no
139 // Call the service to fill the buffer, using size and pi.Token. Set pi.Token to the
140 // next-page token returned by the call.
141 // If fill returns a non-nil error, the buffer will be empty.
142 func (pi *PageInfo) fill(size int) error {
143 tok, err := pi.fetch(size, pi.Token)
145 pi.takeBuf() // clear the buffer
152 // Pageable is implemented by iterators that support paging.
153 type Pageable interface {
154 // PageInfo returns paging information associated with the iterator.
158 // Pager supports retrieving iterator items a page at a time.
164 // NewPager returns a pager that uses iter. Calls to its NextPage method will
165 // obtain exactly pageSize items, unless fewer remain. The pageToken argument
166 // indicates where to start the iteration. Pass the empty string to start at
167 // the beginning, or pass a token retrieved from a call to Pager.NextPage.
169 // If you use an iterator with a Pager, you must not call Next on the iterator.
170 func NewPager(iter Pageable, pageSize int, pageToken string) *Pager {
172 pageInfo: iter.PageInfo(),
175 p.pageInfo.Token = pageToken
177 p.pageInfo.err = errors.New("iterator: page size must be positive")
182 // NextPage retrieves a sequence of items from the iterator and appends them
183 // to slicep, which must be a pointer to a slice of the iterator's item type.
184 // Exactly p.pageSize items will be appended, unless fewer remain.
186 // The first return value is the page token to use for the next page of items.
187 // If empty, there are no more pages. Aside from checking for the end of the
188 // iteration, the returned page token is only needed if the iteration is to be
189 // resumed a later time, in another context (possibly another process).
191 // The second return value is non-nil if an error occurred. It will never be
192 // the special iterator sentinel value Done. To recognize the end of the
193 // iteration, compare nextPageToken to the empty string.
195 // It is possible for NextPage to return a single zero-length page along with
196 // an empty page token when there are no more items in the iteration.
197 func (p *Pager) NextPage(slicep interface{}) (nextPageToken string, err error) {
198 p.pageInfo.nextPageCalled = true
199 if p.pageInfo.err != nil {
200 return "", p.pageInfo.err
202 if p.pageInfo.nextCalled {
203 p.pageInfo.err = errMixed
204 return "", p.pageInfo.err
206 if p.pageInfo.bufLen() > 0 {
207 return "", errors.New("must call NextPage with an empty buffer")
209 // The buffer must be empty here, so takeBuf is a no-op. We call it just to get
210 // the buffer's type.
211 wantSliceType := reflect.PtrTo(reflect.ValueOf(p.pageInfo.takeBuf()).Type())
213 return "", errors.New("nil passed to Pager.NextPage")
215 vslicep := reflect.ValueOf(slicep)
216 if vslicep.Type() != wantSliceType {
217 return "", fmt.Errorf("slicep should be of type %s, got %T", wantSliceType, slicep)
219 for p.pageInfo.bufLen() < p.pageSize {
220 if err := p.pageInfo.fill(p.pageSize - p.pageInfo.bufLen()); err != nil {
222 return "", p.pageInfo.err
224 if p.pageInfo.Token == "" {
229 e.Set(reflect.AppendSlice(e, reflect.ValueOf(p.pageInfo.takeBuf())))
230 return p.pageInfo.Token, nil