Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Code cleanup based on PR feedback. The only functional differences ar…
Browse files Browse the repository at this point in the history
…e some parallelization of async operations that were previously done in serial.
  • Loading branch information
stephen-palmer committed May 15, 2018
1 parent 95cafaa commit 7f3e061
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/cache/cache_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CacheBase extends EventEmitter {
/**
*
*/
async _saveDb() {
_saveDb() {
const save = promisify(this.db.saveDatabase).bind(this.db);
return save();
}
Expand Down
61 changes: 30 additions & 31 deletions lib/cache/cache_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,17 @@ class CacheFS extends CacheBase {
const self = this;
await super.endPutTransaction(transaction);

for(const file of transaction.files) {
try {
const filePath = await self._writeFileToCache(file.type, transaction.guid, transaction.hash, file.file);
helpers.log(consts.LOG_TEST, `Added file to cache: ${file.size} ${filePath}`);
}
catch(err) {
helpers.log(consts.LOG_DBG, err);
}
}
await Promise.all(
transaction.files.map(async (file) => {
try {
const filePath = await self._writeFileToCache(file.type, transaction.guid, transaction.hash, file.file);
helpers.log(consts.LOG_TEST, `Added file to cache: ${file.size} ${filePath}`);
}
catch(err) {
helpers.log(consts.LOG_DBG, err);
}
})
);
}

cleanup(dryRun = true) {
Expand Down Expand Up @@ -158,22 +160,24 @@ class CacheFS extends CacheBase {
clearTimeout(progressTimer);
self.emit('cleanup_search_finish', progressData());

for(const d of deleteItems) {
const guidHash = CacheFS._extractGuidAndHashFromFilepath(d.path);
await Promise.all(
deleteItems.map(async (d) => {
const guidHash = CacheFS._extractGuidAndHashFromFilepath(d.path);

// Make sure we're only deleting valid cached files
if(guidHash.guidStr.length === 0 || guidHash.hashStr.length === 0)
continue;
// Make sure we're only deleting valid cached files
if(guidHash.guidStr.length === 0 || guidHash.hashStr.length === 0)
return;

if(!dryRun) {
await fs.unlink(d.path);
if(this.reliabilityManager !== null) {
this.reliabilityManager.removeEntry(guidHash.guidStr, guidHash.hashStr);
if(!dryRun) {
await fs.unlink(d.path);
if(this.reliabilityManager !== null) {
this.reliabilityManager.removeEntry(guidHash.guidStr, guidHash.hashStr);
}
}
}

self.emit('cleanup_delete_item', d.path);
}
self.emit('cleanup_delete_item', d.path);
})
);

self.emit('cleanup_delete_finish', progressData());
});
Expand Down Expand Up @@ -256,9 +260,7 @@ class PutTransactionFS extends PutTransaction {

async invalidate() {
await super.invalidate();
for(const f of this._files)
await fs.unlink(f.file);

await Promise.all(this._files.map(async (f) => await fs.unlink(f.file)));
this._files = [];
}

Expand All @@ -274,7 +276,7 @@ class PutTransactionFS extends PutTransaction {
throw new Error("Invalid size for write stream");
}

if(Object.values(consts.FILE_TYPE).indexOf(type) < 0) {
if(!Object.values(consts.FILE_TYPE).includes(type)) {
throw new Error(`Unrecognized type '${type}' for transaction.`);
}

Expand All @@ -291,15 +293,12 @@ class PutTransactionFS extends PutTransaction {
}

async writeFilesToPath(filePath) {
const paths = [];
await fs.ensureDir(filePath);
for(const f of this._files) {
return Promise.all(this._files.map(async f => {
const dest = `${path.join(filePath, f.byteHash.toString('hex'))}.${f.type}`;
await fs.copyFile(f.file, dest);
paths.push(dest);
}

return paths;
return dest;
}));
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/cache/cache_ram.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class PutTransactionRAM extends PutTransaction {
throw new Error("Invalid size for write stream");
}

if(Object.values(consts.FILE_TYPE).indexOf(type) < 0) {
if(!Object.values(consts.FILE_TYPE).includes(type)) {
throw new Error(`Unrecognized type '${type}' for transaction.`);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/cache/reliability_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ReliabilityManager {
}
}

cm.listenFor("_updateReliabilityFactorForVersion", async (data) => {
cm.listenFor("_updateReliabilityFactorForVersion", (data) => {
return this._updateReliabilityFactorForVersion(data);
});
}
Expand Down Expand Up @@ -125,7 +125,7 @@ class ReliabilityManager {
* @param {string} guidStr
* @param {string} hashStr
* @param {boolean} create
* @returns {Object}
* @returns {null|Object}
*/
getEntry(guidStr, hashStr, create = false) {
const versionId = `${guidStr}-${hashStr}`;
Expand Down
2 changes: 1 addition & 1 deletion lib/server/client_stream_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ClientStreamProcessor extends Transform {
constructor(options) {
super();

this._options = options;
this._options = options || {};
this._headerBuf = Buffer.allocUnsafe(MAX_HEADER_SIZE);
this._errState = null;
this._readState = {};
Expand Down

0 comments on commit 7f3e061

Please sign in to comment.