Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework the graph walking functions with functional options #42

Merged
merged 3 commits into from
Jul 22, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add a IgnoreErrors() walking option
  • Loading branch information
MichaelMure committed Jul 18, 2019
commit 86e5652465a6e36e6f92ea01d73bebf7eb050aaf
18 changes: 13 additions & 5 deletions merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ const defaultConcurrentFetch = 32

// WalkOptions represent the parameters of a graph walking algorithm
type WalkOptions struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this private (or at least make the fields private). Yeah, I know I don't usually do that but that was a mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

WithRoot bool
IgnoreBadBlock bool
Concurrency int
WithRoot bool
IgnoreErrors bool
Concurrency int
}

// WalkOption is a setter for WalkOptions
Expand Down Expand Up @@ -323,6 +323,14 @@ func Concurrency(worker int) WalkOption {
}
}

// IgnoreErrors is a WalkOption indicating that the walk should attempt to
// continue even when an error occur.
func IgnoreErrors() WalkOption {
return func(walkOptions *WalkOptions) {
walkOptions.IgnoreErrors = true
}
}

// WalkGraph will walk the dag in order (depth first) starting at the given root.
func Walk(ctx context.Context, getLinks GetLinks, c cid.Cid, visit func(cid.Cid) bool, options ...WalkOption) error {
visitDepth := func(c cid.Cid, depth int) bool {
Expand Down Expand Up @@ -356,7 +364,7 @@ func sequentialWalkDepth(ctx context.Context, getLinks GetLinks, root cid.Cid, d
}

links, err := getLinks(ctx, root)
if err != nil {
if err != nil && !options.IgnoreErrors {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the idea is that getLinks should this internally. But I agree this option is convenient.

What if we changed this to an error handler (func(cid.Cid, error) error)? That way, we can provide options like:

IgnoreErrors()
IgnoreMissing() // only ignores ipld.ErrorNotFound.
OnMissing(func(c cid.Cid))

Copy link
Contributor Author

@MichaelMure MichaelMure Jul 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I'll add a generic OnError(func(err error, c cid.Cid) bool) as well.

return err
}

Expand Down Expand Up @@ -437,7 +445,7 @@ func parallelWalkDepth(ctx context.Context, getLinks GetLinks, root cid.Cid, vis

if shouldVisit {
links, err := getLinks(ctx, ci)
if err != nil {
if err != nil && !options.IgnoreErrors {
select {
case errChan <- err:
case <-fetchersCtx.Done():
Expand Down