Skip to content

Commit 12840fe

Browse files
authored
Results.Close should return error (#230)
Results.Close should always return error. Even if it is not currently used for anything, having the ability to return error allows for higher level functionality to handle error conditiond that may be important for future code. This was removed in v0.8.0, as it did not serve a functional purpose.
1 parent b5f5dc9 commit 12840fe

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

Diff for: keytransform/keytransform.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func (d *Datastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error)
9494
}
9595
return r, true
9696
},
97-
Close: func() {
98-
cqr.Close()
97+
Close: func() error {
98+
return cqr.Close()
9999
},
100100
})
101101
return dsq.NaiveQueryApply(nq, qr), nil

Diff for: mount/mount.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,12 @@ func (h *querySet) Pop() interface{} {
140140
return last
141141
}
142142

143-
func (h *querySet) close() {
143+
func (h *querySet) close() error {
144144
for _, qr := range h.heads {
145145
qr.results.Close()
146146
}
147147
h.heads = nil
148+
return nil
148149
}
149150

150151
func (h *querySet) addResults(mount ds.Key, results query.Results) {

Diff for: query/query.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ type Results interface {
153153
Next() <-chan Result // returns a channel to wait for the next result
154154
NextSync() (Result, bool) // blocks and waits to return the next result, second parameter returns false when results are exhausted
155155
Rest() ([]Entry, error) // waits till processing finishes, returns all entries at once.
156-
Close() // client may call Close to signal early exit
156+
Close() error // client may call Close to signal early exit
157157
Done() <-chan struct{} // signals that Results is closed
158158
}
159159

@@ -187,9 +187,10 @@ func (r *results) Rest() ([]Entry, error) {
187187
return es, nil
188188
}
189189

190-
func (r *results) Close() {
190+
func (r *results) Close() error {
191191
r.cancel()
192192
<-r.closed
193+
return nil
193194
}
194195

195196
func (r *results) Query() Query {
@@ -273,17 +274,17 @@ func ResultsFromIterator(q Query, iter Iterator) Results {
273274
}
274275
}
275276

276-
func noopClose() {}
277+
func noopClose() error { return nil }
277278

278279
type Iterator struct {
279280
Next func() (Result, bool)
280-
Close func() // note: might be called more than once
281+
Close func() error // note: might be called more than once
281282
}
282283

283284
type resultsIter struct {
284285
query Query
285286
next func() (Result, bool)
286-
close func()
287+
close func() error
287288
results *results
288289
}
289290

@@ -318,14 +319,15 @@ func (r *resultsIter) Rest() ([]Entry, error) {
318319
return es, nil
319320
}
320321

321-
func (r *resultsIter) Close() {
322+
func (r *resultsIter) Close() error {
322323
if r.results != nil {
323324
// Close results collector. It will call r.close().
324325
r.results.Close()
325326
} else {
326327
// Call r.close() since there is no collector to call it when closed.
327328
r.close()
328329
}
330+
return nil
329331
}
330332

331333
func (r *resultsIter) Query() Query {

Diff for: query/query_impl.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func NaiveFilter(qr Results, filter Filter) Results {
1818
}
1919
}
2020
},
21-
Close: func() {
22-
qr.Close()
21+
Close: func() error {
22+
return qr.Close()
2323
},
2424
})
2525
}
@@ -43,12 +43,12 @@ func NaiveLimit(qr Results, limit int) Results {
4343
limit--
4444
return qr.NextSync()
4545
},
46-
Close: func() {
46+
Close: func() error {
4747
if closed {
48-
return
48+
return nil
4949
}
5050
closed = true
51-
qr.Close()
51+
return qr.Close()
5252
},
5353
})
5454
}
@@ -65,8 +65,8 @@ func NaiveOffset(qr Results, offset int) Results {
6565
}
6666
return qr.NextSync()
6767
},
68-
Close: func() {
69-
qr.Close()
68+
Close: func() error {
69+
return qr.Close()
7070
},
7171
})
7272
}

Diff for: query/query_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func TestResultsFromIteratorNoClose(t *testing.T) {
200200
testResultsFromIterator(t, getKeysViaChan, nil)
201201
}
202202

203-
func testResultsFromIterator(t *testing.T, getKeys func(rs Results) []string, close func()) {
203+
func testResultsFromIterator(t *testing.T, getKeys func(rs Results) []string, close func() error) {
204204
i := 0
205205
results := ResultsFromIterator(Query{}, Iterator{
206206
Next: func() (Result, bool) {
@@ -221,8 +221,9 @@ func testResultsFromIterator(t *testing.T, getKeys func(rs Results) []string, cl
221221

222222
func testResultsFromIteratorWClose(t *testing.T, getKeys func(rs Results) []string) {
223223
closeCalled := 0
224-
testResultsFromIterator(t, getKeys, func() {
224+
testResultsFromIterator(t, getKeys, func() error {
225225
closeCalled++
226+
return nil
226227
})
227228
if closeCalled != 1 {
228229
t.Errorf("close called %d times, expect it to be called just once", closeCalled)

0 commit comments

Comments
 (0)