From 8ca44249835f2c3786b9c8b89f13f51cc71338af Mon Sep 17 00:00:00 2001 From: Christian Gaetano Date: Thu, 4 May 2017 11:00:53 -0400 Subject: [PATCH 1/2] Major performance updates and more debugging The big performance boost here comes from the change to isTrackingPath(). Previously, I was using an Array function with Object.keys on this.library to check for previously tracked paths. That was a huge misstep on my part as it's MUCH faster to simply use a JavaScript Object property lookup. --- .gitignore | 4 ++++ .npmignore | 1 + src/lib/TrackingCache.js | 38 +++++++++++++++++++++++++++++++++----- test/impostr.json | 4 ---- 4 files changed, 38 insertions(+), 9 deletions(-) delete mode 100644 test/impostr.json diff --git a/.gitignore b/.gitignore index 80ff4b6..72a95b8 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,7 @@ Thumbs.db # or `npm run build` # or `gulp build` build/ + +# The test Impostr library +# Use `node test/main.js` to run the test and generate the library +test/impostr.json diff --git a/.npmignore b/.npmignore index b3a4448..061d54c 100644 --- a/.npmignore +++ b/.npmignore @@ -5,3 +5,4 @@ src/ .eslintrc .gitignore gulpfile.babel.js +test/impostr.json diff --git a/src/lib/TrackingCache.js b/src/lib/TrackingCache.js index 278d291..a69b608 100644 --- a/src/lib/TrackingCache.js +++ b/src/lib/TrackingCache.js @@ -60,9 +60,7 @@ export default class TrackingCache { */ isTrackingPath(testPath) { // Force boolean coercion to avoid conditional gotchas - return !!Object.keys(this.library).find(fileKey => - fileKey === TrackedFile.formatAnyPath(testPath), // always format paths to tracked format - ); + return !!this.library[TrackedFile.formatAnyPath(testPath)]; } /** @@ -74,8 +72,8 @@ export default class TrackingCache { * @param {Function} done A function to be called when all newly added files are tracked. */ trackFiles(globPattern, done) { - if (this.debug) console.log(`Tracking files matching pattern ${globPattern}`); - glob(globPattern, (err, files) => { + if (this.debug) console.log(`Tracking files matching pattern ${globPattern}...`); + glob(globPattern, { nodir: true }, (err, files) => { if (err) { if (this.debug) console.log('IMPOSTR ERROR: ', JSON.stringify(err)); throw err; @@ -139,8 +137,23 @@ export default class TrackingCache { */ pruneLibrary() { const deletedPaths = []; + if (this.debug) console.log('Pruning library...'); + + // Only show pruning progress if debugging is enabled + let numFiles; + let currentFile; + if (this.debug) { + numFiles = Object.keys(this.library).length; + currentFile = 0; + } + Object.keys(this.library).forEach((filePathKey) => { + if (this.debug) { + currentFile += 1; + console.log(`[${currentFile}/${numFiles}] Checking if ${filePathKey} exists...`); + } if (!fs.existsSync(filePathKey)) { + console.log(`${filePathKey} doesn't exist. Deleting...`); delete this.library[filePathKey]; deletedPaths.push(filePathKey); } @@ -158,14 +171,29 @@ export default class TrackingCache { // First prune the library so we only compare existing files this.pruneLibrary(); + if (this.debug) console.log('Updating library...'); + + // Only show updating progress if debugging is enabled + let numFiles; + let currentFile; + if (this.debug) { + numFiles = Object.keys(this.library).length; + currentFile = 0; + } + // Generate an array containing paths to updated files const updatedPaths = Object.keys(this.library).filter((filePath) => { + if (this.debug) { + currentFile += 1; + console.log(`[${currentFile}/${numFiles}] Checking if ${filePath} has changed...`); + } // Compare the saved hash to the hash of a new file const currentHash = revHash(fs.readFileSync(filePath)); const savedHash = this.library[filePath]; if (currentHash !== savedHash) { // If the file has changed, update the saved hash AND add this path to the list of changed // files + if (this.debug) console.log(`${filePath} has changed! Updating hash...`); this.library[filePath] = currentHash; return true; } diff --git a/test/impostr.json b/test/impostr.json deleted file mode 100644 index f0fce43..0000000 --- a/test/impostr.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "C:\\Users\\christian.gaetano\\impostr\\test\\sample-cache-files\\04072_milfordsoundsunset_3840x2160.jpg": "c0d3f1f167", - "C:\\Users\\christian.gaetano\\impostr\\test\\sample-cache-files\\04093_whimsical_5120x2880.jpg": "d9139d35d5" -} \ No newline at end of file From 88daefc57d7a765a2bc29a648b0dc2d10addaa18 Mon Sep 17 00:00:00 2001 From: Christian Gaetano Date: Thu, 4 May 2017 11:05:26 -0400 Subject: [PATCH 2/2] Minor version bump in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 367c491..77f118b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "impostr", - "version": "0.1.7", + "version": "0.1.8", "description": "Impostr helps you keep track of who's who and what's what. Simple caching tool for checking for file changes over time.", "main": "build/impostr.js", "repository": "https://github.com/cgatno/impostr",