-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose basic text-based datamodel selector on retrieval
Syntaxt of selection is located at https://pkg.go.dev/github.com/ipld/go-ipld-selector-text-lite#SelectorSpecFromPath Example use, assuming that: - The root of the deal is a plain dag-pb unixfs directory - The directory is not sharded - The user wants to retrieve the first entry in that directory lotus client retrieve --miner f0XXXXX --datamodel-path-selector 'Links/0/Hash' bafyROOTCID ~/output
- Loading branch information
Showing
6 changed files
with
170 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
// must be imported to init() raw-codec support | ||
_ "github.com/ipld/go-ipld-prime/codec/raw" | ||
|
||
"github.com/ipfs/go-cid" | ||
mdagipld "github.com/ipfs/go-ipld-format" | ||
dagpb "github.com/ipld/go-codec-dagpb" | ||
"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" | ||
"github.com/ipld/go-ipld-prime/traversal/selector" | ||
"github.com/ipld/go-ipld-prime/traversal/selector/builder" | ||
) | ||
|
||
func TraverseDag( | ||
ctx context.Context, | ||
ds mdagipld.DAGService, | ||
startFrom cid.Cid, | ||
optionalSelector ipld.Node, | ||
visitCallback traversal.AdvVisitFn, | ||
) error { | ||
|
||
// If no selector is given - use *.* | ||
// See discusion at https://github.com/ipld/go-ipld-prime/issues/171 | ||
if optionalSelector == nil { | ||
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any) | ||
optionalSelector = ssb.ExploreRecursive( | ||
selector.RecursionLimitNone(), | ||
ssb.ExploreUnion( | ||
ssb.Matcher(), | ||
ssb.ExploreAll(ssb.ExploreRecursiveEdge()), | ||
), | ||
).Node() | ||
} | ||
|
||
parsedSelector, err := selector.ParseSelector(optionalSelector) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// not sure what this is for TBH... | ||
linkContext := ipld.LinkContext{Ctx: ctx} | ||
|
||
// this is what allows us to understand dagpb | ||
nodePrototypeChooser := dagpb.AddSupportToChooser( | ||
func(ipld.Link, ipld.LinkContext) (ipld.NodePrototype, error) { | ||
return basicnode.Prototype.Any, nil | ||
}, | ||
) | ||
|
||
// this is how we implement GETs | ||
linkSystem := cidlink.DefaultLinkSystem() | ||
linkSystem.StorageReadOpener = func(_ ipld.LinkContext, lnk ipld.Link) (io.Reader, error) { | ||
if cl, isCid := lnk.(cidlink.Link); !isCid { | ||
return nil, fmt.Errorf("unexpected link type %#v", lnk) | ||
} else { | ||
node, err := ds.Get(context.TODO(), cl.Cid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes.NewBuffer(node.RawData()), nil | ||
} | ||
} | ||
|
||
// this is how we pull the start node out of the DS | ||
startLink := cidlink.Link{Cid: startFrom} | ||
startNodePrototype, err := nodePrototypeChooser(startLink, linkContext) | ||
if err != nil { | ||
return err | ||
} | ||
startNode, err := linkSystem.Load( | ||
linkContext, | ||
startLink, | ||
startNodePrototype, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// this is the actual execution, invoking the supplied callback | ||
return traversal.Progress{ | ||
Cfg: &traversal.Config{ | ||
Ctx: ctx, | ||
LinkSystem: linkSystem, | ||
LinkTargetNodePrototypeChooser: nodePrototypeChooser, | ||
}, | ||
}.WalkAdv(startNode, parsedSelector, visitCallback) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters