@cryptotaxi247 / kubo / commits / e3b568671

feat: make the CoreAPI expose a streaming pin interface

This commit was moved from ipfs/interface-go-ipfs-core@f976af7ba62d0209b53aeef72fb102c4387d3f00 This commit was moved from ipfs/boxo@6eb5c9791b91da125d269d84b201d0acc8e7657a

Michael Muré committed Nov 27, 2019 at 21:40 UTC e3b5686710f471a15d736c62ef2f6e66bbbec8a0
4 files changed +34 -14
core/coreiface/pin.go
+4 -1
@@ -14,6 +14,9 @@ type Pin interface {
14
15 // Type of the pin
16 Type() string
17 +
18 + // if not nil, an error happened. Everything else should be ignored.
19 + Err() error
20 }
21
22 // PinStatus holds information about pin health
@@ -41,7 +44,7 @@ type PinAPI interface {
44 Add(context.Context, path.Path, ...options.PinAddOption) error
45
46 // Ls returns list of pinned objects on this node
44 - Ls(context.Context, ...options.PinLsOption) ([]Pin, error)
47 + Ls(context.Context, ...options.PinLsOption) (<-chan Pin, error)
48
49 // Rm removes pin for object specified by the path
50 Rm(context.Context, path.Path, ...options.PinRmOption) error
core/coreiface/tests/block.go
+1 -1
@@ -225,7 +225,7 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) {
225 t.Fatal(err)
226 }
227
228 - pins, err := api.Pin().Ls(ctx)
228 + pins, err := accPins(api.Pin().Ls(ctx))
229 if err != nil {
230 return
231 }
core/coreiface/tests/pin.go
+28 -11
@@ -67,7 +67,7 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) {
67 t.Fatal(err)
68 }
69
70 - list, err := api.Pin().Ls(ctx)
70 + list, err := accPins(api.Pin().Ls(ctx))
71 if err != nil {
72 t.Fatal(err)
73 }
@@ -89,7 +89,7 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) {
89 t.Fatal(err)
90 }
91
92 - list, err = api.Pin().Ls(ctx)
92 + list, err = accPins(api.Pin().Ls(ctx))
93 if err != nil {
94 t.Fatal(err)
95 }
@@ -141,7 +141,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
141 t.Fatal(err)
142 }
143
144 - list, err := api.Pin().Ls(ctx)
144 + list, err := accPins(api.Pin().Ls(ctx))
145 if err != nil {
146 t.Fatal(err)
147 }
@@ -150,7 +150,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
150 t.Errorf("unexpected pin list len: %d", len(list))
151 }
152
153 - list, err = api.Pin().Ls(ctx, opt.Pin.Type.Direct())
153 + list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Type.Direct()))
154 if err != nil {
155 t.Fatal(err)
156 }
@@ -163,7 +163,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
163 t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpfsPath(nd2.Cid()).String())
164 }
165
166 - list, err = api.Pin().Ls(ctx, opt.Pin.Type.Recursive())
166 + list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Type.Recursive()))
167 if err != nil {
168 t.Fatal(err)
169 }
@@ -176,7 +176,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
176 t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd3.Cid()).String())
177 }
178
179 - list, err = api.Pin().Ls(ctx, opt.Pin.Type.Indirect())
179 + list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Type.Indirect()))
180 if err != nil {
181 t.Fatal(err)
182 }
@@ -390,21 +390,21 @@ func getThreeChainedNodes(t *testing.T, ctx context.Context, api iface.CoreAPI,
390 func assertPinTypes(t *testing.T, ctx context.Context, api iface.CoreAPI, recusive, direct, indirect []cidContainer) {
391 assertPinLsAllConsistency(t, ctx, api)
392
393 - list, err := api.Pin().Ls(ctx, opt.Pin.Type.Recursive())
393 + list, err := accPins(api.Pin().Ls(ctx, opt.Pin.Type.Recursive()))
394 if err != nil {
395 t.Fatal(err)
396 }
397
398 assertPinCids(t, list, recusive...)
399
400 - list, err = api.Pin().Ls(ctx, opt.Pin.Type.Direct())
400 + list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Type.Direct()))
401 if err != nil {
402 t.Fatal(err)
403 }
404
405 assertPinCids(t, list, direct...)
406
407 - list, err = api.Pin().Ls(ctx, opt.Pin.Type.Indirect())
407 + list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Type.Indirect()))
408 if err != nil {
409 t.Fatal(err)
410 }
@@ -454,7 +454,7 @@ func assertPinCids(t *testing.T, pins []iface.Pin, cids ...cidContainer) {
454 // assertPinLsAllConsistency verifies that listing all pins gives the same result as listing the pin types individually
455 func assertPinLsAllConsistency(t *testing.T, ctx context.Context, api iface.CoreAPI) {
456 t.Helper()
457 - allPins, err := api.Pin().Ls(ctx)
457 + allPins, err := accPins(api.Pin().Ls(ctx))
458 if err != nil {
459 t.Fatal(err)
460 }
@@ -485,7 +485,7 @@ func assertPinLsAllConsistency(t *testing.T, ctx context.Context, api iface.Core
485 }
486
487 for typeStr, pinProps := range typeMap {
488 - pins, err := api.Pin().Ls(ctx, pinProps.PinLsOption)
488 + pins, err := accPins(api.Pin().Ls(ctx, pinProps.PinLsOption))
489 if err != nil {
490 t.Fatal(err)
491 }
@@ -505,3 +505,20 @@ func assertPinLsAllConsistency(t *testing.T, ctx context.Context, api iface.Core
505 }
506 }
507 }
508 +
509 +func accPins(pins <-chan iface.Pin, err error) ([]iface.Pin, error) {
510 + if err != nil {
511 + return nil, err
512 + }
513 +
514 + var result []iface.Pin
515 +
516 + for pin := range pins {
517 + if pin.Err() != nil {
518 + return nil, pin.Err()
519 + }
520 + result = append(result, pin)
521 + }
522 +
523 + return result, nil
524 +}
core/coreiface/tests/unixfs.go
+1 -1
@@ -542,7 +542,7 @@ func (tp *TestSuite) TestAddPinned(t *testing.T) {
542 t.Fatal(err)
543 }
544
545 - pins, err := api.Pin().Ls(ctx)
545 + pins, err := accPins(api.Pin().Ls(ctx))
546 if err != nil {
547 t.Fatal(err)
548 }