Skip to content

Commit

Permalink
coreapi: DAG API proposal
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Dec 8, 2017
1 parent a4f9333 commit c780466
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 {
Expand Down
67 changes: 67 additions & 0 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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"
)

type DagAPI CoreAPI

func (api *DagAPI) Add(ctx context.Context, src io.Reader, inputEnc string, format cid.Prefix) ([]coreiface.Node, error) {
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)
}
10 changes: 9 additions & 1 deletion core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type Reader interface {

type CoreAPI interface {
Unixfs() UnixfsAPI
Dag() DagAPI

ResolvePath(context.Context, Path) (Path, error)
ResolveNode(context.Context, Path) (Node, error)
ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get?
}

type UnixfsAPI interface {
Expand All @@ -38,6 +40,12 @@ type UnixfsAPI interface {
Ls(context.Context, Path) ([]*Link, error)
}

type DagAPI interface {
Add(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)
Expand Down

0 comments on commit c780466

Please sign in to comment.