LUCENE-4456: clean up more exception handling

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1394551 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-10-05 14:39:55 +00:00
parent 66de20b635
commit 2ef69d2ec3
13 changed files with 69 additions and 41 deletions

View File

@ -37,6 +37,7 @@ import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
/** Concrete class that reads the current doc/freq/skip
* postings format.
@ -101,30 +102,7 @@ public class SepPostingsReader extends PostingsReaderBase {
@Override
public void close() throws IOException {
try {
if (freqIn != null)
freqIn.close();
} finally {
try {
if (docIn != null)
docIn.close();
} finally {
try {
if (skipIn != null)
skipIn.close();
} finally {
try {
if (posIn != null) {
posIn.close();
}
} finally {
if (payloadIn != null) {
payloadIn.close();
}
}
}
}
}
IOUtils.close(freqIn, docIn, skipIn, posIn, payloadIn);
}
private static final class SepTermState extends BlockTermState {

View File

@ -52,6 +52,7 @@ public class SimpleTextFieldInfosReader extends FieldInfosReader {
IndexInput input = directory.openInput(fileName, iocontext);
BytesRef scratch = new BytesRef();
boolean success = false;
try {
SimpleTextUtil.readLine(input, scratch);
@ -127,9 +128,15 @@ public class SimpleTextFieldInfosReader extends FieldInfosReader {
throw new CorruptIndexException("did not read all bytes from file \"" + fileName + "\": read " + input.getFilePointer() + " vs size " + input.length() + " (resource: " + input + ")");
}
return new FieldInfos(infos);
FieldInfos fieldInfos = new FieldInfos(infos);
success = true;
return fieldInfos;
} finally {
input.close();
if (success) {
input.close();
} else {
IOUtils.closeWhileHandlingException(input);
}
}
}

View File

@ -29,6 +29,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
/**
* writes plaintext field infos files
@ -62,6 +63,7 @@ public class SimpleTextFieldInfosWriter extends FieldInfosWriter {
final String fileName = IndexFileNames.segmentFileName(segmentName, "", FIELD_INFOS_EXTENSION);
IndexOutput out = directory.createOutput(fileName, context);
BytesRef scratch = new BytesRef();
boolean success = false;
try {
SimpleTextUtil.write(out, NUMFIELDS);
SimpleTextUtil.write(out, Integer.toString(infos.size()), scratch);
@ -125,8 +127,13 @@ public class SimpleTextFieldInfosWriter extends FieldInfosWriter {
}
}
}
success = true;
} finally {
out.close();
if (success) {
out.close();
} else {
IOUtils.closeWhileHandlingException(out);
}
}
}

View File

@ -96,10 +96,16 @@ public class SimpleTextPerDocProducer extends PerDocProducerBase {
@Override
public void close() throws IOException {
boolean success = false;
try {
super.close();
success = true;
} finally {
IOUtils.close(input);
if (success) {
IOUtils.close(input);
} else {
IOUtils.closeWhileHandlingException(input);
}
}
}

View File

@ -60,7 +60,9 @@ public class SimpleTextStoredFieldsReader extends StoredFieldsReader {
success = true;
} finally {
if (!success) {
close();
try {
close();
} catch (Throwable t) {} // ensure we throw our original exception
}
}
readIndex();

View File

@ -66,7 +66,9 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader {
success = true;
} finally {
if (!success) {
close();
try {
close();
} catch (Throwable t) {} // ensure we throw our original exception
}
}
readIndex();

View File

@ -85,7 +85,9 @@ public abstract class PerDocProducerBase extends PerDocProducer {
} finally {
if (!success) {
// if we fail we must close all opened resources if there are any
closeInternal(values.values());
try {
closeInternal(values.values());
} catch (Throwable t) {} // keep our original exception
}
}
return values;

View File

@ -70,11 +70,17 @@ public class Lucene40DocValuesConsumer extends DocValuesWriterBase {
public void abort() {
try {
close();
} catch (IOException ignored) {
}
IOUtils.deleteFilesIgnoringExceptions(mainDirectory, IndexFileNames.segmentFileName(
} catch (Throwable t) {
// ignore
} finally {
// TODO: why the inconsistency here? we do this, but not SimpleText (which says IFD
// will do it).
// TODO: check that IFD really does this always, even if codec abort() throws a
// RuntimeException (e.g. ThreadInterruptedException)
IOUtils.deleteFilesIgnoringExceptions(mainDirectory, IndexFileNames.segmentFileName(
segmentName, segmentSuffix, IndexFileNames.COMPOUND_FILE_EXTENSION),
IndexFileNames.segmentFileName(segmentName, segmentSuffix,
IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION));
}
}
}

View File

@ -32,6 +32,7 @@ import org.apache.lucene.index.DocValues;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.IOUtils;
/**
* Lucene 4.0 FieldInfos reader.
@ -50,6 +51,7 @@ public class Lucene40FieldInfosReader extends FieldInfosReader {
final String fileName = IndexFileNames.segmentFileName(segmentName, "", Lucene40FieldInfosWriter.FIELD_INFOS_EXTENSION);
IndexInput input = directory.openInput(fileName, iocontext);
boolean success = false;
try {
CodecUtil.checkHeader(input, Lucene40FieldInfosWriter.CODEC_NAME,
Lucene40FieldInfosWriter.FORMAT_START,
@ -97,10 +99,15 @@ public class Lucene40FieldInfosReader extends FieldInfosReader {
if (input.getFilePointer() != input.length()) {
throw new CorruptIndexException("did not read all bytes from file \"" + fileName + "\": read " + input.getFilePointer() + " vs size " + input.length() + " (resource: " + input + ")");
}
return new FieldInfos(infos);
FieldInfos fieldInfos = new FieldInfos(infos);
success = true;
return fieldInfos;
} finally {
input.close();
if (success) {
input.close();
} else {
IOUtils.closeWhileHandlingException(input);
}
}
}

View File

@ -28,6 +28,7 @@ import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.IOUtils;
/**
* Lucene 4.0 FieldInfos writer.
@ -60,6 +61,7 @@ public class Lucene40FieldInfosWriter extends FieldInfosWriter {
public void write(Directory directory, String segmentName, FieldInfos infos, IOContext context) throws IOException {
final String fileName = IndexFileNames.segmentFileName(segmentName, "", FIELD_INFOS_EXTENSION);
IndexOutput output = directory.createOutput(fileName, context);
boolean success = false;
try {
CodecUtil.writeHeader(output, CODEC_NAME, FORMAT_CURRENT);
output.writeVInt(infos.size());
@ -92,8 +94,13 @@ public class Lucene40FieldInfosWriter extends FieldInfosWriter {
output.writeByte(val);
output.writeStringStringMap(fi.attributes());
}
success = true;
} finally {
output.close();
if (success) {
output.close();
} else {
IOUtils.closeWhileHandlingException(output);
}
}
}

View File

@ -102,7 +102,9 @@ public final class Lucene40StoredFieldsReader extends StoredFieldsReader impleme
// of things that were opened so that we don't have to
// wait for a GC to do so.
if (!success) {
close();
try {
close();
} catch (Throwable t) {} // ensure we throw our original exception
}
}
}

View File

@ -139,7 +139,9 @@ public class Lucene40TermVectorsReader extends TermVectorsReader implements Clos
// of things that were opened so that we don't have to
// wait for a GC to do so.
if (!success) {
close();
try {
close();
} catch (Throwable t) {} // ensure we throw our original exception
}
}
}

View File

@ -335,7 +335,7 @@ public final class IOUtils {
for (String name : files) {
try {
dir.deleteFile(name);
} catch (IOException ignored) {
} catch (Throwable ignored) {
// ignore
}
}