11package unixfsnode
22
33import (
4+ "io"
5+
46 "github.com/ipld/go-ipld-prime"
57 "github.com/ipld/go-ipld-prime/datamodel"
68 "github.com/ipld/go-ipld-prime/linking"
79 "github.com/ipld/go-ipld-prime/node/basicnode"
10+ "github.com/ipld/go-ipld-prime/traversal"
811 "github.com/ipld/go-ipld-prime/traversal/selector"
912 "github.com/ipld/go-ipld-prime/traversal/selector/builder"
1013)
@@ -26,10 +29,30 @@ var ExploreAllRecursivelySelector = specBuilder(func(ssb builder.SelectorSpecBui
2629// not, but not its contents.
2730// MatchUnixfsPreloadSelector is precompiled for use with
2831// UnixFSPathSelectorBuilder().
32+ //
33+ // NOTE: This selector may be deprecated in a future release. Users should
34+ // instead use MatchUnixFSEntitySelector instead, which is intended to have the
35+ // same effect but doesn't use the "unixfs-preload" ADL.
2936var MatchUnixFSPreloadSelector = specBuilder (func (ssb builder.SelectorSpecBuilder ) builder.SelectorSpec {
3037 return ssb .ExploreInterpretAs ("unixfs-preload" , ssb .Matcher ())
3138})
3239
40+ // MatchUnixFSEntitySelector is a selector that will match a single node and its
41+ // direct children.
42+ //
43+ // For UnixFS files, this will match the file and its blocks.
44+ //
45+ // For UnixFS directories, and will iterate through the list of child links but
46+ // will not iterate _into_ the child directories.
47+ var MatchUnixFSEntitySelector = specBuilder (func (ssb builder.SelectorSpecBuilder ) builder.SelectorSpec {
48+ return ssb .ExploreInterpretAs ("unixfs" , ssb .ExploreUnion (ssb .Matcher (),
49+ ssb .ExploreRecursive (
50+ selector .RecursionLimitDepth (1 ),
51+ ssb .ExploreAll (ssb .ExploreRecursiveEdge ()),
52+ ),
53+ ))
54+ })
55+
3356// MatchUnixFSSelector is a selector that will match a single node, similar to
3457// selectorparse.CommonSelector_MatchPoint, but uses the "unixfs" ADL to load
3558// as UnixFS data. Unlike MatchUnixFSPreloadSelector, this selector will not
@@ -40,6 +63,26 @@ var MatchUnixFSSelector = specBuilder(func(ssb builder.SelectorSpecBuilder) buil
4063 return ssb .ExploreInterpretAs ("unixfs" , ssb .Matcher ())
4164})
4265
66+ // BytesConsumingMatcher is a traversal.WalkMatching matcher function that
67+ // consumes the bytes of a LargeBytesNode where one is matched. Use this in
68+ // conjunction with the Match* selectors in this package to ensure that all
69+ // blocks of sharded files are loaded during a traversal, or that the subset
70+ // of blocks required to fulful a range selector are loaded.
71+ func BytesConsumingMatcher (p traversal.Progress , n datamodel.Node ) error {
72+ if lbn , ok := n .(datamodel.LargeBytesNode ); ok {
73+ rdr , err := lbn .AsLargeBytes ()
74+ if err != nil {
75+ return err
76+ }
77+ _ , err = io .Copy (io .Discard , rdr )
78+ return err
79+ }
80+ return nil
81+ }
82+
83+ // AddUnixFSReificationToLinkSystem will add both unixfs and unixfs-preload
84+ // reifiers to a LinkSystem. This is primarily useful for traversals that use
85+ // an interpretAs clause, such as Match* selectors in this package.
4386func AddUnixFSReificationToLinkSystem (lsys * ipld.LinkSystem ) {
4487 if lsys .KnownReifiers == nil {
4588 lsys .KnownReifiers = make (map [string ]linking.NodeReifier )
0 commit comments