Skip to content

Apache Lucene open-source search software

Remove boolean success from various codec classes (#14995)

Simon Cooper6e712fc089a2
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java
index d5bdc3a83f3..91d18ff9250 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java
@@ -107,7 +107,6 @@ public class BlockTermsReader extends FieldsProducer {
state.segmentInfo.name, state.segmentSuffix, BlockTermsWriter.TERMS_EXTENSION);
in = state.directory.openInput(filename, state.context);
- boolean success = false;
try {
CodecUtil.checkIndexHeader(
in,
@@ -171,11 +170,9 @@ public class BlockTermsReader extends FieldsProducer {
throw new CorruptIndexException("duplicate fields: " + fieldInfo.name, in);
}
}
- success = true;
- } finally {
- if (!success) {
- in.close();
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, in);
+ throw t;
}
this.indexReader = indexReader;
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java
index 9fbde960083..3a7dacaa527 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java
@@ -96,7 +96,6 @@ public class BlockTermsWriter extends FieldsConsumer {
this.termsIndexWriter = termsIndexWriter;
maxDoc = state.segmentInfo.maxDoc();
out = state.directory.createOutput(termsFileName, state.context);
- boolean success = false;
try {
fieldInfos = state.fieldInfos;
CodecUtil.writeIndexHeader(
@@ -108,11 +107,9 @@ public class BlockTermsWriter extends FieldsConsumer {
// System.out.println("BTW.init seg=" + state.segmentName);
postingsWriter.init(out, state); // have consumer write its format/header
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(out);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, out);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.java
index 64419703f85..698af805f59 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.java
@@ -31,7 +31,6 @@ import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.Accountables;
import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.PagedBytes;
import org.apache.lucene.util.packed.MonotonicBlockPackedReader;
@@ -68,11 +67,8 @@ public class FixedGapTermsIndexReader extends TermsIndexReaderBase {
state.segmentInfo.name,
state.segmentSuffix,
FixedGapTermsIndexWriter.TERMS_INDEX_EXTENSION);
- final IndexInput in = state.directory.openInput(fileName, state.context);
- boolean success = false;
-
- try {
+ try (IndexInput in = state.directory.openInput(fileName, state.context)) {
CodecUtil.checkIndexHeader(
in,
@@ -138,13 +134,7 @@ public class FixedGapTermsIndexReader extends TermsIndexReaderBase {
throw new CorruptIndexException("duplicate field: " + fieldInfo.name, in);
}
}
- success = true;
} finally {
- if (success) {
- IOUtils.close(in);
- } else {
- IOUtils.closeWhileHandlingException(in);
- }
termBytesReader = termBytes.freeze(true);
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexWriter.java
index b92f7bf51ed..8d3ee8789e0 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexWriter.java
@@ -70,18 +70,15 @@ public class FixedGapTermsIndexWriter extends TermsIndexWriterBase {
IndexFileNames.segmentFileName(
state.segmentInfo.name, state.segmentSuffix, TERMS_INDEX_EXTENSION);
out = state.directory.createOutput(indexFileName, state.context);
- boolean success = false;
try {
CodecUtil.writeIndexHeader(
out, CODEC_NAME, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
out.writeVInt(termIndexInterval);
out.writeVInt(PackedInts.VERSION_CURRENT);
out.writeVInt(BLOCKSIZE);
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(out);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, out);
+ throw t;
}
}
@@ -207,8 +204,7 @@ public class FixedGapTermsIndexWriter extends TermsIndexWriterBase {
@Override
public void close() throws IOException {
if (out != null) {
- boolean success = false;
- try {
+ try (IndexOutput _ = out) {
final long dirStart = out.getFilePointer();
final int fieldCount = fields.size();
@@ -234,13 +230,7 @@ public class FixedGapTermsIndexWriter extends TermsIndexWriterBase {
}
writeTrailer(dirStart);
CodecUtil.writeFooter(out);
- success = true;
} finally {
- if (success) {
- IOUtils.close(out);
- } else {
- IOUtils.closeWhileHandlingException(out);
- }
out = null;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/VariableGapTermsIndexWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/VariableGapTermsIndexWriter.java
index ab0ad75ca8a..ce9122a1fb4 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/VariableGapTermsIndexWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/VariableGapTermsIndexWriter.java
@@ -187,7 +187,6 @@ public class VariableGapTermsIndexWriter extends TermsIndexWriterBase {
IndexFileNames.segmentFileName(
state.segmentInfo.name, state.segmentSuffix, TERMS_INDEX_EXTENSION);
- boolean success = false;
try {
metaOut = state.directory.createOutput(metaFileName, state.context);
out = state.directory.createOutput(indexFileName, state.context);
@@ -199,11 +198,9 @@ public class VariableGapTermsIndexWriter extends TermsIndexWriterBase {
state.segmentSuffix);
CodecUtil.writeIndexHeader(
out, CODEC_NAME, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/BlockTreeOrdsPostingsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/BlockTreeOrdsPostingsFormat.java
index 68c842c72e9..f27908bf1e9 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/BlockTreeOrdsPostingsFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/BlockTreeOrdsPostingsFormat.java
@@ -69,31 +69,23 @@ public class BlockTreeOrdsPostingsFormat extends PostingsFormat {
public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException {
PostingsWriterBase postingsWriter = new Lucene103PostingsWriter(state);
- boolean success = false;
try {
- FieldsConsumer ret =
- new OrdsBlockTreeTermsWriter(state, postingsWriter, minTermBlockSize, maxTermBlockSize);
- success = true;
- return ret;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsWriter);
- }
+ return new OrdsBlockTreeTermsWriter(
+ 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 OrdsBlockTreeTermsReader(postingsReader, state);
- success = true;
- return ret;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsReader);
- }
+ return new OrdsBlockTreeTermsReader(postingsReader, state);
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, postingsReader);
+ throw t;
}
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsReader.java
index ef95f68a697..1469d53d388 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsReader.java
@@ -63,7 +63,6 @@ public final class OrdsBlockTreeTermsReader extends FieldsProducer {
state.segmentInfo.name, state.segmentSuffix, OrdsBlockTreeTermsWriter.TERMS_EXTENSION);
in = state.directory.openInput(termsFile, state.context);
- boolean success = false;
IndexInput indexIn = null;
try {
@@ -173,13 +172,9 @@ public final class OrdsBlockTreeTermsReader extends FieldsProducer {
}
}
indexIn.close();
-
- success = true;
- } finally {
- if (!success) {
- // this.close() will close in:
- IOUtils.closeWhileHandlingException(indexIn, this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, indexIn, this);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java
index 8518afabab8..67f4a966b0f 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java
@@ -178,7 +178,6 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {
IndexFileNames.segmentFileName(
state.segmentInfo.name, state.segmentSuffix, TERMS_EXTENSION);
out = state.directory.createOutput(termsFileName, state.context);
- boolean success = false;
IndexOutput indexOut = null;
try {
fieldInfos = state.fieldInfos;
@@ -204,11 +203,9 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {
// System.out.println("BTW.init seg=" + state.segmentName);
postingsWriter.init(out, state); // have consumer write its format/header
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(out, indexOut);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, out, indexOut);
+ throw t;
}
this.indexOut = indexOut;
}
@@ -928,9 +925,9 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {
}
closed = true;
- boolean success = false;
- try {
-
+ try (out;
+ indexOut;
+ postingsWriter) {
final long dirStart = out.getFilePointer();
final long indexDirStart = indexOut.getFilePointer();
@@ -960,13 +957,6 @@ public final class OrdsBlockTreeTermsWriter extends FieldsConsumer {
CodecUtil.writeFooter(out);
indexOut.writeLong(indexDirStart);
CodecUtil.writeFooter(indexOut);
- success = true;
- } finally {
- if (success) {
- IOUtils.close(out, indexOut, postingsWriter);
- } else {
- IOUtils.closeWhileHandlingException(out, indexOut, postingsWriter);
- }
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/bloom/BloomFilteringPostingsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/bloom/BloomFilteringPostingsFormat.java
index 2a0472fa028..76e9bf4ecf4 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/bloom/BloomFilteringPostingsFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/bloom/BloomFilteringPostingsFormat.java
@@ -151,10 +151,8 @@ public final class BloomFilteringPostingsFormat extends PostingsFormat {
String bloomFileName =
IndexFileNames.segmentFileName(
state.segmentInfo.name, state.segmentSuffix, BLOOM_EXTENSION);
- ChecksumIndexInput bloomIn = null;
- boolean success = false;
- try {
- bloomIn = state.directory.openChecksumInput(bloomFileName);
+
+ try (ChecksumIndexInput bloomIn = state.directory.openChecksumInput(bloomFileName)) {
CodecUtil.checkIndexHeader(
bloomIn,
BLOOM_CODEC_NAME,
@@ -176,12 +174,9 @@ public final class BloomFilteringPostingsFormat extends PostingsFormat {
bloomsByFieldName.put(fieldInfo.name, bloom);
}
CodecUtil.checkFooter(bloomIn);
- IOUtils.close(bloomIn);
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(bloomIn, delegateFieldsProducer);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, delegateFieldsProducer);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTPostingsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTPostingsFormat.java
index 224d47e8f91..e535ccd33db 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTPostingsFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTPostingsFormat.java
@@ -43,30 +43,22 @@ public final class FSTPostingsFormat extends PostingsFormat {
public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException {
PostingsWriterBase postingsWriter = new Lucene103PostingsWriter(state);
- boolean success = false;
try {
- FieldsConsumer ret = new FSTTermsWriter(state, postingsWriter);
- success = true;
- return ret;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsWriter);
- }
+ return new FSTTermsWriter(state, postingsWriter);
+ } 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 FSTTermsReader(state, postingsReader);
- success = true;
- return ret;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsReader);
- }
+ return new FSTTermsReader(state, postingsReader);
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, postingsReader);
+ throw t;
}
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsReader.java
index 4ab0c55b5d3..943298ffbf9 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsReader.java
@@ -81,7 +81,6 @@ public class FSTTermsReader extends FieldsProducer {
IndexInput in = this.fstTermsInput;
- boolean success = false;
try {
CodecUtil.checkIndexHeader(
in,
@@ -110,11 +109,9 @@ public class FSTTermsReader extends FieldsProducer {
TermsReader previous = fields.put(fieldInfo.name, current);
checkFieldSummary(state.segmentInfo, in, current, previous);
}
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(in);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, in);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java
index 1e8c442ccc6..73d864d3579 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java
@@ -129,7 +129,6 @@ public class FSTTermsWriter extends FieldsConsumer {
this.out = state.directory.createOutput(termsFileName, state.context);
this.maxDoc = state.segmentInfo.maxDoc();
- boolean success = false;
try {
CodecUtil.writeIndexHeader(
out,
@@ -139,11 +138,9 @@ public class FSTTermsWriter extends FieldsConsumer {
state.segmentSuffix);
this.postingsWriter.init(out, state);
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(out);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, out);
+ throw t;
}
}
@@ -188,8 +185,8 @@ public class FSTTermsWriter extends FieldsConsumer {
@Override
public void close() throws IOException {
if (out != null) {
- boolean success = false;
- try {
+ try (IndexOutput _ = out;
+ postingsWriter) {
// write field summary
final long dirStart = out.getFilePointer();
@@ -206,13 +203,7 @@ public class FSTTermsWriter extends FieldsConsumer {
}
writeTrailer(out, dirStart);
CodecUtil.writeFooter(out);
- success = true;
} finally {
- if (success) {
- IOUtils.close(out, postingsWriter);
- } else {
- IOUtils.closeWhileHandlingException(out, postingsWriter);
- }
out = null;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDWriter.java
index 93c97869143..4ee760a106e 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDWriter.java
@@ -687,7 +687,6 @@ final class SimpleTextBKDWriter implements Closeable {
BKDRadixSelector radixSelector =
new BKDRadixSelector(config, maxPointsSortInHeap, tempDir, tempFileNamePrefix);
- boolean success = false;
try {
build(
@@ -706,12 +705,9 @@ final class SimpleTextBKDWriter implements Closeable {
assert tempDir.getCreatedFiles().isEmpty();
// System.out.println("write time: " + ((System.nanoTime() - t1) / (double)
// TimeUnit.MILLISECONDS.toNanos(1)) + " ms");
-
- success = true;
- } finally {
- if (success == false) {
- IOUtils.deleteFilesIgnoringExceptions(tempDir, tempDir.getCreatedFiles());
- }
+ } catch (Throwable t) {
+ IOUtils.deleteFilesSuppressingExceptions(t, tempDir, tempDir.getCreatedFiles());
+ throw t;
}
// Write index:
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesWriter.java
index aefdeba2c2d..61e0d58501e 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesWriter.java
@@ -40,7 +40,6 @@ import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder;
-import org.apache.lucene.util.IOUtils;
class SimpleTextDocValuesWriter extends DocValuesConsumer {
static final BytesRef END = new BytesRef("END");
@@ -583,20 +582,13 @@ class SimpleTextDocValuesWriter extends DocValuesConsumer {
@Override
public void close() throws IOException {
if (data != null) {
- boolean success = false;
- try {
+ try (IndexOutput _ = data) {
assert !fieldsSeen.isEmpty();
// TODO: sheisty to do this here?
SimpleTextUtil.write(data, END);
SimpleTextUtil.writeNewline(data);
SimpleTextUtil.writeChecksum(data, scratch);
- success = true;
} finally {
- if (success) {
- IOUtils.close(data);
- } else {
- IOUtils.closeWhileHandlingException(data);
- }
data = null;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosFormat.java
index fefbb44bd80..ee8c111e5b8 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosFormat.java
@@ -37,7 +37,6 @@ import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder;
-import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.StringHelper;
/**
@@ -82,11 +81,9 @@ public class SimpleTextFieldInfosFormat extends FieldInfosFormat {
throws IOException {
final String fileName =
IndexFileNames.segmentFileName(segmentInfo.name, segmentSuffix, FIELD_INFOS_EXTENSION);
- ChecksumIndexInput input = directory.openChecksumInput(fileName);
BytesRefBuilder scratch = new BytesRefBuilder();
- boolean success = false;
- try {
+ try (ChecksumIndexInput input = directory.openChecksumInput(fileName)) {
SimpleTextUtil.readLine(input, scratch);
assert StringHelper.startsWith(scratch.get(), NUMFIELDS);
@@ -206,15 +203,7 @@ public class SimpleTextFieldInfosFormat extends FieldInfosFormat {
SimpleTextUtil.checkFooter(input);
- FieldInfos fieldInfos = new FieldInfos(infos);
- success = true;
- return fieldInfos;
- } finally {
- if (success) {
- input.close();
- } else {
- IOUtils.closeWhileHandlingException(input);
- }
+ return new FieldInfos(infos);
}
}
@@ -248,10 +237,8 @@ public class SimpleTextFieldInfosFormat extends FieldInfosFormat {
throws IOException {
final String fileName =
IndexFileNames.segmentFileName(segmentInfo.name, segmentSuffix, FIELD_INFOS_EXTENSION);
- IndexOutput out = directory.createOutput(fileName, context);
BytesRefBuilder scratch = new BytesRefBuilder();
- boolean success = false;
- try {
+ try (IndexOutput out = directory.createOutput(fileName, context)) {
SimpleTextUtil.write(out, NUMFIELDS);
SimpleTextUtil.write(out, Integer.toString(infos.size()), scratch);
SimpleTextUtil.writeNewline(out);
@@ -347,13 +334,6 @@ public class SimpleTextFieldInfosFormat extends FieldInfosFormat {
SimpleTextUtil.writeNewline(out);
}
SimpleTextUtil.writeChecksum(out, scratch);
- success = true;
- } finally {
- if (success) {
- out.close();
- } else {
- IOUtils.closeWhileHandlingException(out);
- }
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java
index 90087cf964e..7fb1787a769 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java
@@ -80,14 +80,11 @@ class SimpleTextFieldsReader extends FieldsProducer {
SimpleTextPostingsFormat.getPostingsFileName(
state.segmentInfo.name, state.segmentSuffix),
state.context);
- boolean success = false;
try {
fields = readFields(in.clone());
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsReader.java
index a1bc4fc5257..be2896d1cda 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsReader.java
@@ -79,7 +79,6 @@ public class SimpleTextKnnVectorsReader extends KnnVectorsReader {
readState.segmentSuffix,
SimpleTextKnnVectorsFormat.VECTOR_EXTENSION);
- boolean success = false;
try (ChecksumIndexInput in = readState.directory.openChecksumInput(metaFileName)) {
int fieldNumber = readInt(in, FIELD_NUMBER);
while (fieldNumber != -1) {
@@ -106,11 +105,9 @@ public class SimpleTextKnnVectorsReader extends KnnVectorsReader {
SimpleTextUtil.checkFooter(in);
dataIn = readState.directory.openInput(vectorFileName, IOContext.DEFAULT);
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsWriter.java
index eaf4b657755..c7d5840658c 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsWriter.java
@@ -49,7 +49,6 @@ public class SimpleTextKnnVectorsWriter extends BufferingKnnVectorsWriter {
private final BytesRefBuilder scratch = new BytesRefBuilder();
SimpleTextKnnVectorsWriter(SegmentWriteState state) throws IOException {
- boolean success = false;
// exception handling to pass TestSimpleTextKnnVectorsFormat#testRandomExceptions
try {
String metaFileName =
@@ -65,11 +64,9 @@ public class SimpleTextKnnVectorsWriter extends BufferingKnnVectorsWriter {
state.segmentSuffix,
SimpleTextKnnVectorsFormat.VECTOR_EXTENSION);
vectorData = state.directory.createOutput(vectorDataFileName, state.context);
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextLiveDocsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextLiveDocsFormat.java
index 50e9bcb0c7e..d2851c87b2d 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextLiveDocsFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextLiveDocsFormat.java
@@ -31,7 +31,6 @@ import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder;
import org.apache.lucene.util.CharsRefBuilder;
-import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.StringHelper;
/**
@@ -58,11 +57,8 @@ public class SimpleTextLiveDocsFormat extends LiveDocsFormat {
String fileName =
IndexFileNames.fileNameFromGeneration(info.info.name, LIVEDOCS_EXTENSION, info.getDelGen());
- ChecksumIndexInput in = null;
- boolean success = false;
- try {
- in = dir.openChecksumInput(fileName);
+ try (ChecksumIndexInput in = dir.openChecksumInput(fileName)) {
SimpleTextUtil.readLine(in, scratch);
assert StringHelper.startsWith(scratch.get(), SIZE);
int size = parseIntAt(scratch.get(), SIZE.length, scratchUTF16);
@@ -79,14 +75,7 @@ public class SimpleTextLiveDocsFormat extends LiveDocsFormat {
SimpleTextUtil.checkFooter(in);
- success = true;
return new SimpleTextBits(bits, size);
- } finally {
- if (success) {
- IOUtils.close(in);
- } else {
- IOUtils.closeWhileHandlingException(in);
- }
}
}
@@ -105,10 +94,8 @@ public class SimpleTextLiveDocsFormat extends LiveDocsFormat {
String fileName =
IndexFileNames.fileNameFromGeneration(
info.info.name, LIVEDOCS_EXTENSION, info.getNextDelGen());
- IndexOutput out = null;
- boolean success = false;
- try {
- out = dir.createOutput(fileName, context);
+
+ try (IndexOutput out = dir.createOutput(fileName, context)) {
SimpleTextUtil.write(out, SIZE);
SimpleTextUtil.write(out, Integer.toString(size), scratch);
SimpleTextUtil.writeNewline(out);
@@ -124,13 +111,6 @@ public class SimpleTextLiveDocsFormat extends LiveDocsFormat {
SimpleTextUtil.write(out, END);
SimpleTextUtil.writeNewline(out);
SimpleTextUtil.writeChecksum(out, scratch);
- success = true;
- } finally {
- if (success) {
- IOUtils.close(out);
- } else {
- IOUtils.closeWhileHandlingException(out);
- }
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPointsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPointsReader.java
index 5d6c41663ca..1c03684f691 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPointsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPointsReader.java
@@ -83,7 +83,6 @@ class SimpleTextPointsReader extends PointsReader {
SimpleTextUtil.checkFooter(in);
}
- boolean success = false;
String fileName =
IndexFileNames.segmentFileName(
readState.segmentInfo.name,
@@ -94,11 +93,9 @@ class SimpleTextPointsReader extends PointsReader {
for (Map.Entry<String, Long> ent : fieldToFileOffset.entrySet()) {
readers.put(ent.getKey(), initReader(ent.getValue()));
}
- success = true;
- } finally {
- if (success == false) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
this.readState = readState;
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsReader.java
index d95e82f62ec..ff396af78a4 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsReader.java
@@ -69,23 +69,15 @@ public class SimpleTextStoredFieldsReader extends StoredFieldsReader {
public SimpleTextStoredFieldsReader(
Directory directory, SegmentInfo si, FieldInfos fn, IOContext context) throws IOException {
this.fieldInfos = fn;
- boolean success = false;
try {
in =
directory.openInput(
IndexFileNames.segmentFileName(
si.name, "", SimpleTextStoredFieldsWriter.FIELDS_EXTENSION),
context);
- success = true;
- } finally {
- if (!success) {
- try {
- close();
- } catch (
- @SuppressWarnings("unused")
- Throwable t) {
- } // ensure we throw our original exception
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
readIndex(si.maxDoc());
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsWriter.java
index 34a270672cb..d49ca07dc0b 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsWriter.java
@@ -58,16 +58,13 @@ public class SimpleTextStoredFieldsWriter extends StoredFieldsWriter {
public SimpleTextStoredFieldsWriter(Directory directory, String segment, IOContext context)
throws IOException {
- boolean success = false;
try {
out =
directory.createOutput(
IndexFileNames.segmentFileName(segment, "", FIELDS_EXTENSION), context);
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java
index e2795851763..3751309c345 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java
@@ -78,21 +78,13 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader {
public SimpleTextTermVectorsReader(Directory directory, SegmentInfo si, IOContext context)
throws IOException {
- boolean success = false;
try {
in =
directory.openInput(
IndexFileNames.segmentFileName(si.name, "", VECTORS_EXTENSION), context);
- success = true;
- } finally {
- if (!success) {
- try {
- close();
- } catch (
- @SuppressWarnings("unused")
- Throwable t) {
- } // ensure we throw our original exception
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
readIndex(si.maxDoc());
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsWriter.java
index 9b9f0cb0ede..b6ec1d450e6 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsWriter.java
@@ -63,16 +63,13 @@ public class SimpleTextTermVectorsWriter extends TermVectorsWriter {
public SimpleTextTermVectorsWriter(Directory directory, String segment, IOContext context)
throws IOException {
- boolean success = false;
try {
out =
directory.createOutput(
IndexFileNames.segmentFileName(segment, "", VECTORS_EXTENSION), context);
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(this);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, this);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitPostingsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitPostingsFormat.java
index 28c706332c3..64c3a4f5609 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitPostingsFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitPostingsFormat.java
@@ -114,33 +114,23 @@ public class UniformSplitPostingsFormat extends PostingsFormat {
@Override
public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException {
PostingsWriterBase postingsWriter = new Lucene103PostingsWriter(state);
- boolean success = false;
try {
- FieldsConsumer termsWriter =
- createUniformSplitTermsWriter(
- postingsWriter, state, targetNumBlockLines, deltaNumLines, blockEncoder);
- success = true;
- return termsWriter;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsWriter);
- }
+ return createUniformSplitTermsWriter(
+ postingsWriter, state, targetNumBlockLines, deltaNumLines, blockEncoder);
+ } 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 termsReader =
- createUniformSplitTermsReader(postingsReader, state, blockDecoder);
- success = true;
- return termsReader;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(postingsReader);
- }
+ return createUniformSplitTermsReader(postingsReader, state, blockDecoder);
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, postingsReader);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTermsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTermsReader.java
index 18e1dba0325..39a234b0eb4 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTermsReader.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTermsReader.java
@@ -107,7 +107,6 @@ public class UniformSplitTermsReader extends FieldsProducer {
throws IOException {
IndexInput dictionaryInput = null;
IndexInput blockInput = null;
- boolean success = false;
try {
this.postingsReader = postingsReader;
String segmentName = state.segmentInfo.name;
@@ -165,12 +164,9 @@ public class UniformSplitTermsReader extends FieldsProducer {
List<String> fieldNames = new ArrayList<>(fieldToTermsMap.keySet());
Collections.sort(fieldNames);
sortedFieldNames = Collections.unmodifiableList(fieldNames);
-
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(blockInput, dictionaryInput);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, blockInput, dictionaryInput);
+ throw t;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTermsWriter.java
index fe65ae73c9b..de1c3a6a37f 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTermsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/uniformsplit/UniformSplitTermsWriter.java
@@ -188,7 +188,6 @@ public class UniformSplitTermsWriter extends FieldsConsumer {
validateSettings(targetNumBlockLines, deltaNumLines);
IndexOutput blockOutput = null;
IndexOutput dictionaryOutput = null;
- boolean success = false;
try {
this.fieldInfos = state.fieldInfos;
this.postingsWriter = postingsWriter;
@@ -220,11 +219,9 @@ public class UniformSplitTermsWriter extends FieldsConsumer {
this.blockOutput = blockOutput;
this.dictionaryOutput = dictionaryOutput;
- success = true;
- } finally {
- if (!success) {
- IOUtils.closeWhileHandlingException(blockOutput, dictionaryOutput);
- }
+ } catch (Throwable t) {
+ IOUtils.closeWhileSuppressingExceptions(t, blockOutput, dictionaryOutput);
+ throw t;
}
}