Commit fa3a9e2e authored by Jeromy Johnson's avatar Jeromy Johnson

Merge pull request #794 from jbenet/feat/refs-faster

teach ipfs refs -r how to use GetBlocks
parents 5253b6ab 3cda8ca6
......@@ -227,6 +227,14 @@ type RefWriter struct {
// WriteRefs writes refs of the given object to the underlying writer.
func (rw *RefWriter) WriteRefs(n *dag.Node) (int, error) {
if rw.Recursive {
return rw.writeRefsRecursive(n)
} else {
return rw.writeRefsSingle(n)
}
}
func (rw *RefWriter) writeRefsRecursive(n *dag.Node) (int, error) {
nkey, err := n.Key()
if err != nil {
return 0, err
......@@ -236,29 +244,23 @@ func (rw *RefWriter) WriteRefs(n *dag.Node) (int, error) {
return 0, nil
}
count := 0
for _, l := range n.Links {
lk := u.Key(l.Hash)
var count int
for i, ng := range rw.DAG.GetDAG(rw.Ctx, n) {
lk := u.Key(n.Links[i].Hash)
if rw.skip(lk) {
continue
}
if err := rw.WriteEdge(nkey, lk, l.Name); err != nil {
if err := rw.WriteEdge(nkey, lk, n.Links[i].Name); err != nil {
return count, err
}
count++
if !rw.Recursive {
continue
}
child, err := l.GetNode(rw.DAG)
nd, err := ng.Get()
if err != nil {
return count, err
}
c, err := rw.WriteRefs(child)
c, err := rw.writeRefsRecursive(nd)
count += c
if err != nil {
return count, err
......@@ -267,6 +269,32 @@ func (rw *RefWriter) WriteRefs(n *dag.Node) (int, error) {
return count, nil
}
func (rw *RefWriter) writeRefsSingle(n *dag.Node) (int, error) {
nkey, err := n.Key()
if err != nil {
return 0, err
}
if rw.skip(nkey) {
return 0, nil
}
count := 0
for _, l := range n.Links {
lk := u.Key(l.Hash)
if rw.skip(lk) {
continue
}
if err := rw.WriteEdge(nkey, lk, l.Name); err != nil {
return count, err
}
count++
}
return count, nil
}
// skip returns whether to skip a key
func (rw *RefWriter) skip(k u.Key) bool {
if !rw.Unique {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment