-
Notifications
You must be signed in to change notification settings - Fork 44
/
iterator.go
202 lines (178 loc) · 5.15 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package kivik
import (
"context"
"io"
"net/http"
"sync"
"github.com/go-kivik/kivik/v4/driver"
internal "github.com/go-kivik/kivik/v4/int/errors"
)
type iterator interface {
Next(interface{}) error
Close() error
}
// possible states of the result set iterator
const (
// stateReady is the initial state before [ResultSet.Next] or
// [ResultSet.NextResultSet] is called.
stateReady = iota
// stateResultSetReady is the state after calling [ResultSet.NextResultSet]
stateResultSetReady
// stateResultSetRowReady is the state after calling [ResultSet.Next] within
// a result set.
stateResultSetRowReady
// stateEOQ is the state after having reached the final row in a result set.
// [ResultSet.ResultSetNext] should be called next.
stateEOQ
// stateRowReady is the state when not iterating resultsets, after
// [ResultSet.Next] has been called.
stateRowReady
// stateClosed means the last row has been retrieved. The iterator is no
// longer usable.
stateClosed
)
type iter struct {
feed iterator
onClose func()
mu sync.Mutex
state int // Set to true once Next() has been called
err error // non-nil only if state == stateClosed
wg sync.WaitGroup
cancel func() // cancel function to exit context goroutine when iterator is closed
curVal interface{}
}
// isReady returns an error if the iterator is not ready, because it has been
// closed, or has not been made ready yet.
func (i *iter) isReady() error {
if i.state == stateClosed {
return &internal.Error{Status: http.StatusBadRequest, Message: "kivik: Iterator is closed"}
}
if !stateIsReady(i.state) {
return &internal.Error{Status: http.StatusBadRequest, Message: "kivik: Iterator access before calling Next"}
}
return nil
}
func stateIsReady(state int) bool {
switch state {
case stateRowReady, stateResultSetReady, stateResultSetRowReady, stateClosed:
return true
}
return false
}
// newIterator instantiates a new iterator.
//
// ctx is a possibly-cancellable context. zeroValue is an empty instance of
// the data type this iterator iterates over feed is the iterator interface,
// which typically wraps a driver.X iterator
func newIterator(ctx context.Context, onClose func(), feed iterator, zeroValue interface{}) *iter {
i := &iter{
onClose: onClose,
feed: feed,
curVal: zeroValue,
}
ctx, i.cancel = context.WithCancel(ctx)
go i.awaitDone(ctx)
return i
}
// errIterator instantiates a new iterator that is already closed, and only
// returns an error.
func errIterator(err error) *iter {
return &iter{
state: stateClosed,
err: err,
}
}
// awaitDone blocks until the rows are closed or the context is cancelled, then
// closes the iterator if it's still open.
func (i *iter) awaitDone(ctx context.Context) {
<-ctx.Done()
i.mu.Lock()
_ = i.closeErr(ctx.Err())
i.mu.Unlock()
}
// Next prepares the next iterator result value for reading. It returns true on
// success, or false if there is no next result or an error occurs while
// preparing it. [Err] should be consulted to distinguish between the two.
func (i *iter) Next() bool {
i.mu.Lock()
defer i.mu.Unlock()
return i.next()
}
// next is the same as Next but doesn't do its own locking.
func (i *iter) next() bool {
if i.state == stateClosed {
return false
}
for {
err := i.feed.Next(i.curVal)
if err == driver.EOQ {
if i.state == stateResultSetReady || i.state == stateResultSetRowReady {
i.state = stateEOQ
i.err = nil
return false
}
continue
}
switch i.state {
case stateResultSetReady, stateResultSetRowReady:
i.state = stateResultSetRowReady
default:
i.state = stateRowReady
}
i.err = err
if i.err != nil {
_ = i.closeErr(nil)
return false
}
return true
}
}
// Close closes the iterator, preventing further enumeration, and freeing any
// resources (such as the http request body) of the underlying feed. If [Next]
// is called and there are no further results, the iterator is closed
// automatically and it will suffice to check the result of [Err]. Close is
// idempotent and does not affect the result of [Err].
func (i *iter) Close() error {
i.mu.Lock()
defer i.mu.Unlock()
i.wg.Wait()
return i.closeErr(nil)
}
func (i *iter) closeErr(err error) error {
if i.state == stateClosed {
return nil
}
i.state = stateClosed
if i.err == nil {
i.err = err
}
err = i.feed.Close()
if i.cancel != nil {
i.cancel()
}
if i.onClose != nil {
i.onClose()
}
return err
}
// Err returns the error, if any, that was encountered during iteration. Err may
// be called after an explicit or implicit [Close].
func (i *iter) Err() error {
i.mu.Lock()
defer i.mu.Unlock()
if i.err == io.EOF {
return nil
}
return i.err
}