Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve paths in CoreApi #4672

Merged
merged 15 commits into from
Jul 17, 2018
35 changes: 27 additions & 8 deletions core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
type BlockAPI CoreAPI

type BlockStat struct {
path coreiface.Path
path coreiface.ResolvedPath
size int
}

func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.Path, error) {
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.ResolvedPath, error) {
settings, err := caopts.BlockPutOptions(opts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -65,11 +65,16 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
return nil, err
}

return ParseCid(b.Cid()), nil
return coreiface.IpldPath(b.Cid()), nil
}

func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {
b, err := api.node.Blocks.GetBlock(ctx, p.Cid())
rp, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}

b, err := api.node.Blocks.GetBlock(ctx, rp.Cid())
if err != nil {
return nil, err
}
Expand All @@ -78,11 +83,16 @@ func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, erro
}

func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.BlockRmOption) error {
rp, err := api.core().ResolvePath(ctx, p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we're missing a func PathToCid(p coreiface.Path) (*cid.Cid, error) function.

if err != nil {
return err
}

settings, err := caopts.BlockRmOptions(opts...)
if err != nil {
return err
}
cids := []*cid.Cid{p.Cid()}
cids := []*cid.Cid{rp.Cid()}
o := util.RmBlocksOpts{Force: settings.Force}

out, err := util.RmBlocks(api.node.Blockstore, api.node.Pinning, cids, o)
Expand Down Expand Up @@ -111,13 +121,18 @@ func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.Bl
}

func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.BlockStat, error) {
b, err := api.node.Blocks.GetBlock(ctx, p.Cid())
rp, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}

b, err := api.node.Blocks.GetBlock(ctx, rp.Cid())
if err != nil {
return nil, err
}

return &BlockStat{
path: ParseCid(b.Cid()),
path: coreiface.IpldPath(b.Cid()),
size: len(b.RawData()),
}, nil
}
Expand All @@ -126,6 +141,10 @@ func (bs *BlockStat) Size() int {
return bs.size
}

func (bs *BlockStat) Path() coreiface.Path {
func (bs *BlockStat) Path() coreiface.ResolvedPath {
return bs.path
}

func (api *BlockAPI) core() coreiface.CoreAPI {
return (*CoreAPI)(api)
}
92 changes: 0 additions & 92 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,8 @@ Interfaces here aren't yet completely stable.
package coreapi

import (
"context"

core "github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
namesys "github.com/ipfs/go-ipfs/namesys"
ipfspath "github.com/ipfs/go-ipfs/path"
resolver "github.com/ipfs/go-ipfs/path/resolver"
uio "github.com/ipfs/go-ipfs/unixfs/io"

cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
)

type CoreAPI struct {
Expand Down Expand Up @@ -71,86 +62,3 @@ func (api *CoreAPI) Object() coreiface.ObjectAPI {
func (api *CoreAPI) Pin() coreiface.PinAPI {
return (*PinAPI)(api)
}

// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
// resolved Node.
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
return resolveNode(ctx, api.node.DAG, api.node.Namesys, p)
}

func resolveNode(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (ipld.Node, error) {
p, err := resolvePath(ctx, ng, nsys, p)
if err != nil {
return nil, err
}

node, err := ng.Get(ctx, p.Cid())
if err != nil {
return nil, err
}
return node, nil
}

// ResolvePath resolves the path `p` using Unixfs resolver, returns the
// resolved path.
// TODO: store all of ipfspath.Resolver.ResolvePathComponents() in Path
func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.Path, error) {
return resolvePath(ctx, api.node.DAG, api.node.Namesys, p)
}

func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (coreiface.Path, error) {
if p.Resolved() {
return p, nil
}

r := &resolver.Resolver{
DAG: ng,
ResolveOnce: uio.ResolveUnixfsOnce,
}

p2 := ipfspath.FromString(p.String())
node, err := core.Resolve(ctx, nsys, r, p2)
if err == core.ErrNoNamesys {
return nil, coreiface.ErrOffline
} else if err != nil {
return nil, err
}

var root *cid.Cid
if p2.IsJustAKey() {
root = node.Cid()
}

return ResolvedPath(p.String(), node.Cid(), root), nil
}

// Implements coreiface.Path
type path struct {
path ipfspath.Path
cid *cid.Cid
root *cid.Cid
}

// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
func ParsePath(p string) (coreiface.Path, error) {
pp, err := ipfspath.ParsePath(p)
if err != nil {
return nil, err
}
return &path{path: pp}, nil
}

// ParseCid parses the path from `c`, returns the parsed path.
func ParseCid(c *cid.Cid) coreiface.Path {
return &path{path: ipfspath.FromCid(c), cid: c, root: c}
}

// ResolvePath parses path from string `p`, returns parsed path.
func ResolvedPath(p string, c *cid.Cid, r *cid.Cid) coreiface.Path {
return &path{path: ipfspath.FromString(p), cid: c, root: r}
}

func (p *path) String() string { return p.path.String() }
func (p *path) Cid() *cid.Cid { return p.cid }
func (p *path) Root() *cid.Cid { return p.root }
func (p *path) Resolved() bool { return p.cid != nil }
6 changes: 3 additions & 3 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type DagAPI CoreAPI
// Put inserts data using specified format and input encoding. Unless used with
// `WithCodes` or `WithHash`, the defaults "dag-cbor" and "sha256" are used.
// Returns the path of the inserted data.
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) {
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.ResolvedPath, error) {
settings, err := caopts.DagPutOptions(opts...)
if err != nil {
return nil, err
Expand All @@ -44,7 +44,7 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
return nil, err
}

return ParseCid(nds[0].Cid()), nil
return coreiface.IpldPath(nds[0].Cid()), nil
}

// Get resolves `path` using Unixfs resolver, returns the resolved Node.
Expand All @@ -66,7 +66,7 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.Da
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))
out[n], err = coreiface.ParsePath(gopath.Join(p.String(), p2))
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions core/coreapi/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"strings"
"testing"

coreapi "github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"

opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)

var (
Expand Down Expand Up @@ -74,7 +73,7 @@ func TestPath(t *testing.T) {
t.Error(err)
}

p, err := coreapi.ParsePath(path.Join(res.Cid().String(), "lnk"))
p, err := coreiface.ParsePath(path.Join(res.Cid().String(), "lnk"))
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/coreapi/interface/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type BlockStat interface {
Size() int

// Path returns path to the block
Path() Path
Path() ResolvedPath
}

// BlockAPI specifies the interface to the block layer
type BlockAPI interface {
// Put imports raw block data, hashing it using specified settings.
Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
Put(context.Context, io.Reader, ...options.BlockPutOption) (ResolvedPath, error)

// Get attempts to resolve the path and return a reader for data in the block
Get(context.Context, Path) (io.Reader, error)
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/interface/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type CoreAPI interface {
Object() ObjectAPI

// ResolvePath resolves the path using Unixfs resolver
ResolvePath(context.Context, Path) (Path, error)
ResolvePath(context.Context, Path) (ResolvedPath, error)

// ResolveNode resolves the path (if not resolved already) using Unixfs
// resolver, gets and returns the resolved Node
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/interface/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DagAPI interface {
// Put inserts data using specified format and input encoding.
// 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)
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (ResolvedPath, error)

// Get attempts to resolve and get the node specified by the path
Get(ctx context.Context, path Path) (ipld.Node, error)
Expand Down
10 changes: 5 additions & 5 deletions core/coreapi/interface/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ObjectAPI interface {
New(context.Context, ...options.ObjectNewOption) (ipld.Node, error)

// Put imports the data into merkledag
Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)
Put(context.Context, io.Reader, ...options.ObjectPutOption) (ResolvedPath, error)

// Get returns the node for the path
Get(context.Context, Path) (ipld.Node, error)
Expand All @@ -55,14 +55,14 @@ type ObjectAPI interface {
// AddLink adds a link under the specified path. child path can point to a
// subdirectory within the patent which must be present (can be overridden
// with WithCreate option).
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Path, error)
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (ResolvedPath, error)

// RmLink removes a link from the node
RmLink(ctx context.Context, base Path, link string) (Path, error)
RmLink(ctx context.Context, base Path, link string) (ResolvedPath, error)

// AppendData appends data to the node
AppendData(context.Context, Path, io.Reader) (Path, error)
AppendData(context.Context, Path, io.Reader) (ResolvedPath, error)

// SetData sets the data contained in the node
SetData(context.Context, Path, io.Reader) (Path, error)
SetData(context.Context, Path, io.Reader) (ResolvedPath, error)
}
Loading