Skip to content

Commit

Permalink
Uses DAG node get result to determine link cached status
Browse files Browse the repository at this point in the history
Link cached status was determined by checking the presence of link hash in blockstore. Now link cached status is determined by successfully getting node from DAG.

License: MIT
Signed-off-by: Sivachandran <[email protected]>
  • Loading branch information
sivachandran committed Apr 15, 2016
1 parent eee9549 commit 60bc015
Showing 1 changed file with 24 additions and 50 deletions.
74 changes: 24 additions & 50 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strconv"
"strings"

blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
key "github.com/ipfs/go-ipfs/blocks/key"
bserv "github.com/ipfs/go-ipfs/blockservice"
cmds "github.com/ipfs/go-ipfs/commands"
Expand Down Expand Up @@ -53,7 +52,7 @@ Note: List all references recursively by using the flag '-r'.
cmds.StringArg("ipfs-path", true, true, "Path to the object(s) to list refs from.").EnableStdin(),
},
Options: []cmds.Option{
cmds.StringOption("format", "Emit edges with given format. Available tokens: <src> <dst> <linkname> <linksize>"),
cmds.StringOption("format", "Emit edges with given format. Available tokens: <src> <dst> <linkname> <linksize> <linkcached>"),
cmds.BoolOption("edges", "e", "Emit edge format: `<from> -> <to>`."),
cmds.BoolOption("unique", "u", "Omit duplicate refs from output."),
cmds.BoolOption("recursive", "r", "Recursively list links of child nodes."),
Expand Down Expand Up @@ -118,7 +117,6 @@ Note: List all references recursively by using the flag '-r'.
out: out,
DAG: ds,
Ctx: ctx,
Bstore: n.Blockstore,
Unique: unique,
PrintEdge: edges,
PrintFmt: format,
Expand Down Expand Up @@ -224,10 +222,9 @@ type RefWrapper struct {
}

type RefWriter struct {
out chan interface{}
DAG dag.DAGService
Ctx context.Context
Bstore blockstore.Blockstore
out chan interface{}
DAG dag.DAGService
Ctx context.Context

Unique bool
Recursive bool
Expand All @@ -253,54 +250,31 @@ func (rw *RefWriter) writeRefsRecursive(n *dag.Node) (int, error) {
}

var count int
if rw.Cached {
for _, link := range n.Links {
lk := key.Key(link.Hash)
if rw.skip(lk) {
continue
}

block, _ := rw.Bstore.Get(lk)
cached := (block != nil)

if err := rw.WriteEdge(nkey, lk, link.Name, link.Size, cached); err != nil {
return count, err
}
for i, ng := range dag.GetDAG(rw.Ctx, rw.DAG, n) {
lk := key.Key(n.Links[i].Hash)
if rw.skip(lk) {
continue
}

if cached {
nd, err := dag.DecodeProtobuf(block.Data)
if err != nil {
return count, err
}
nd, err := ng.Get(rw.Ctx)

c, err := rw.writeRefsRecursive(nd)
count += c
if err != nil {
return count, err
}
}
linkCached := (nd != nil)
if werr := rw.WriteEdge(nkey, lk, n.Links[i].Name, n.Links[i].Size, linkCached); werr != nil {
return count, werr
}
} else {
for i, ng := range dag.GetDAG(rw.Ctx, rw.DAG, n) {
lk := key.Key(n.Links[i].Hash)
if rw.skip(lk) {
continue
}

if err := rw.WriteEdge(nkey, lk, n.Links[i].Name, n.Links[i].Size, true); err != nil {
return count, err
}

nd, err := ng.Get(rw.Ctx)
if err != nil {
if err != nil {
if rw.Cached {
continue
} else {
return count, err
}
}

c, err := rw.writeRefsRecursive(nd)
count += c
if err != nil {
return count, err
}
c, err := rw.writeRefsRecursive(nd)
count += c
if err != nil {
return count, err
}
}

Expand Down Expand Up @@ -351,7 +325,7 @@ func (rw *RefWriter) skip(k key.Key) bool {
}

// Write one edge
func (rw *RefWriter) WriteEdge(from, to key.Key, linkname string, linksize uint64, cached bool) error {
func (rw *RefWriter) WriteEdge(from, to key.Key, linkname string, linksize uint64, linkcached bool) error {
if rw.Ctx != nil {
select {
case <-rw.Ctx.Done(): // just in case.
Expand All @@ -368,7 +342,7 @@ func (rw *RefWriter) WriteEdge(from, to key.Key, linkname string, linksize uint6
s = strings.Replace(s, "<dst>", to.B58String(), -1)
s = strings.Replace(s, "<linkname>", linkname, -1)
s = strings.Replace(s, "<linksize>", strconv.FormatUint(linksize, 10), -1)
s = strings.Replace(s, "<cached>", strconv.FormatBool(cached), -1)
s = strings.Replace(s, "<linkcached>", strconv.FormatBool(linkcached), -1)
case rw.PrintEdge:
s = from.B58String() + " -> " + to.B58String()
default:
Expand Down

0 comments on commit 60bc015

Please sign in to comment.