Skip to content

Commit

Permalink
extract node interface
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 14, 2016
1 parent 01aee44 commit 48f7e14
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 199 deletions.
14 changes: 8 additions & 6 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
path "github.com/ipfs/go-ipfs/path"
unixfs "github.com/ipfs/go-ipfs/unixfs"
unixfspb "github.com/ipfs/go-ipfs/unixfs/pb"

node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
)

type LsLink struct {
Expand Down Expand Up @@ -50,7 +52,7 @@ The JSON output contains type information.
cmds.BoolOption("resolve-type", "Resolve linked objects to find out their types.").Default(true),
},
Run: func(req cmds.Request, res cmds.Response) {
node, err := req.InvocContext().GetNode()
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand All @@ -70,9 +72,9 @@ The JSON output contains type information.

paths := req.Arguments()

var dagnodes []merkledag.Node
var dagnodes []node.Node
for _, fpath := range paths {
dagnode, err := core.Resolve(req.Context(), node, path.Path(fpath))
dagnode, err := core.Resolve(req.Context(), nd, path.Path(fpath))
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand All @@ -90,8 +92,8 @@ The JSON output contains type information.
var linkNode *merkledag.ProtoNode
t := unixfspb.Data_DataType(-1)
linkKey := link.Cid
if ok, err := node.Blockstore.Has(linkKey); ok && err == nil {
b, err := node.Blockstore.Get(linkKey)
if ok, err := nd.Blockstore.Has(linkKey); ok && err == nil {
b, err := nd.Blockstore.Get(linkKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand All @@ -104,7 +106,7 @@ The JSON output contains type information.
}

if linkNode == nil && resolve {
nd, err := link.GetNode(req.Context(), node.DAG)
nd, err := link.GetNode(req.Context(), nd.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
19 changes: 10 additions & 9 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
ft "github.com/ipfs/go-ipfs/unixfs"

cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
)

// ErrObjectTooLarge is returned when too much data was read from stdin. current limit 2m
Expand Down Expand Up @@ -290,10 +291,10 @@ var ObjectStatCmd = &cmds.Command{

res.SetOutput(ns)
},
Type: dag.NodeStat{},
Type: node.NodeStat{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
ns := res.Output().(*dag.NodeStat)
ns := res.Output().(*node.NodeStat)

buf := new(bytes.Buffer)
w := func(s string, n int) {
Expand Down Expand Up @@ -556,7 +557,7 @@ func getObjectEnc(o interface{}) objectEncoding {
return objectEncoding(v)
}

func getOutput(dagnode dag.Node) (*Object, error) {
func getOutput(dagnode node.Node) (*Object, error) {
c := dagnode.Cid()
output := &Object{
Hash: c.String(),
Expand All @@ -575,25 +576,25 @@ func getOutput(dagnode dag.Node) (*Object, error) {
}

// converts the Node object into a real dag.ProtoNode
func deserializeNode(node *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
dagnode := new(dag.ProtoNode)
switch dataFieldEncoding {
case "text":
dagnode.SetData([]byte(node.Data))
dagnode.SetData([]byte(nd.Data))
case "base64":
data, _ := base64.StdEncoding.DecodeString(node.Data)
data, _ := base64.StdEncoding.DecodeString(nd.Data)
dagnode.SetData(data)
default:
return nil, fmt.Errorf("Unkown data field encoding")
}

dagnode.SetLinks(make([]*dag.Link, len(node.Links)))
for i, link := range node.Links {
dagnode.SetLinks(make([]*node.Link, len(nd.Links)))
for i, link := range nd.Links {
c, err := cid.Decode(link.Hash)
if err != nil {
return nil, err
}
dagnode.Links()[i] = &dag.Link{
dagnode.Links()[i] = &node.Link{
Name: link.Name,
Size: link.Size,
Cid: c,
Expand Down
11 changes: 6 additions & 5 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
path "github.com/ipfs/go-ipfs/path"

cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
)

Expand Down Expand Up @@ -195,8 +196,8 @@ var refsMarshallerMap = cmds.MarshalerMap{
},
}

func objectsForPaths(ctx context.Context, n *core.IpfsNode, paths []string) ([]dag.Node, error) {
objects := make([]dag.Node, len(paths))
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))
if err != nil {
Expand Down Expand Up @@ -225,14 +226,14 @@ type RefWriter struct {
}

// WriteRefs writes refs of the given object to the underlying writer.
func (rw *RefWriter) WriteRefs(n dag.Node) (int, error) {
func (rw *RefWriter) WriteRefs(n node.Node) (int, error) {
if rw.Recursive {
return rw.writeRefsRecursive(n)
}
return rw.writeRefsSingle(n)
}

func (rw *RefWriter) writeRefsRecursive(n dag.Node) (int, error) {
func (rw *RefWriter) writeRefsRecursive(n node.Node) (int, error) {
nc := n.Cid()

var count int
Expand Down Expand Up @@ -260,7 +261,7 @@ func (rw *RefWriter) writeRefsRecursive(n dag.Node) (int, error) {
return count, nil
}

func (rw *RefWriter) writeRefsSingle(n dag.Node) (int, error) {
func (rw *RefWriter) writeRefsSingle(n node.Node) (int, error) {
c := n.Cid()

if rw.skip(c) {
Expand Down
6 changes: 3 additions & 3 deletions core/corerepo/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ objects.
package corerepo

import (
"context"
"fmt"

"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"

context "context"
cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
)

func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
dagnodes := make([]merkledag.Node, 0)
dagnodes := make([]node.Node, 0)
for _, fpath := range paths {
dagnode, err := core.Resolve(ctx, n, path.Path(fpath))
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions core/pathresolver.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package core

import (
"context"
"errors"
"strings"

context "context"

merkledag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"

cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
)

// ErrNoNamesys is an explicit error for when an IPFS node doesn't
Expand All @@ -19,7 +19,7 @@ var ErrNoNamesys = errors.New(
// Resolve resolves the given path by parsing out protocol-specific
// entries (e.g. /ipns/<node-key>) and then going through the /ipfs/
// entries and returning the final merkledag node.
func Resolve(ctx context.Context, n *IpfsNode, p path.Path) (merkledag.Node, error) {
func Resolve(ctx context.Context, n *IpfsNode, p path.Path) (node.Node, error) {
if strings.HasPrefix(p.String(), "/ipns/") {
// resolve ipns paths

Expand Down
5 changes: 3 additions & 2 deletions merkledag/coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
pb "github.com/ipfs/go-ipfs/merkledag/pb"

cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
)

Expand All @@ -22,9 +23,9 @@ func (n *ProtoNode) unmarshal(encoded []byte) error {
}

pbnl := pbn.GetLinks()
n.links = make([]*Link, len(pbnl))
n.links = make([]*node.Link, len(pbnl))
for i, l := range pbnl {
n.links[i] = &Link{Name: l.GetName(), Size: l.GetTsize()}
n.links[i] = &node.Link{Name: l.GetName(), Size: l.GetTsize()}
c, err := cid.Cast(l.GetHash())
if err != nil {
return fmt.Errorf("Link hash #%d is not valid multihash. %v", i, err)
Expand Down
Loading

0 comments on commit 48f7e14

Please sign in to comment.