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

Commit

Permalink
Resolver: convert to interface.
Browse files Browse the repository at this point in the history
This converts Resolver to an interface and leaves its implementation to the
BasicResolver type.

This should cause minimal distruption upstream (*Resolver -> Resolver) and
opens the door to swap the BasicResolver for custom Resolvers.
  • Loading branch information
hsanjuan committed Feb 25, 2022
1 parent f4c3aaa commit 27fabe4
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,38 @@ func (e ErrNoLink) Error() string {
// It references a FetcherFactory, which is uses to resolve nodes.
// TODO: now that this is more modular, try to unify this code with the
// the resolvers in namesys
type Resolver struct {
type Resolver interface {
// ResolveToLastNode walks the given path and returns the cid of the
// last block referenced by the path, and the path segments to
// traverse from the final block boundary to the final node within the
// block.
ResolveToLastNode(ctx context.Context, fpath path.Path) (cid.Cid, []string, error)
// ResolvePath fetches the node for given path. It returns the last
// item returned by ResolvePathComponents and the last link traversed
// which can be used to recover the block.
ResolvePath(ctx context.Context, fpath path.Path) (ipld.Node, ipld.Link, error)
// ResolvePathComponents fetches the nodes for each segment of the given path.
// It uses the first path component as a hash (key) of the first node, then
// resolves all other components walking the links via a selector traversal
ResolvePathComponents(ctx context.Context, fpath path.Path) ([]ipld.Node, error)
}

// BasicResolver implements the Resolver interface.
type BasicResolver struct {
FetcherFactory fetcher.Factory
}

// NewBasicResolver constructs a new basic resolver.
func NewBasicResolver(fetcherFactory fetcher.Factory) *Resolver {
return &Resolver{
func NewBasicResolver(fetcherFactory fetcher.Factory) Resolver {
return &BasicResolver{
FetcherFactory: fetcherFactory,
}
}

// ResolveToLastNode walks the given path and returns the cid of the last block
// referenced by the path, and the path segments to traverse from the final block boundary to the final node
// within the block.
func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (cid.Cid, []string, error) {
// ResolveToLastNode walks the given path and returns the cid of the last
// block referenced by the path, and the path segments to traverse from the
// final block boundary to the final node within the block.
func (r *BasicResolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (cid.Cid, []string, error) {
c, p, err := path.SplitAbsPath(fpath)
if err != nil {
return cid.Cid{}, nil, err
Expand Down Expand Up @@ -125,7 +142,7 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (cid.
//
// Note: if/when the context is cancelled or expires then if a multi-block ADL node is returned then it may not be
// possible to load certain values.
func (r *Resolver) ResolvePath(ctx context.Context, fpath path.Path) (ipld.Node, ipld.Link, error) {
func (r *BasicResolver) ResolvePath(ctx context.Context, fpath path.Path) (ipld.Node, ipld.Link, error) {
// validate path
if err := fpath.IsValid(); err != nil {
return nil, nil, err
Expand Down Expand Up @@ -162,7 +179,7 @@ func ResolveSingle(ctx context.Context, ds format.NodeGetter, nd format.Node, na
//
// Note: if/when the context is cancelled or expires then if a multi-block ADL node is returned then it may not be
// possible to load certain values.
func (r *Resolver) ResolvePathComponents(ctx context.Context, fpath path.Path) ([]ipld.Node, error) {
func (r *BasicResolver) ResolvePathComponents(ctx context.Context, fpath path.Path) ([]ipld.Node, error) {
//lint:ignore SA1019 TODO: replace EventBegin
evt := log.EventBegin(ctx, "resolvePathComponents", logging.LoggableMap{"fpath": fpath})
defer evt.Done()
Expand Down Expand Up @@ -200,7 +217,7 @@ func (r *Resolver) ResolvePathComponents(ctx context.Context, fpath path.Path) (
//
// Note: if/when the context is cancelled or expires then if a multi-block ADL node is returned then it may not be
// possible to load certain values.
func (r *Resolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names []string) ([]ipld.Node, error) {
func (r *BasicResolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names []string) ([]ipld.Node, error) {
//lint:ignore SA1019 TODO: replace EventBegin
evt := log.EventBegin(ctx, "resolveLinks", logging.LoggableMap{"names": names})
defer evt.Done()
Expand All @@ -226,7 +243,7 @@ func (r *Resolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names []stri

// 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 ipld.Node) ([]ipld.Node, cid.Cid, int, error) {
func (r *BasicResolver) resolveNodes(ctx context.Context, c cid.Cid, sel ipld.Node) ([]ipld.Node, cid.Cid, int, error) {
session := r.FetcherFactory.NewSession(ctx)

// traverse selector
Expand Down

0 comments on commit 27fabe4

Please sign in to comment.