1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 01:12:24 +08:00

"pin verify": don't use a pointer to a slice.

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
This commit is contained in:
Kevin Atkinson
2017-03-29 17:44:43 -04:00
parent 325e1c0839
commit 6e898ea678

View File

@ -515,9 +515,7 @@ func pinLsAll(typeStr string, ctx context.Context, n *core.IpfsNode) (map[string
} }
type pinStatus struct { type pinStatus struct {
// use pointer to array slice to reduce memory usage badNodes []badNode
// the value should not be nil unless uninitialized
badNodes *[]badNode
} }
type badNode struct { type badNode struct {
@ -533,44 +531,29 @@ type pinVerifyRes struct {
func pinVerify(ctx context.Context, n *core.IpfsNode) <-chan pinVerifyRes { func pinVerify(ctx context.Context, n *core.IpfsNode) <-chan pinVerifyRes {
visited := make(map[string]pinStatus) visited := make(map[string]pinStatus)
getLinks := n.DAG.GetOfflineLinkService().GetLinks getLinks := n.DAG.GetOfflineLinkService().GetLinks
emptySlice := &[]badNode{}
recPins := n.Pinning.RecursiveKeys() recPins := n.Pinning.RecursiveKeys()
var checkPin func(root *cid.Cid) pinStatus var checkPin func(root *cid.Cid) pinStatus
checkPin = func(root *cid.Cid) pinStatus { checkPin = func(root *cid.Cid) pinStatus {
key := root.String() key := root.String()
if status, ok := visited[key]; ok { if status, ok := visited[key]; ok {
if status.badNodes == nil {
return pinStatus{&[]badNode{badNode{
cid: root,
err: fmt.Errorf("Cycle Detected.")}}}
}
return status return status
} }
links, err := getLinks(ctx, root) links, err := getLinks(ctx, root)
if err != nil { if err != nil {
status := pinStatus{&[]badNode{badNode{cid: root, err: err}}} status := pinStatus{[]badNode{badNode{cid: root, err: err}}}
visited[key] = status visited[key] = status
return status return status
} }
status := pinStatus{} status := pinStatus{}
visited[key] = status // paranoid mode cycle detection
for _, lnk := range links { for _, lnk := range links {
res := checkPin(lnk.Cid) res := checkPin(lnk.Cid)
if len(*res.badNodes) > 0 { if len(res.badNodes) > 0 {
if status.badNodes == nil { status.badNodes = append(status.badNodes, res.badNodes...)
status.badNodes = res.badNodes
} else {
slice := append(*status.badNodes, *res.badNodes...)
status.badNodes = &slice
}
} }
} }
if status.badNodes == nil {
status.badNodes = emptySlice // prevent special cases
}
visited[key] = status visited[key] = status
return status return status
@ -589,11 +572,11 @@ func pinVerify(ctx context.Context, n *core.IpfsNode) <-chan pinVerifyRes {
} }
func (r pinVerifyRes) Format(out io.Writer) { func (r pinVerifyRes) Format(out io.Writer) {
if len(*r.badNodes) == 0 { if len(r.badNodes) == 0 {
fmt.Fprintf(out, "%s ok\n", r.cid) fmt.Fprintf(out, "%s ok\n", r.cid)
} else { } else {
fmt.Fprintf(out, "%s broken\n", r.cid) fmt.Fprintf(out, "%s broken\n", r.cid)
for _, e := range *r.badNodes { for _, e := range r.badNodes {
fmt.Fprintf(out, " %s: %s\n", e.cid, e.err) fmt.Fprintf(out, " %s: %s\n", e.cid, e.err)
} }
} }