From 289629653f40e30327932a43cc5088071699b70d Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Fri, 25 Feb 2022 10:04:05 +0100 Subject: [PATCH 1/2] Resolver: convert to interface. 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. --- resolver/resolver.go | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/resolver/resolver.go b/resolver/resolver.go index 47dd470..55c322a 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -41,25 +41,42 @@ func (e ErrNoLink) Error() string { return fmt.Sprintf("no link named %q under %s", e.Name, e.Node.String()) } -// Resolver provides path resolution to IPFS +// Resolver provides path resolution to IPFS. +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. // 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 { +// the resolvers in namesys. +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 @@ -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 @@ -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() @@ -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() @@ -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 From 29ae0276833f231c99258840ec806f06964e082e Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Wed, 23 Mar 2022 19:21:31 +0100 Subject: [PATCH 2/2] Resolver: unexport BasicResover --- resolver/resolver.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/resolver/resolver.go b/resolver/resolver.go index 55c322a..64778c1 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -58,17 +58,17 @@ type Resolver interface { ResolvePathComponents(ctx context.Context, fpath path.Path) ([]ipld.Node, error) } -// BasicResolver implements the Resolver interface. +// basicResolver implements the Resolver interface. // 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 BasicResolver struct { +type basicResolver struct { FetcherFactory fetcher.Factory } // NewBasicResolver constructs a new basic resolver. func NewBasicResolver(fetcherFactory fetcher.Factory) Resolver { - return &BasicResolver{ + return &basicResolver{ FetcherFactory: fetcherFactory, } } @@ -76,7 +76,7 @@ func NewBasicResolver(fetcherFactory fetcher.Factory) Resolver { // 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) { +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 @@ -142,7 +142,7 @@ func (r *BasicResolver) ResolveToLastNode(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 *BasicResolver) 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 @@ -179,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 *BasicResolver) 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() @@ -217,7 +217,7 @@ func (r *BasicResolver) ResolvePathComponents(ctx context.Context, fpath path.Pa // // 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 *BasicResolver) 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() @@ -243,7 +243,7 @@ func (r *BasicResolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names [ // 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 *BasicResolver) 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