Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Commit

Permalink
Handle empty file lists
Browse files Browse the repository at this point in the history
This patch handles when the list of files to be sorted is empty
  • Loading branch information
Yuri Astrakhan committed Feb 14, 2017
1 parent cd8f194 commit 4186abc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
36 changes: 22 additions & 14 deletions lib/fileParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function parseSourceFile(srcFile, zoomLevels, zoomLvlIndex, cleanupList) {
.pipe(es.mapSync(function write(line) {
lineInd++;
if (line === '') return undefined; // skip
var parts = line.split(separator);
let parts = line.split(separator);
if (parts.length !== 3) {
throw new Err('Line #%d has %d "%s"-separated values instead of 3', lineInd, parts.length, separator);
}
Expand Down Expand Up @@ -103,17 +103,21 @@ function parseSourceFile(srcFile, zoomLevels, zoomLvlIndex, cleanupList) {
}

function sortFile(files, cleanupList) {
return tmp.tmpName({prefix: 'tilerator-sorted-'}).then(function (dstFile) {

return Promise.try(() => {
if (files.length === 0) {
throw new Err('sortFile() called with an empty list of files');
}
return tmp.tmpName({prefix: 'tilerator-sorted-'});
}).then(function (dstFile) {
cleanupList.push(dstFile);

// Unit testing
if (module.exports.onTemp) module.exports.onTemp(undefined, dstFile);

return new Promise(function (resolve, reject) {
var srcFiles = files.map(escapeShellArg).join(' ');
// Sort temp file as numbers (-n), destination (-o dstFile), removing duplicate lines (-u)
var command = 'sort -u -n -o ' + escapeShellArg(dstFile) + ' ' + srcFiles;
let srcFiles = files.map(escapeShellArg).join(' '),
command = 'sort -u -n -o ' + escapeShellArg(dstFile) + ' ' + srcFiles;
core.log('info', 'Sorting: ' + command);
exec(command,
function (error, stdout, stderr) {
Expand Down Expand Up @@ -143,7 +147,7 @@ function sortFile(files, cleanupList) {
function addJobsFromSortedFile(filepath, options, addJobCallback) {
return new Promise(function (resolve, reject) {
core.log('info', 'Adding jobs from ' + filepath);
var zoom = options.zoom,
let zoom = options.zoom,
zoomDiff = options.fromZoom !== undefined && options.fromZoom < zoom ? zoom - options.fromZoom : 0,
breakOnDivider = Math.pow(4, zoomDiff),
tilesCountSoftLimit = 500000,
Expand Down Expand Up @@ -185,7 +189,7 @@ function addJobsFromSortedFile(filepath, options, addJobCallback) {
.pipe(es.mapSync(function (line) {
lineInd++;
if (line === '') return undefined; // skip
var idx = assertInt(line, lineInd);
let idx = assertInt(line, lineInd);
if (!core.isValidIndex(idx, zoom)) {
throw new Err('Bad value %d in the sort result', idx);
} else if (rangeStart === false) {
Expand Down Expand Up @@ -232,7 +236,7 @@ function addJobsFromSortedFile(filepath, options, addJobCallback) {
* @returns {Promise}
*/
function fileParser(filepath, options, addJobCallback) {
var cleanupList = [],
let cleanupList = [],
lastParsedFile;

return Promise.try(function () {
Expand Down Expand Up @@ -268,10 +272,14 @@ function fileParser(filepath, options, addJobCallback) {
}
lastParsedFile = filepath[i];
}
return sortFile(tempFiles2, cleanupList);
return tempFiles2.length > 0 ? sortFile(tempFiles2, cleanupList) : false;
}).then(function (sortedFile) {
cleanupList.push(sortedFile);
return addJobsFromSortedFile(sortedFile, options, addJobCallback);
if (sortedFile) {
cleanupList.push(sortedFile);
return addJobsFromSortedFile(sortedFile, options, addJobCallback);
} else {
return [];
}
}).then(function (result) {
core.log('info', 'Finished job creation... no more unemployment... at least for a bit');
return result;
Expand All @@ -280,7 +288,7 @@ function fileParser(filepath, options, addJobCallback) {
return addJobsFromSortedFile(filepath, options, addJobCallback);
}
}).then(function(addedJobs) {
var result = {
let result = {
jobs: addedJobs
};
if (lastParsedFile) {
Expand All @@ -303,12 +311,12 @@ function fileParser(filepath, options, addJobCallback) {
}

function escapeShellArg(arg) {
var newArg = arg.replace(/(["\s'$`\\])/g, '\\$1');
let newArg = arg.replace(/(["\s'$`\\])/g, '\\$1');
return newArg.indexOf('\\') > -1 ? "'" + newArg + "'" : arg;
}

function assertInt(value, lineInd) {
var v = parseInt(value);
let v = parseInt(value);
if (v.toString() !== value) {
throw new Err('Line #%d has a non-integer value: "%s"', lineInd, typeof value === 'string' ? value.slice(0, 20) : value);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tilerator-jobprocessor",
"version": "0.0.10",
"version": "0.0.11",
"description": "Processes tile generation jobs",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4186abc

Please sign in to comment.