Skip to content

Commit

Permalink
coreapi: separate path into two types
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Feb 8, 2018
1 parent c2dd400 commit 87cb6a2
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 148 deletions.
23 changes: 19 additions & 4 deletions core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type BlockStat struct {
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 @@ -72,7 +72,12 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
}

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.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 @@ -81,11 +86,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.ResolvePath(ctx, p)
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 @@ -116,7 +126,12 @@ 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.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 Down
4 changes: 1 addition & 3 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package coreapi
import (
core "github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)

type CoreAPI struct {
node *core.IpfsNode
*caopts.ApiOptions
}

// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
api := &CoreAPI{n, nil}
api := &CoreAPI{n}
return api
}

Expand Down
4 changes: 2 additions & 2 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DagAPI struct {
// 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 Down Expand Up @@ -68,7 +68,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 = api.ParsePath(ctx, gopath.Join(p.String(), p2))
out[n], err = api.ParsePath(gopath.Join(p.String(), p2))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestPath(t *testing.T) {
t.Error(err)
}

p, err := api.ParsePath(ctx, path.Join(res.Cid().String(), "lnk"))
p, err := api.ParsePath(path.Join(res.Cid().String(), "lnk"))
if err != nil {
t.Error(err)
}
Expand Down
49 changes: 29 additions & 20 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@ import (

// Path is a generic wrapper for paths used in the API. A path can be resolved
// to a CID using one of Resolve functions in the API.
// TODO: figure out/explain namespaces
type Path interface {
// String returns the path as a string.
String() string

// Namespace returns the first component of the path
Namespace() string
}

// ResolvedPath is a resolved Path
type ResolvedPath interface {
// Cid returns cid referred to by path
Cid() *cid.Cid

// Root returns cid of root path
Root() *cid.Cid
// Resolved returns whether path has been fully resolved
Resolved() bool

//TODO: Path remainder

Path
}

// TODO: should we really copy these?
Expand Down Expand Up @@ -61,7 +72,7 @@ type BlockStat interface {
// Pin holds information about pinned resource
type Pin interface {
// Path to the pinned object
Path() Path
Path() ResolvedPath

// Type of the pin
Type() string
Expand All @@ -79,7 +90,7 @@ type PinStatus interface {
// BadPinNode is a node that has been marked as bad by Pin.Verify
type BadPinNode interface {
// Path is the path of the node
Path() Path
Path() ResolvedPath

// Err is the reason why the node has been marked as bad
Err() error
Expand All @@ -101,33 +112,31 @@ type CoreAPI interface {

// Key returns an implementation of Key API.
Key() KeyAPI

// Pin returns an implementation of Pin API
Pin() PinAPI

// ObjectAPI returns an implementation of Object API
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
ResolveNode(context.Context, Path) (Node, error)

// ParsePath parses string path to a Path
ParsePath(context.Context, string, ...options.ParsePathOption) (Path, error)

// WithResolve is an option for ParsePath which when set to true tells
// ParsePath to also resolve the path
WithResolve(bool) options.ParsePathOption
ParsePath(string) (Path, error)

// ParseCid creates new path from the provided CID
ParseCid(*cid.Cid) Path
ParseCid(*cid.Cid) ResolvedPath
}

// UnixfsAPI is the basic interface to immutable files in IPFS
type UnixfsAPI interface {
// Add imports the data from the reader into merkledag file
Add(context.Context, io.Reader) (Path, error)
Add(context.Context, io.Reader) (ResolvedPath, error)

// Cat returns a reader for the file
Cat(context.Context, Path) (Reader, error)
Expand All @@ -139,7 +148,7 @@ type UnixfsAPI interface {
// 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)

// WithFormat is an option for Put which specifies the multicodec to use to
// serialize the object. Default is "v0"
Expand Down Expand Up @@ -173,7 +182,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)

// WithInputEnc is an option for Put which specifies the input encoding of the
// data. Default is "json", most formats/codecs support "raw"
Expand Down Expand Up @@ -290,13 +299,13 @@ type ObjectAPI interface {
WithType(string) options.ObjectNewOption

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

// WithInputEnc is an option for Put which specifies the input encoding of the
// data. Default is "json".
//
// Supported encodings:
// * "protobuf"
// * "protobuf"reselved version of Path
// * "json"
WithInputEnc(e string) options.ObjectPutOption

Expand All @@ -323,20 +332,20 @@ 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)

// WithCreate is an option for AddLink which specifies whether create required
// directories for the child
WithCreate(create bool) options.ObjectAddLinkOption

// 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)
}

// ObjectStat provides information about dag nodes
Expand Down
30 changes: 0 additions & 30 deletions core/coreapi/interface/options/path.go

This file was deleted.

12 changes: 6 additions & 6 deletions core/coreapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (
return n, nil
}

func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.Path, error) {
func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.ResolvedPath, error) {
options, err := caopts.ObjectPutOptions(opts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -183,7 +183,7 @@ func (api *ObjectAPI) Stat(ctx context.Context, path coreiface.Path) (*coreiface
return out, nil
}

func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.Path, error) {
func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.ResolvedPath, error) {
options, err := caopts.ObjectAddLinkOptions(opts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -224,7 +224,7 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
return api.ParseCid(nnode.Cid()), nil
}

func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Path, error) {
func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.ResolvedPath, error) {
baseNd, err := api.core().ResolveNode(ctx, base)
if err != nil {
return nil, err
Expand All @@ -250,15 +250,15 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri
return api.ParseCid(nnode.Cid()), nil
}

func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
return api.patchData(ctx, path, r, true)
}

func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
return api.patchData(ctx, path, r, false)
}

func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.Path, error) {
func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.ResolvedPath, error) {
nd, err := api.core().ResolveNode(ctx, path)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 87cb6a2

Please sign in to comment.