coreapi: DAG API proposal
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Dec 8, 2017 at 22:51 UTC
82924fbedf8189072bc1dfcdfbe843048c3be114
3 files changed
+89
-1
core/coreapi/coreapi.go
+4
@@ -25,6 +25,10 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
25
return (*UnixfsAPI)(api)
26
}
27
28
+func (api *CoreAPI) Dag() coreiface.DagAPI {
29
+ return (*DagAPI)(api)
30
+}
31
+
32
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {
33
p, err := api.ResolvePath(ctx, p)
34
if err != nil {
core/coreapi/dag.go
new
+77
@@ -0,0 +1,77 @@
1
+package coreapi
2
+
3
+import (
4
+ "context"
5
+ "fmt"
6
+ "io"
7
+
8
+ gopath "path"
9
+
10
+ coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
11
+ coredag "github.com/ipfs/go-ipfs/core/coredag"
12
+
13
+ cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
14
+ mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
15
+)
16
+
17
+type DagAPI CoreAPI
18
+
19
+func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) {
20
+ if format == nil {
21
+ format = &cid.Prefix{
22
+ Version: 1,
23
+ Codec: cid.DagCBOR,
24
+ MhType: mh.SHA2_256,
25
+ MhLength: mh.DefaultLengths[mh.SHA2_256],
26
+ }
27
+ }
28
+
29
+ codec, ok := cid.CodecToStr[format.Codec]
30
+ if !ok {
31
+ return nil, fmt.Errorf("invalid codec %d", format.Codec)
32
+ }
33
+
34
+ nds, err := coredag.ParseInputs(inputEnc, codec, src, format.MhType, format.MhLength)
35
+ if err != nil {
36
+ return nil, err
37
+ }
38
+ if len(nds) == 0 {
39
+ return nil, fmt.Errorf("no node returned from ParseInputs")
40
+ }
41
+
42
+ out := make([]coreiface.Node, len(nds))
43
+ for n, nd := range nds {
44
+ _, err := api.node.DAG.Add(nd)
45
+ if err != nil {
46
+ return nil, err
47
+ }
48
+ out[n] = nd
49
+ }
50
+
51
+ return out, nil
52
+}
53
+
54
+func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
55
+ return api.core().ResolveNode(ctx, path)
56
+}
57
+
58
+func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) {
59
+ n, err := api.Get(ctx, p)
60
+ if err != nil {
61
+ return nil, err
62
+ }
63
+ paths := n.Tree("", depth)
64
+ out := make([]coreiface.Path, len(paths))
65
+ for n, p2 := range paths {
66
+ out[n], err = ParsePath(gopath.Join(p.String(), p2))
67
+ if err != nil {
68
+ return nil, err
69
+ }
70
+ }
71
+
72
+ return out, nil
73
+}
74
+
75
+func (api *DagAPI) core() coreiface.CoreAPI {
76
+ return (*CoreAPI)(api)
77
+}
core/coreapi/interface/interface.go
+8
-1
@@ -34,13 +34,14 @@ type Reader interface {
34
type CoreAPI interface {
35
// Unixfs returns an implementation of Unixfs API
36
Unixfs() UnixfsAPI
37
+ Dag() DagAPI
38
39
// ResolvePath resolves the path using Unixfs resolver
40
ResolvePath(context.Context, Path) (Path, error)
41
42
// ResolveNode resolves the path (if not resolved already) using Unixfs
43
// resolver, gets and returns the resolved Node
43
- ResolveNode(context.Context, Path) (Node, error)
44
+ ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get?
45
}
46
47
// UnixfsAPI is the basic interface to immutable files in IPFS
@@ -55,6 +56,12 @@ type UnixfsAPI interface {
56
Ls(context.Context, Path) ([]*Link, error)
57
}
58
59
+type DagAPI interface {
60
+ Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error)
61
+ Get(ctx context.Context, path Path) (Node, error)
62
+ Tree(ctx context.Context, path Path, depth int) ([]Path, error)
63
+}
64
+
65
// type ObjectAPI interface {
66
// New() (cid.Cid, Object)
67
// Get(string) (Object, error)