Skip to content

Commit 056398d

Browse files
authored
Merge pull request #52 from ipfs/rvagg/nopreload-byte-ranges
feat: add entity matcher w/o preload, add matcher fn for consuming bytes
2 parents 364a549 + 166be19 commit 056398d

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

signaling.go

+43
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package unixfsnode
22

33
import (
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.
2936
var 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.
4386
func AddUnixFSReificationToLinkSystem(lsys *ipld.LinkSystem) {
4487
if lsys.KnownReifiers == nil {
4588
lsys.KnownReifiers = make(map[string]linking.NodeReifier)

signalling_test.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ var exploreAllJson = mustDagJson(selectorparse.CommonSelector_ExploreAllRecursiv
2222
// explore interpret-as (~), next (>), match (.), interpreted as unixfs-preload
2323
var matchUnixfsPreloadJson = `{"~":{">":{".":{}},"as":"unixfs-preload"}}`
2424

25+
// explore interpret-as (~), next (>), union (|) of match (.) and explore recursive (R) edge (@) with a depth of 1, interpreted as unixfs
26+
var matchUnixfsEntityJson = `{"~":{">":{"|":[{".":{}},{"R":{":>":{"a":{">":{"@":{}}}},"l":{"depth":1}}}]},"as":"unixfs"}}`
27+
2528
// match interpret-as (~), next (>), match (.), interpreted as unixfs
2629
var matchUnixfsJson = `{"~":{">":{".":{}},"as":"unixfs"}}`
2730

@@ -93,11 +96,17 @@ func TestUnixFSPathSelectorBuilder(t *testing.T) {
9396
expextedSelector: exploreAllJson,
9497
},
9598
{
96-
name: "empty path shallow",
99+
name: "empty path shallow (preload)",
97100
path: "",
98101
target: unixfsnode.MatchUnixFSPreloadSelector,
99102
expextedSelector: matchUnixfsPreloadJson,
100103
},
104+
{
105+
name: "empty path shallow (entity)",
106+
path: "",
107+
target: unixfsnode.MatchUnixFSEntitySelector,
108+
expextedSelector: matchUnixfsEntityJson,
109+
},
101110
{
102111
name: "single field",
103112
path: "/foo",
@@ -112,11 +121,17 @@ func TestUnixFSPathSelectorBuilder(t *testing.T) {
112121
matchPath: true,
113122
},
114123
{
115-
name: "single field shallow",
124+
name: "single field shallow (preload)",
116125
path: "/foo",
117126
expextedSelector: jsonFields(matchUnixfsPreloadJson, "foo"),
118127
target: unixfsnode.MatchUnixFSPreloadSelector,
119128
},
129+
{
130+
name: "single field shallow (entity)",
131+
path: "/foo",
132+
expextedSelector: jsonFields(matchUnixfsEntityJson, "foo"),
133+
target: unixfsnode.MatchUnixFSEntitySelector,
134+
},
120135
{
121136
name: "multiple fields",
122137
path: "/foo/bar",

0 commit comments

Comments
 (0)