LUCENE-4209: Enforce cleanup on success and failure while sorting partitions

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1359953 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-07-10 23:41:09 +00:00
parent 20b748a484
commit 891dcd75e9
1 changed files with 47 additions and 37 deletions

View File

@ -193,50 +193,60 @@ public final class Sort {
output.delete();
ArrayList<File> merges = new ArrayList<File>();
ByteSequencesReader is = new ByteSequencesReader(input);
boolean success = false;
boolean success2 = false;
try {
int lines = 0;
while ((lines = readPartition(is)) > 0) {
merges.add(sortPartition(lines));
sortInfo.tempMergeFiles++;
sortInfo.lines += lines;
// Handle intermediate merges.
if (merges.size() == maxTempFiles) {
File intermediate = File.createTempFile("sort", "intermediate", tempDirectory);
mergePartitions(merges, intermediate);
for (File file : merges) {
file.delete();
}
merges.clear();
merges.add(intermediate);
ByteSequencesReader is = new ByteSequencesReader(input);
boolean success = false;
try {
int lines = 0;
while ((lines = readPartition(is)) > 0) {
merges.add(sortPartition(lines));
sortInfo.tempMergeFiles++;
}
}
success = true;
} finally {
if (success)
IOUtils.close(is);
else
IOUtils.closeWhileHandlingException(is);
}
sortInfo.lines += lines;
// One partition, try to rename or copy if unsuccessful.
if (merges.size() == 1) {
File single = merges.get(0);
// If simple rename doesn't work this means the output is
// on a different volume or something. Copy the input then.
if (!single.renameTo(output)) {
copy(single, output);
single.delete();
// Handle intermediate merges.
if (merges.size() == maxTempFiles) {
File intermediate = File.createTempFile("sort", "intermediate", tempDirectory);
try {
mergePartitions(merges, intermediate);
} finally {
for (File file : merges) {
file.delete();
}
merges.clear();
merges.add(intermediate);
}
sortInfo.tempMergeFiles++;
}
}
success = true;
} finally {
if (success)
IOUtils.close(is);
else
IOUtils.closeWhileHandlingException(is);
}
} else {
// otherwise merge the partitions with a priority queue.
mergePartitions(merges, output);
// One partition, try to rename or copy if unsuccessful.
if (merges.size() == 1) {
File single = merges.get(0);
// If simple rename doesn't work this means the output is
// on a different volume or something. Copy the input then.
if (!single.renameTo(output)) {
copy(single, output);
}
} else {
// otherwise merge the partitions with a priority queue.
mergePartitions(merges, output);
}
success2 = true;
} finally {
for (File file : merges) {
file.delete();
}
if (!success2) {
output.delete();
}
}
sortInfo.totalTime = (System.currentTimeMillis() - sortInfo.totalTime);