Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
fix(deps): update to latest go fetcher (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Jul 22, 2021
1 parent c9abe36 commit 1b4a2c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ipfs/go-blockservice v0.1.4
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-datastore v0.4.5
github.com/ipfs/go-fetcher v1.2.0
github.com/ipfs/go-fetcher v1.2.1-0.20210410011454-571518e2eca7
github.com/ipfs/go-ipfs-blockstore v0.1.4
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipld-cbor v0.0.3
Expand Down
46 changes: 18 additions & 28 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
"github.com/ipfs/go-blockservice"
cid "github.com/ipfs/go-cid"
"github.com/ipfs/go-fetcher"
ipld "github.com/ipfs/go-ipld-format"
fetcherhelpers "github.com/ipfs/go-fetcher/helpers"
bsfetcher "github.com/ipfs/go-fetcher/impl/blockservice"
format "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
"github.com/ipld/go-ipld-prime"
ipldp "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
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"
)

Expand Down Expand Up @@ -47,12 +49,12 @@ func (e ErrNoLink) Error() string {
// TODO: now that this is more modular, try to unify this code with the
// the resolvers in namesys
type Resolver struct {
FetchConfig fetcher.FetcherConfig
FetchConfig bsfetcher.FetcherConfig
}

// NewBasicResolver constructs a new basic resolver.
func NewBasicResolver(bs blockservice.BlockService) *Resolver {
fc := fetcher.NewFetcherConfig(bs)
fc := bsfetcher.NewFetcherConfig(bs)
return &Resolver{
FetchConfig: fc,
}
Expand All @@ -72,10 +74,7 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (cid.
}

// create a selector to traverse and match all path segments
pathSelector, err := pathAllSelector(p[:len(p)-1])
if err != nil {
return cid.Cid{}, nil, err
}
pathSelector := pathAllSelector(p[:len(p)-1])

// resolve node before last path segment
nodes, lastCid, depth, err := r.resolveNodes(ctx, c, pathSelector)
Expand Down Expand Up @@ -135,10 +134,7 @@ func (r *Resolver) ResolvePath(ctx context.Context, fpath path.Path) (ipldp.Node
}

// create a selector to traverse all path segments but only match the last
pathSelector, err := pathLeafSelector(p)
if err != nil {
return nil, nil, err
}
pathSelector := pathLeafSelector(p)

nodes, c, _, err := r.resolveNodes(ctx, c, pathSelector)
if err != nil {
Expand All @@ -153,7 +149,7 @@ func (r *Resolver) ResolvePath(ctx context.Context, fpath path.Path) (ipldp.Node
// ResolveSingle simply resolves one hop of a path through a graph with no
// extra context (does not opaquely resolve through sharded nodes)
// Deprecated: fetch node as ipld-prime or convert it and then use a selector to traverse through it.
func ResolveSingle(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names []string) (*ipld.Link, []string, error) {
func ResolveSingle(ctx context.Context, ds format.NodeGetter, nd format.Node, names []string) (*format.Link, []string, error) {
return nd.ResolveLink(names)
}

Expand All @@ -175,10 +171,7 @@ func (r *Resolver) ResolvePathComponents(ctx context.Context, fpath path.Path) (
}

// create a selector to traverse all path segments but only match the last
pathSelector, err := pathAllSelector(p)
if err != nil {
return nil, err
}
pathSelector := pathAllSelector(p)

nodes, _, _, err := r.resolveNodes(ctx, c, pathSelector)
return nodes, err
Expand All @@ -197,10 +190,7 @@ func (r *Resolver) ResolveLinks(ctx context.Context, ndd ipldp.Node, names []str
defer evt.Done()

// create a selector to traverse all path segments but only match the last
pathSelector, err := pathAllSelector(names)
if err != nil {
return nil, err
}
pathSelector := pathAllSelector(names)

// create a new cancellable session
ctx, cancel := context.WithTimeout(ctx, time.Minute)
Expand All @@ -210,7 +200,7 @@ func (r *Resolver) ResolveLinks(ctx context.Context, ndd ipldp.Node, names []str

// traverse selector
nodes := []ipldp.Node{ndd}
err = session.NodeMatching(ctx, ndd, pathSelector, func(res fetcher.FetchResult) error {
err := session.NodeMatching(ctx, ndd, pathSelector, func(res fetcher.FetchResult) error {
nodes = append(nodes, res.Node)
return nil
})
Expand All @@ -223,7 +213,7 @@ func (r *Resolver) ResolveLinks(ctx context.Context, ndd ipldp.Node, names []str

// Finds nodes matching the selector starting with a cid. Returns the matched nodes, the cid of the block containing
// the last node, and the depth of the last node within its block (root is depth 0).
func (r *Resolver) resolveNodes(ctx context.Context, c cid.Cid, sel selector.Selector) ([]ipldp.Node, cid.Cid, int, error) {
func (r *Resolver) resolveNodes(ctx context.Context, c cid.Cid, sel ipld.Node) ([]ipldp.Node, cid.Cid, int, error) {
// create a new cancellable session
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
Expand All @@ -234,7 +224,7 @@ func (r *Resolver) resolveNodes(ctx context.Context, c cid.Cid, sel selector.Sel
lastLink := cid.Undef
depth := 0
nodes := []ipldp.Node{}
err := fetcher.BlockMatching(ctx, session, cidlink.Link{c}, sel, func(res fetcher.FetchResult) error {
err := fetcherhelpers.BlockMatching(ctx, session, cidlink.Link{c}, sel, func(res fetcher.FetchResult) error {
if res.LastBlockLink == nil {
res.LastBlockLink = cidlink.Link{c}
}
Expand All @@ -261,14 +251,14 @@ func (r *Resolver) resolveNodes(ctx context.Context, c cid.Cid, sel selector.Sel
return nodes, lastLink, depth, nil
}

func pathLeafSelector(path []string) (selector.Selector, error) {
func pathLeafSelector(path []string) ipld.Node {
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
return pathSelector(path, ssb, func(p string, s builder.SelectorSpec) builder.SelectorSpec {
return ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) { efsb.Insert(p, s) })
})
}

func pathAllSelector(path []string) (selector.Selector, error) {
func pathAllSelector(path []string) ipld.Node {
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
return pathSelector(path, ssb, func(p string, s builder.SelectorSpec) builder.SelectorSpec {
return ssb.ExploreUnion(
Expand All @@ -278,10 +268,10 @@ func pathAllSelector(path []string) (selector.Selector, error) {
})
}

func pathSelector(path []string, ssb builder.SelectorSpecBuilder, reduce func(string, builder.SelectorSpec) builder.SelectorSpec) (selector.Selector, error) {
func pathSelector(path []string, ssb builder.SelectorSpecBuilder, reduce func(string, builder.SelectorSpec) builder.SelectorSpec) ipld.Node {
spec := ssb.Matcher()
for i := len(path) - 1; i >= 0; i-- {
spec = reduce(path[i], spec)
}
return spec.Selector()
return spec.Node()
}

0 comments on commit 1b4a2c3

Please sign in to comment.