diff --git a/core/commands/ls.go b/core/commands/ls.go index f22518123cf..e2546699fa7 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -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 { @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/core/commands/object/object.go b/core/commands/object/object.go index 707a92b9e7b..6f2f2d29122 100644 --- a/core/commands/object/object.go +++ b/core/commands/object/object.go @@ -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 @@ -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) { @@ -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(), @@ -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, diff --git a/core/commands/refs.go b/core/commands/refs.go index 27aeb513e8d..3cd23291596 100644 --- a/core/commands/refs.go +++ b/core/commands/refs.go @@ -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" ) @@ -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 { @@ -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 @@ -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) { diff --git a/core/corerepo/pinning.go b/core/corerepo/pinning.go index 6723f94d747..41ade74e94a 100644 --- a/core/corerepo/pinning.go +++ b/core/corerepo/pinning.go @@ -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 { diff --git a/core/pathresolver.go b/core/pathresolver.go index e8ef7751484..8bcf011695f 100644 --- a/core/pathresolver.go +++ b/core/pathresolver.go @@ -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 @@ -19,7 +19,7 @@ var ErrNoNamesys = errors.New( // Resolve resolves the given path by parsing out protocol-specific // entries (e.g. /ipns/) 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 diff --git a/merkledag/coding.go b/merkledag/coding.go index 1d1badd3bf7..c37a63db530 100644 --- a/merkledag/coding.go +++ b/merkledag/coding.go @@ -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" ) @@ -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) diff --git a/merkledag/merkledag.go b/merkledag/merkledag.go index 16125a1fd14..22392789297 100644 --- a/merkledag/merkledag.go +++ b/merkledag/merkledag.go @@ -13,6 +13,7 @@ import ( logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ) var log = logging.Logger("merkledag") @@ -20,9 +21,9 @@ var ErrNotFound = fmt.Errorf("merkledag: not found") // DAGService is an IPFS Merkle DAG service. type DAGService interface { - Add(Node) (*cid.Cid, error) - Get(context.Context, *cid.Cid) (Node, error) - Remove(Node) error + Add(node.Node) (*cid.Cid, error) + Get(context.Context, *cid.Cid) (node.Node, error) + Remove(node.Node) error // GetDAG returns, in order, all the single leve child // nodes of the passed in node. @@ -36,7 +37,7 @@ type DAGService interface { type LinkService interface { // Return all links for a node, may be more effect than // calling Get in DAGService - GetLinks(context.Context, *cid.Cid) ([]*Link, error) + GetLinks(context.Context, *cid.Cid) ([]*node.Link, error) GetOfflineLinkService() LinkService } @@ -45,19 +46,6 @@ func NewDAGService(bs bserv.BlockService) *dagService { return &dagService{Blocks: bs} } -type Node interface { - Resolve(path []string) (*Link, []string, error) - Links() []*Link - Tree() []string - - Stat() (*NodeStat, error) - Size() (uint64, error) - Cid() *cid.Cid - Loggable() map[string]interface{} - RawData() []byte - String() string -} - // dagService is an IPFS Merkle DAG service. // - the root is virtual (like a forest) // - stores nodes' data in a BlockService @@ -68,7 +56,7 @@ type dagService struct { } // Add adds a node to the dagService, storing the block in the BlockService -func (n *dagService) Add(nd Node) (*cid.Cid, error) { +func (n *dagService) Add(nd node.Node) (*cid.Cid, error) { if n == nil { // FIXME remove this assertion. protect with constructor invariant return nil, fmt.Errorf("dagService is nil") } @@ -81,7 +69,7 @@ func (n *dagService) Batch() *Batch { } // Get retrieves a node from the dagService, fetching the block in the BlockService -func (n *dagService) Get(ctx context.Context, c *cid.Cid) (Node, error) { +func (n *dagService) Get(ctx context.Context, c *cid.Cid) (node.Node, error) { if n == nil { return nil, fmt.Errorf("dagService is nil") } @@ -97,7 +85,7 @@ func (n *dagService) Get(ctx context.Context, c *cid.Cid) (Node, error) { return nil, fmt.Errorf("Failed to get block for %s: %v", c, err) } - var res Node + var res node.Node switch c.Type() { case cid.Protobuf: out, err := DecodeProtobuf(b.RawData()) @@ -116,7 +104,7 @@ func (n *dagService) Get(ctx context.Context, c *cid.Cid) (Node, error) { return res, nil } -func (n *dagService) GetLinks(ctx context.Context, c *cid.Cid) ([]*Link, error) { +func (n *dagService) GetLinks(ctx context.Context, c *cid.Cid) ([]*node.Link, error) { node, err := n.Get(ctx, c) if err != nil { return nil, err @@ -133,7 +121,7 @@ func (n *dagService) GetOfflineLinkService() LinkService { } } -func (n *dagService) Remove(nd Node) error { +func (n *dagService) Remove(nd node.Node) error { return n.Blocks.DeleteBlock(nd) } @@ -155,7 +143,7 @@ func FindLinks(links []*cid.Cid, c *cid.Cid, start int) []int { } type NodeOption struct { - Node Node + Node node.Node Err error } @@ -178,7 +166,7 @@ func (ds *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *Node c := b.Cid() - var nd Node + var nd node.Node switch c.Type() { case cid.Protobuf: decnd, err := DecodeProtobuf(b.RawData()) @@ -209,7 +197,7 @@ func (ds *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *Node // GetDAG will fill out all of the links of the given Node. // It returns a channel of nodes, which the caller can receive // all the child nodes of 'root' on, in proper order. -func GetDAG(ctx context.Context, ds DAGService, root Node) []NodeGetter { +func GetDAG(ctx context.Context, ds DAGService, root node.Node) []NodeGetter { var cids []*cid.Cid for _, lnk := range root.Links() { cids = append(cids, lnk.Cid) @@ -281,16 +269,16 @@ func dedupeKeys(cids []*cid.Cid) []*cid.Cid { func newNodePromise(ctx context.Context) NodeGetter { return &nodePromise{ - recv: make(chan Node, 1), + recv: make(chan node.Node, 1), ctx: ctx, err: make(chan error, 1), } } type nodePromise struct { - cache Node + cache node.Node clk sync.Mutex - recv chan Node + recv chan node.Node ctx context.Context err chan error } @@ -300,9 +288,9 @@ type nodePromise struct { // from its internal channels, subsequent calls will return the // cached node. type NodeGetter interface { - Get(context.Context) (Node, error) + Get(context.Context) (node.Node, error) Fail(err error) - Send(Node) + Send(node.Node) } func (np *nodePromise) Fail(err error) { @@ -318,7 +306,7 @@ func (np *nodePromise) Fail(err error) { np.err <- err } -func (np *nodePromise) Send(nd Node) { +func (np *nodePromise) Send(nd node.Node) { var already bool np.clk.Lock() if np.cache != nil { @@ -334,7 +322,7 @@ func (np *nodePromise) Send(nd Node) { np.recv <- nd } -func (np *nodePromise) Get(ctx context.Context) (Node, error) { +func (np *nodePromise) Get(ctx context.Context) (node.Node, error) { np.clk.Lock() c := np.cache np.clk.Unlock() @@ -362,7 +350,7 @@ type Batch struct { MaxSize int } -func (t *Batch) Add(nd Node) (*cid.Cid, error) { +func (t *Batch) Add(nd node.Node) (*cid.Cid, error) { t.blocks = append(t.blocks, nd) t.size += len(nd.RawData()) if t.size > t.MaxSize { diff --git a/merkledag/merkledag_test.go b/merkledag/merkledag_test.go index 9ade523a767..310134fa099 100644 --- a/merkledag/merkledag_test.go +++ b/merkledag/merkledag_test.go @@ -2,6 +2,7 @@ package merkledag_test import ( "bytes" + "context" "errors" "fmt" "io" @@ -19,10 +20,10 @@ import ( mdpb "github.com/ipfs/go-ipfs/merkledag/pb" dstest "github.com/ipfs/go-ipfs/merkledag/test" uio "github.com/ipfs/go-ipfs/unixfs/io" - key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key" - "context" cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid" + key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util" ) @@ -85,7 +86,7 @@ func SubtestNodeStat(t *testing.T, n *ProtoNode) { k := n.Key() - expected := NodeStat{ + expected := node.NodeStat{ NumLinks: len(n.Links()), BlockSize: len(enc), LinksSize: len(enc) - len(n.Data()), // includes framing. @@ -206,7 +207,7 @@ func runBatchFetchTest(t *testing.T, read io.Reader) { } } -func assertCanGet(t *testing.T, ds DAGService, n Node) { +func assertCanGet(t *testing.T, ds DAGService, n node.Node) { if _, err := ds.Get(context.Background(), n.Cid()); err != nil { t.Fatal(err) } @@ -268,8 +269,8 @@ func TestEnumerateChildren(t *testing.T) { t.Fatal(err) } - var traverse func(n Node) - traverse = func(n Node) { + var traverse func(n node.Node) + traverse = func(n node.Node) { // traverse dag and check for _, lnk := range n.Links() { c := lnk.Cid diff --git a/merkledag/node.go b/merkledag/node.go index ca2d2102137..4c01c9c9c1b 100644 --- a/merkledag/node.go +++ b/merkledag/node.go @@ -1,21 +1,22 @@ package merkledag import ( - "fmt" - "context" + "fmt" cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid" mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash" key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ) +var ErrNotProtobuf = fmt.Errorf("expected protobuf dag node") var ErrLinkNotFound = fmt.Errorf("no link by that name") // Node represents a node in the IPFS Merkle DAG. // nodes have opaque data and a set of navigable links. type ProtoNode struct { - links []*Link + links []*node.Link data []byte // cache encoded/marshaled value @@ -24,57 +25,12 @@ type ProtoNode struct { cached *cid.Cid } -// NodeStat is a statistics object for a Node. Mostly sizes. -type NodeStat struct { - Hash string - NumLinks int // number of links in link table - BlockSize int // size of the raw, encoded data - LinksSize int // size of the links segment - DataSize int // size of the data segment - CumulativeSize int // cumulative size of object and its references -} - -func (ns NodeStat) String() string { - f := "NodeStat{NumLinks: %d, BlockSize: %d, LinksSize: %d, DataSize: %d, CumulativeSize: %d}" - return fmt.Sprintf(f, ns.NumLinks, ns.BlockSize, ns.LinksSize, ns.DataSize, ns.CumulativeSize) -} - -// Link represents an IPFS Merkle DAG Link between Nodes. -type Link struct { - // utf string name. should be unique per object - Name string // utf8 - - // cumulative size of target object - Size uint64 - - // multihash of the target object - Cid *cid.Cid -} - -type LinkSlice []*Link +type LinkSlice []*node.Link func (ls LinkSlice) Len() int { return len(ls) } func (ls LinkSlice) Swap(a, b int) { ls[a], ls[b] = ls[b], ls[a] } func (ls LinkSlice) Less(a, b int) bool { return ls[a].Name < ls[b].Name } -// MakeLink creates a link to the given node -func MakeLink(n Node) (*Link, error) { - s, err := n.Size() - if err != nil { - return nil, err - } - - return &Link{ - Size: s, - Cid: n.Cid(), - }, nil -} - -// GetNode returns the MDAG Node that this link points to -func (l *Link) GetNode(ctx context.Context, serv DAGService) (Node, error) { - return serv.Get(ctx, l.Cid) -} - func NodeWithData(d []byte) *ProtoNode { return &ProtoNode{data: d} } @@ -83,13 +39,13 @@ func NodeWithData(d []byte) *ProtoNode { func (n *ProtoNode) AddNodeLink(name string, that *ProtoNode) error { n.encoded = nil - lnk, err := MakeLink(that) - - lnk.Name = name + lnk, err := node.MakeLink(that) if err != nil { return err } + lnk.Name = name + n.AddRawLink(name, lnk) return nil @@ -97,9 +53,9 @@ func (n *ProtoNode) AddNodeLink(name string, that *ProtoNode) error { // AddNodeLinkClean adds a link to another node. without keeping a reference to // the child node -func (n *ProtoNode) AddNodeLinkClean(name string, that Node) error { +func (n *ProtoNode) AddNodeLinkClean(name string, that node.Node) error { n.encoded = nil - lnk, err := MakeLink(that) + lnk, err := node.MakeLink(that) if err != nil { return err } @@ -109,9 +65,9 @@ func (n *ProtoNode) AddNodeLinkClean(name string, that Node) error { } // AddRawLink adds a copy of a link to this node -func (n *ProtoNode) AddRawLink(name string, l *Link) error { +func (n *ProtoNode) AddRawLink(name string, l *node.Link) error { n.encoded = nil - n.links = append(n.links, &Link{ + n.links = append(n.links, &node.Link{ Name: name, Size: l.Size, Cid: l.Cid, @@ -123,7 +79,7 @@ func (n *ProtoNode) AddRawLink(name string, l *Link) error { // Remove a link on this node by the given name func (n *ProtoNode) RemoveNodeLink(name string) error { n.encoded = nil - good := make([]*Link, 0, len(n.links)) + good := make([]*node.Link, 0, len(n.links)) var found bool for _, l := range n.links { @@ -143,10 +99,10 @@ func (n *ProtoNode) RemoveNodeLink(name string) error { } // Return a copy of the link with given name -func (n *ProtoNode) GetNodeLink(name string) (*Link, error) { +func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) { for _, l := range n.links { if l.Name == name { - return &Link{ + return &node.Link{ Name: l.Name, Size: l.Size, Cid: l.Cid, @@ -156,8 +112,6 @@ func (n *ProtoNode) GetNodeLink(name string) (*Link, error) { return nil, ErrLinkNotFound } -var ErrNotProtobuf = fmt.Errorf("expected protobuf dag node") - func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds DAGService, name string) (*ProtoNode, error) { nd, err := n.GetLinkedNode(ctx, ds, name) if err != nil { @@ -172,7 +126,7 @@ func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds DAGService, name return pbnd, nil } -func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds DAGService, name string) (Node, error) { +func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds DAGService, name string) (node.Node, error) { lnk, err := n.GetNodeLink(name) if err != nil { return nil, err @@ -191,7 +145,7 @@ func (n *ProtoNode) Copy() *ProtoNode { } if len(n.links) > 0 { - nnode.links = make([]*Link, len(n.links)) + nnode.links = make([]*node.Link, len(n.links)) copy(nnode.links, n.links) } return nnode @@ -238,7 +192,7 @@ func (n *ProtoNode) Size() (uint64, error) { } // Stat returns statistics on the node. -func (n *ProtoNode) Stat() (*NodeStat, error) { +func (n *ProtoNode) Stat() (*node.NodeStat, error) { enc, err := n.EncodeProtobuf(false) if err != nil { return nil, err @@ -249,7 +203,7 @@ func (n *ProtoNode) Stat() (*NodeStat, error) { return nil, err } - return &NodeStat{ + return &node.NodeStat{ Hash: n.Key().B58String(), NumLinks: len(n.links), BlockSize: len(enc), @@ -291,15 +245,15 @@ func (n *ProtoNode) Multihash() mh.Multihash { return n.cached.Hash() } -func (n *ProtoNode) Links() []*Link { +func (n *ProtoNode) Links() []*node.Link { return n.links } -func (n *ProtoNode) SetLinks(links []*Link) { +func (n *ProtoNode) SetLinks(links []*node.Link) { n.links = links } -func (n *ProtoNode) Resolve(path []string) (*Link, []string, error) { +func (n *ProtoNode) Resolve(path []string) (*node.Link, []string, error) { if len(path) == 0 { return nil, nil, fmt.Errorf("end of path, no more links to resolve") } diff --git a/merkledag/node_test.go b/merkledag/node_test.go index 4054d6b9316..392a51ea282 100644 --- a/merkledag/node_test.go +++ b/merkledag/node_test.go @@ -1,23 +1,24 @@ package merkledag_test import ( + "context" "testing" . "github.com/ipfs/go-ipfs/merkledag" mdtest "github.com/ipfs/go-ipfs/merkledag/test" - "context" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ) func TestRemoveLink(t *testing.T) { nd := &ProtoNode{} - nd.SetLinks([]*Link{ - &Link{Name: "a"}, - &Link{Name: "b"}, - &Link{Name: "a"}, - &Link{Name: "a"}, - &Link{Name: "c"}, - &Link{Name: "a"}, + nd.SetLinks([]*node.Link{ + {Name: "a"}, + {Name: "b"}, + {Name: "a"}, + {Name: "a"}, + {Name: "c"}, + {Name: "a"}, }) err := nd.RemoveNodeLink("a") @@ -65,10 +66,10 @@ func TestFindLink(t *testing.T) { } nd := &ProtoNode{} - nd.SetLinks([]*Link{ - &Link{Name: "a", Cid: k}, - &Link{Name: "c", Cid: k}, - &Link{Name: "b", Cid: k}, + nd.SetLinks([]*node.Link{ + {Name: "a", Cid: k}, + {Name: "c", Cid: k}, + {Name: "b", Cid: k}, }) _, err = ds.Add(nd) @@ -112,10 +113,10 @@ func TestFindLink(t *testing.T) { func TestNodeCopy(t *testing.T) { nd := &ProtoNode{} - nd.SetLinks([]*Link{ - &Link{Name: "a"}, - &Link{Name: "c"}, - &Link{Name: "b"}, + nd.SetLinks([]*node.Link{ + {Name: "a"}, + {Name: "c"}, + {Name: "b"}, }) nd.SetData([]byte("testing")) diff --git a/merkledag/traverse/traverse.go b/merkledag/traverse/traverse.go index fdc06d2cddc..17e1b666c83 100644 --- a/merkledag/traverse/traverse.go +++ b/merkledag/traverse/traverse.go @@ -2,11 +2,10 @@ package traverse import ( - "errors" - "context" + "errors" - mdag "github.com/ipfs/go-ipfs/merkledag" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ) // Order is an identifier for traversal algorithm orders @@ -20,7 +19,7 @@ const ( // Options specifies a series of traversal options type Options struct { - DAG mdag.DAGService // the dagservice to fetch nodes + DAG node.NodeGetter // the dagservice to fetch nodes Order Order // what order to traverse in Func Func // the function to perform at each step ErrFunc ErrFunc // see ErrFunc. Optional @@ -30,7 +29,7 @@ type Options struct { // State is a current traversal state type State struct { - Node mdag.Node + Node node.Node Depth int } @@ -39,7 +38,7 @@ type traversal struct { seen map[string]struct{} } -func (t *traversal) shouldSkip(n mdag.Node) (bool, error) { +func (t *traversal) shouldSkip(n node.Node) (bool, error) { if t.opts.SkipDuplicates { k := n.Cid() if _, found := t.seen[k.KeyString()]; found { @@ -59,9 +58,9 @@ func (t *traversal) callFunc(next State) error { // stop processing. if it returns a nil node, just skip it. // // the error handling is a little complicated. -func (t *traversal) getNode(link *mdag.Link) (mdag.Node, error) { +func (t *traversal) getNode(link *node.Link) (node.Node, error) { - getNode := func(l *mdag.Link) (mdag.Node, error) { + getNode := func(l *node.Link) (node.Node, error) { next, err := l.GetNode(context.TODO(), t.opts.DAG) if err != nil { return nil, err @@ -99,7 +98,7 @@ type Func func(current State) error // type ErrFunc func(err error) error -func Traverse(root mdag.Node, o Options) error { +func Traverse(root node.Node, o Options) error { t := traversal{ opts: o, seen: map[string]struct{}{}, diff --git a/merkledag/traverse/traverse_test.go b/merkledag/traverse/traverse_test.go index c7dd93a4757..fc8d053fa1d 100644 --- a/merkledag/traverse/traverse_test.go +++ b/merkledag/traverse/traverse_test.go @@ -7,6 +7,8 @@ import ( mdag "github.com/ipfs/go-ipfs/merkledag" mdagtest "github.com/ipfs/go-ipfs/merkledag/test" + + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ) func TestDFSPreNoSkip(t *testing.T) { @@ -321,7 +323,7 @@ func TestBFSSkip(t *testing.T) { `)) } -func testWalkOutputs(t *testing.T, root mdag.Node, opts Options, expect []byte) { +func testWalkOutputs(t *testing.T, root node.Node, opts Options, expect []byte) { expect = bytes.TrimLeft(expect, "\n") buf := new(bytes.Buffer) @@ -348,7 +350,7 @@ func testWalkOutputs(t *testing.T, root mdag.Node, opts Options, expect []byte) } } -func newFan(t *testing.T, ds mdag.DAGService) mdag.Node { +func newFan(t *testing.T, ds mdag.DAGService) node.Node { a := mdag.NodeWithData([]byte("/a")) addLink(t, ds, a, child(t, ds, a, "aa")) addLink(t, ds, a, child(t, ds, a, "ab")) @@ -357,7 +359,7 @@ func newFan(t *testing.T, ds mdag.DAGService) mdag.Node { return a } -func newLinkedList(t *testing.T, ds mdag.DAGService) mdag.Node { +func newLinkedList(t *testing.T, ds mdag.DAGService) node.Node { a := mdag.NodeWithData([]byte("/a")) aa := child(t, ds, a, "aa") aaa := child(t, ds, aa, "aaa") @@ -370,7 +372,7 @@ func newLinkedList(t *testing.T, ds mdag.DAGService) mdag.Node { return a } -func newBinaryTree(t *testing.T, ds mdag.DAGService) mdag.Node { +func newBinaryTree(t *testing.T, ds mdag.DAGService) node.Node { a := mdag.NodeWithData([]byte("/a")) aa := child(t, ds, a, "aa") ab := child(t, ds, a, "ab") @@ -383,7 +385,7 @@ func newBinaryTree(t *testing.T, ds mdag.DAGService) mdag.Node { return a } -func newBinaryDAG(t *testing.T, ds mdag.DAGService) mdag.Node { +func newBinaryDAG(t *testing.T, ds mdag.DAGService) node.Node { a := mdag.NodeWithData([]byte("/a")) aa := child(t, ds, a, "aa") aaa := child(t, ds, aa, "aaa") @@ -400,7 +402,7 @@ func newBinaryDAG(t *testing.T, ds mdag.DAGService) mdag.Node { return a } -func addLink(t *testing.T, ds mdag.DAGService, a, b mdag.Node) { +func addLink(t *testing.T, ds mdag.DAGService, a, b node.Node) { to := string(a.(*mdag.ProtoNode).Data()) + "2" + string(b.(*mdag.ProtoNode).Data()) if _, err := ds.Add(b); err != nil { t.Error(err) @@ -410,6 +412,6 @@ func addLink(t *testing.T, ds mdag.DAGService, a, b mdag.Node) { } } -func child(t *testing.T, ds mdag.DAGService, a mdag.Node, name string) mdag.Node { +func child(t *testing.T, ds mdag.DAGService, a node.Node, name string) node.Node { return mdag.NodeWithData([]byte(string(a.(*mdag.ProtoNode).Data()) + "/" + name)) } diff --git a/mfs/mfs_test.go b/mfs/mfs_test.go index f9c79769c74..dcec37356c1 100644 --- a/mfs/mfs_test.go +++ b/mfs/mfs_test.go @@ -2,6 +2,7 @@ package mfs import ( "bytes" + "context" "errors" "fmt" "io" @@ -23,8 +24,8 @@ import ( ft "github.com/ipfs/go-ipfs/unixfs" uio "github.com/ipfs/go-ipfs/unixfs/io" - "context" cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util" ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore" dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync" @@ -291,12 +292,12 @@ func TestDirectoryLoadFromDag(t *testing.T) { dirhash := dir.Cid() top := emptyDirNode() - top.SetLinks([]*dag.Link{ - &dag.Link{ + top.SetLinks([]*node.Link{ + { Name: "a", Cid: fihash, }, - &dag.Link{ + { Name: "b", Cid: dirhash, }, diff --git a/package.json b/package.json index 69764805b4d..e411881fe34 100644 --- a/package.json +++ b/package.json @@ -269,6 +269,12 @@ "hash": "QmTgcWwxttM74AY7UYA6qMP9WpzfBEjbZntx7ZWLttRMJJ", "name": "floodsub", "version": "0.7.0" + }, + { + "author": "whyrusleeping", + "hash": "QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB", + "name": "go-ipld-node", + "version": "0.1.0" } ], "gxVersion": "0.4.0", diff --git a/path/resolver.go b/path/resolver.go index 14ed1d87c87..e4bfe8f796c 100644 --- a/path/resolver.go +++ b/path/resolver.go @@ -11,6 +11,7 @@ import ( logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ) var log = logging.Logger("path") @@ -61,7 +62,7 @@ func SplitAbsPath(fpath Path) (*cid.Cid, []string, error) { // ResolvePath fetches the node for given path. It returns the last item // returned by ResolvePathComponents. -func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (merkledag.Node, error) { +func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (node.Node, error) { // validate path if err := fpath.IsValid(); err != nil { return nil, err @@ -77,7 +78,7 @@ func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (merkledag.Node, // ResolvePathComponents fetches the nodes for each segment of the given path. // It uses the first path component as a hash (key) of the first node, then // resolves all other components walking the links, with ResolveLinks. -func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]merkledag.Node, error) { +func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]node.Node, error) { h, parts, err := SplitAbsPath(fpath) if err != nil { return nil, err @@ -99,9 +100,9 @@ func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]mer // // ResolveLinks(nd, []string{"foo", "bar", "baz"}) // would retrieve "baz" in ("bar" in ("foo" in nd.Links).Links).Links -func (s *Resolver) ResolveLinks(ctx context.Context, ndd merkledag.Node, names []string) ([]merkledag.Node, error) { +func (s *Resolver) ResolveLinks(ctx context.Context, ndd node.Node, names []string) ([]node.Node, error) { - result := make([]merkledag.Node, 0, len(names)+1) + result := make([]node.Node, 0, len(names)+1) result = append(result, ndd) nd := ndd // dup arg workaround diff --git a/path/resolver_test.go b/path/resolver_test.go index b0130bb17d4..652f3879653 100644 --- a/path/resolver_test.go +++ b/path/resolver_test.go @@ -1,15 +1,16 @@ package path_test import ( + "context" "fmt" "testing" - context "context" - merkledag "github.com/ipfs/go-ipfs/merkledag" dagmock "github.com/ipfs/go-ipfs/merkledag/test" path "github.com/ipfs/go-ipfs/path" + key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" util "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util" ) @@ -39,7 +40,7 @@ func TestRecurivePathResolution(t *testing.T) { t.Fatal(err) } - for _, n := range []merkledag.Node{a, b, c} { + for _, n := range []node.Node{a, b, c} { _, err = dagService.Add(n) if err != nil { t.Fatal(err) diff --git a/pin/pin.go b/pin/pin.go index 4a59e78d9ff..10c60c25698 100644 --- a/pin/pin.go +++ b/pin/pin.go @@ -3,17 +3,17 @@ package pin import ( + "context" "fmt" "os" "sync" "time" mdag "github.com/ipfs/go-ipfs/merkledag" - key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key" - context "context" logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore" ) @@ -83,7 +83,7 @@ func StringToPinMode(s string) (PinMode, bool) { type Pinner interface { IsPinned(*cid.Cid) (string, bool, error) IsPinnedWithType(*cid.Cid, PinMode) (string, bool, error) - Pin(context.Context, mdag.Node, bool) error + Pin(context.Context, node.Node, bool) error Unpin(context.Context, *cid.Cid, bool) error // Check if a set of keys are pinned, more efficient than @@ -162,11 +162,10 @@ func NewPinner(dstore ds.Datastore, serv, internal mdag.DAGService) Pinner { } // Pin the given node, optionally recursive -func (p *pinner) Pin(ctx context.Context, node mdag.Node, recurse bool) error { +func (p *pinner) Pin(ctx context.Context, node node.Node, recurse bool) error { p.lock.Lock() defer p.lock.Unlock() c := node.Cid() - k := key.Key(c.Hash()) if recurse { if p.recursePin.Has(c) { @@ -190,7 +189,7 @@ func (p *pinner) Pin(ctx context.Context, node mdag.Node, recurse bool) error { } if p.recursePin.Has(c) { - return fmt.Errorf("%s already pinned recursively", k.B58String()) + return fmt.Errorf("%s already pinned recursively", c.String()) } p.directPin.Add(c) @@ -248,7 +247,6 @@ func (p *pinner) IsPinnedWithType(c *cid.Cid, mode PinMode) (string, bool, error // isPinnedWithType is the implementation of IsPinnedWithType that does not lock. // intended for use by other pinned methods that already take locks func (p *pinner) isPinnedWithType(c *cid.Cid, mode PinMode) (string, bool, error) { - k := key.Key(c.Hash()) switch mode { case Any, Direct, Indirect, Recursive, Internal: default: @@ -279,7 +277,7 @@ func (p *pinner) isPinnedWithType(c *cid.Cid, mode PinMode) (string, bool, error // Default is Indirect for _, rc := range p.recursePin.Keys() { - has, err := hasChild(p.dserv, rc, k) + has, err := hasChild(p.dserv, rc, c) if err != nil { return "", false, err } @@ -521,14 +519,14 @@ func (p *pinner) PinWithMode(c *cid.Cid, mode PinMode) { } } -func hasChild(ds mdag.LinkService, root *cid.Cid, child key.Key) (bool, error) { +func hasChild(ds mdag.LinkService, root *cid.Cid, child *cid.Cid) (bool, error) { links, err := ds.GetLinks(context.Background(), root) if err != nil { return false, err } for _, lnk := range links { c := lnk.Cid - if key.Key(c.Hash()) == child { + if lnk.Cid.Equals(child) { return true, nil } diff --git a/pin/set.go b/pin/set.go index 1a1f9f3bf60..eaaba7884c9 100644 --- a/pin/set.go +++ b/pin/set.go @@ -12,9 +12,11 @@ import ( "github.com/ipfs/go-ipfs/merkledag" "github.com/ipfs/go-ipfs/pin/internal/pb" + cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid" "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key" "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ) const ( @@ -47,7 +49,7 @@ type itemIterator func() (c *cid.Cid, ok bool) type keyObserver func(*cid.Cid) type sortByHash struct { - links []*merkledag.Link + links []*node.Link } func (s sortByHash) Len() int { @@ -67,9 +69,9 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint if err != nil { return nil, err } - links := make([]*merkledag.Link, 0, defaultFanout+maxItems) + links := make([]*node.Link, 0, defaultFanout+maxItems) for i := 0; i < defaultFanout; i++ { - links = append(links, &merkledag.Link{Cid: emptyKey}) + links = append(links, &node.Link{Cid: emptyKey}) } // add emptyKey to our set of internal pinset objects @@ -97,7 +99,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint break } - links = append(links, &merkledag.Link{Cid: k}) + links = append(links, &node.Link{Cid: k}) } n.SetLinks(links) @@ -159,7 +161,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint internalKeys(childKey) // overwrite the 'empty key' in the existing links array - n.Links()[h] = &merkledag.Link{ + n.Links()[h] = &node.Link{ Cid: childKey, Size: size, } @@ -212,7 +214,7 @@ func writeHdr(n *merkledag.ProtoNode, hdr *pb.Set) error { return nil } -type walkerFunc func(idx int, link *merkledag.Link) error +type walkerFunc func(idx int, link *node.Link) error func walkItems(ctx context.Context, dag merkledag.DAGService, n *merkledag.ProtoNode, fn walkerFunc, children keyObserver) error { hdr, err := readHdr(n) @@ -269,7 +271,7 @@ func loadSet(ctx context.Context, dag merkledag.DAGService, root *merkledag.Prot } var res []*cid.Cid - walk := func(idx int, link *merkledag.Link) error { + walk := func(idx int, link *node.Link) error { res = append(res, link.Cid) return nil } diff --git a/tar/format.go b/tar/format.go index 79a2e6339e3..052ff22142f 100644 --- a/tar/format.go +++ b/tar/format.go @@ -3,6 +3,7 @@ package tarfmt import ( "archive/tar" "bytes" + "context" "errors" "io" "io/ioutil" @@ -14,9 +15,9 @@ import ( dagutil "github.com/ipfs/go-ipfs/merkledag/utils" path "github.com/ipfs/go-ipfs/path" uio "github.com/ipfs/go-ipfs/unixfs/io" - logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" - context "context" + logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" + node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node" ) var log = logging.Logger("tarfmt") @@ -106,7 +107,7 @@ func escapePath(pth string) string { } type tarReader struct { - links []*dag.Link + links []*node.Link ds dag.DAGService childRead *tarReader