Skip to content

Commit 81dc70c

Browse files
authored
Merge pull request #6 from ipfs/do-not-descend-into-subtrees
perf: do not descend into subtrees
2 parents b4e3a3b + cca3531 commit 81dc70c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/dir-flat.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
1818
type: 'dir'
1919
}
2020

21+
// we are at the max depth so no need to descend into children
22+
if (options.maxDepth && options.maxDepth <= depth) {
23+
return pull.values([dir])
24+
}
25+
2126
const streams = [
2227
pull(
2328
pull.values(node.links),
29+
pull.filter((item) => accepts === undefined || item.name === accepts),
2430
pull.map((link) => ({
2531
depth: depth + 1,
2632
size: link.size,
@@ -31,7 +37,6 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
3137
pathRest: pathRest.slice(1),
3238
type: 'dir'
3339
})),
34-
pull.filter((item) => accepts === undefined || item.linkName === accepts),
3540
resolve
3641
)
3742
]

src/dir-hamt-sharded.js

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
1919
}
2020
}
2121

22+
// we are at the max depth so no need to descend into children
23+
if (options.maxDepth && options.maxDepth <= depth) {
24+
return pull.values([dir])
25+
}
26+
2227
const streams = [
2328
pull(
2429
pull.values(node.links),
@@ -31,6 +36,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
3136
if (p && pathRest.length) {
3237
accept = (p === pathRest[0])
3338
}
39+
3440
if (accept) {
3541
return {
3642
depth: p ? depth + 1 : depth,
@@ -49,6 +55,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
4955
)
5056
]
5157

58+
// place dir before if not specifying subtree
5259
if (!pathRest.length || options.fullPath) {
5360
streams.unshift(pull.values([dir]))
5461
}

0 commit comments

Comments
 (0)