Skip to content

Commit 7dee31b

Browse files
Merge pull request #3493 from ipfs/feat/coreapi-refinements
Core API refinements and efficiency improvements
2 parents 21072a5 + ee45b8d commit 7dee31b

File tree

6 files changed

+149
-72
lines changed

6 files changed

+149
-72
lines changed

core/coreapi/coreapi.go

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,83 @@ import (
55

66
core "github.com/ipfs/go-ipfs/core"
77
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
8-
path "github.com/ipfs/go-ipfs/path"
8+
ipfspath "github.com/ipfs/go-ipfs/path"
99

10-
ipld "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
10+
cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
1111
)
1212

13-
func resolve(ctx context.Context, n *core.IpfsNode, p string) (ipld.Node, error) {
14-
pp, err := path.ParsePath(p)
13+
type CoreAPI struct {
14+
node *core.IpfsNode
15+
}
16+
17+
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
18+
api := &CoreAPI{n}
19+
return api
20+
}
21+
22+
func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
23+
return (*UnixfsAPI)(api)
24+
}
25+
26+
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {
27+
p, err := api.ResolvePath(ctx, p)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
node, err := api.node.DAG.Get(ctx, p.Cid())
1533
if err != nil {
1634
return nil, err
1735
}
36+
return node, nil
37+
}
1838

19-
dagnode, err := core.Resolve(ctx, n.Namesys, n.Resolver, pp)
39+
// TODO: store all of ipfspath.Resolver.ResolvePathComponents() in Path
40+
func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.Path, error) {
41+
if p.Resolved() {
42+
return p, nil
43+
}
44+
45+
p2 := ipfspath.FromString(p.String())
46+
node, err := core.Resolve(ctx, api.node.Namesys, api.node.Resolver, p2)
2047
if err == core.ErrNoNamesys {
2148
return nil, coreiface.ErrOffline
2249
} else if err != nil {
2350
return nil, err
2451
}
25-
return dagnode, nil
52+
53+
var root *cid.Cid
54+
if p2.IsJustAKey() {
55+
root = node.Cid()
56+
}
57+
58+
return ResolvedPath(p.String(), node.Cid(), root), nil
59+
}
60+
61+
// Implements coreiface.Path
62+
type path struct {
63+
path ipfspath.Path
64+
cid *cid.Cid
65+
root *cid.Cid
66+
}
67+
68+
func ParsePath(p string) (coreiface.Path, error) {
69+
pp, err := ipfspath.ParsePath(p)
70+
if err != nil {
71+
return nil, err
72+
}
73+
return &path{path: pp}, nil
74+
}
75+
76+
func ParseCid(c *cid.Cid) coreiface.Path {
77+
return &path{path: ipfspath.FromCid(c), cid: c, root: c}
2678
}
79+
80+
func ResolvedPath(p string, c *cid.Cid, r *cid.Cid) coreiface.Path {
81+
return &path{path: ipfspath.FromString(p), cid: c, root: r}
82+
}
83+
84+
func (p *path) String() string { return p.path.String() }
85+
func (p *path) Cid() *cid.Cid { return p.cid }
86+
func (p *path) Root() *cid.Cid { return p.root }
87+
func (p *path) Resolved() bool { return p.cid != nil }

core/coreapi/interface/interface.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,33 @@ import (
99
ipld "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
1010
)
1111

12-
// type CoreAPI interface {
13-
// ID() CoreID
14-
// Version() CoreVersion
15-
// }
12+
type Path interface {
13+
String() string
14+
Cid() *cid.Cid
15+
Root() *cid.Cid
16+
Resolved() bool
17+
}
1618

19+
// TODO: should we really copy these?
20+
// if we didn't, godoc would generate nice links straight to go-ipld-node
21+
type Node ipld.Node
1722
type Link ipld.Link
1823

1924
type Reader interface {
2025
io.ReadSeeker
2126
io.Closer
2227
}
2328

29+
type CoreAPI interface {
30+
Unixfs() UnixfsAPI
31+
ResolvePath(context.Context, Path) (Path, error)
32+
ResolveNode(context.Context, Path) (Node, error)
33+
}
34+
2435
type UnixfsAPI interface {
25-
Add(context.Context, io.Reader) (*cid.Cid, error)
26-
Cat(context.Context, string) (Reader, error)
27-
Ls(context.Context, string) ([]*Link, error)
36+
Add(context.Context, io.Reader) (Path, error)
37+
Cat(context.Context, Path) (Reader, error)
38+
Ls(context.Context, Path) ([]*Link, error)
2839
}
2940

3041
// type ObjectAPI interface {
@@ -50,5 +61,4 @@ type UnixfsAPI interface {
5061
// }
5162

5263
var ErrIsDir = errors.New("object is a directory")
53-
var ErrIsNonDag = errors.New("not a merkledag object")
5464
var ErrOffline = errors.New("can't resolve, ipfs node is offline")

core/coreapi/unixfs.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,29 @@ import (
44
"context"
55
"io"
66

7-
core "github.com/ipfs/go-ipfs/core"
87
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
98
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
109
uio "github.com/ipfs/go-ipfs/unixfs/io"
1110

1211
cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
1312
)
1413

15-
type UnixfsAPI struct {
16-
node *core.IpfsNode
17-
}
18-
19-
func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI {
20-
api := &UnixfsAPI{n}
21-
return api
22-
}
14+
type UnixfsAPI CoreAPI
2315

24-
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) {
16+
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.Path, error) {
2517
k, err := coreunix.AddWithContext(ctx, api.node, r)
2618
if err != nil {
2719
return nil, err
2820
}
29-
return cid.Decode(k)
21+
c, err := cid.Decode(k)
22+
if err != nil {
23+
return nil, err
24+
}
25+
return ParseCid(c), nil
3026
}
3127

32-
func (api *UnixfsAPI) Cat(ctx context.Context, p string) (coreiface.Reader, error) {
33-
dagnode, err := resolve(ctx, api.node, p)
28+
func (api *UnixfsAPI) Cat(ctx context.Context, p coreiface.Path) (coreiface.Reader, error) {
29+
dagnode, err := api.core().ResolveNode(ctx, p)
3430
if err != nil {
3531
return nil, err
3632
}
@@ -44,8 +40,8 @@ func (api *UnixfsAPI) Cat(ctx context.Context, p string) (coreiface.Reader, erro
4440
return r, nil
4541
}
4642

47-
func (api *UnixfsAPI) Ls(ctx context.Context, p string) ([]*coreiface.Link, error) {
48-
dagnode, err := resolve(ctx, api.node, p)
43+
func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*coreiface.Link, error) {
44+
dagnode, err := api.core().ResolveNode(ctx, p)
4945
if err != nil {
5046
return nil, err
5147
}
@@ -57,3 +53,7 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p string) ([]*coreiface.Link, erro
5753
}
5854
return links, nil
5955
}
56+
57+
func (api *UnixfsAPI) core() coreiface.CoreAPI {
58+
return (*CoreAPI)(api)
59+
}

core/coreapi/unixfs_test.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import (
1919
)
2020

2121
// `echo -n 'hello, world!' | ipfs add`
22-
var hello = "QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
22+
var hello = coreapi.ResolvedPath("/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk", nil, nil)
2323
var helloStr = "hello, world!"
2424

2525
// `ipfs object new unixfs-dir`
26-
var emptyUnixfsDir = "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
26+
var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", nil, nil)
2727

2828
// `echo -n | ipfs add`
29-
var emptyUnixfsFile = "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
29+
var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)
3030

3131
func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
3232
r := &repo.Mock{
@@ -41,7 +41,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
4141
if err != nil {
4242
return nil, nil, err
4343
}
44-
api := coreapi.NewUnixfsAPI(node)
44+
api := coreapi.NewCoreAPI(node).Unixfs()
4545
return node, api, nil
4646
}
4747

@@ -53,13 +53,13 @@ func TestAdd(t *testing.T) {
5353
}
5454

5555
str := strings.NewReader(helloStr)
56-
c, err := api.Add(ctx, str)
56+
p, err := api.Add(ctx, str)
5757
if err != nil {
5858
t.Error(err)
5959
}
6060

61-
if c.String() != hello {
62-
t.Fatalf("expected CID %s, got: %s", hello, c)
61+
if p.String() != hello.String() {
62+
t.Fatalf("expected path %s, got: %s", hello, p)
6363
}
6464

6565
r, err := api.Cat(ctx, hello)
@@ -85,13 +85,13 @@ func TestAddEmptyFile(t *testing.T) {
8585
}
8686

8787
str := strings.NewReader("")
88-
c, err := api.Add(ctx, str)
88+
p, err := api.Add(ctx, str)
8989
if err != nil {
9090
t.Error(err)
9191
}
9292

93-
if c.String() != emptyUnixfsFile {
94-
t.Fatalf("expected CID %s, got: %s", hello, c)
93+
if p.String() != emptyFile.String() {
94+
t.Fatalf("expected path %s, got: %s", hello, p)
9595
}
9696
}
9797

@@ -103,16 +103,17 @@ func TestCatBasic(t *testing.T) {
103103
}
104104

105105
hr := strings.NewReader(helloStr)
106-
k, err := coreunix.Add(node, hr)
106+
p, err := coreunix.Add(node, hr)
107107
if err != nil {
108108
t.Fatal(err)
109109
}
110+
p = "/ipfs/" + p
110111

111-
if k != hello {
112-
t.Fatalf("expected CID %s, got: %s", hello, k)
112+
if p != hello.String() {
113+
t.Fatalf("expected CID %s, got: %s", hello, p)
113114
}
114115

115-
r, err := api.Cat(ctx, k)
116+
r, err := api.Cat(ctx, hello)
116117
if err != nil {
117118
t.Fatal(err)
118119
}
@@ -139,7 +140,7 @@ func TestCatEmptyFile(t *testing.T) {
139140
t.Fatal(err)
140141
}
141142

142-
r, err := api.Cat(ctx, emptyUnixfsFile)
143+
r, err := api.Cat(ctx, emptyFile)
143144
if err != nil {
144145
t.Fatal(err)
145146
}
@@ -165,8 +166,13 @@ func TestCatDir(t *testing.T) {
165166
if err != nil {
166167
t.Error(err)
167168
}
169+
p := coreapi.ParseCid(c)
170+
171+
if p.String() != emptyDir.String() {
172+
t.Fatalf("expected path %s, got: %s", emptyDir, p)
173+
}
168174

169-
_, err = api.Cat(ctx, c.String())
175+
_, err = api.Cat(ctx, emptyDir)
170176
if err != coreiface.ErrIsDir {
171177
t.Fatalf("expected ErrIsDir, got: %s", err)
172178
}
@@ -184,7 +190,7 @@ func TestCatNonUnixfs(t *testing.T) {
184190
t.Error(err)
185191
}
186192

187-
_, err = api.Cat(ctx, c.String())
193+
_, err = api.Cat(ctx, coreapi.ParseCid(c))
188194
if !strings.Contains(err.Error(), "proto: required field") {
189195
t.Fatalf("expected protobuf error, got: %s", err)
190196
}
@@ -197,7 +203,7 @@ func TestCatOffline(t *testing.T) {
197203
t.Error(err)
198204
}
199205

200-
_, err = api.Cat(ctx, "/ipns/Qmfoobar")
206+
_, err = api.Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
201207
if err != coreiface.ErrOffline {
202208
t.Fatalf("expected ErrOffline, got: %", err)
203209
}
@@ -211,17 +217,17 @@ func TestLs(t *testing.T) {
211217
}
212218

213219
r := strings.NewReader("content-of-file")
214-
p, _, err := coreunix.AddWrapped(node, r, "name-of-file")
220+
k, _, err := coreunix.AddWrapped(node, r, "name-of-file")
215221
if err != nil {
216222
t.Error(err)
217223
}
218-
parts := strings.Split(p, "/")
224+
parts := strings.Split(k, "/")
219225
if len(parts) != 2 {
220-
t.Errorf("unexpected path:", p)
226+
t.Errorf("unexpected path:", k)
221227
}
222-
k := parts[0]
228+
p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil)
223229

224-
links, err := api.Ls(ctx, k)
230+
links, err := api.Ls(ctx, p)
225231
if err != nil {
226232
t.Error(err)
227233
}
@@ -236,7 +242,7 @@ func TestLs(t *testing.T) {
236242
t.Fatalf("expected name = name-of-file, got %s", links[0].Name)
237243
}
238244
if links[0].Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
239-
t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", links[0].Cid.String())
245+
t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", links[0].Cid)
240246
}
241247
}
242248

@@ -247,12 +253,12 @@ func TestLsEmptyDir(t *testing.T) {
247253
t.Error(err)
248254
}
249255

250-
c, err := node.DAG.Add(unixfs.EmptyDirNode())
256+
_, err = node.DAG.Add(unixfs.EmptyDirNode())
251257
if err != nil {
252258
t.Error(err)
253259
}
254260

255-
links, err := api.Ls(ctx, c.String())
261+
links, err := api.Ls(ctx, emptyDir)
256262
if err != nil {
257263
t.Error(err)
258264
}
@@ -275,7 +281,7 @@ func TestLsNonUnixfs(t *testing.T) {
275281
t.Error(err)
276282
}
277283

278-
links, err := api.Ls(ctx, c.String())
284+
links, err := api.Ls(ctx, coreapi.ParseCid(c))
279285
if err != nil {
280286
t.Error(err)
281287
}

core/corehttp/gateway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
2828
Headers: cfg.Gateway.HTTPHeaders,
2929
Writable: writable,
3030
PathPrefixes: cfg.Gateway.PathPrefixes,
31-
}, coreapi.NewUnixfsAPI(n))
31+
}, coreapi.NewCoreAPI(n))
3232

3333
for _, p := range paths {
3434
mux.Handle(p+"/", gateway)

0 commit comments

Comments
 (0)