Skip to content

Commit

Permalink
Merge pull request ipfs/go-fetcher#24 from ipfs/fix/selector-helpers
Browse files Browse the repository at this point in the history
Fix selector helpers

This commit was moved from ipfs/go-fetcher@86467b9
  • Loading branch information
warpfork committed Aug 16, 2021
2 parents 49cba97 + d38d37b commit 3fcf467
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
34 changes: 13 additions & 21 deletions fetcher/helpers/traversal.go
Expand Up @@ -5,10 +5,21 @@ import (

"github.com/ipfs/go-fetcher"
"github.com/ipld/go-ipld-prime"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
)

var matchAllSelector ipld.Node

func init() {
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
matchAllSelector = ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreUnion(
ssb.Matcher(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge()),
)).Node()
}

// Block fetches a schemaless node graph corresponding to single block by link.
func Block(ctx context.Context, f fetcher.Fetcher, link ipld.Link) (ipld.Node, error) {
prototype, err := f.PrototypeFromLink(link)
Expand All @@ -21,30 +32,11 @@ func Block(ctx context.Context, f fetcher.Fetcher, link ipld.Link) (ipld.Node, e
// BlockMatching traverses a schemaless node graph starting with the given link using the given selector and possibly crossing
// block boundaries. Each matched node is sent to the FetchResult channel.
func BlockMatching(ctx context.Context, f fetcher.Fetcher, root ipld.Link, match ipld.Node, cb fetcher.FetchCallback) error {
prototype, err := f.PrototypeFromLink(root)
if err != nil {
return err
}
return f.BlockMatchingOfType(ctx, root, match, prototype, cb)
return f.BlockMatchingOfType(ctx, root, match, nil, cb)
}

// BlockAll traverses all nodes in the graph linked by root. The nodes will be untyped and send over the results
// channel.
func BlockAll(ctx context.Context, f fetcher.Fetcher, root ipld.Link, cb fetcher.FetchCallback) error {
prototype, err := f.PrototypeFromLink(root)
if err != nil {
return err
}
return BlockAllOfType(ctx, f, root, prototype, cb)
}

// BlockAllOfType traverses all nodes in the graph linked by root. The nodes will typed according to ptype
// and send over the results channel.
func BlockAllOfType(ctx context.Context, f fetcher.Fetcher, root ipld.Link, ptype ipld.NodePrototype, cb fetcher.FetchCallback) error {
ssb := builder.NewSelectorSpecBuilder(ptype)
allSelector := ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreUnion(
ssb.Matcher(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge()),
)).Node()
return f.BlockMatchingOfType(ctx, root, allSelector, ptype, cb)
return f.BlockMatchingOfType(ctx, root, matchAllSelector, nil, cb)
}
8 changes: 6 additions & 2 deletions fetcher/impl/blockservice/fetcher.go
Expand Up @@ -97,10 +97,14 @@ func (f *fetcherSession) NodeMatching(ctx context.Context, node ipld.Node, match
}

func (f *fetcherSession) BlockMatchingOfType(ctx context.Context, root ipld.Link, match ipld.Node,
ptype ipld.NodePrototype, cb fetcher.FetchCallback) error {
_ ipld.NodePrototype, cb fetcher.FetchCallback) error {

// retrieve first node
node, err := f.BlockOfType(ctx, root, ptype)
prototype, err := f.PrototypeFromLink(root)
if err != nil {
return err
}
node, err := f.BlockOfType(ctx, root, prototype)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion fetcher/impl/blockservice/fetcher_test.go
Expand Up @@ -269,7 +269,7 @@ func TestHelpers(t *testing.T) {
defer cancel()

results := []fetcher.FetchResult{}
err = helpers.BlockAllOfType(ctx, session, cidlink.Link{Cid: block1.Cid()}, basicnode.Prototype__Any{}, func(res fetcher.FetchResult) error {
err = helpers.BlockAll(ctx, session, cidlink.Link{Cid: block1.Cid()}, func(res fetcher.FetchResult) error {
results = append(results, res)
return nil
})
Expand Down

0 comments on commit 3fcf467

Please sign in to comment.