Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
fix walk func
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Nov 13, 2020
1 parent ce846a1 commit e94c5ab
Showing 1 changed file with 11 additions and 33 deletions.
44 changes: 11 additions & 33 deletions ipld/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package ipld

import (
"context"
"errors"

"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"

Expand Down Expand Up @@ -57,49 +55,33 @@ func Walk(ctx context.Context, id cid.Cid, bs blockstream.BlockStreamer, handler
in <- []cid.Cid{id}
defer close(in)

out, cherr := bs.Stream(ctx, in)
out, errCh := bs.Stream(ctx, in)
for {
select {
case b, ok := <-out:
if !ok {
return errors.New("stream channel closed")
}
case b := <-out:
remains--

nd, err := format.Decode(b)
if err != nil {
return err
}

shouldHandle := true
if nd.Cid().Type() != cid.Raw {
shouldHandle, err = wo.visit(nd.Cid())
if err != nil {
return err
}
}

if shouldHandle {
err = wo.handle(nd)
if err != nil {
return err
}
err = wo.handle(nd)
if err != nil {
return err
}

ids := make([]cid.Cid, 0, len(nd.Links()))
for _, l := range nd.Links() {
if l.Cid.Type() != cid.Raw {
ids = append(ids, l.Cid)
continue
}

v, err := wo.visit(l.Cid)
if err != nil {
return err
}
if v {
ids = append(ids, l.Cid)
if !v {
continue
}

ids = append(ids, l.Cid)
}

if len(ids) == 0 {
Expand All @@ -114,15 +96,11 @@ func Walk(ctx context.Context, id cid.Cid, bs blockstream.BlockStreamer, handler
case in <- ids:
case <-ctx.Done():
return ctx.Err()
case err := <-cherr:
cancel()
return err
}
case err := <-errCh:
return err
case <-ctx.Done():
return ctx.Err()
case err := <-cherr:
cancel()
return err
}
}
}
Expand Down

0 comments on commit e94c5ab

Please sign in to comment.