@cryptotaxi247 / kubo / commits / 772164cc7

Fix EnumerateChildren & hasChild to take a *cid.Cid instead of []*mdag.Link

Author: Kevin Atkinson <k@kevina.org> Fix EnumerateChildren & hasChild to take a *cid.Cid instead of []*mdag.Link Author: Jeromy Johnson <why@ipfs.io> make FetchGraph use a cid pin: fix TestPinRecursiveFail License: MIT Signed-off-by: Jeromy <why@ipfs.io> License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Oct 3, 2016 at 21:38 UTC 772164cc7a792498ed8da1b31756420a9cbff95c
8 files changed +37 -51
core/commands/dht.go
+1 -5
@@ -370,12 +370,8 @@ func provideKeysRec(ctx context.Context, r routing.IpfsRouting, dserv dag.DAGSer
370 provided := cid.NewSet()
371 for _, c := range cids {
372 kset := cid.NewSet()
373 - node, err := dserv.Get(ctx, c)
374 - if err != nil {
375 - return err
376 - }
373
378 - err = dag.EnumerateChildrenAsync(ctx, dserv, node, kset.Visit)
374 + err := dag.EnumerateChildrenAsync(ctx, dserv, c, kset.Visit)
375 if err != nil {
376 return err
377 }
core/commands/pin.go
+1 -5
@@ -328,11 +328,7 @@ func pinLsAll(typeStr string, ctx context.Context, n *core.IpfsNode) (map[string
328 if typeStr == "indirect" || typeStr == "all" {
329 set := cid.NewSet()
330 for _, k := range n.Pinning.RecursiveKeys() {
331 - links, err := n.DAG.GetLinks(ctx, k)
332 - if err != nil {
333 - return nil, err
334 - }
335 - err = dag.EnumerateChildren(n.Context(), n.DAG, links, set.Visit, false)
331 + err := dag.EnumerateChildren(n.Context(), n.DAG, k, set.Visit, false)
332 if err != nil {
333 return nil, err
334 }
core/coreunix/add_test.go
+1 -5
@@ -156,13 +156,9 @@ func TestAddGCLive(t *testing.T) {
156
157 ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
158 defer cancel()
159 - root, err := node.DAG.Get(ctx, last)
160 - if err != nil {
161 - t.Fatal(err)
162 - }
159
160 set := cid.NewSet()
165 - err = dag.EnumerateChildren(ctx, node.DAG, root.Links, set.Visit, false)
161 + err = dag.EnumerateChildren(ctx, node.DAG, last, set.Visit, false)
162 if err != nil {
163 t.Fatal(err)
164 }
merkledag/merkledag.go
+16 -13
@@ -126,8 +126,8 @@ func (n *dagService) Remove(nd *Node) error {
126 }
127
128 // FetchGraph fetches all nodes that are children of the given node
129 -func FetchGraph(ctx context.Context, root *Node, serv DAGService) error {
130 - return EnumerateChildrenAsync(ctx, serv, root, cid.NewSet().Visit)
129 +func FetchGraph(ctx context.Context, c *cid.Cid, serv DAGService) error {
130 + return EnumerateChildrenAsync(ctx, serv, c, cid.NewSet().Visit)
131 }
132
133 // FindLinks searches this nodes links for the given key,
@@ -394,19 +394,17 @@ func legacyCidFromLink(lnk *Link) *cid.Cid {
394 // EnumerateChildren will walk the dag below the given root node and add all
395 // unseen children to the passed in set.
396 // TODO: parallelize to avoid disk latency perf hits?
397 -func EnumerateChildren(ctx context.Context, ds LinkService, links []*Link, visit func(*cid.Cid) bool, bestEffort bool) error {
397 +func EnumerateChildren(ctx context.Context, ds LinkService, root *cid.Cid, visit func(*cid.Cid) bool, bestEffort bool) error {
398 + links, err := ds.GetLinks(ctx, root)
399 + if bestEffort && err == ErrNotFound {
400 + return nil
401 + } else if err != nil {
402 + return err
403 + }
404 for _, lnk := range links {
405 c := legacyCidFromLink(lnk)
406 if visit(c) {
401 - children, err := ds.GetLinks(ctx, c)
402 - if err != nil {
403 - if bestEffort && err == ErrNotFound {
404 - continue
405 - } else {
406 - return err
407 - }
408 - }
409 - err = EnumerateChildren(ctx, ds, children, visit, bestEffort)
407 + err = EnumerateChildren(ctx, ds, c, visit, bestEffort)
408 if err != nil {
409 return err
410 }
@@ -415,7 +413,7 @@ func EnumerateChildren(ctx context.Context, ds LinkService, links []*Link, visit
413 return nil
414 }
415
418 -func EnumerateChildrenAsync(ctx context.Context, ds DAGService, root *Node, visit func(*cid.Cid) bool) error {
416 +func EnumerateChildrenAsync(ctx context.Context, ds DAGService, c *cid.Cid, visit func(*cid.Cid) bool) error {
417 toprocess := make(chan []*cid.Cid, 8)
418 nodes := make(chan *NodeOption, 8)
419
@@ -425,6 +423,11 @@ func EnumerateChildrenAsync(ctx context.Context, ds DAGService, root *Node, visi
423
424 go fetchNodes(ctx, ds, toprocess, nodes)
425
426 + root, err := ds.Get(ctx, c)
427 + if err != nil {
428 + return err
429 + }
430 +
431 nodes <- &NodeOption{Node: root}
432 live := 1
433
merkledag/merkledag_test.go
+4 -4
@@ -231,7 +231,7 @@ func TestFetchGraph(t *testing.T) {
231 t.Fatal(err)
232 }
233
234 - err = FetchGraph(context.TODO(), root, dservs[1])
234 + err = FetchGraph(context.TODO(), root.Cid(), dservs[1])
235 if err != nil {
236 t.Fatal(err)
237 }
@@ -241,7 +241,7 @@ func TestFetchGraph(t *testing.T) {
241
242 offline_ds := NewDAGService(bs)
243
244 - err = EnumerateChildren(context.Background(), offline_ds, root.Links, func(_ *cid.Cid) bool { return true }, false)
244 + err = EnumerateChildren(context.Background(), offline_ds, root.Cid(), func(_ *cid.Cid) bool { return true }, false)
245 if err != nil {
246 t.Fatal(err)
247 }
@@ -258,7 +258,7 @@ func TestEnumerateChildren(t *testing.T) {
258 }
259
260 set := cid.NewSet()
261 - err = EnumerateChildren(context.Background(), ds, root.Links, set.Visit, false)
261 + err = EnumerateChildren(context.Background(), ds, root.Cid(), set.Visit, false)
262 if err != nil {
263 t.Fatal(err)
264 }
@@ -269,7 +269,7 @@ func TestEnumerateChildren(t *testing.T) {
269 for _, lnk := range n.Links {
270 c := cid.NewCidV0(lnk.Hash)
271 if !set.Has(c) {
272 - t.Fatal("missing key in set!")
272 + t.Fatal("missing key in set! ", lnk.Hash.B58String())
273 }
274 child, err := ds.Get(context.Background(), c)
275 if err != nil {
pin/gc/gc.go
+1 -5
@@ -71,13 +71,9 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin.
71 func Descendants(ctx context.Context, ls dag.LinkService, set key.KeySet, roots []*cid.Cid, bestEffort bool) error {
72 for _, c := range roots {
73 set.Add(key.Key(c.Hash()))
74 - links, err := ls.GetLinks(ctx, c)
75 - if err != nil {
76 - return err
77 - }
74
75 // EnumerateChildren recursively walks the dag and adds the keys to the given set
80 - err = dag.EnumerateChildren(ctx, ls, links, func(c *cid.Cid) bool {
76 + err := dag.EnumerateChildren(ctx, ls, c, func(c *cid.Cid) bool {
77 k := key.Key(c.Hash())
78 seen := set.Has(k)
79 if seen {
pin/pin.go
+8 -14
@@ -178,7 +178,7 @@ func (p *pinner) Pin(ctx context.Context, node *mdag.Node, recurse bool) error {
178 }
179
180 // fetch entire graph
181 - err := mdag.FetchGraph(ctx, node, p.dserv)
181 + err := mdag.FetchGraph(ctx, c, p.dserv)
182 if err != nil {
183 return err
184 }
@@ -279,12 +279,7 @@ func (p *pinner) isPinnedWithType(c *cid.Cid, mode PinMode) (string, bool, error
279
280 // Default is Indirect
281 for _, rc := range p.recursePin.Keys() {
282 - links, err := p.dserv.GetLinks(context.Background(), rc)
283 - if err != nil {
284 - return "", false, err
285 - }
286 -
287 - has, err := hasChild(p.dserv, links, k)
282 + has, err := hasChild(p.dserv, rc, k)
283 if err != nil {
284 return "", false, err
285 }
@@ -521,19 +516,18 @@ func (p *pinner) PinWithMode(c *cid.Cid, mode PinMode) {
516 }
517 }
518
524 -func hasChild(ds mdag.LinkService, links []*mdag.Link, child key.Key) (bool, error) {
519 +func hasChild(ds mdag.LinkService, root *cid.Cid, child key.Key) (bool, error) {
520 + links, err := ds.GetLinks(context.Background(), root)
521 + if err != nil {
522 + return false, err
523 + }
524 for _, lnk := range links {
525 c := cid.NewCidV0(lnk.Hash)
526 if key.Key(c.Hash()) == child {
527 return true, nil
528 }
529
531 - children, err := ds.GetLinks(context.Background(), c)
532 - if err != nil {
533 - return false, err
534 - }
535 -
536 - has, err := hasChild(ds, children, child)
530 + has, err := hasChild(ds, c, child)
531 if err != nil {
532 return false, err
533 }
pin/pin_test.go
+5
@@ -225,6 +225,11 @@ func TestPinRecursiveFail(t *testing.T) {
225 t.Fatal(err)
226 }
227
228 + _, err = dserv.Add(a)
229 + if err != nil {
230 + t.Fatal(err)
231 + }
232 +
233 // this one is time based... but shouldnt cause any issues
234 mctx, _ = context.WithTimeout(ctx, time.Second)
235 err = p.Pin(mctx, a, true)