Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit ecadbbe

Browse files
committed
feat: make the CoreAPI expose a streaming pin interface
1 parent 7bbf5bb commit ecadbbe

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

pin.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type Pin interface {
1414

1515
// Type of the pin
1616
Type() string
17+
18+
// if not nil, an error happened. Everything else should be ignored.
19+
Err() error
1720
}
1821

1922
// PinStatus holds information about pin health
@@ -41,7 +44,7 @@ type PinAPI interface {
4144
Add(context.Context, path.Path, ...options.PinAddOption) error
4245

4346
// 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)
4548

4649
// Rm removes pin for object specified by the path
4750
Rm(context.Context, path.Path, ...options.PinRmOption) error

tests/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) {
225225
t.Fatal(err)
226226
}
227227

228-
pins, err := api.Pin().Ls(ctx)
228+
pins, err := accPins(api.Pin().Ls(ctx))
229229
if err != nil {
230230
return
231231
}

tests/pin.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) {
6767
t.Fatal(err)
6868
}
6969

70-
list, err := api.Pin().Ls(ctx)
70+
list, err := accPins(api.Pin().Ls(ctx))
7171
if err != nil {
7272
t.Fatal(err)
7373
}
@@ -89,7 +89,7 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) {
8989
t.Fatal(err)
9090
}
9191

92-
list, err = api.Pin().Ls(ctx)
92+
list, err = accPins(api.Pin().Ls(ctx))
9393
if err != nil {
9494
t.Fatal(err)
9595
}
@@ -141,7 +141,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
141141
t.Fatal(err)
142142
}
143143

144-
list, err := api.Pin().Ls(ctx)
144+
list, err := accPins(api.Pin().Ls(ctx))
145145
if err != nil {
146146
t.Fatal(err)
147147
}
@@ -150,7 +150,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
150150
t.Errorf("unexpected pin list len: %d", len(list))
151151
}
152152

153-
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Direct())
153+
list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Direct()))
154154
if err != nil {
155155
t.Fatal(err)
156156
}
@@ -163,7 +163,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
163163
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpfsPath(nd2.Cid()).String())
164164
}
165165

166-
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Recursive())
166+
list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Recursive()))
167167
if err != nil {
168168
t.Fatal(err)
169169
}
@@ -176,7 +176,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
176176
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd3.Cid()).String())
177177
}
178178

179-
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Indirect())
179+
list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Indirect()))
180180
if err != nil {
181181
t.Fatal(err)
182182
}
@@ -390,21 +390,21 @@ func getThreeChainedNodes(t *testing.T, ctx context.Context, api iface.CoreAPI,
390390
func assertPinTypes(t *testing.T, ctx context.Context, api iface.CoreAPI, recusive, direct, indirect []cidContainer) {
391391
assertPinLsAllConsistency(t, ctx, api)
392392

393-
list, err := api.Pin().Ls(ctx, opt.Pin.Type.Recursive())
393+
list, err := accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Recursive()))
394394
if err != nil {
395395
t.Fatal(err)
396396
}
397397

398398
assertPinCids(t, list, recusive...)
399399

400-
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Direct())
400+
list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Direct()))
401401
if err != nil {
402402
t.Fatal(err)
403403
}
404404

405405
assertPinCids(t, list, direct...)
406406

407-
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Indirect())
407+
list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Indirect()))
408408
if err != nil {
409409
t.Fatal(err)
410410
}
@@ -454,7 +454,7 @@ func assertPinCids(t *testing.T, pins []iface.Pin, cids ...cidContainer) {
454454
// assertPinLsAllConsistency verifies that listing all pins gives the same result as listing the pin types individually
455455
func assertPinLsAllConsistency(t *testing.T, ctx context.Context, api iface.CoreAPI) {
456456
t.Helper()
457-
allPins, err := api.Pin().Ls(ctx)
457+
allPins, err := accPins(api.Pin().Ls(ctx))
458458
if err != nil {
459459
t.Fatal(err)
460460
}
@@ -485,7 +485,7 @@ func assertPinLsAllConsistency(t *testing.T, ctx context.Context, api iface.Core
485485
}
486486

487487
for typeStr, pinProps := range typeMap {
488-
pins, err := api.Pin().Ls(ctx, pinProps.PinLsOption)
488+
pins, err := accPins(api.Pin().Ls(ctx, pinProps.PinLsOption))
489489
if err != nil {
490490
t.Fatal(err)
491491
}
@@ -505,3 +505,20 @@ func assertPinLsAllConsistency(t *testing.T, ctx context.Context, api iface.Core
505505
}
506506
}
507507
}
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+
}

tests/unixfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ func (tp *TestSuite) TestAddPinned(t *testing.T) {
542542
t.Fatal(err)
543543
}
544544

545-
pins, err := api.Pin().Ls(ctx)
545+
pins, err := accPins(api.Pin().Ls(ctx))
546546
if err != nil {
547547
t.Fatal(err)
548548
}

0 commit comments

Comments
 (0)