Skip to content

Apache Lucene open-source search software

Catch and re-throw Throwable rather than using a success boolean (#14633)

Simon Cooper7d3c7bb6c7ce
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/SortingStrategy.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/SortingStrategy.java
index e1c59020d44..acf8a534d3d 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/SortingStrategy.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/SortingStrategy.java
@@ -120,17 +120,13 @@ public abstract class SortingStrategy {
var sorter = new OfflineSorter(tempDir, tempFileNamePrefix, BytesRefComparator.NATURAL);
String sorted;
- boolean success = false;
try {
sorted = sorter.sort(output.getName());
- success = true;
- } finally {
- if (success) {
- tempDir.deleteFile(output.getName());
- } else {
- IOUtils.deleteFilesIgnoringExceptions(tempDir, output.getName());
- }
+ } catch (Throwable t) {
+ IOUtils.deleteFilesSuppressingExceptions(t, tempDir, output.getName());
+ throw t;
}
+ tempDir.deleteFile(output.getName());
return sorted;
}
};
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsFormat.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsFormat.java
index 5528532229f..4eb6a2f0cf3 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsFormat.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsFormat.java
@@ -389,15 +389,11 @@ public class Lucene101PostingsFormat extends PostingsFormat {
@Override
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
PostingsReaderBase postingsReader = new Lucene101PostingsReader(state);
- boolean success = false;
try {
- FieldsProducer ret = new Lucene90BlockTreeTermsReader(postingsReader, state);
- success = true;
- return ret;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsReader);
- }
+ return new Lucene90BlockTreeTermsReader(postingsReader, state);
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, postingsReader);
+ throw t;
}
}
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsReader.java
index 33793eb19d4..a2234462c80 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsReader.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene101/Lucene101PostingsReader.java
@@ -90,53 +90,44 @@ public final class Lucene101PostingsReader extends PostingsReaderBase {
IndexFileNames.segmentFileName(
state.segmentInfo.name, state.segmentSuffix, Lucene101PostingsFormat.META_EXTENSION);
final long expectedDocFileLength, expectedPosFileLength, expectedPayFileLength;
- ChecksumIndexInput metaIn = null;
- boolean success = false;
int version;
- try {
- metaIn = state.directory.openChecksumInput(metaName);
- version =
- CodecUtil.checkIndexHeader(
- metaIn,
- META_CODEC,
- VERSION_START,
- VERSION_CURRENT,
- state.segmentInfo.getId(),
- state.segmentSuffix);
- maxNumImpactsAtLevel0 = metaIn.readInt();
- maxImpactNumBytesAtLevel0 = metaIn.readInt();
- maxNumImpactsAtLevel1 = metaIn.readInt();
- maxImpactNumBytesAtLevel1 = metaIn.readInt();
- expectedDocFileLength = metaIn.readLong();
- if (state.fieldInfos.hasProx()) {
- expectedPosFileLength = metaIn.readLong();
- if (state.fieldInfos.hasPayloads() || state.fieldInfos.hasOffsets()) {
- expectedPayFileLength = metaIn.readLong();
+ try (ChecksumIndexInput metaIn = state.directory.openChecksumInput(metaName)) {
+ try {
+ version =
+ CodecUtil.checkIndexHeader(
+ metaIn,
+ META_CODEC,
+ VERSION_START,
+ VERSION_CURRENT,
+ state.segmentInfo.getId(),
+ state.segmentSuffix);
+ maxNumImpactsAtLevel0 = metaIn.readInt();
+ maxImpactNumBytesAtLevel0 = metaIn.readInt();
+ maxNumImpactsAtLevel1 = metaIn.readInt();
+ maxImpactNumBytesAtLevel1 = metaIn.readInt();
+ expectedDocFileLength = metaIn.readLong();
+ if (state.fieldInfos.hasProx()) {
+ expectedPosFileLength = metaIn.readLong();
+ if (state.fieldInfos.hasPayloads() || state.fieldInfos.hasOffsets()) {
+ expectedPayFileLength = metaIn.readLong();
+ } else {
+ expectedPayFileLength = -1;
+ }
} else {
+ expectedPosFileLength = -1;
expectedPayFileLength = -1;
}
- } else {
- expectedPosFileLength = -1;
- expectedPayFileLength = -1;
- }
- CodecUtil.checkFooter(metaIn, null);
- success = true;
- } catch (Throwable t) {
- if (metaIn != null) {
- CodecUtil.checkFooter(metaIn, t);
- throw new AssertionError("unreachable");
- } else {
- throw t;
- }
- } finally {
- if (success) {
- metaIn.close();
- } else {
- IOUtils.closeWhileHandlingException(metaIn);
+ CodecUtil.checkFooter(metaIn, null);
+ } catch (Throwable t) {
+ if (metaIn != null) {
+ CodecUtil.checkFooter(metaIn, t);
+ throw new AssertionError("unreachable");
+ } else {
+ throw t;
+ }
}
}
- success = false;
IndexInput docIn = null;
IndexInput posIn = null;
IndexInput payIn = null;
@@ -182,11 +173,9 @@ public final class Lucene101PostingsReader extends PostingsReaderBase {
this.docIn = docIn;
this.posIn = posIn;
this.payIn = payIn;
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(docIn, posIn, payIn);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, docIn, posIn, payIn);
+ throw t;
}
}
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/compressing/FieldsIndexReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/compressing/FieldsIndexReader.java
index a2b153ae04e..9994948795c 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/compressing/FieldsIndexReader.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/compressing/FieldsIndexReader.java
@@ -27,6 +27,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.RandomAccessInput;
+import org.apache.lucene.util.IOUtils;
final class FieldsIndexReader extends FieldsIndex {
@@ -68,16 +69,13 @@ final class FieldsIndexReader extends FieldsIndex {
indexInput =
EndiannessReverserUtil.openInput(
dir, IndexFileNames.segmentFileName(name, suffix, extension), IOContext.DEFAULT);
- boolean success = false;
try {
CodecUtil.checkIndexHeader(
indexInput, codecName + "Idx", VERSION_START, VERSION_CURRENT, id, suffix);
CodecUtil.retrieveChecksum(indexInput);
- success = true;
- } finally {
- if (success == false) {
- indexInput.close();
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, indexInput);
+ throw t;
}
final RandomAccessInput docsSlice =
indexInput.randomAccessSlice(docsStartPointer, docsEndPointer - docsStartPointer);
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene95/Lucene95HnswVectorsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene95/Lucene95HnswVectorsReader.java
index d8ce482907a..bbeda6930f3 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene95/Lucene95HnswVectorsReader.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene95/Lucene95HnswVectorsReader.java
@@ -71,7 +71,6 @@ public final class Lucene95HnswVectorsReader extends KnnVectorsReader implements
Lucene95HnswVectorsReader(SegmentReadState state) throws IOException {
this.fieldInfos = state.fieldInfos;
int versionMeta = readMetadata(state);
- boolean success = false;
try {
vectorData =
openDataInput(
@@ -85,11 +84,9 @@ public final class Lucene95HnswVectorsReader extends KnnVectorsReader implements
versionMeta,
Lucene95HnswVectorsFormat.VECTOR_INDEX_EXTENSION,
Lucene95HnswVectorsFormat.VECTOR_INDEX_CODEC_NAME);
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -125,7 +122,6 @@ public final class Lucene95HnswVectorsReader extends KnnVectorsReader implements
String fileName =
IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, fileExtension);
IndexInput in = state.directory.openInput(fileName, state.context);
- boolean success = false;
try {
int versionVectorData =
CodecUtil.checkIndexHeader(
@@ -146,12 +142,10 @@ public final class Lucene95HnswVectorsReader extends KnnVectorsReader implements
in);
}
CodecUtil.retrieveChecksum(in);
- success = true;
return in;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(in);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, in);
+ throw t;
}
}
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsFormat.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsFormat.java
index f4fbc49c514..7697dc4c4b8 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsFormat.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsFormat.java
@@ -390,15 +390,11 @@ public class Lucene99PostingsFormat extends PostingsFormat {
@Override
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
PostingsReaderBase postingsReader = new Lucene99PostingsReader(state);
- boolean success = false;
try {
- FieldsProducer ret = new Lucene90BlockTreeTermsReader(postingsReader, state);
- success = true;
- return ret;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsReader);
- }
+ return new Lucene90BlockTreeTermsReader(postingsReader, state);
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, postingsReader);
+ throw t;
}
}
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsReader.java
index 5a815c49be5..1cc41e23926 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsReader.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene99/Lucene99PostingsReader.java
@@ -66,7 +66,6 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {
/** Sole constructor. */
public Lucene99PostingsReader(SegmentReadState state) throws IOException {
- boolean success = false;
IndexInput docIn = null;
IndexInput posIn = null;
IndexInput payIn = null;
@@ -118,11 +117,9 @@ public final class Lucene99PostingsReader extends PostingsReaderBase {
this.docIn = docIn;
this.posIn = posIn;
this.payIn = payIn;
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(docIn, posIn, payIn);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, docIn, posIn, payIn);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java
index 0c0a884030b..e84c04b5e89 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsReader.java
@@ -78,7 +78,6 @@ class Lucene102BinaryQuantizedVectorsReader extends FlatVectorsReader {
state.segmentInfo.name,
state.segmentSuffix,
Lucene102BinaryQuantizedVectorsFormat.META_EXTENSION);
- boolean success = false;
try (ChecksumIndexInput meta = state.directory.openChecksumInput(metaFileName)) {
Throwable priorE = null;
try {
@@ -106,11 +105,9 @@ class Lucene102BinaryQuantizedVectorsReader extends FlatVectorsReader {
// graph.
state.context.withHints(
FileTypeHint.DATA, FileDataHint.KNN_VECTORS, DataAccessHint.RANDOM));
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -294,7 +291,6 @@ class Lucene102BinaryQuantizedVectorsReader extends FlatVectorsReader {
String fileName =
IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, fileExtension);
IndexInput in = state.directory.openInput(fileName, context);
- boolean success = false;
try {
int versionVectorData =
CodecUtil.checkIndexHeader(
@@ -315,12 +311,10 @@ class Lucene102BinaryQuantizedVectorsReader extends FlatVectorsReader {
in);
}
CodecUtil.retrieveChecksum(in);
- success = true;
return in;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(in);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, in);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsWriter.java
index fc42e93bf9a..d881fdd5b28 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene102/Lucene102BinaryQuantizedVectorsWriter.java
@@ -99,7 +99,6 @@ public class Lucene102BinaryQuantizedVectorsWriter extends FlatVectorsWriter {
state.segmentSuffix,
Lucene102BinaryQuantizedVectorsFormat.VECTOR_DATA_EXTENSION);
this.rawVectorDelegate = rawVectorDelegate;
- boolean success = false;
try {
meta = state.directory.createOutput(metaFileName, state.context);
binarizedVectorData =
@@ -117,11 +116,9 @@ public class Lucene102BinaryQuantizedVectorsWriter extends FlatVectorsWriter {
Lucene102BinaryQuantizedVectorsFormat.VERSION_CURRENT,
state.segmentInfo.getId(),
state.segmentSuffix);
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -452,7 +449,6 @@ public class Lucene102BinaryQuantizedVectorsWriter extends FlatVectorsWriter {
IndexOutput tempScoreQuantizedVectorData = null;
IndexInput binarizedDataInput = null;
IndexInput binarizedScoreDataInput = null;
- boolean success = false;
OptimizedScalarQuantizer quantizer =
new OptimizedScalarQuantizer(fieldInfo.getVectorSimilarityFunction());
try {
@@ -497,9 +493,14 @@ public class Lucene102BinaryQuantizedVectorsWriter extends FlatVectorsWriter {
centroid,
cDotC,
docsWithField);
- success = true;
+
final IndexInput finalBinarizedDataInput = binarizedDataInput;
final IndexInput finalBinarizedScoreDataInput = binarizedScoreDataInput;
+ tempQuantizedVectorData = null;
+ tempScoreQuantizedVectorData = null;
+ binarizedDataInput = null;
+ binarizedScoreDataInput = null;
+
OffHeapBinarizedVectorValues vectorValues =
new OffHeapBinarizedVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
@@ -526,22 +527,22 @@ public class Lucene102BinaryQuantizedVectorsWriter extends FlatVectorsWriter {
IOUtils.deleteFilesIgnoringExceptions(
segmentWriteState.directory, tempQuantizedVectorName, tempScoreQuantizedVectorName);
});
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(
- tempQuantizedVectorData,
- tempScoreQuantizedVectorData,
- binarizedDataInput,
- binarizedScoreDataInput);
- if (tempQuantizedVectorData != null) {
- IOUtils.deleteFilesIgnoringExceptions(
- segmentWriteState.directory, tempQuantizedVectorData.getName());
- }
- if (tempScoreQuantizedVectorData != null) {
- IOUtils.deleteFilesIgnoringExceptions(
- segmentWriteState.directory, tempScoreQuantizedVectorData.getName());
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(
+ t,
+ tempQuantizedVectorData,
+ tempScoreQuantizedVectorData,
+ binarizedDataInput,
+ binarizedScoreDataInput);
+ if (tempQuantizedVectorData != null) {
+ IOUtils.deleteFilesSuppressingExceptions(
+ t, segmentWriteState.directory, tempQuantizedVectorData.getName());
+ }
+ if (tempScoreQuantizedVectorData != null) {
+ IOUtils.deleteFilesSuppressingExceptions(
+ t, segmentWriteState.directory, tempScoreQuantizedVectorData.getName());
}
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsFormat.java
index 3c489f9d3ca..04880504311 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsFormat.java
@@ -409,32 +409,23 @@ public final class Lucene103PostingsFormat extends PostingsFormat {
@Override
public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException {
PostingsWriterBase postingsWriter = new Lucene103PostingsWriter(state, version);
- boolean success = false;
try {
- FieldsConsumer ret =
- new Lucene103BlockTreeTermsWriter(
- state, postingsWriter, minTermBlockSize, maxTermBlockSize);
- success = true;
- return ret;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsWriter);
- }
+ return new Lucene103BlockTreeTermsWriter(
+ state, postingsWriter, minTermBlockSize, maxTermBlockSize);
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, postingsWriter);
+ throw t;
}
}
@Override
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
PostingsReaderBase postingsReader = new Lucene103PostingsReader(state);
- boolean success = false;
try {
- FieldsProducer ret = new Lucene103BlockTreeTermsReader(postingsReader, state);
- success = true;
- return ret;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsReader);
- }
+ return new Lucene103BlockTreeTermsReader(postingsReader, state);
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, postingsReader);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsReader.java
index 2a9c251e657..3ecaddef617 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsReader.java
@@ -93,53 +93,44 @@ public final class Lucene103PostingsReader extends PostingsReaderBase {
IndexFileNames.segmentFileName(
state.segmentInfo.name, state.segmentSuffix, Lucene103PostingsFormat.META_EXTENSION);
final long expectedDocFileLength, expectedPosFileLength, expectedPayFileLength;
- ChecksumIndexInput metaIn = null;
- boolean success = false;
int version;
- try {
- metaIn = state.directory.openChecksumInput(metaName);
- version =
- CodecUtil.checkIndexHeader(
- metaIn,
- META_CODEC,
- VERSION_START,
- VERSION_CURRENT,
- state.segmentInfo.getId(),
- state.segmentSuffix);
- maxNumImpactsAtLevel0 = metaIn.readInt();
- maxImpactNumBytesAtLevel0 = metaIn.readInt();
- maxNumImpactsAtLevel1 = metaIn.readInt();
- maxImpactNumBytesAtLevel1 = metaIn.readInt();
- expectedDocFileLength = metaIn.readLong();
- if (state.fieldInfos.hasProx()) {
- expectedPosFileLength = metaIn.readLong();
- if (state.fieldInfos.hasPayloads() || state.fieldInfos.hasOffsets()) {
- expectedPayFileLength = metaIn.readLong();
+ try (ChecksumIndexInput metaIn = state.directory.openChecksumInput(metaName)) {
+ try {
+ version =
+ CodecUtil.checkIndexHeader(
+ metaIn,
+ META_CODEC,
+ VERSION_START,
+ VERSION_CURRENT,
+ state.segmentInfo.getId(),
+ state.segmentSuffix);
+ maxNumImpactsAtLevel0 = metaIn.readInt();
+ maxImpactNumBytesAtLevel0 = metaIn.readInt();
+ maxNumImpactsAtLevel1 = metaIn.readInt();
+ maxImpactNumBytesAtLevel1 = metaIn.readInt();
+ expectedDocFileLength = metaIn.readLong();
+ if (state.fieldInfos.hasProx()) {
+ expectedPosFileLength = metaIn.readLong();
+ if (state.fieldInfos.hasPayloads() || state.fieldInfos.hasOffsets()) {
+ expectedPayFileLength = metaIn.readLong();
+ } else {
+ expectedPayFileLength = -1;
+ }
} else {
+ expectedPosFileLength = -1;
expectedPayFileLength = -1;
}
- } else {
- expectedPosFileLength = -1;
- expectedPayFileLength = -1;
- }
- CodecUtil.checkFooter(metaIn, null);
- success = true;
- } catch (Throwable t) {
- if (metaIn != null) {
- CodecUtil.checkFooter(metaIn, t);
- throw new AssertionError("unreachable");
- } else {
- throw t;
- }
- } finally {
- if (success) {
- metaIn.close();
- } else {
- IOUtils.closeWhileHandlingException(metaIn);
+ CodecUtil.checkFooter(metaIn, null);
+ } catch (Throwable t) {
+ if (metaIn != null) {
+ CodecUtil.checkFooter(metaIn, t);
+ throw new AssertionError("unreachable");
+ } else {
+ throw t;
+ }
}
}
- success = false;
IndexInput docIn = null;
IndexInput posIn = null;
IndexInput payIn = null;
@@ -185,11 +176,9 @@ public final class Lucene103PostingsReader extends PostingsReaderBase {
this.docIn = docIn;
this.posIn = posIn;
this.payIn = payIn;
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(docIn, posIn, payIn);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, docIn, posIn, payIn);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsWriter.java
index b5d5047e623..7d37d0822ba 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/Lucene103PostingsWriter.java
@@ -152,7 +152,6 @@ public class Lucene103PostingsWriter extends PushPostingsWriterBase {
metaOut = state.directory.createOutput(metaFileName, state.context);
IndexOutput posOut = null;
IndexOutput payOut = null;
- boolean success = false;
try {
docOut = state.directory.createOutput(docFileName, state.context);
CodecUtil.writeIndexHeader(
@@ -205,11 +204,9 @@ public class Lucene103PostingsWriter extends PushPostingsWriterBase {
}
this.payOut = payOut;
this.posOut = posOut;
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(metaOut, docOut, posOut, payOut);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, metaOut, docOut, posOut, payOut);
+ throw t;
}
docDeltaBuffer = new int[BLOCK_SIZE];
@@ -694,38 +691,37 @@ public class Lucene103PostingsWriter extends PushPostingsWriterBase {
@Override
public void close() throws IOException {
// TODO: add a finish() at least to PushBase? DV too...?
- boolean success = false;
try {
- if (docOut != null) {
- CodecUtil.writeFooter(docOut);
- }
- if (posOut != null) {
- CodecUtil.writeFooter(posOut);
- }
- if (payOut != null) {
- CodecUtil.writeFooter(payOut);
- }
- if (metaOut != null) {
- metaOut.writeInt(maxNumImpactsAtLevel0);
- metaOut.writeInt(maxImpactNumBytesAtLevel0);
- metaOut.writeInt(maxNumImpactsAtLevel1);
- metaOut.writeInt(maxImpactNumBytesAtLevel1);
- metaOut.writeLong(docOut.getFilePointer());
+ try {
+ if (docOut != null) {
+ CodecUtil.writeFooter(docOut);
+ }
if (posOut != null) {
- metaOut.writeLong(posOut.getFilePointer());
- if (payOut != null) {
- metaOut.writeLong(payOut.getFilePointer());
+ CodecUtil.writeFooter(posOut);
+ }
+ if (payOut != null) {
+ CodecUtil.writeFooter(payOut);
+ }
+ if (metaOut != null) {
+ metaOut.writeInt(maxNumImpactsAtLevel0);
+ metaOut.writeInt(maxImpactNumBytesAtLevel0);
+ metaOut.writeInt(maxNumImpactsAtLevel1);
+ metaOut.writeInt(maxImpactNumBytesAtLevel1);
+ metaOut.writeLong(docOut.getFilePointer());
+ if (posOut != null) {
+ metaOut.writeLong(posOut.getFilePointer());
+ if (payOut != null) {
+ metaOut.writeLong(payOut.getFilePointer());
+ }
}
+ CodecUtil.writeFooter(metaOut);
}
- CodecUtil.writeFooter(metaOut);
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, metaOut, docOut, posOut, payOut);
+ throw t;
}
- success = true;
+ IOUtils.close(metaOut, docOut, posOut, payOut);
} finally {
- if (success) {
- IOUtils.close(metaOut, docOut, posOut, payOut);
- } else {
- IOUtils.closeWhileHandlingException(metaOut, docOut, posOut, payOut);
- }
metaOut = docOut = posOut = payOut = null;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsReader.java
index 28e9741f66d..095f2d849e0 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsReader.java
@@ -106,8 +106,6 @@ public final class Lucene103BlockTreeTermsReader extends FieldsProducer {
/** Sole constructor. */
public Lucene103BlockTreeTermsReader(PostingsReaderBase postingsReader, SegmentReadState state)
throws IOException {
- boolean success = false;
-
this.postingsReader = postingsReader;
this.segment = state.segmentInfo.name;
@@ -235,12 +233,9 @@ public final class Lucene103BlockTreeTermsReader extends FieldsProducer {
fieldInfos = state.fieldInfos;
this.fieldMap = fieldMap;
this.fieldList = sortFieldNames(fieldMap, state.fieldInfos);
- success = true;
- } finally {
- if (!success) {
- // this.close() will close in:
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java
index ee4689183ab..1ce0ebd3509 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/Lucene103BlockTreeTermsWriter.java
@@ -293,7 +293,6 @@ public final class Lucene103BlockTreeTermsWriter extends FieldsConsumer {
state.segmentSuffix,
Lucene103BlockTreeTermsReader.TERMS_EXTENSION);
termsOut = state.directory.createOutput(termsName, state.context);
- boolean success = false;
IndexOutput metaOut = null, indexOut = null;
try {
CodecUtil.writeIndexHeader(
@@ -334,11 +333,9 @@ public final class Lucene103BlockTreeTermsWriter extends FieldsConsumer {
this.metaOut = metaOut;
this.indexOut = indexOut;
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(metaOut, termsOut, indexOut);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, metaOut, termsOut, indexOut);
+ throw t;
}
}
@@ -1132,7 +1129,6 @@ public final class Lucene103BlockTreeTermsWriter extends FieldsConsumer {
}
closed = true;
- boolean success = false;
try {
metaOut.writeVInt(fields.size());
for (ByteBuffersDataOutput fieldMeta : fields) {
@@ -1143,14 +1139,11 @@ public final class Lucene103BlockTreeTermsWriter extends FieldsConsumer {
CodecUtil.writeFooter(termsOut);
metaOut.writeLong(termsOut.getFilePointer());
CodecUtil.writeFooter(metaOut);
- success = true;
- } finally {
- if (success) {
- IOUtils.close(metaOut, termsOut, indexOut, postingsWriter);
- } else {
- IOUtils.closeWhileHandlingException(metaOut, termsOut, indexOut, postingsWriter);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, metaOut, termsOut, indexOut, postingsWriter);
+ throw t;
}
+ IOUtils.close(metaOut, termsOut, indexOut, postingsWriter);
}
private static void writeBytesRef(DataOutput out, BytesRef bytes) throws IOException {
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java
index 83820fcfeb0..8d986b63983 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java
@@ -64,7 +64,6 @@ final class Lucene90CompoundReader extends CompoundDirectory {
String entriesFileName =
IndexFileNames.segmentFileName(segmentName, "", Lucene90CompoundFormat.ENTRIES_EXTENSION);
this.entries = readEntries(si.getId(), directory, entriesFileName);
- boolean success = false;
// find the last FileEntry in the map (largest offset+length) and add length of codec footer:
final long expectedLength =
@@ -92,12 +91,9 @@ final class Lucene90CompoundReader extends CompoundDirectory {
"length should be " + expectedLength + " bytes, but is " + handle.length() + " instead",
handle);
}
-
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(handle);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, handle);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java
index 13f1463d0db..8223320dfcc 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java
@@ -78,7 +78,6 @@ final class Lucene90DocValuesConsumer extends DocValuesConsumer {
String metaExtension)
throws IOException {
this.termsDictBuffer = new byte[1 << 14];
- boolean success = false;
try {
String dataName =
IndexFileNames.segmentFileName(
@@ -102,32 +101,29 @@ final class Lucene90DocValuesConsumer extends DocValuesConsumer {
state.segmentSuffix);
maxDoc = state.segmentInfo.maxDoc();
this.skipIndexIntervalSize = skipIndexIntervalSize;
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@Override
public void close() throws IOException {
- boolean success = false;
try {
- if (meta != null) {
- meta.writeInt(-1); // write EOF marker
- CodecUtil.writeFooter(meta); // write checksum
- }
- if (data != null) {
- CodecUtil.writeFooter(data); // write checksum
+ try {
+ if (meta != null) {
+ meta.writeInt(-1); // write EOF marker
+ CodecUtil.writeFooter(meta); // write checksum
+ }
+ if (data != null) {
+ CodecUtil.writeFooter(data); // write checksum
+ }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, data, meta);
+ throw t;
}
- success = true;
+ IOUtils.close(data, meta);
} finally {
- if (success) {
- IOUtils.close(data, meta);
- } else {
- IOUtils.closeWhileHandlingException(data, meta);
- }
meta = data = null;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java
index fd213591d0c..e1b5b2cf672 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java
@@ -116,7 +116,6 @@ final class Lucene90DocValuesProducer extends DocValuesProducer {
IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, dataExtension);
// Doc-values have a forward-only access pattern
this.data = state.directory.openInput(dataName, state.context.withHints(FileTypeHint.DATA));
- boolean success = false;
try {
final int version2 =
CodecUtil.checkIndexHeader(
@@ -136,12 +135,9 @@ final class Lucene90DocValuesProducer extends DocValuesProducer {
// for FOOTER_MAGIC + algorithmID. This is cheap and can detect some forms of corruption
// such as file truncation.
CodecUtil.retrieveChecksum(data);
-
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this.data);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, data);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsConsumer.java
index 5157cabcc75..085c0b52f1e 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsConsumer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsConsumer.java
@@ -42,7 +42,6 @@ final class Lucene90NormsConsumer extends NormsConsumer {
String metaCodec,
String metaExtension)
throws IOException {
- boolean success = false;
try {
String dataName =
IndexFileNames.segmentFileName(
@@ -57,32 +56,29 @@ final class Lucene90NormsConsumer extends NormsConsumer {
CodecUtil.writeIndexHeader(
meta, metaCodec, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
maxDoc = state.segmentInfo.maxDoc();
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@Override
public void close() throws IOException {
- boolean success = false;
try {
- if (meta != null) {
- meta.writeInt(-1); // write EOF marker
- CodecUtil.writeFooter(meta); // write checksum
+ try {
+ if (meta != null) {
+ meta.writeInt(-1); // write EOF marker
+ CodecUtil.writeFooter(meta); // write checksum
+ }
+ if (data != null) {
+ CodecUtil.writeFooter(data); // write checksum
+ }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, data, meta);
+ throw t;
}
- if (data != null) {
- CodecUtil.writeFooter(data); // write checksum
- }
- success = true;
+ IOUtils.close(data, meta);
} finally {
- if (success) {
- IOUtils.close(data, meta);
- } else {
- IOUtils.closeWhileHandlingException(data, meta);
- }
meta = data = null;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsProducer.java
index 2cb55cc83ad..63af4946d50 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsProducer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90NormsProducer.java
@@ -83,7 +83,6 @@ final class Lucene90NormsProducer extends NormsProducer implements Cloneable {
IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, dataExtension);
// Norms have a forward-only access pattern
data = state.directory.openInput(dataName, state.context.withHints(FileTypeHint.DATA));
- boolean success = false;
try {
final int version2 =
CodecUtil.checkIndexHeader(
@@ -103,12 +102,9 @@ final class Lucene90NormsProducer extends NormsProducer implements Cloneable {
// for FOOTER_MAGIC + algorithmID. This is cheap and can detect some forms of corruption
// such as file truncation.
CodecUtil.retrieveChecksum(data);
-
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this.data);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this.data);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90PointsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90PointsReader.java
index 7a38d6905c2..e896cf7e0af 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90PointsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90PointsReader.java
@@ -57,7 +57,6 @@ public class Lucene90PointsReader extends PointsReader {
readState.segmentSuffix,
Lucene90PointsFormat.DATA_EXTENSION);
- boolean success = false;
try {
indexIn =
readState.directory.openInput(
@@ -119,11 +118,9 @@ public class Lucene90PointsReader extends PointsReader {
// know that indexLength and dataLength are very likely correct.
CodecUtil.retrieveChecksum(indexIn, indexLength);
CodecUtil.retrieveChecksum(dataIn, dataLength);
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90PointsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90PointsWriter.java
index 456d12cd962..ed40f3ebb4a 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90PointsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90PointsWriter.java
@@ -64,7 +64,6 @@ public class Lucene90PointsWriter extends PointsWriter {
writeState.segmentSuffix,
Lucene90PointsFormat.DATA_EXTENSION);
dataOut = writeState.directory.createOutput(dataFileName, writeState.context);
- boolean success = false;
try {
CodecUtil.writeIndexHeader(
dataOut,
@@ -98,12 +97,9 @@ public class Lucene90PointsWriter extends PointsWriter {
Lucene90PointsFormat.VERSION_CURRENT,
writeState.segmentInfo.getId(),
writeState.segmentSuffix);
-
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java
index 9f7ca61890f..a5d9e276044 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java
@@ -133,7 +133,6 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsRea
throws IOException {
this.compressionMode = compressionMode;
final String segment = si.name;
- boolean success = false;
fieldInfos = fn;
numDocs = si.maxDoc();
@@ -225,18 +224,16 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsRea
CodecUtil.checkFooter(metaIn, null);
metaIn.close();
-
- success = true;
} catch (Throwable t) {
- if (metaIn != null) {
- CodecUtil.checkFooter(metaIn, t);
- throw new AssertionError("unreachable");
- } else {
- throw t;
- }
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this, metaIn);
+ try {
+ if (metaIn != null) {
+ CodecUtil.checkFooter(metaIn, t);
+ throw new AssertionError("unreachable");
+ } else {
+ throw t;
+ }
+ } finally {
+ IOUtils.closeWhileSuppressingExceptions(t, this, metaIn);
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsWriter.java
index 68805763e78..eb52978b04f 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsWriter.java
@@ -124,7 +124,6 @@ public final class Lucene90CompressingStoredFieldsWriter extends StoredFieldsWri
this.endOffsets = new int[16];
this.numBufferedDocs = 0;
- boolean success = false;
try {
metaStream =
directory.createOutput(
@@ -154,12 +153,9 @@ public final class Lucene90CompressingStoredFieldsWriter extends StoredFieldsWri
context);
metaStream.writeVInt(chunkSize);
-
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(metaStream, fieldsStream, indexWriter);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, metaStream, fieldsStream, indexWriter);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingTermVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingTermVectorsReader.java
index d091d019295..ec96e787ede 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingTermVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingTermVectorsReader.java
@@ -138,7 +138,6 @@ public final class Lucene90CompressingTermVectorsReader extends TermVectorsReade
throws IOException {
this.compressionMode = compressionMode;
final String segment = si.name;
- boolean success = false;
fieldInfos = fn;
numDocs = si.maxDoc();
@@ -227,18 +226,16 @@ public final class Lucene90CompressingTermVectorsReader extends TermVectorsReade
this.prefetchedBlockIDCache = new long[PREFETCH_CACHE_SIZE];
Arrays.fill(prefetchedBlockIDCache, -1);
-
- success = true;
} catch (Throwable t) {
- if (metaIn != null) {
- CodecUtil.checkFooter(metaIn, t);
- throw new AssertionError("unreachable");
- } else {
- throw t;
- }
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this, metaIn);
+ try {
+ if (metaIn != null) {
+ CodecUtil.checkFooter(metaIn, t);
+ throw new AssertionError("unreachable");
+ } else {
+ throw t;
+ }
+ } finally {
+ IOUtils.closeWhileSuppressingExceptions(t, this, metaIn);
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingTermVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingTermVectorsWriter.java
index a070c6be47b..e053e389a8d 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingTermVectorsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingTermVectorsWriter.java
@@ -253,7 +253,6 @@ public final class Lucene90CompressingTermVectorsWriter extends TermVectorsWrite
payloadBytes = ByteBuffersDataOutput.newResettableInstance();
lastTerm = new BytesRef(ArrayUtil.oversize(30, 1));
- boolean success = false;
try {
metaStream =
directory.createOutput(
@@ -295,12 +294,10 @@ public final class Lucene90CompressingTermVectorsWriter extends TermVectorsWrite
startOffsetsBuf = new int[1024];
lengthsBuf = new int[1024];
payloadLengthsBuf = new int[1024];
-
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(metaStream, vectorsStream, indexWriter, indexWriter);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(
+ t, metaStream, vectorsStream, indexWriter, indexWriter);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java
index 4ddcb7b2d43..b42f9e8a63d 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java
@@ -69,7 +69,6 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
super(scorer);
int versionMeta = readMetadata(state);
this.fieldInfos = state.fieldInfos;
- boolean success = false;
try {
vectorData =
openDataInput(
@@ -81,11 +80,9 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
// in the HNSW graph.
state.context.withHints(
FileTypeHint.DATA, FileDataHint.KNN_VECTORS, DataAccessHint.RANDOM));
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -125,7 +122,6 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
String fileName =
IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, fileExtension);
IndexInput in = state.directory.openInput(fileName, context);
- boolean success = false;
try {
int versionVectorData =
CodecUtil.checkIndexHeader(
@@ -146,12 +142,10 @@ public final class Lucene99FlatVectorsReader extends FlatVectorsReader {
in);
}
CodecUtil.retrieveChecksum(in);
- success = true;
return in;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(in);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, in);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java
index 6af43624365..d6645d854c3 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java
@@ -87,7 +87,6 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
state.segmentSuffix,
Lucene99FlatVectorsFormat.VECTOR_DATA_EXTENSION);
- boolean success = false;
try {
meta = state.directory.createOutput(metaFileName, state.context);
vectorData = state.directory.createOutput(vectorDataFileName, state.context);
@@ -104,11 +103,9 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
Lucene99FlatVectorsFormat.VERSION_CURRENT,
state.segmentInfo.getId(),
state.segmentSuffix);
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -260,7 +257,6 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
segmentWriteState.directory.createTempOutput(
vectorData.getName(), "temp", segmentWriteState.context);
IndexInput vectorDataInput = null;
- boolean success = false;
try {
// write the vector data to a temporary file
DocsWithFieldSet docsWithField =
@@ -297,8 +293,10 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
vectorDataOffset,
vectorDataLength,
docsWithField);
- success = true;
+
final IndexInput finalVectorDataInput = vectorDataInput;
+ vectorDataInput = null;
+
final RandomVectorScorerSupplier randomVectorScorerSupplier =
switch (fieldInfo.getVectorEncoding()) {
case BYTE ->
@@ -329,12 +327,11 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
},
docsWithField.cardinality(),
randomVectorScorerSupplier);
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(vectorDataInput, tempVectorData);
- IOUtils.deleteFilesIgnoringExceptions(
- segmentWriteState.directory, tempVectorData.getName());
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, vectorDataInput, tempVectorData);
+ IOUtils.deleteFilesSuppressingExceptions(
+ t, segmentWriteState.directory, tempVectorData.getName());
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
index 8b31336eaf1..c950edc0f2d 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
@@ -81,7 +81,6 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
throws IOException {
this.fields = new IntObjectHashMap<>();
this.flatVectorsReader = flatVectorsReader;
- boolean success = false;
this.fieldInfos = state.fieldInfos;
String metaFileName =
IndexFileNames.segmentFileName(
@@ -117,11 +116,9 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
FileDataHint.KNN_VECTORS,
DataAccessHint.RANDOM,
PreloadHint.INSTANCE));
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -153,7 +150,6 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
String fileName =
IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, fileExtension);
IndexInput in = state.directory.openInput(fileName, context);
- boolean success = false;
try {
int versionVectorData =
CodecUtil.checkIndexHeader(
@@ -174,12 +170,10 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
in);
}
CodecUtil.retrieveChecksum(in);
- success = true;
return in;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(in);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, in);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsWriter.java
index 5c2e0855f67..1334eab7dc8 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsWriter.java
@@ -104,7 +104,6 @@ public final class Lucene99HnswVectorsWriter extends KnnVectorsWriter {
state.segmentSuffix,
Lucene99HnswVectorsFormat.VECTOR_INDEX_EXTENSION);
- boolean success = false;
try {
meta = state.directory.createOutput(metaFileName, state.context);
vectorIndex = state.directory.createOutput(indexDataFileName, state.context);
@@ -121,11 +120,9 @@ public final class Lucene99HnswVectorsWriter extends KnnVectorsWriter {
Lucene99HnswVectorsFormat.VERSION_CURRENT,
state.segmentInfo.getId(),
state.segmentSuffix);
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -354,7 +351,6 @@ public final class Lucene99HnswVectorsWriter extends KnnVectorsWriter {
public void mergeOneField(FieldInfo fieldInfo, MergeState mergeState) throws IOException {
CloseableRandomVectorScorerSupplier scorerSupplier =
flatVectorWriter.mergeOneFieldToIndex(fieldInfo, mergeState);
- boolean success = false;
try {
long vectorIndexOffset = vectorIndex.getFilePointer();
// build the graph using the temporary vector data
@@ -403,14 +399,11 @@ public final class Lucene99HnswVectorsWriter extends KnnVectorsWriter {
scorerSupplier.totalVectorCount(),
graph,
vectorIndexNodeOffsets);
- success = true;
- } finally {
- if (success) {
- IOUtils.close(scorerSupplier);
- } else {
- IOUtils.closeWhileHandlingException(scorerSupplier);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, scorerSupplier);
+ throw t;
}
+ IOUtils.close(scorerSupplier);
}
/**
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
index d48831b742a..59ff0a0beae 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
@@ -80,7 +80,6 @@ public final class Lucene99ScalarQuantizedVectorsReader extends FlatVectorsReade
state.segmentInfo.name,
state.segmentSuffix,
Lucene99ScalarQuantizedVectorsFormat.META_EXTENSION);
- boolean success = false;
try (ChecksumIndexInput meta = state.directory.openChecksumInput(metaFileName)) {
Throwable priorE = null;
try {
@@ -108,11 +107,9 @@ public final class Lucene99ScalarQuantizedVectorsReader extends FlatVectorsReade
// graph.
state.context.withHints(
FileTypeHint.DATA, FileDataHint.KNN_VECTORS, DataAccessHint.RANDOM));
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -222,7 +219,6 @@ public final class Lucene99ScalarQuantizedVectorsReader extends FlatVectorsReade
String fileName =
IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, fileExtension);
IndexInput in = state.directory.openInput(fileName, context);
- boolean success = false;
try {
int versionVectorData =
CodecUtil.checkIndexHeader(
@@ -243,12 +239,10 @@ public final class Lucene99ScalarQuantizedVectorsReader extends FlatVectorsReade
in);
}
CodecUtil.retrieveChecksum(in);
- success = true;
return in;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(in);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, in);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsWriter.java
index bcfbb30c261..c3b48a3d6d3 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsWriter.java
@@ -167,7 +167,6 @@ public final class Lucene99ScalarQuantizedVectorsWriter extends FlatVectorsWrite
state.segmentSuffix,
Lucene99ScalarQuantizedVectorsFormat.VECTOR_DATA_EXTENSION);
this.rawVectorDelegate = rawVectorDelegate;
- boolean success = false;
try {
meta = state.directory.createOutput(metaFileName, state.context);
quantizedVectorData =
@@ -185,11 +184,9 @@ public final class Lucene99ScalarQuantizedVectorsWriter extends FlatVectorsWrite
version,
state.segmentInfo.getId(),
state.segmentSuffix);
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
@@ -480,7 +477,6 @@ public final class Lucene99ScalarQuantizedVectorsWriter extends FlatVectorsWrite
segmentWriteState.directory.createTempOutput(
quantizedVectorData.getName(), "temp", segmentWriteState.context);
IndexInput quantizationDataInput = null;
- boolean success = false;
try {
MergedQuantizedVectorValues byteVectorValues =
MergedQuantizedVectorValues.mergeQuantizedByteVectorValues(
@@ -507,8 +503,9 @@ public final class Lucene99ScalarQuantizedVectorsWriter extends FlatVectorsWrite
mergedQuantizationState.getLowerQuantile(),
mergedQuantizationState.getUpperQuantile(),
docsWithField);
- success = true;
final IndexInput finalQuantizationDataInput = quantizationDataInput;
+ quantizationDataInput = null;
+
return new ScalarQuantizedCloseableRandomVectorScorerSupplier(
() -> {
IOUtils.close(finalQuantizationDataInput);
@@ -524,13 +521,12 @@ public final class Lucene99ScalarQuantizedVectorsWriter extends FlatVectorsWrite
compress,
fieldInfo.getVectorSimilarityFunction(),
vectorsScorer,
- quantizationDataInput)));
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(tempQuantizedVectorData, quantizationDataInput);
- IOUtils.deleteFilesIgnoringExceptions(
- segmentWriteState.directory, tempQuantizedVectorData.getName());
- }
+ finalQuantizationDataInput)));
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, tempQuantizedVectorData, quantizationDataInput);
+ IOUtils.deleteFilesSuppressingExceptions(
+ t, segmentWriteState.directory, tempQuantizedVectorData.getName());
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldDocValuesFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldDocValuesFormat.java
index 9350c016f67..58407de98c3 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldDocValuesFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldDocValuesFormat.java
@@ -281,7 +281,6 @@ public abstract class PerFieldDocValuesFormat extends DocValuesFormat {
public FieldsReader(final SegmentReadState readState) throws IOException {
// Init each unique format:
- boolean success = false;
try {
// Read field name -> format name
for (FieldInfo fi : readState.fieldInfos) {
@@ -307,11 +306,9 @@ public abstract class PerFieldDocValuesFormat extends DocValuesFormat {
}
}
}
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(formats.values());
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, formats.values());
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldKnnVectorsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldKnnVectorsFormat.java
index cb2b596133d..1fd0f329a8a 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldKnnVectorsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldKnnVectorsFormat.java
@@ -206,7 +206,6 @@ public abstract class PerFieldKnnVectorsFormat extends KnnVectorsFormat {
public FieldsReader(final SegmentReadState readState) throws IOException {
this.fieldInfos = readState.fieldInfos;
// Init each unique format:
- boolean success = false;
Map<String, KnnVectorsReader> formats = new HashMap<>();
try {
// Read field name -> format name
@@ -233,11 +232,9 @@ public abstract class PerFieldKnnVectorsFormat extends KnnVectorsFormat {
}
}
}
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(formats.values());
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, formats.values());
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java
index 182bed5797c..b75098ab228 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java
@@ -143,7 +143,6 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {
Map<PostingsFormat, FieldsGroup> formatToGroups = buildFieldsGroupMapping(fields);
// Write postings
- boolean success = false;
try {
for (Map.Entry<PostingsFormat, FieldsGroup> ent : formatToGroups.entrySet()) {
PostingsFormat format = ent.getKey();
@@ -162,11 +161,9 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {
toClose.add(consumer);
consumer.write(maskedFields, norms);
}
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(toClose);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, toClose);
+ throw t;
}
}
@@ -184,7 +181,6 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {
Map<PostingsFormat, FieldsGroup> formatToGroups = buildFieldsGroupMapping(indexedFieldNames);
// Merge postings
- boolean success = false;
try {
for (Map.Entry<PostingsFormat, FieldsGroup> ent : formatToGroups.entrySet()) {
PostingsFormat format = ent.getKey();
@@ -194,11 +190,9 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {
toClose.add(consumer);
consumer.merge(PerFieldMergeState.restrictFields(mergeState, group.fields), norms);
}
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(toClose);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, toClose);
+ throw t;
}
}
@@ -297,7 +291,6 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {
public FieldsReader(final SegmentReadState readState) throws IOException {
// Read _X.per and init each format:
- boolean success = false;
try {
// Read field name -> format name
for (FieldInfo fi : readState.fieldInfos) {
@@ -322,11 +315,9 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {
}
}
}
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(formats.values());
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, formats.values());
+ throw t;
}
this.segment = readState.segmentInfo.name;
diff --git a/lucene/core/src/java/org/apache/lucene/store/Directory.java b/lucene/core/src/java/org/apache/lucene/store/Directory.java
index 369e6f45473..80236f5c186 100644
--- a/lucene/core/src/java/org/apache/lucene/store/Directory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/Directory.java
@@ -176,15 +176,12 @@ public abstract class Directory implements Closeable {
*/
public void copyFrom(Directory from, String src, String dest, IOContext context)
throws IOException {
- boolean success = false;
try (IndexInput is = from.openInput(src, IOContext.READONCE);
IndexOutput os = createOutput(dest, context)) {
os.copyBytes(is, is.length());
- success = true;
- } finally {
- if (!success) {
- IOUtils.deleteFilesIgnoringExceptions(this, dest);
- }
+ } catch (Throwable t) {
+ IOUtils.deleteFilesSuppressingExceptions(t, this, dest);
+ throw t;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
index 3840eb60948..e1dcb9514ec 100644
--- a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
+++ b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
@@ -95,7 +95,49 @@ public final class IOUtils {
}
/**
- * Closes all given <code>Closeable</code>s, suppressing all thrown exceptions. Some of the <code>
+ * Closes all given <code>Closeable</code>s, suppressing all thrown Throwables in {@code ex}. Some
+ * of the <code>
+ * Closeable</code>s may be null, they are ignored.
+ *
+ * @param objects objects to call <code>close()</code> on
+ */
+ public static void closeWhileSuppressingExceptions(Throwable ex, Closeable... objects) {
+ closeWhileSuppressingExceptions(ex, Arrays.asList(objects));
+ }
+
+ /**
+ * Closes all given <code>Closeable</code>s, suppressing all thrown Throwables in {@code ex}. Even
+ * if a {@link Error} is thrown all given closeable are closed.
+ *
+ * @see #closeWhileHandlingException(Closeable...)
+ */
+ public static void closeWhileSuppressingExceptions(
+ Throwable ex, Iterable<? extends Closeable> objects) {
+ Error firstError = ex instanceof Error err ? err : null;
+
+ for (Closeable object : objects) {
+ try {
+ if (object != null) {
+ object.close();
+ }
+ } catch (Throwable e) {
+ if (firstError == null && e instanceof Error err) {
+ // don't try and suppress it - this Error should be the thing that is thrown
+ firstError = err;
+ firstError.addSuppressed(ex);
+ } else {
+ ex.addSuppressed(e);
+ }
+ }
+ }
+
+ if (firstError != null) {
+ throw firstError;
+ }
+ }
+
+ /**
+ * Closes all given <code>Closeable</code>s, ignoring all thrown exceptions. Some of the <code>
* Closeable</code>s may be null, they are ignored.
*
* @param objects objects to call <code>close()</code> on
@@ -105,7 +147,7 @@ public final class IOUtils {
}
/**
- * Closes all given <code>Closeable</code>s, suppressing all thrown non {@link Error} exceptions.
+ * Closes all given <code>Closeable</code>s, ignoring all thrown non {@link Error} exceptions.
* Even if a {@link Error} is thrown all given closeable are closed.
*
* @see #closeWhileHandlingException(Closeable...)
@@ -174,7 +216,7 @@ public final class IOUtils {
}
/**
- * Deletes all given files, suppressing all thrown IOExceptions.
+ * Deletes all given files, ignoring all thrown Throwables.
*
* <p>Note that the files should not be null.
*/
@@ -190,10 +232,53 @@ public final class IOUtils {
}
}
+ /**
+ * Deletes all given files, ignoring all thrown Throwables.
+ *
+ * <p>Note that the files should not be null.
+ */
public static void deleteFilesIgnoringExceptions(Directory dir, String... files) {
deleteFilesIgnoringExceptions(dir, Arrays.asList(files));
}
+ /**
+ * Deletes all given files, suppressing all thrown Throwables in {@code ex}.
+ *
+ * <p>Note that the files should not be null.
+ */
+ public static void deleteFilesSuppressingExceptions(
+ Throwable ex, Directory dir, Collection<String> files) {
+ Error firstError = ex instanceof Error err ? err : null;
+
+ for (String name : files) {
+ try {
+ dir.deleteFile(name);
+ } catch (Throwable d) {
+ if (firstError == null && d instanceof Error err) {
+ // don't try and suppress it - this Error should be the thing that is thrown
+ firstError = err;
+ firstError.addSuppressed(ex);
+ } else {
+ ex.addSuppressed(d);
+ }
+ }
+ }
+
+ if (firstError != null) {
+ throw firstError;
+ }
+ }
+
+ /**
+ * Deletes all given files, suppressing all thrown Throwables in {@code ex}.
+ *
+ * <p>Note that the files should not be null.
+ */
+ public static void deleteFilesSuppressingExceptions(
+ Throwable ex, Directory dir, String... files) {
+ deleteFilesSuppressingExceptions(ex, dir, Arrays.asList(files));
+ }
+
/**
* Deletes all given file names. Some of the file names may be null; they are ignored. After
* everything is deleted, the method either throws the first exception it hit while deleting, or
@@ -220,7 +305,7 @@ public final class IOUtils {
}
/**
- * Deletes all given files, suppressing all thrown IOExceptions.
+ * Deletes all given files, ignoring all thrown Throwables.
*
* <p>Some of the files may be null, if so they are ignored.
*/
@@ -229,7 +314,7 @@ public final class IOUtils {
}
/**
- * Deletes all given files, suppressing all thrown IOExceptions.
+ * Deletes all given files, ignoring all thrown Throwables.
*
* <p>Some of the files may be null, if so they are ignored.
*/
@@ -247,6 +332,33 @@ public final class IOUtils {
}
}
+ /**
+ * Deletes all given files, suppressing all thrown Throwables in {@code ex}.
+ *
+ * <p>Some of the files may be null, if so they are ignored.
+ */
+ public static void deleteFilesSuppressingExceptions(Throwable ex, Path... files) {
+ deleteFilesSuppressingExceptions(ex, Arrays.asList(files));
+ }
+
+ /**
+ * Deletes all given files, suppressing all thrown Throwables in {@code ex}.
+ *
+ * <p>Some of the files may be null, if so they are ignored.
+ */
+ public static void deleteFilesSuppressingExceptions(
+ Throwable ex, Collection<? extends Path> files) {
+ for (Path name : files) {
+ if (name != null) {
+ try {
+ Files.delete(name);
+ } catch (Throwable d) {
+ ex.addSuppressed(d);
+ }
+ }
+ }
+ }
+
/**
* Deletes all given <code>Path</code>s, if they exist. Some of the <code>File</code>s may be
* null; they are ignored. After everything is deleted, the method either throws the first
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java b/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java
index 44c7b3add64..e08bc0ef0c5 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java
@@ -16,6 +16,13 @@
*/
package org.apache.lucene.util;
+import static org.hamcrest.Matchers.arrayContaining;
+import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.sameInstance;
+
+import java.io.Closeable;
+import java.io.IOError;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
@@ -66,6 +73,42 @@ public class TestIOUtils extends LuceneTestCase {
// actually deletes file2
}
+ public void testCloseExposesErrors() {
+ Closeable exceptionClose =
+ () -> {
+ throw new IOException("IO");
+ };
+ Closeable errorClose =
+ () -> {
+ throw new IOError(new IOException("IOERR"));
+ };
+
+ Exception ex = new Exception("Ex");
+ IOUtils.closeWhileSuppressingExceptions(ex, exceptionClose);
+ assertThat(ex.getSuppressed(), arrayContaining(instanceOf(IOException.class)));
+
+ Error topErr = new Error("Err");
+ Error thrown =
+ expectThrows(
+ Error.class,
+ () -> IOUtils.closeWhileSuppressingExceptions(topErr, exceptionClose, errorClose));
+ assertThat(thrown, sameInstance(topErr));
+ assertThat(
+ thrown.getSuppressed(),
+ arrayContainingInAnyOrder(instanceOf(IOException.class), instanceOf(IOError.class)));
+
+ // the IOError takes precedence, and is thrown in preference to the Exception
+ Exception suppressedEx = new Exception("Ex");
+ thrown =
+ expectThrows(
+ IOError.class,
+ () ->
+ IOUtils.closeWhileSuppressingExceptions(suppressedEx, exceptionClose, errorClose));
+ assertThat(thrown.getSuppressed(), arrayContaining(suppressedEx));
+ assertThat(
+ thrown.getSuppressed()[0].getSuppressed(), arrayContaining(instanceOf(IOException.class)));
+ }
+
public void testDeleteFileIfExists() throws Exception {
Path dir = createTempDir();
Path file1 = dir.resolve("file1");