From 82924fbedf8189072bc1dfcdfbe843048c3be114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 8 Dec 2017 22:51:35 +0100 Subject: [PATCH 1/4] coreapi: DAG API proposal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/coreapi/coreapi.go | 4 ++ core/coreapi/dag.go | 77 +++++++++++++++++++++++++++++ core/coreapi/interface/interface.go | 9 +++- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 core/coreapi/dag.go diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 108c0e915fe..5c5471eb82a 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -25,6 +25,10 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI { return (*UnixfsAPI)(api) } +func (api *CoreAPI) Dag() coreiface.DagAPI { + return (*DagAPI)(api) +} + func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) { p, err := api.ResolvePath(ctx, p) if err != nil { diff --git a/core/coreapi/dag.go b/core/coreapi/dag.go new file mode 100644 index 00000000000..f32cf0b7390 --- /dev/null +++ b/core/coreapi/dag.go @@ -0,0 +1,77 @@ +package coreapi + +import ( + "context" + "fmt" + "io" + + gopath "path" + + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" + coredag "github.com/ipfs/go-ipfs/core/coredag" + + cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid" + mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash" +) + +type DagAPI CoreAPI + +func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) { + if format == nil { + format = &cid.Prefix{ + Version: 1, + Codec: cid.DagCBOR, + MhType: mh.SHA2_256, + MhLength: mh.DefaultLengths[mh.SHA2_256], + } + } + + codec, ok := cid.CodecToStr[format.Codec] + if !ok { + return nil, fmt.Errorf("invalid codec %d", format.Codec) + } + + nds, err := coredag.ParseInputs(inputEnc, codec, src, format.MhType, format.MhLength) + if err != nil { + return nil, err + } + if len(nds) == 0 { + return nil, fmt.Errorf("no node returned from ParseInputs") + } + + out := make([]coreiface.Node, len(nds)) + for n, nd := range nds { + _, err := api.node.DAG.Add(nd) + if err != nil { + return nil, err + } + out[n] = nd + } + + return out, nil +} + +func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { + return api.core().ResolveNode(ctx, path) +} + +func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) { + n, err := api.Get(ctx, p) + if err != nil { + return nil, err + } + paths := n.Tree("", depth) + out := make([]coreiface.Path, len(paths)) + for n, p2 := range paths { + out[n], err = ParsePath(gopath.Join(p.String(), p2)) + if err != nil { + return nil, err + } + } + + return out, nil +} + +func (api *DagAPI) core() coreiface.CoreAPI { + return (*CoreAPI)(api) +} diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index b753c418441..508588a04c5 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -34,13 +34,14 @@ type Reader interface { type CoreAPI interface { // Unixfs returns an implementation of Unixfs API Unixfs() UnixfsAPI + Dag() DagAPI // ResolvePath resolves the path using Unixfs resolver ResolvePath(context.Context, Path) (Path, error) // ResolveNode resolves the path (if not resolved already) using Unixfs // resolver, gets and returns the resolved Node - ResolveNode(context.Context, Path) (Node, error) + ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get? } // UnixfsAPI is the basic interface to immutable files in IPFS @@ -55,6 +56,12 @@ type UnixfsAPI interface { Ls(context.Context, Path) ([]*Link, error) } +type DagAPI interface { + Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) + Get(ctx context.Context, path Path) (Node, error) + Tree(ctx context.Context, path Path, depth int) ([]Path, error) +} + // type ObjectAPI interface { // New() (cid.Cid, Object) // Get(string) (Object, error) From b40a6f88ebd78a51fb68fc43797e20bf3a43b67f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 11 Dec 2017 17:22:21 +0100 Subject: [PATCH 2/4] coreapi: add tests for dag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/coreapi/coreapi.go | 2 +- core/coreapi/dag.go | 14 ++--- core/coreapi/dag_test.go | 93 +++++++++++++++++++++++++++++ core/coreapi/interface/interface.go | 12 +++- core/coreapi/unixfs_test.go | 27 +++++---- 5 files changed, 125 insertions(+), 23 deletions(-) create mode 100644 core/coreapi/dag_test.go diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 5c5471eb82a..7f4d3d26a0f 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -26,7 +26,7 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI { } func (api *CoreAPI) Dag() coreiface.DagAPI { - return (*DagAPI)(api) + return (*dagAPI)(api) } func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) { diff --git a/core/coreapi/dag.go b/core/coreapi/dag.go index f32cf0b7390..83d0c78ce6e 100644 --- a/core/coreapi/dag.go +++ b/core/coreapi/dag.go @@ -10,13 +10,13 @@ import ( coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coredag "github.com/ipfs/go-ipfs/core/coredag" - cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid" - mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash" + mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash" + cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid" ) -type DagAPI CoreAPI +type dagAPI CoreAPI -func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) { +func (api *dagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) { if format == nil { format = &cid.Prefix{ Version: 1, @@ -51,11 +51,11 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, form return out, nil } -func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { +func (api *dagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { return api.core().ResolveNode(ctx, path) } -func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) { +func (api *dagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) { n, err := api.Get(ctx, p) if err != nil { return nil, err @@ -72,6 +72,6 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]cor return out, nil } -func (api *DagAPI) core() coreiface.CoreAPI { +func (api *dagAPI) core() coreiface.CoreAPI { return (*CoreAPI)(api) } diff --git a/core/coreapi/dag_test.go b/core/coreapi/dag_test.go new file mode 100644 index 00000000000..ee2d8ee22c8 --- /dev/null +++ b/core/coreapi/dag_test.go @@ -0,0 +1,93 @@ +package coreapi_test + +import ( + "context" + "path" + "strings" + "testing" + + coreapi "github.com/ipfs/go-ipfs/core/coreapi" +) + +var ( + treeExpected = map[string]struct{}{ + "a": {}, + "b": {}, + "c": {}, + "c/d": {}, + "c/e": {}, + } +) + +func TestPut(t *testing.T) { + ctx := context.Background() + _, api, err := makeAPI(ctx) + if err != nil { + t.Error(err) + } + + res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), "json", nil) + if err != nil { + t.Error(err) + } + + if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" { + t.Errorf("got wrong cid: %s", res[0].Cid().String()) + } +} + +func TestPath(t *testing.T) { + ctx := context.Background() + _, api, err := makeAPI(ctx) + if err != nil { + t.Error(err) + } + + sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`), "json", nil) + if err != nil { + t.Error(err) + } + + res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`), "json", nil) + if err != nil { + t.Error(err) + } + + p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk")) + if err != nil { + t.Error(err) + } + + nd, err := api.Dag().Get(ctx, p) + if err != nil { + t.Error(err) + } + + if nd.Cid().String() != sub[0].Cid().String() { + t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub[0].Cid().String()) + } +} + +func TestTree(t *testing.T) { + ctx := context.Background() + _, api, err := makeAPI(ctx) + if err != nil { + t.Error(err) + } + + res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), "json", nil) + if err != nil { + t.Error(err) + } + + lst := res[0].Tree("", -1) + if len(lst) != len(treeExpected) { + t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected)) + } + + for _, ent := range lst { + if _, ok := treeExpected[ent]; !ok { + t.Errorf("unexpected tree entry %s", ent) + } + } +} diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index 508588a04c5..ecccc1c6438 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -41,7 +41,7 @@ type CoreAPI interface { // ResolveNode resolves the path (if not resolved already) using Unixfs // resolver, gets and returns the resolved Node - ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get? + ResolveNode(context.Context, Path) (Node, error) } // UnixfsAPI is the basic interface to immutable files in IPFS @@ -56,9 +56,17 @@ type UnixfsAPI interface { Ls(context.Context, Path) ([]*Link, error) } +// DagAPI specifies the interface to IPLD type DagAPI interface { - Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) + // Put inserts data using specified format and input encoding. + // If format is not specified (nil), default dag-cbor/sha256 is used + Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) //TODO: make format optional + + // Get attempts to resolve and get the node specified by the path Get(ctx context.Context, path Path) (Node, error) + + // Tree returns list of paths within a node specified by the path. + // To get all paths in a tree, set depth to -1 Tree(ctx context.Context, path Path, depth int) ([]Path, error) } diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index d816eebf85f..01beccc20b4 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -17,6 +17,7 @@ import ( config "github.com/ipfs/go-ipfs/repo/config" ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2" unixfs "github.com/ipfs/go-ipfs/unixfs" + cbor "gx/ipfs/QmeZv9VXw2SfVbX55LV6kGTWASKBc9ZxAVqGBeJcDGdoXy/go-ipld-cbor" ) @@ -30,7 +31,7 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs // `echo -n | ipfs add` var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil) -func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) { +func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) { r := &repo.Mock{ C: config.Config{ Identity: config.Identity{ @@ -43,7 +44,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) { if err != nil { return nil, nil, err } - api := coreapi.NewCoreAPI(node).Unixfs() + api := coreapi.NewCoreAPI(node) return node, api, nil } @@ -55,7 +56,7 @@ func TestAdd(t *testing.T) { } str := strings.NewReader(helloStr) - p, err := api.Add(ctx, str) + p, err := api.Unixfs().Add(ctx, str) if err != nil { t.Error(err) } @@ -64,7 +65,7 @@ func TestAdd(t *testing.T) { t.Fatalf("expected path %s, got: %s", hello, p) } - r, err := api.Cat(ctx, hello) + r, err := api.Unixfs().Cat(ctx, hello) if err != nil { t.Fatal(err) } @@ -87,7 +88,7 @@ func TestAddEmptyFile(t *testing.T) { } str := strings.NewReader("") - p, err := api.Add(ctx, str) + p, err := api.Unixfs().Add(ctx, str) if err != nil { t.Error(err) } @@ -115,7 +116,7 @@ func TestCatBasic(t *testing.T) { t.Fatalf("expected CID %s, got: %s", hello, p) } - r, err := api.Cat(ctx, hello) + r, err := api.Unixfs().Cat(ctx, hello) if err != nil { t.Fatal(err) } @@ -142,7 +143,7 @@ func TestCatEmptyFile(t *testing.T) { t.Fatal(err) } - r, err := api.Cat(ctx, emptyFile) + r, err := api.Unixfs().Cat(ctx, emptyFile) if err != nil { t.Fatal(err) } @@ -174,7 +175,7 @@ func TestCatDir(t *testing.T) { t.Fatalf("expected path %s, got: %s", emptyDir, p) } - _, err = api.Cat(ctx, emptyDir) + _, err = api.Unixfs().Cat(ctx, emptyDir) if err != coreiface.ErrIsDir { t.Fatalf("expected ErrIsDir, got: %s", err) } @@ -192,7 +193,7 @@ func TestCatNonUnixfs(t *testing.T) { t.Error(err) } - _, err = api.Cat(ctx, coreapi.ParseCid(c)) + _, err = api.Unixfs().Cat(ctx, coreapi.ParseCid(c)) if !strings.Contains(err.Error(), "proto: required field") { t.Fatalf("expected protobuf error, got: %s", err) } @@ -205,7 +206,7 @@ func TestCatOffline(t *testing.T) { t.Error(err) } - _, err = api.Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil)) + _, err = api.Unixfs().Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil)) if err != coreiface.ErrOffline { t.Fatalf("expected ErrOffline, got: %s", err) } @@ -229,7 +230,7 @@ func TestLs(t *testing.T) { } p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil) - links, err := api.Ls(ctx, p) + links, err := api.Unixfs().Ls(ctx, p) if err != nil { t.Error(err) } @@ -260,7 +261,7 @@ func TestLsEmptyDir(t *testing.T) { t.Error(err) } - links, err := api.Ls(ctx, emptyDir) + links, err := api.Unixfs().Ls(ctx, emptyDir) if err != nil { t.Error(err) } @@ -288,7 +289,7 @@ func TestLsNonUnixfs(t *testing.T) { t.Error(err) } - links, err := api.Ls(ctx, coreapi.ParseCid(c)) + links, err := api.Unixfs().Ls(ctx, coreapi.ParseCid(c)) if err != nil { t.Error(err) } From f153c01dfa6917acf46df6fb36780decb59cd89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sun, 17 Dec 2017 03:23:50 +0100 Subject: [PATCH 3/4] coreapi: functional options for DagAPI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/coreapi/coreapi.go | 2 +- core/coreapi/dag.go | 40 +++++++------ core/coreapi/dag_test.go | 27 +++++++-- core/coreapi/interface/interface.go | 25 +++++++- core/coreapi/interface/options/dag.go | 83 +++++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 26 deletions(-) create mode 100644 core/coreapi/interface/options/dag.go diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 7f4d3d26a0f..5c0326b51e8 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -26,7 +26,7 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI { } func (api *CoreAPI) Dag() coreiface.DagAPI { - return (*dagAPI)(api) + return &DagAPI{api, nil} } func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) { diff --git a/core/coreapi/dag.go b/core/coreapi/dag.go index 83d0c78ce6e..de796bd4dc9 100644 --- a/core/coreapi/dag.go +++ b/core/coreapi/dag.go @@ -8,30 +8,29 @@ import ( gopath "path" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" coredag "github.com/ipfs/go-ipfs/core/coredag" - mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash" cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid" ) -type dagAPI CoreAPI +type DagAPI struct { + *CoreAPI + *caopts.DagOptions +} -func (api *dagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) { - if format == nil { - format = &cid.Prefix{ - Version: 1, - Codec: cid.DagCBOR, - MhType: mh.SHA2_256, - MhLength: mh.DefaultLengths[mh.SHA2_256], - } +func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) ([]coreiface.Node, error) { + settings, err := caopts.DagPutOptions(opts...) + if err != nil { + return nil, err } - codec, ok := cid.CodecToStr[format.Codec] + codec, ok := cid.CodecToStr[settings.Codec] if !ok { - return nil, fmt.Errorf("invalid codec %d", format.Codec) + return nil, fmt.Errorf("invalid codec %d", settings.Codec) } - nds, err := coredag.ParseInputs(inputEnc, codec, src, format.MhType, format.MhLength) + nds, err := coredag.ParseInputs(settings.InputEnc, codec, src, settings.MhType, settings.MhLength) if err != nil { return nil, err } @@ -51,16 +50,21 @@ func (api *dagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, form return out, nil } -func (api *dagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { +func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { return api.core().ResolveNode(ctx, path) } -func (api *dagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) { +func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.DagTreeOption) ([]coreiface.Path, error) { + settings, err := caopts.DagTreeOptions(opts...) + if err != nil { + return nil, err + } + n, err := api.Get(ctx, p) if err != nil { return nil, err } - paths := n.Tree("", depth) + paths := n.Tree("", settings.Depth) out := make([]coreiface.Path, len(paths)) for n, p2 := range paths { out[n], err = ParsePath(gopath.Join(p.String(), p2)) @@ -72,6 +76,6 @@ func (api *dagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]cor return out, nil } -func (api *dagAPI) core() coreiface.CoreAPI { - return (*CoreAPI)(api) +func (api *DagAPI) core() coreiface.CoreAPI { + return api.CoreAPI } diff --git a/core/coreapi/dag_test.go b/core/coreapi/dag_test.go index ee2d8ee22c8..b528afbaa15 100644 --- a/core/coreapi/dag_test.go +++ b/core/coreapi/dag_test.go @@ -7,6 +7,8 @@ import ( "testing" coreapi "github.com/ipfs/go-ipfs/core/coreapi" + + mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash" ) var ( @@ -26,7 +28,7 @@ func TestPut(t *testing.T) { t.Error(err) } - res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), "json", nil) + res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`)) if err != nil { t.Error(err) } @@ -36,6 +38,23 @@ func TestPut(t *testing.T) { } } +func TestPutWithHash(t *testing.T) { + ctx := context.Background() + _, api, err := makeAPI(ctx) + if err != nil { + t.Error(err) + } + + res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), api.Dag().WithHash(mh.ID, -1)) + if err != nil { + t.Error(err) + } + + if res[0].Cid().String() != "z5hRLNd2sv4z1c" { + t.Errorf("got wrong cid: %s", res[0].Cid().String()) + } +} + func TestPath(t *testing.T) { ctx := context.Background() _, api, err := makeAPI(ctx) @@ -43,12 +62,12 @@ func TestPath(t *testing.T) { t.Error(err) } - sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`), "json", nil) + sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`)) if err != nil { t.Error(err) } - res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`), "json", nil) + res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`)) if err != nil { t.Error(err) } @@ -75,7 +94,7 @@ func TestTree(t *testing.T) { t.Error(err) } - res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), "json", nil) + res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`)) if err != nil { t.Error(err) } diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index ecccc1c6438..f43700e9cb7 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -7,6 +7,8 @@ import ( "errors" "io" + options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" + ipld "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format" cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid" ) @@ -60,14 +62,31 @@ type UnixfsAPI interface { type DagAPI interface { // Put inserts data using specified format and input encoding. // If format is not specified (nil), default dag-cbor/sha256 is used - Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) //TODO: make format optional + Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) ([]Node, error) + + // WithInputEnc is an option for Put which specifies the input encoding of the + // data. Default is "json", most formats/codecs support "raw" + WithInputEnc(enc string) options.DagPutOption + + // WithCodec is an option for Put which specifies the multicodec to use to + // serialize the object. Default is cid.DagCBOR (0x71) + WithCodec(codec uint64) options.DagPutOption + + // WithHash is an option for Put which specifies the multihash settings to use + // when hashing the object. Default is based on the codec used + // (mh.SHA2_256 (0x12) for DagCBOR). If mhLen is set to -1, default length for + // the hash will be used + WithHash(mhType uint64, mhLen int) options.DagPutOption // Get attempts to resolve and get the node specified by the path Get(ctx context.Context, path Path) (Node, error) // Tree returns list of paths within a node specified by the path. - // To get all paths in a tree, set depth to -1 - Tree(ctx context.Context, path Path, depth int) ([]Path, error) + Tree(ctx context.Context, path Path, opts ...options.DagTreeOption) ([]Path, error) + + // WithDepth is an option for Tree which specifies maximum depth of the + // returned tree. Default is -1 (no depth limit) + WithDepth(depth int) options.DagTreeOption } // type ObjectAPI interface { diff --git a/core/coreapi/interface/options/dag.go b/core/coreapi/interface/options/dag.go new file mode 100644 index 00000000000..7850c4bc3a8 --- /dev/null +++ b/core/coreapi/interface/options/dag.go @@ -0,0 +1,83 @@ +package options + +import ( + "math" + + cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid" +) + +type DagPutSettings struct { + InputEnc string + Codec uint64 + MhType uint64 + MhLength int +} + +type DagTreeSettings struct { + Depth int +} + +type DagPutOption func(*DagPutSettings) error +type DagTreeOption func(*DagTreeSettings) error + +func DagPutOptions(opts ...DagPutOption) (*DagPutSettings, error) { + options := &DagPutSettings{ + InputEnc: "json", + Codec: cid.DagCBOR, + MhType: math.MaxUint64, + MhLength: -1, + } + + for _, opt := range opts { + err := opt(options) + if err != nil { + return nil, err + } + } + return options, nil +} + +func DagTreeOptions(opts ...DagTreeOption) (*DagTreeSettings, error) { + options := &DagTreeSettings{ + Depth: -1, + } + + for _, opt := range opts { + err := opt(options) + if err != nil { + return nil, err + } + } + return options, nil +} + +type DagOptions struct{} + +func (api *DagOptions) WithInputEnc(enc string) DagPutOption { + return func(settings *DagPutSettings) error { + settings.InputEnc = enc + return nil + } +} + +func (api *DagOptions) WithCodec(codec uint64) DagPutOption { + return func(settings *DagPutSettings) error { + settings.Codec = codec + return nil + } +} + +func (api *DagOptions) WithHash(mhType uint64, mhLen int) DagPutOption { + return func(settings *DagPutSettings) error { + settings.MhType = mhType + settings.MhLength = mhLen + return nil + } +} + +func (api *DagOptions) WithDepth(depth int) DagTreeOption { + return func(settings *DagTreeSettings) error { + settings.Depth = depth + return nil + } +} From db318333abaca6490bfa618966816b1fc6d756a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 21 Dec 2017 01:56:41 +0100 Subject: [PATCH 4/4] coreapi: dag review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- core/coreapi/dag.go | 14 +++++--------- core/coreapi/dag_test.go | 25 +++++++++++++++---------- core/coreapi/interface/interface.go | 5 +++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/core/coreapi/dag.go b/core/coreapi/dag.go index de796bd4dc9..c5d59bc1395 100644 --- a/core/coreapi/dag.go +++ b/core/coreapi/dag.go @@ -19,7 +19,7 @@ type DagAPI struct { *caopts.DagOptions } -func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) ([]coreiface.Node, error) { +func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) { settings, err := caopts.DagPutOptions(opts...) if err != nil { return nil, err @@ -38,16 +38,12 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut return nil, fmt.Errorf("no node returned from ParseInputs") } - out := make([]coreiface.Node, len(nds)) - for n, nd := range nds { - _, err := api.node.DAG.Add(nd) - if err != nil { - return nil, err - } - out[n] = nd + _, err = api.node.DAG.Add(nds[0]) + if err != nil { + return nil, err } - return out, nil + return ParseCid(nds[0].Cid()), nil } func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { diff --git a/core/coreapi/dag_test.go b/core/coreapi/dag_test.go index b528afbaa15..745337e5672 100644 --- a/core/coreapi/dag_test.go +++ b/core/coreapi/dag_test.go @@ -33,8 +33,8 @@ func TestPut(t *testing.T) { t.Error(err) } - if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" { - t.Errorf("got wrong cid: %s", res[0].Cid().String()) + if res.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" { + t.Errorf("got wrong cid: %s", res.Cid().String()) } } @@ -50,8 +50,8 @@ func TestPutWithHash(t *testing.T) { t.Error(err) } - if res[0].Cid().String() != "z5hRLNd2sv4z1c" { - t.Errorf("got wrong cid: %s", res[0].Cid().String()) + if res.Cid().String() != "z5hRLNd2sv4z1c" { + t.Errorf("got wrong cid: %s", res.Cid().String()) } } @@ -67,12 +67,12 @@ func TestPath(t *testing.T) { t.Error(err) } - res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`)) + res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub.Cid().String()+`"}}`)) if err != nil { t.Error(err) } - p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk")) + p, err := coreapi.ParsePath(path.Join(res.Cid().String(), "lnk")) if err != nil { t.Error(err) } @@ -82,8 +82,8 @@ func TestPath(t *testing.T) { t.Error(err) } - if nd.Cid().String() != sub[0].Cid().String() { - t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub[0].Cid().String()) + if nd.Cid().String() != sub.Cid().String() { + t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub.Cid().String()) } } @@ -94,12 +94,17 @@ func TestTree(t *testing.T) { t.Error(err) } - res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`)) + c, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`)) if err != nil { t.Error(err) } - lst := res[0].Tree("", -1) + res, err := api.Dag().Get(ctx, c) + if err != nil { + t.Error(err) + } + + lst := res.Tree("", -1) if len(lst) != len(treeExpected) { t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected)) } diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index f43700e9cb7..e51888e6012 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -61,8 +61,9 @@ type UnixfsAPI interface { // DagAPI specifies the interface to IPLD type DagAPI interface { // Put inserts data using specified format and input encoding. - // If format is not specified (nil), default dag-cbor/sha256 is used - Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) ([]Node, error) + // Unless used with WithCodec or WithHash, the defaults "dag-cbor" and + // "sha256" are used. + Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error) // WithInputEnc is an option for Put which specifies the input encoding of the // data. Default is "json", most formats/codecs support "raw"