Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit d16a4e4

Browse files
committed
perf: do not list directory contents when statting files
Restrict how deeply directory trees are listed License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent bc946c3 commit d16a4e4

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

src/cli/read.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const pull = require('pull-stream/pull')
44
const through = require('pull-stream/throughs/through')
5-
const collect = require('pull-stream/sinks/collect')
5+
const onEnd = require('pull-stream/sinks/on-end')
66
const {
77
print
88
} = require('./utils')
@@ -43,7 +43,7 @@ module.exports = {
4343
through(buffer => {
4444
print(buffer, false)
4545
}),
46-
collect((error) => {
46+
onEnd((error) => {
4747
if (error) {
4848
return reject(error)
4949
}

src/core/stat.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const waterfall = require('async/waterfall')
1010
const pull = require('pull-stream/pull')
1111
const collect = require('pull-stream/sinks/collect')
1212
const asyncMap = require('pull-stream/throughs/async-map')
13-
const filter = require('pull-stream/throughs/filter')
1413
const exporter = require('ipfs-unixfs-exporter')
1514
const log = require('debug')('ipfs:mfs:stat')
1615

@@ -37,12 +36,9 @@ module.exports = (context) => {
3736
({ mfsPath, depth }, cb) => {
3837
pull(
3938
exporter(mfsPath, context.ipld, {
40-
maxDepth: depth + 1,
41-
fullPath: true
39+
maxDepth: depth
4240
}),
43-
filter(node => node.depth === depth),
4441

45-
// load DAGNodes for each file
4642
asyncMap((file, cb) => {
4743
loadNode(context, {
4844
cid: file.hash

src/core/utils/add-link.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const addLink = (context, options, callback) => {
7272
return addToShardedDirectory(context, options, callback)
7373
}
7474

75-
if (options.parent.links.length === options.shardSplitThreshold) {
75+
if (options.parent.links.length >= options.shardSplitThreshold) {
7676
log('Converting directory to sharded directory')
7777

7878
return convertToShardedDirectory(context, options, callback)
@@ -151,8 +151,21 @@ const addToShardedDirectory = async (context, options, callback) => {
151151
})
152152
}
153153

154+
const existingFile = options.parent.links
155+
.filter(link => link.name.substring(2) === options.name)
156+
.pop()
157+
158+
if (existingFile) {
159+
log(`Updating file ${existingFile.name}`)
160+
161+
return addToDirectory(context, {
162+
...options,
163+
name: existingFile.name
164+
}, callback)
165+
}
166+
154167
const existingUnshardedFile = options.parent.links
155-
.filter(link => link.name.substring(0, 2) === prefix || link.name.substring(2) === options.name)
168+
.filter(link => link.name.substring(0, 2) === prefix)
156169
.pop()
157170

158171
if (existingUnshardedFile) {
@@ -194,7 +207,7 @@ const addToShardedDirectory = async (context, options, callback) => {
194207
})
195208
}
196209

197-
log(`Updating or appending ${prefix + options.name} to shard`)
210+
log(`Appending ${prefix + options.name} to shard`)
198211

199212
return addToDirectory(context, {
200213
...options,

src/core/utils/constants.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const Key = require('interface-datastore').Key
44

55
const FILE_TYPES = {
66
file: 0,
7-
directory: 1
7+
directory: 1,
8+
'hamt-sharded-directory': 1
89
}
910

1011
module.exports = {

0 commit comments

Comments
 (0)