Skip to content

Commit

Permalink
reuse walkGatewaySimpleSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Mar 25, 2024
1 parent 55f0d3a commit 2f7a31f
Show file tree
Hide file tree
Showing 3 changed files with 1,237 additions and 1,418 deletions.
53 changes: 31 additions & 22 deletions gateway/backend_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (bb *BlocksBackend) GetCAR(ctx context.Context, p path.ImmutablePath, param

// TODO: support selectors passed as request param: https://github.com/ipfs/kubo/issues/8769
// TODO: this is very slow if blocks are remote due to linear traversal. Do we need deterministic traversals here?
carWriteErr := walkGatewaySimpleSelector(ctx, lastCid, remainder, params, &lsys)
carWriteErr := walkGatewaySimpleSelector(ctx, lastCid, nil, remainder, params, &lsys)

// io.PipeWriter.CloseWithError always returns nil.
_ = w.CloseWithError(carWriteErr)
Expand All @@ -361,7 +361,7 @@ func (bb *BlocksBackend) GetCAR(ctx context.Context, p path.ImmutablePath, param
}

// walkGatewaySimpleSelector walks the subgraph described by the path and terminal element parameters
func walkGatewaySimpleSelector(ctx context.Context, lastCid cid.Cid, remainder []string, params CarParams, lsys *ipld.LinkSystem) error {
func walkGatewaySimpleSelector(ctx context.Context, lastCid cid.Cid, terminalBlk blocks.Block, remainder []string, params CarParams, lsys *ipld.LinkSystem) error {
lctx := ipld.LinkContext{Ctx: ctx}
pathTerminalCidLink := cidlink.Link{Cid: lastCid}

Expand All @@ -371,13 +371,39 @@ func walkGatewaySimpleSelector(ctx context.Context, lastCid cid.Cid, remainder [
return err
}

// If we're asking for everything then give it
if params.Scope == DagScopeAll {
lastCidNode, err := lsys.Load(lctx, pathTerminalCidLink, basicnode.Prototype.Any)
pc := dagpb.AddSupportToChooser(func(lnk ipld.Link, lnkCtx ipld.LinkContext) (ipld.NodePrototype, error) {
if tlnkNd, ok := lnkCtx.LinkNode.(schema.TypedLinkNode); ok {
return tlnkNd.LinkTargetNodePrototype(), nil
}
return basicnode.Prototype.Any, nil
})

np, err := pc(pathTerminalCidLink, lctx)
if err != nil {
return err
}

var lastCidNode datamodel.Node
if terminalBlk != nil {
decoder, err := lsys.DecoderChooser(pathTerminalCidLink)
if err != nil {
return err
}
nb := np.NewBuilder()
blockData := terminalBlk.RawData()
if err := decoder(nb, bytes.NewReader(blockData)); err != nil {
return err
}
lastCidNode = nb.Build()
} else {
lastCidNode, err = lsys.Load(lctx, pathTerminalCidLink, np)
if err != nil {
return err
}
}

// If we're asking for everything then give it
if params.Scope == DagScopeAll {
sel, err := selector.ParseSelector(selectorparse.CommonSelector_ExploreAllRecursively)
if err != nil {
return err
Expand All @@ -403,23 +429,6 @@ func walkGatewaySimpleSelector(ctx context.Context, lastCid cid.Cid, remainder [
// From now on, dag-scope=entity!
// Since we need more of the graph load it to figure out what we have
// This includes determining if the terminal node is UnixFS or not
pc := dagpb.AddSupportToChooser(func(lnk ipld.Link, lnkCtx ipld.LinkContext) (ipld.NodePrototype, error) {
if tlnkNd, ok := lnkCtx.LinkNode.(schema.TypedLinkNode); ok {
return tlnkNd.LinkTargetNodePrototype(), nil
}
return basicnode.Prototype.Any, nil
})

np, err := pc(pathTerminalCidLink, lctx)
if err != nil {
return err
}

lastCidNode, err := lsys.Load(lctx, pathTerminalCidLink, np)
if err != nil {
return err
}

if pbn, ok := lastCidNode.(dagpb.PBNode); !ok {
// If it's not valid dag-pb then we're done
return nil
Expand Down
Loading

0 comments on commit 2f7a31f

Please sign in to comment.