Skip to content

Commit

Permalink
Merge pull request ipfs#30 from ipfs/fix-stat-perf-and-links
Browse files Browse the repository at this point in the history
perf: do not list directory contents when statting files
  • Loading branch information
achingbrain authored Nov 28, 2018
2 parents f21ac14 + d16a4e4 commit 1627fe1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/cli/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const pull = require('pull-stream/pull')
const through = require('pull-stream/throughs/through')
const collect = require('pull-stream/sinks/collect')
const onEnd = require('pull-stream/sinks/on-end')
const {
print
} = require('./utils')
Expand Down Expand Up @@ -43,7 +43,7 @@ module.exports = {
through(buffer => {
print(buffer, false)
}),
collect((error) => {
onEnd((error) => {
if (error) {
return reject(error)
}
Expand Down
6 changes: 1 addition & 5 deletions src/core/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const waterfall = require('async/waterfall')
const pull = require('pull-stream/pull')
const collect = require('pull-stream/sinks/collect')
const asyncMap = require('pull-stream/throughs/async-map')
const filter = require('pull-stream/throughs/filter')
const exporter = require('ipfs-unixfs-exporter')
const log = require('debug')('ipfs:mfs:stat')

Expand All @@ -37,12 +36,9 @@ module.exports = (context) => {
({ mfsPath, depth }, cb) => {
pull(
exporter(mfsPath, context.ipld, {
maxDepth: depth + 1,
fullPath: true
maxDepth: depth
}),
filter(node => node.depth === depth),

// load DAGNodes for each file
asyncMap((file, cb) => {
loadNode(context, {
cid: file.hash
Expand Down
19 changes: 16 additions & 3 deletions src/core/utils/add-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const addLink = (context, options, callback) => {
return addToShardedDirectory(context, options, callback)
}

if (options.parent.links.length === options.shardSplitThreshold) {
if (options.parent.links.length >= options.shardSplitThreshold) {
log('Converting directory to sharded directory')

return convertToShardedDirectory(context, options, callback)
Expand Down Expand Up @@ -151,8 +151,21 @@ const addToShardedDirectory = async (context, options, callback) => {
})
}

const existingFile = options.parent.links
.filter(link => link.name.substring(2) === options.name)
.pop()

if (existingFile) {
log(`Updating file ${existingFile.name}`)

return addToDirectory(context, {
...options,
name: existingFile.name
}, callback)
}

const existingUnshardedFile = options.parent.links
.filter(link => link.name.substring(0, 2) === prefix || link.name.substring(2) === options.name)
.filter(link => link.name.substring(0, 2) === prefix)
.pop()

if (existingUnshardedFile) {
Expand Down Expand Up @@ -194,7 +207,7 @@ const addToShardedDirectory = async (context, options, callback) => {
})
}

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

return addToDirectory(context, {
...options,
Expand Down
3 changes: 2 additions & 1 deletion src/core/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const Key = require('interface-datastore').Key

const FILE_TYPES = {
file: 0,
directory: 1
directory: 1,
'hamt-sharded-directory': 1
}

module.exports = {
Expand Down

0 comments on commit 1627fe1

Please sign in to comment.