@cryptotaxi247 / kubo / commits / 6e4fd937f

Refactor EnumerateChildren to avoid need for bestEffort parameter.

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Feb 16, 2017 at 20:39 UTC 6e4fd937fd5a6ff453cc626ec64509c680ba25cd
5 files changed +22 -15
core/commands/pin.go
+1 -1
@@ -400,7 +400,7 @@ func pinLsAll(typeStr string, ctx context.Context, n *core.IpfsNode) (map[string
400 if typeStr == "indirect" || typeStr == "all" {
401 set := cid.NewSet()
402 for _, k := range n.Pinning.RecursiveKeys() {
403 - err := dag.EnumerateChildren(n.Context(), n.DAG, k, set.Visit, false)
403 + err := dag.EnumerateChildren(n.Context(), n.DAG.GetLinks, k, set.Visit)
404 if err != nil {
405 return nil, err
406 }
core/coreunix/add_test.go
+1 -1
@@ -163,7 +163,7 @@ func TestAddGCLive(t *testing.T) {
163 defer cancel()
164
165 set := cid.NewSet()
166 - err = dag.EnumerateChildren(ctx, node.DAG, last, set.Visit, false)
166 + err = dag.EnumerateChildren(ctx, node.DAG.GetLinks, last, set.Visit)
167 if err != nil {
168 t.Fatal(err)
169 }
merkledag/merkledag.go
+5 -6
@@ -383,17 +383,16 @@ func (t *Batch) Commit() error {
383 // EnumerateChildren will walk the dag below the given root node and add all
384 // unseen children to the passed in set.
385 // TODO: parallelize to avoid disk latency perf hits?
386 -func EnumerateChildren(ctx context.Context, ds LinkService, root *cid.Cid, visit func(*cid.Cid) bool, bestEffort bool) error {
387 - links, err := ds.GetLinks(ctx, root)
388 - if bestEffort && err == ErrNotFound {
389 - return nil
390 - } else if err != nil {
386 +type GetLinks func(context.Context, *cid.Cid) ([]*node.Link, error)
387 +func EnumerateChildren(ctx context.Context, getLinks GetLinks, root *cid.Cid, visit func(*cid.Cid) bool) error {
388 + links, err := getLinks(ctx, root)
389 + if err != nil {
390 return err
391 }
392 for _, lnk := range links {
393 c := lnk.Cid
394 if visit(c) {
396 - err = EnumerateChildren(ctx, ds, c, visit, bestEffort)
395 + err = EnumerateChildren(ctx, getLinks, c, visit)
396 if err != nil {
397 return err
398 }
merkledag/merkledag_test.go
+2 -2
@@ -249,7 +249,7 @@ func TestFetchGraph(t *testing.T) {
249
250 offline_ds := NewDAGService(bs)
251
252 - err = EnumerateChildren(context.Background(), offline_ds, root.Cid(), func(_ *cid.Cid) bool { return true }, false)
252 + err = EnumerateChildren(context.Background(), offline_ds.GetLinks, root.Cid(), func(_ *cid.Cid) bool { return true })
253 if err != nil {
254 t.Fatal(err)
255 }
@@ -266,7 +266,7 @@ func TestEnumerateChildren(t *testing.T) {
266 }
267
268 set := cid.NewSet()
269 - err = EnumerateChildren(context.Background(), ds, root.Cid(), set.Visit, false)
269 + err = EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit)
270 if err != nil {
271 t.Fatal(err)
272 }
pin/gc/gc.go
+13 -5
@@ -9,6 +9,7 @@ import (
9
10 logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
11 cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
12 + node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
13 )
14
15 var log = logging.Logger("gc")
@@ -68,12 +69,12 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin.
69 return output, nil
70 }
71
71 -func Descendants(ctx context.Context, ls dag.LinkService, set *cid.Set, roots []*cid.Cid, bestEffort bool) error {
72 +func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots []*cid.Cid) error {
73 for _, c := range roots {
74 set.Add(c)
75
76 // EnumerateChildren recursively walks the dag and adds the keys to the given set
76 - err := dag.EnumerateChildren(ctx, ls, c, set.Visit, bestEffort)
77 + err := dag.EnumerateChildren(ctx, getLinks, c, set.Visit)
78 if err != nil {
79 return err
80 }
@@ -86,12 +87,19 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo
87 // KeySet currently implemented in memory, in the future, may be bloom filter or
88 // disk backed to conserve memory.
89 gcs := cid.NewSet()
89 - err := Descendants(ctx, ls, gcs, pn.RecursiveKeys(), false)
90 + err := Descendants(ctx, ls.GetLinks, gcs, pn.RecursiveKeys())
91 if err != nil {
92 return nil, err
93 }
94
94 - err = Descendants(ctx, ls, gcs, bestEffortRoots, true)
95 + bestEffortGetLinks := func(ctx context.Context, cid *cid.Cid) ([]*node.Link, error) {
96 + links, err := ls.GetLinks(ctx, cid)
97 + if err == dag.ErrNotFound {
98 + err = nil
99 + }
100 + return links, err
101 + }
102 + err = Descendants(ctx, bestEffortGetLinks, gcs, bestEffortRoots)
103 if err != nil {
104 return nil, err
105 }
@@ -100,7 +108,7 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo
108 gcs.Add(k)
109 }
110
103 - err = Descendants(ctx, ls, gcs, pn.InternalPins(), false)
111 + err = Descendants(ctx, ls.GetLinks, gcs, pn.InternalPins())
112 if err != nil {
113 return nil, err
114 }