Skip to content

Commit 74f7110

Browse files
authored
No goprocess (#223)
* remove goprocess from api * replace goprocess with context * Close functions do not need to return error * replace ResultsWithProcess with ResultsWithContext * remove depricated ResultsWithChan function. * remove ResultBuilder to simplify query API The ResultBuilder is unnecessary as all of its functionality is available through ResultsWithContext.
1 parent 924d783 commit 74f7110

File tree

11 files changed

+125
-218
lines changed

11 files changed

+125
-218
lines changed

examples/fs.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,30 @@ func (d *Datastore) Query(ctx context.Context, q query.Query) (query.Results, er
134134
filepath.Walk(d.path, walkFn)
135135
close(results)
136136
}()
137-
r := query.ResultsWithChan(q, results)
137+
138+
r := query.ResultsWithContext(q, func(ctx context.Context, out chan<- query.Result) {
139+
loop:
140+
for {
141+
select {
142+
case <-ctx.Done(): // client told us to close early
143+
break loop
144+
case e, more := <-results:
145+
if !more {
146+
return
147+
}
148+
149+
select {
150+
case out <- e:
151+
case <-ctx.Done(): // client told us to close early
152+
break loop
153+
}
154+
}
155+
}
156+
// Drain results on cancel so writer is unblocked.
157+
for range results {
158+
}
159+
})
160+
138161
r = query.NaiveQueryApply(q, r)
139162
return r, nil
140163
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/ipfs/go-datastore
33
require (
44
github.com/google/uuid v1.6.0
55
github.com/ipfs/go-ipfs-delay v0.0.1
6-
github.com/jbenet/goprocess v0.1.4
76
go.opentelemetry.io/otel v1.16.0
87
go.opentelemetry.io/otel/trace v1.16.0
98
go.uber.org/multierr v1.11.0

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1111
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1212
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
1313
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
14-
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
15-
github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o=
16-
github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
1714
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
1815
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1916
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

keytransform/keytransform.go

Lines changed: 2 additions & 2 deletions
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() error {
98-
return cqr.Close()
97+
Close: func() {
98+
cqr.Close()
9999
},
100100
})
101101
return dsq.NaiveQueryApply(nq, qr), nil

mount/mount.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,8 @@ func (qr *queryResults) advance() bool {
101101
qr.next = query.Result{}
102102
r, more := qr.results.NextSync()
103103
if !more {
104-
err := qr.results.Close()
104+
qr.results.Close()
105105
qr.results = nil
106-
if err != nil {
107-
// One more result, the error.
108-
qr.next = query.Result{Error: err}
109-
return true
110-
}
111106
return false
112107
}
113108

@@ -145,19 +140,11 @@ func (h *querySet) Pop() interface{} {
145140
return last
146141
}
147142

148-
func (h *querySet) close() error {
149-
var errs []error
143+
func (h *querySet) close() {
150144
for _, qr := range h.heads {
151-
err := qr.results.Close()
152-
if err != nil {
153-
errs = append(errs, err)
154-
}
145+
qr.results.Close()
155146
}
156147
h.heads = nil
157-
if len(errs) > 0 {
158-
return errs[0]
159-
}
160-
return nil
161148
}
162149

163150
func (h *querySet) addResults(mount ds.Key, results query.Results) {
@@ -339,7 +326,7 @@ func (d *Datastore) Query(ctx context.Context, master query.Query) (query.Result
339326
results, err := dstore.Query(ctx, qi)
340327

341328
if err != nil {
342-
_ = queries.close()
329+
queries.close()
343330
return nil, err
344331
}
345332
queries.addResults(mount, results)

mount/mount_test.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,7 @@ func TestQuerySimple(t *testing.T) {
263263
t.Errorf("did not see wanted key %q in %+v", myKey, entries)
264264
}
265265

266-
err = res.Close()
267-
if err != nil {
268-
t.Errorf("result.Close failed %d", err)
269-
}
266+
res.Close()
270267
}
271268

272269
func TestQueryAcrossMounts(t *testing.T) {
@@ -307,10 +304,7 @@ func TestQueryAcrossMounts(t *testing.T) {
307304
}
308305
entries, err := res.Rest()
309306
if err != nil {
310-
err = res.Close()
311-
if err != nil {
312-
t.Errorf("result.Close failed %d", err)
313-
}
307+
res.Close()
314308
t.Fatalf("Query Results.Rest fail: %v\n", err)
315309
}
316310
if len(entries) != len(values) {
@@ -407,10 +401,7 @@ func TestQueryAcrossMountsWithSort(t *testing.T) {
407401
}
408402
}
409403

410-
err = res.Close()
411-
if err != nil {
412-
t.Errorf("result.Close failed %d", err)
413-
}
404+
res.Close()
414405
}
415406

416407
func TestQueryLimitAcrossMountsWithSort(t *testing.T) {
@@ -473,10 +464,7 @@ func TestQueryLimitAcrossMountsWithSort(t *testing.T) {
473464
}
474465
}
475466

476-
err = res.Close()
477-
if err != nil {
478-
t.Errorf("result.Close failed %d", err)
479-
}
467+
res.Close()
480468
}
481469

482470
func TestQueryLimitAndOffsetAcrossMountsWithSort(t *testing.T) {
@@ -540,10 +528,7 @@ func TestQueryLimitAndOffsetAcrossMountsWithSort(t *testing.T) {
540528
}
541529
}
542530

543-
err = res.Close()
544-
if err != nil {
545-
t.Errorf("result.Close failed %d", err)
546-
}
531+
res.Close()
547532
}
548533

549534
func TestQueryFilterAcrossMountsWithSort(t *testing.T) {
@@ -606,10 +591,7 @@ func TestQueryFilterAcrossMountsWithSort(t *testing.T) {
606591
}
607592
}
608593

609-
err = res.Close()
610-
if err != nil {
611-
t.Errorf("result.Close failed %d", err)
612-
}
594+
res.Close()
613595
}
614596

615597
func TestQueryLimitAndOffsetWithNoData(t *testing.T) {
@@ -639,10 +621,7 @@ func TestQueryLimitAndOffsetWithNoData(t *testing.T) {
639621
t.Fatalf("expected %d entries, but got %d", len(expect), len(entries))
640622
}
641623

642-
err = res.Close()
643-
if err != nil {
644-
t.Errorf("result.Close failed %d", err)
645-
}
624+
res.Close()
646625
}
647626

648627
func TestQueryLimitWithNotEnoughData(t *testing.T) {
@@ -682,10 +661,7 @@ func TestQueryLimitWithNotEnoughData(t *testing.T) {
682661
t.Fatalf("expected %d entries, but got %d", len(expect), len(entries))
683662
}
684663

685-
err = res.Close()
686-
if err != nil {
687-
t.Errorf("result.Close failed %d", err)
688-
}
664+
res.Close()
689665
}
690666

691667
func TestQueryOffsetWithNotEnoughData(t *testing.T) {
@@ -722,10 +698,7 @@ func TestQueryOffsetWithNotEnoughData(t *testing.T) {
722698
t.Fatalf("expected %d entries, but got %d", len(expect), len(entries))
723699
}
724700

725-
err = res.Close()
726-
if err != nil {
727-
t.Errorf("result.Close failed %d", err)
728-
}
701+
res.Close()
729702
}
730703

731704
func TestLookupPrio(t *testing.T) {

namespace/namespace_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ func (ks *DSSuite) TestQuery(c *C) {
119119
c.Check(string(ent.Value), Equals, string(expect[i].Value))
120120
}
121121

122-
err = qres.Close()
123-
c.Check(err, Equals, nil)
122+
qres.Close()
124123

125124
qres, err = nsds.Query(ctx, dsq.Query{Prefix: "bar"})
126125
c.Check(err, Equals, nil)

0 commit comments

Comments
 (0)