Skip to content

Commit

Permalink
Merge pull request ipfs/go-merkledag#43 from ipfs/fix/with-root
Browse files Browse the repository at this point in the history
fix: include root in searches by default

This commit was moved from ipfs/go-merkledag@ea4cf39
  • Loading branch information
Stebalien committed Jul 24, 2019
2 parents e697c7d + 368a8fc commit 123dda8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions ipld/merkledag/merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func FetchGraphWithDepthLimit(ctx context.Context, root cid.Cid, depthLim int, s
// If we have a ProgressTracker, we wrap the visit function to handle it
v, _ := ctx.Value(progressContextKey).(*ProgressTracker)
if v == nil {
return WalkDepth(ctx, GetLinksDirect(ng), root, visit, Concurrent(), WithRoot())
return WalkDepth(ctx, GetLinksDirect(ng), root, visit, Concurrent())
}

visitProgress := func(c cid.Cid, depth int) bool {
Expand All @@ -208,7 +208,7 @@ func FetchGraphWithDepthLimit(ctx context.Context, root cid.Cid, depthLim int, s
}
return false
}
return WalkDepth(ctx, GetLinksDirect(ng), root, visitProgress, Concurrent(), WithRoot())
return WalkDepth(ctx, GetLinksDirect(ng), root, visitProgress, Concurrent())
}

// GetMany gets many nodes from the DAG at once.
Expand Down Expand Up @@ -288,7 +288,7 @@ const defaultConcurrentFetch = 32

// walkOptions represent the parameters of a graph walking algorithm
type walkOptions struct {
WithRoot bool
SkipRoot bool
Concurrency int
ErrorHandler func(c cid.Cid, err error) error
}
Expand All @@ -306,10 +306,10 @@ func (wo *walkOptions) addHandler(handler func(c cid.Cid, err error) error) {
}
}

// WithRoot is a WalkOption indicating that the root node should be visited
func WithRoot() WalkOption {
// SkipRoot is a WalkOption indicating that the root node should skipped
func SkipRoot() WalkOption {
return func(walkOptions *walkOptions) {
walkOptions.WithRoot = true
walkOptions.SkipRoot = true
}
}

Expand Down Expand Up @@ -403,7 +403,7 @@ func WalkDepth(ctx context.Context, getLinks GetLinks, c cid.Cid, visit func(cid
}

func sequentialWalkDepth(ctx context.Context, getLinks GetLinks, root cid.Cid, depth int, visit func(cid.Cid, int) bool, options *walkOptions) error {
if depth != 0 || options.WithRoot {
if !(options.SkipRoot && depth == 0) {
if !visit(root, depth) {
return nil
}
Expand Down Expand Up @@ -484,7 +484,7 @@ func parallelWalkDepth(ctx context.Context, getLinks GetLinks, root cid.Cid, vis
var shouldVisit bool

// bypass the root if needed
if depth != 0 || options.WithRoot {
if !(options.SkipRoot && depth == 0) {
visitlk.Lock()
shouldVisit = visit(ci, depth)
visitlk.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion ipld/merkledag/merkledag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func TestFetchGraphWithDepthLimit(t *testing.T) {

}

err = WalkDepth(context.Background(), offlineDS.GetLinks, root.Cid(), visitF, WithRoot())
err = WalkDepth(context.Background(), offlineDS.GetLinks, root.Cid(), visitF)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 123dda8

Please sign in to comment.