Unverified Commit ea4cf399 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #43 from ipfs/fix/with-root

fix: include root in searches by default
parents 89dd2ad2 f457eb47
...@@ -198,7 +198,7 @@ func FetchGraphWithDepthLimit(ctx context.Context, root cid.Cid, depthLim int, s ...@@ -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 // If we have a ProgressTracker, we wrap the visit function to handle it
v, _ := ctx.Value(progressContextKey).(*ProgressTracker) v, _ := ctx.Value(progressContextKey).(*ProgressTracker)
if v == nil { 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 { visitProgress := func(c cid.Cid, depth int) bool {
...@@ -208,7 +208,7 @@ func FetchGraphWithDepthLimit(ctx context.Context, root cid.Cid, depthLim int, s ...@@ -208,7 +208,7 @@ func FetchGraphWithDepthLimit(ctx context.Context, root cid.Cid, depthLim int, s
} }
return false 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. // GetMany gets many nodes from the DAG at once.
...@@ -288,7 +288,7 @@ const defaultConcurrentFetch = 32 ...@@ -288,7 +288,7 @@ const defaultConcurrentFetch = 32
// walkOptions represent the parameters of a graph walking algorithm // walkOptions represent the parameters of a graph walking algorithm
type walkOptions struct { type walkOptions struct {
WithRoot bool SkipRoot bool
Concurrency int Concurrency int
ErrorHandler func(c cid.Cid, err error) error ErrorHandler func(c cid.Cid, err error) error
} }
...@@ -306,10 +306,10 @@ func (wo *walkOptions) addHandler(handler func(c cid.Cid, err error) error) { ...@@ -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 // SkipRoot is a WalkOption indicating that the root node should skipped
func WithRoot() WalkOption { func SkipRoot() WalkOption {
return func(walkOptions *walkOptions) { return func(walkOptions *walkOptions) {
walkOptions.WithRoot = true walkOptions.SkipRoot = true
} }
} }
...@@ -403,7 +403,7 @@ func WalkDepth(ctx context.Context, getLinks GetLinks, c cid.Cid, visit func(cid ...@@ -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 { 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) { if !visit(root, depth) {
return nil return nil
} }
...@@ -484,7 +484,7 @@ func parallelWalkDepth(ctx context.Context, getLinks GetLinks, root cid.Cid, vis ...@@ -484,7 +484,7 @@ func parallelWalkDepth(ctx context.Context, getLinks GetLinks, root cid.Cid, vis
var shouldVisit bool var shouldVisit bool
// bypass the root if needed // bypass the root if needed
if depth != 0 || options.WithRoot { if !(options.SkipRoot && depth == 0) {
visitlk.Lock() visitlk.Lock()
shouldVisit = visit(ci, depth) shouldVisit = visit(ci, depth)
visitlk.Unlock() visitlk.Unlock()
......
...@@ -386,7 +386,7 @@ func TestFetchGraphWithDepthLimit(t *testing.T) { ...@@ -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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment