1
1
package unixfsnode
2
2
3
3
import (
4
+ "io"
5
+
4
6
"github.com/ipld/go-ipld-prime"
5
7
"github.com/ipld/go-ipld-prime/datamodel"
6
8
"github.com/ipld/go-ipld-prime/linking"
7
9
"github.com/ipld/go-ipld-prime/node/basicnode"
10
+ "github.com/ipld/go-ipld-prime/traversal"
8
11
"github.com/ipld/go-ipld-prime/traversal/selector"
9
12
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
10
13
)
@@ -26,10 +29,30 @@ var ExploreAllRecursivelySelector = specBuilder(func(ssb builder.SelectorSpecBui
26
29
// not, but not its contents.
27
30
// MatchUnixfsPreloadSelector is precompiled for use with
28
31
// 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.
29
36
var MatchUnixFSPreloadSelector = specBuilder (func (ssb builder.SelectorSpecBuilder ) builder.SelectorSpec {
30
37
return ssb .ExploreInterpretAs ("unixfs-preload" , ssb .Matcher ())
31
38
})
32
39
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
+
33
56
// MatchUnixFSSelector is a selector that will match a single node, similar to
34
57
// selectorparse.CommonSelector_MatchPoint, but uses the "unixfs" ADL to load
35
58
// as UnixFS data. Unlike MatchUnixFSPreloadSelector, this selector will not
@@ -40,6 +63,26 @@ var MatchUnixFSSelector = specBuilder(func(ssb builder.SelectorSpecBuilder) buil
40
63
return ssb .ExploreInterpretAs ("unixfs" , ssb .Matcher ())
41
64
})
42
65
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.
43
86
func AddUnixFSReificationToLinkSystem (lsys * ipld.LinkSystem ) {
44
87
if lsys .KnownReifiers == nil {
45
88
lsys .KnownReifiers = make (map [string ]linking.NodeReifier )
0 commit comments