Skip to content

Commit

Permalink
add AbortableScanView function (#23245)
Browse files Browse the repository at this point in the history
  • Loading branch information
rculpepper authored Sep 22, 2023
1 parent a074bf9 commit 68dd82c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions sdk/logical/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ func ScanView(ctx context.Context, view ClearableView, cb func(path string)) err
return nil
}

// AbortableScanView is used to scan all the keys in a view iteratively,
// but will abort the scan if cb returns false
func AbortableScanView(ctx context.Context, view ClearableView, cb func(path string) (cont bool)) error {
frontier := []string{""}
for len(frontier) > 0 {
n := len(frontier)
current := frontier[n-1]
frontier = frontier[:n-1]

// List the contents
contents, err := view.List(ctx, current)
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("list failed at path %q: {{err}}", current), err)
}

// Handle the contents in the directory
for _, c := range contents {
// Exit if the context has been canceled
if ctx.Err() != nil {
return ctx.Err()
}
fullPath := current + c
if strings.HasSuffix(c, "/") {
frontier = append(frontier, fullPath)
} else {
if !cb(fullPath) {
return nil
}
}
}
}
return nil
}

// CollectKeys is used to collect all the keys in a view
func CollectKeys(ctx context.Context, view ClearableView) ([]string, error) {
return CollectKeysWithPrefix(ctx, view, "")
Expand Down

0 comments on commit 68dd82c

Please sign in to comment.