From b28c85fb748160342b6de0f8d660965f65ed144d Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Mon, 3 Mar 2025 18:50:46 -1000 Subject: [PATCH] Results.Close should return error 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. --- keytransform/keytransform.go | 4 ++-- mount/mount.go | 3 ++- query/query.go | 14 ++++++++------ query/query_impl.go | 14 +++++++------- query/query_test.go | 5 +++-- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/keytransform/keytransform.go b/keytransform/keytransform.go index 3e4b8ed6..c1c74149 100644 --- a/keytransform/keytransform.go +++ b/keytransform/keytransform.go @@ -94,8 +94,8 @@ func (d *Datastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) } return r, true }, - Close: func() { - cqr.Close() + Close: func() error { + return cqr.Close() }, }) return dsq.NaiveQueryApply(nq, qr), nil diff --git a/mount/mount.go b/mount/mount.go index 32737f4b..aa1d2606 100644 --- a/mount/mount.go +++ b/mount/mount.go @@ -140,11 +140,12 @@ func (h *querySet) Pop() interface{} { return last } -func (h *querySet) close() { +func (h *querySet) close() error { for _, qr := range h.heads { qr.results.Close() } h.heads = nil + return nil } func (h *querySet) addResults(mount ds.Key, results query.Results) { diff --git a/query/query.go b/query/query.go index d7693771..d3e4f1c6 100644 --- a/query/query.go +++ b/query/query.go @@ -153,7 +153,7 @@ type Results interface { Next() <-chan Result // returns a channel to wait for the next result NextSync() (Result, bool) // blocks and waits to return the next result, second parameter returns false when results are exhausted Rest() ([]Entry, error) // waits till processing finishes, returns all entries at once. - Close() // client may call Close to signal early exit + Close() error // client may call Close to signal early exit Done() <-chan struct{} // signals that Results is closed } @@ -187,9 +187,10 @@ func (r *results) Rest() ([]Entry, error) { return es, nil } -func (r *results) Close() { +func (r *results) Close() error { r.cancel() <-r.closed + return nil } func (r *results) Query() Query { @@ -273,17 +274,17 @@ func ResultsFromIterator(q Query, iter Iterator) Results { } } -func noopClose() {} +func noopClose() error { return nil } type Iterator struct { Next func() (Result, bool) - Close func() // note: might be called more than once + Close func() error // note: might be called more than once } type resultsIter struct { query Query next func() (Result, bool) - close func() + close func() error results *results } @@ -318,7 +319,7 @@ func (r *resultsIter) Rest() ([]Entry, error) { return es, nil } -func (r *resultsIter) Close() { +func (r *resultsIter) Close() error { if r.results != nil { // Close results collector. It will call r.close(). r.results.Close() @@ -326,6 +327,7 @@ func (r *resultsIter) Close() { // Call r.close() since there is no collector to call it when closed. r.close() } + return nil } func (r *resultsIter) Query() Query { diff --git a/query/query_impl.go b/query/query_impl.go index 403dddbd..3db36575 100644 --- a/query/query_impl.go +++ b/query/query_impl.go @@ -18,8 +18,8 @@ func NaiveFilter(qr Results, filter Filter) Results { } } }, - Close: func() { - qr.Close() + Close: func() error { + return qr.Close() }, }) } @@ -43,12 +43,12 @@ func NaiveLimit(qr Results, limit int) Results { limit-- return qr.NextSync() }, - Close: func() { + Close: func() error { if closed { - return + return nil } closed = true - qr.Close() + return qr.Close() }, }) } @@ -65,8 +65,8 @@ func NaiveOffset(qr Results, offset int) Results { } return qr.NextSync() }, - Close: func() { - qr.Close() + Close: func() error { + return qr.Close() }, }) } diff --git a/query/query_test.go b/query/query_test.go index 0b3195a5..3347f09b 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -200,7 +200,7 @@ func TestResultsFromIteratorNoClose(t *testing.T) { testResultsFromIterator(t, getKeysViaChan, nil) } -func testResultsFromIterator(t *testing.T, getKeys func(rs Results) []string, close func()) { +func testResultsFromIterator(t *testing.T, getKeys func(rs Results) []string, close func() error) { i := 0 results := ResultsFromIterator(Query{}, Iterator{ Next: func() (Result, bool) { @@ -221,8 +221,9 @@ func testResultsFromIterator(t *testing.T, getKeys func(rs Results) []string, cl func testResultsFromIteratorWClose(t *testing.T, getKeys func(rs Results) []string) { closeCalled := 0 - testResultsFromIterator(t, getKeys, func() { + testResultsFromIterator(t, getKeys, func() error { closeCalled++ + return nil }) if closeCalled != 1 { t.Errorf("close called %d times, expect it to be called just once", closeCalled)