Skip to content

Commit

Permalink
perf: scan one level
Browse files Browse the repository at this point in the history
Used writable stream instead of promises.
  • Loading branch information
blond committed May 15, 2016
1 parent adab821 commit 58cdc49
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions techs/levels.js
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const fs = require('fs');
const stream = require('stream');

const vow = require('vow');
const enb = require('enb');
Expand Down Expand Up @@ -174,33 +175,30 @@ module.exports = buildFlow.create()
_forceScanLevel: function (levelpath) {
return new vow.Promise((resolve, reject) => {
const data = {};
const promises = [];

walk([levelpath])
.on('data', function (file) {
const id = stringifyEntity(file.entity);

promises.push(new Promise((resolve, reject) => {
.on('error', reject)
.pipe(new stream.Writable({
objectMode: true,
write: function (file, encoding, callback) {
fs.stat(file.path, (err, stats) => {
if (err) {
return reject(err);
return callback(err);
}

const id = stringifyEntity(file.entity);

file.isDirectory = stats.isDirectory();
file.mtime = stats.mtime;

(data[id] || (data[id] = [])).push(file);

resolve();
callback();
});
}));
})
}
}))
.on('error', reject)
.on('end', () => {
Promise.all(promises)
.catch(reject)
.then(() => resolve(data));
});
.on('finish', () => resolve(data));
});
}
})
Expand Down

0 comments on commit 58cdc49

Please sign in to comment.