Skip to content

Commit

Permalink
make path resolver no longer require whole node for construction
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Oct 24, 2016
1 parent 6f3ae5d commit 26f42c4
Show file tree
Hide file tree
Showing 23 changed files with 162 additions and 52 deletions.
2 changes: 1 addition & 1 deletion core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
// this is kinda sketchy and could cause data loss
n.Pinning = pin.NewPinner(n.Repo.Datastore(), n.DAG, internalDag)
}
n.Resolver = &path.Resolver{DAG: n.DAG}
n.Resolver = path.NewBasicResolver(n.DAG)

err = n.loadFilesRoot()
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions core/commands/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
mfs "github.com/ipfs/go-ipfs/mfs"
path "github.com/ipfs/go-ipfs/path"
ft "github.com/ipfs/go-ipfs/unixfs"
uio "github.com/ipfs/go-ipfs/unixfs/io"

logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
Expand Down Expand Up @@ -259,11 +260,15 @@ func getNodeFromPath(ctx context.Context, node *core.IpfsNode, p string) (node.N
return nil, err
}

nd, err := core.Resolve(ctx, node, np)
resolver := &path.Resolver{
DAG: node.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

nd, err := core.Resolve(ctx, node.Namesys, resolver, np)
if err != nil {
return nil, err
}

pbnd, ok := nd.(*dag.ProtoNode)
if !ok {
return nil, dag.ErrNotProtobuf
Expand Down
2 changes: 1 addition & 1 deletion core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
}
p := path.Path(req.Arguments()[0])
ctx := req.Context()
dn, err := core.Resolve(ctx, node, p)
dn, err := core.Resolve(ctx, node.Namesys, node.Resolver, p)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
14 changes: 13 additions & 1 deletion core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
merkledag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
unixfs "github.com/ipfs/go-ipfs/unixfs"
uio "github.com/ipfs/go-ipfs/unixfs/io"
unixfspb "github.com/ipfs/go-ipfs/unixfs/pb"

node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
Expand Down Expand Up @@ -74,7 +75,18 @@ The JSON output contains type information.

var dagnodes []node.Node
for _, fpath := range paths {
dagnode, err := core.Resolve(req.Context(), nd, path.Path(fpath))
p, err := path.ParsePath(fpath)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

r := &path.Resolver{
DAG: nd.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

dagnode, err := core.Resolve(req.Context(), nd.Namesys, r, p)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
4 changes: 2 additions & 2 deletions core/commands/object/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ Example:

ctx := req.Context()

obj_a, err := core.Resolve(ctx, node, pa)
obj_a, err := core.Resolve(ctx, node.Namesys, node.Resolver, pa)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

obj_b, err := core.Resolve(ctx, node, pb)
obj_b, err := core.Resolve(ctx, node.Namesys, node.Resolver, pb)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
8 changes: 4 additions & 4 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ is the raw data of the object.
return
}

node, err := core.Resolve(req.Context(), n, fpath)
node, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -140,7 +140,7 @@ multihash.
}

fpath := path.Path(req.Arguments()[0])
node, err := core.Resolve(req.Context(), n, fpath)
node, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -204,7 +204,7 @@ This command outputs data in the following encodings:

fpath := path.Path(req.Arguments()[0])

object, err := core.Resolve(req.Context(), n, fpath)
object, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -277,7 +277,7 @@ var ObjectStatCmd = &cmds.Command{

fpath := path.Path(req.Arguments()[0])

object, err := core.Resolve(req.Context(), n, fpath)
object, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
10 changes: 5 additions & 5 deletions core/commands/object/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ the limit will not be respected by the network.
return
}

rootnd, err := core.Resolve(req.Context(), nd, root)
rootnd, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, root)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -141,7 +141,7 @@ Example:
return
}

root, err := core.Resolve(req.Context(), nd, rp)
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, rp)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -205,7 +205,7 @@ Removes a link by the given name from root.
return
}

root, err := core.Resolve(req.Context(), nd, rootp)
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, rootp)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -280,7 +280,7 @@ to a file containing 'bar', and returns the hash of the new object.
return
}

root, err := core.Resolve(req.Context(), nd, rootp)
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, rootp)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -312,7 +312,7 @@ to a file containing 'bar', and returns the hash of the new object.

e := dagutils.NewDagEditor(rtpb, nd.DAG)

childnd, err := core.Resolve(req.Context(), nd, childp)
childnd, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, childp)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
2 changes: 1 addition & 1 deletion core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func pinLsKeys(args []string, typeStr string, ctx context.Context, n *core.IpfsN
return nil, err
}

dagNode, err := core.Resolve(ctx, n, pth)
dagNode, err := core.Resolve(ctx, n.Namesys, n.Resolver, pth)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func publish(ctx context.Context, n *core.IpfsNode, k crypto.PrivKey, ref path.P

if opts.verifyExists {
// verify the path exists
_, err := core.Resolve(ctx, n, ref)
_, err := core.Resolve(ctx, n.Namesys, n.Resolver, ref)
if err != nil {
return nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,13 @@ var refsMarshallerMap = cmds.MarshalerMap{

func objectsForPaths(ctx context.Context, n *core.IpfsNode, paths []string) ([]node.Node, error) {
objects := make([]node.Node, len(paths))
for i, p := range paths {
o, err := core.Resolve(ctx, n, path.Path(p))
for i, sp := range paths {
p, err := path.ParsePath(sp)
if err != nil {
return nil, err
}

o, err := core.Resolve(ctx, n.Namesys, n.Resolver, p)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Resolve the value of an IPFS DAG path:
return
}

node, err := core.Resolve(req.Context(), n, p)
node, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, p)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
2 changes: 1 addition & 1 deletion core/commands/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var tarCatCmd = &cmds.Command{
return
}

root, err := core.Resolve(req.Context(), nd, p)
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, p)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
9 changes: 8 additions & 1 deletion core/commands/unixfs/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
merkledag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
unixfs "github.com/ipfs/go-ipfs/unixfs"
uio "github.com/ipfs/go-ipfs/unixfs/io"
unixfspb "github.com/ipfs/go-ipfs/unixfs/pb"
)

Expand Down Expand Up @@ -87,7 +88,13 @@ possible, please use 'ipfs ls' instead.

for _, fpath := range paths {
ctx := req.Context()
merkleNode, err := core.Resolve(ctx, node, path.Path(fpath))

resolver := &path.Resolver{
DAG: node.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

merkleNode, err := core.Resolve(ctx, node.Namesys, resolver, path.Path(fpath))
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
4 changes: 2 additions & 2 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import (
pin "github.com/ipfs/go-ipfs/pin"
repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config"
uio "github.com/ipfs/go-ipfs/unixfs/io"
ft "github.com/ipfs/go-ipfs/unixfs"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
)

Expand Down Expand Up @@ -504,7 +504,7 @@ func (n *IpfsNode) loadFilesRoot() error {

switch {
case err == ds.ErrNotFound || val == nil:
nd = uio.NewEmptyDirectory()
nd = ft.EmptyDirNode()
_, err := n.DAG.Add(nd)
if err != nil {
return fmt.Errorf("failure writing to dagstore: %s", err)
Expand Down
23 changes: 18 additions & 5 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
dag "github.com/ipfs/go-ipfs/merkledag"
dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
path "github.com/ipfs/go-ipfs/path"
ft "github.com/ipfs/go-ipfs/unixfs"
uio "github.com/ipfs/go-ipfs/unixfs/io"

routing "gx/ipfs/QmNUgVQTYnXQVrGT2rajZYsuKV8GYdiL91cdZSQDKNPNgE/go-libp2p-routing"
Expand Down Expand Up @@ -153,7 +154,13 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
ipnsHostname = true
}

nd, err := core.Resolve(ctx, i.node, path.Path(urlPath))
p, err := path.ParsePath(urlPath)
if err != nil {
webError(w, "Invalid Path Error", err, http.StatusBadRequest)
return
}

nd, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, p)
// If node is in offline mode the error code and message should be different
if err == core.ErrNoNamesys && !i.node.OnlineMode() {
w.WriteHeader(http.StatusServiceUnavailable)
Expand Down Expand Up @@ -240,8 +247,14 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
return
}

p, err := path.ParsePath(urlPath + "/index.html")
if err != nil {
internalWebError(w, err)
return
}

// return index page instead.
nd, err := core.Resolve(ctx, i.node, path.Path(urlPath+"/index.html"))
nd, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, p)
if err != nil {
internalWebError(w, err)
return
Expand Down Expand Up @@ -356,7 +369,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {

var newnode node.Node
if rsegs[len(rsegs)-1] == "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" {
newnode = uio.NewEmptyDirectory()
newnode = ft.EmptyDirNode()
} else {
putNode, err := i.newDagFromReader(r.Body)
if err != nil {
Expand All @@ -372,7 +385,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
}

var newcid *cid.Cid
rnode, err := core.Resolve(ctx, i.node, rootPath)
rnode, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, rootPath)
switch ev := err.(type) {
case path.ErrNoLink:
// ev.Node < node where resolve failed
Expand All @@ -397,7 +410,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
}

e := dagutils.NewDagEditor(pbnd, i.node.DAG)
err = e.InsertNodeAtPath(ctx, newPath, newnode, uio.NewEmptyDirectory)
err = e.InsertNodeAtPath(ctx, newPath, newnode, ft.EmptyDirNode)
if err != nil {
webError(w, "putHandler: InsertNodeAtPath failed", err, http.StatusInternalServerError)
return
Expand Down
7 changes: 6 additions & 1 deletion core/corerepo/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import (
func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
dagnodes := make([]node.Node, 0)
for _, fpath := range paths {
dagnode, err := core.Resolve(ctx, n, path.Path(fpath))
p, err := path.ParsePath(fpath)
if err != nil {
return nil, err
}

dagnode, err := core.Resolve(ctx, n.Namesys, n.Resolver, p)
if err != nil {
return nil, fmt.Errorf("pin: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/coreunix/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func Cat(ctx context.Context, n *core.IpfsNode, pstr string) (*uio.DagReader, error) {
dagNode, err := core.Resolve(ctx, n, path.Path(pstr))
dagNode, err := core.Resolve(ctx, n.Namesys, n.Resolver, path.Path(pstr))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 26f42c4

Please sign in to comment.