Skip to content

Commit a06d554

Browse files
authored
Merge pull request ipfs/interface-go-ipfs-core#49 from MichaelMure/pinls
feat: make the CoreAPI expose a streaming pin interface This commit was moved from ipfs/interface-go-ipfs-core@292d906
2 parents 093084e + b1c5044 commit a06d554

File tree

4 files changed

+76
-30
lines changed

4 files changed

+76
-30
lines changed

coreiface/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

coreiface/tests/block.go

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
package tests
22

33
import (
4+
"bytes"
45
"context"
5-
"github.com/ipfs/interface-go-ipfs-core/path"
6+
"io"
67
"io/ioutil"
78
"strings"
89
"testing"
910

1011
coreiface "github.com/ipfs/interface-go-ipfs-core"
1112
opt "github.com/ipfs/interface-go-ipfs-core/options"
13+
"github.com/ipfs/interface-go-ipfs-core/path"
1214

1315
mh "github.com/multiformats/go-multihash"
1416
)
1517

18+
var (
19+
pbCid = "QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN"
20+
cborCid = "bafyreicnga62zhxnmnlt6ymq5hcbsg7gdhqdu6z4ehu3wpjhvqnflfy6nm"
21+
cborKCid = "bafyr2qgsohbwdlk7ajmmbb4lhoytmest4wdbe5xnexfvtxeatuyqqmwv3fgxp3pmhpc27gwey2cct56gloqefoqwcf3yqiqzsaqb7p4jefhcw"
22+
)
23+
24+
func pbBlock() io.Reader {
25+
return bytes.NewReader([]byte{10, 12, 8, 2, 18, 6, 104, 101, 108, 108, 111, 10, 24, 6})
26+
}
27+
28+
func cborBlock() io.Reader {
29+
return bytes.NewReader([]byte{101, 72, 101, 108, 108, 111})
30+
}
31+
1632
func (tp *TestSuite) TestBlock(t *testing.T) {
1733
tp.hasApi(t, func(api coreiface.CoreAPI) error {
1834
if api.Block() == nil {
@@ -38,12 +54,12 @@ func (tp *TestSuite) TestBlockPut(t *testing.T) {
3854
t.Fatal(err)
3955
}
4056

41-
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
57+
res, err := api.Block().Put(ctx, pbBlock())
4258
if err != nil {
4359
t.Fatal(err)
4460
}
4561

46-
if res.Path().Cid().String() != "QmPyo15ynbVrSTVdJL9th7JysHaAbXt9dM9tXk1bMHbRtk" {
62+
if res.Path().Cid().String() != pbCid {
4763
t.Errorf("got wrong cid: %s", res.Path().Cid().String())
4864
}
4965
}
@@ -56,12 +72,12 @@ func (tp *TestSuite) TestBlockPutFormat(t *testing.T) {
5672
t.Fatal(err)
5773
}
5874

59-
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("cbor"))
75+
res, err := api.Block().Put(ctx, cborBlock(), opt.Block.Format("cbor"))
6076
if err != nil {
6177
t.Fatal(err)
6278
}
6379

64-
if res.Path().Cid().String() != "bafyreiayl6g3gitr7ys7kyng7sjywlrgimdoymco3jiyab6rozecmoazne" {
80+
if res.Path().Cid().String() != cborCid {
6581
t.Errorf("got wrong cid: %s", res.Path().Cid().String())
6682
}
6783
}
@@ -74,12 +90,17 @@ func (tp *TestSuite) TestBlockPutHash(t *testing.T) {
7490
t.Fatal(err)
7591
}
7692

77-
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
93+
res, err := api.Block().Put(
94+
ctx,
95+
cborBlock(),
96+
opt.Block.Hash(mh.KECCAK_512, -1),
97+
opt.Block.Format("cbor"),
98+
)
7899
if err != nil {
79100
t.Fatal(err)
80101
}
81102

82-
if res.Path().Cid().String() != "bafyb2qgdh7w6dcq24u65xbtdoehyavegnpvxcqce7ttvs6ielgmwdfxrahmu37d33atik57x5y6s7d7qz32aasuwgirh3ocn6ywswqdifvu6e" {
103+
if res.Path().Cid().String() != cborKCid {
83104
t.Errorf("got wrong cid: %s", res.Path().Cid().String())
84105
}
85106
}
@@ -92,7 +113,7 @@ func (tp *TestSuite) TestBlockGet(t *testing.T) {
92113
t.Fatal(err)
93114
}
94115

95-
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
116+
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw"))
96117
if err != nil {
97118
t.Fatal(err)
98119
}
@@ -130,7 +151,7 @@ func (tp *TestSuite) TestBlockRm(t *testing.T) {
130151
t.Fatal(err)
131152
}
132153

133-
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
154+
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw"))
134155
if err != nil {
135156
t.Fatal(err)
136157
}
@@ -184,7 +205,7 @@ func (tp *TestSuite) TestBlockStat(t *testing.T) {
184205
t.Fatal(err)
185206
}
186207

187-
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
208+
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw"))
188209
if err != nil {
189210
t.Fatal(err)
190211
}
@@ -211,7 +232,7 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) {
211232
t.Fatal(err)
212233
}
213234

214-
_, err = api.Block().Put(ctx, strings.NewReader(`Hello`))
235+
_, err = api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw"))
215236
if err != nil {
216237
t.Fatal(err)
217238
}
@@ -220,14 +241,19 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) {
220241
t.Fatal("expected 0 pins")
221242
}
222243

223-
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Pin(true))
244+
res, err := api.Block().Put(
245+
ctx,
246+
strings.NewReader(`Hello`),
247+
opt.Block.Pin(true),
248+
opt.Block.Format("raw"),
249+
)
224250
if err != nil {
225251
t.Fatal(err)
226252
}
227253

228-
pins, err := api.Pin().Ls(ctx)
254+
pins, err := accPins(api.Pin().Ls(ctx))
229255
if err != nil {
230-
return
256+
t.Fatal(err)
231257
}
232258
if len(pins) != 1 {
233259
t.Fatal("expected 1 pin")

coreiface/tests/pin.go

Lines changed: 31 additions & 14 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.Type.Direct()))
154154
if err != nil {
155155
t.Fatal(err)
156156
}
@@ -160,10 +160,10 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
160160
}
161161

162162
if list[0].Path().String() != path.IpldPath(nd3.Cid()).String() {
163-
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpfsPath(nd2.Cid()).String())
163+
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpfsPath(nd3.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.Type.Recursive()))
167167
if err != nil {
168168
t.Fatal(err)
169169
}
@@ -173,10 +173,10 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
173173
}
174174

175175
if list[0].Path().String() != path.IpldPath(nd2.Cid()).String() {
176-
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd3.Cid()).String())
176+
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd2.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.Type.Indirect()))
180180
if err != nil {
181181
t.Fatal(err)
182182
}
@@ -186,7 +186,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
186186
}
187187

188188
if list[0].Path().Cid().String() != p0.Cid().String() {
189-
t.Error("unexpected path")
189+
t.Errorf("unexpected path, %s != %s", list[0].Path().Cid().String(), p0.Cid().String())
190190
}
191191

192192
res, err := api.Pin().Verify(ctx)
@@ -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.Type.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.Type.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.Type.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+
}

coreiface/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)