Skip to content

Commit 6e712fc

Browse files
authored
Remove boolean success from various codec classes (#14995)
1 parent 45a7c38 commit 6e712fc

27 files changed

+102
-282
lines changed

lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsReader.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public BlockTermsReader(
107107
state.segmentInfo.name, state.segmentSuffix, BlockTermsWriter.TERMS_EXTENSION);
108108
in = state.directory.openInput(filename, state.context);
109109

110-
boolean success = false;
111110
try {
112111
CodecUtil.checkIndexHeader(
113112
in,
@@ -171,11 +170,9 @@ public BlockTermsReader(
171170
throw new CorruptIndexException("duplicate fields: " + fieldInfo.name, in);
172171
}
173172
}
174-
success = true;
175-
} finally {
176-
if (!success) {
177-
in.close();
178-
}
173+
} catch (Throwable t) {
174+
IOUtils.closeWhileSuppressingExceptions(t, in);
175+
throw t;
179176
}
180177

181178
this.indexReader = indexReader;

lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public BlockTermsWriter(
9696
this.termsIndexWriter = termsIndexWriter;
9797
maxDoc = state.segmentInfo.maxDoc();
9898
out = state.directory.createOutput(termsFileName, state.context);
99-
boolean success = false;
10099
try {
101100
fieldInfos = state.fieldInfos;
102101
CodecUtil.writeIndexHeader(
@@ -108,11 +107,9 @@ public BlockTermsWriter(
108107
// System.out.println("BTW.init seg=" + state.segmentName);
109108

110109
postingsWriter.init(out, state); // have consumer write its format/header
111-
success = true;
112-
} finally {
113-
if (!success) {
114-
IOUtils.closeWhileHandlingException(out);
115-
}
110+
} catch (Throwable t) {
111+
IOUtils.closeWhileSuppressingExceptions(t, out);
112+
throw t;
116113
}
117114
}
118115

lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.apache.lucene.util.Accountable;
3232
import org.apache.lucene.util.Accountables;
3333
import org.apache.lucene.util.BytesRef;
34-
import org.apache.lucene.util.IOUtils;
3534
import org.apache.lucene.util.PagedBytes;
3635
import org.apache.lucene.util.packed.MonotonicBlockPackedReader;
3736

@@ -68,11 +67,8 @@ public FixedGapTermsIndexReader(SegmentReadState state) throws IOException {
6867
state.segmentInfo.name,
6968
state.segmentSuffix,
7069
FixedGapTermsIndexWriter.TERMS_INDEX_EXTENSION);
71-
final IndexInput in = state.directory.openInput(fileName, state.context);
7270

73-
boolean success = false;
74-
75-
try {
71+
try (IndexInput in = state.directory.openInput(fileName, state.context)) {
7672

7773
CodecUtil.checkIndexHeader(
7874
in,
@@ -138,13 +134,7 @@ public FixedGapTermsIndexReader(SegmentReadState state) throws IOException {
138134
throw new CorruptIndexException("duplicate field: " + fieldInfo.name, in);
139135
}
140136
}
141-
success = true;
142137
} finally {
143-
if (success) {
144-
IOUtils.close(in);
145-
} else {
146-
IOUtils.closeWhileHandlingException(in);
147-
}
148138
termBytesReader = termBytes.freeze(true);
149139
}
150140
}

lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexWriter.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,15 @@ public FixedGapTermsIndexWriter(SegmentWriteState state, int termIndexInterval)
7070
IndexFileNames.segmentFileName(
7171
state.segmentInfo.name, state.segmentSuffix, TERMS_INDEX_EXTENSION);
7272
out = state.directory.createOutput(indexFileName, state.context);
73-
boolean success = false;
7473
try {
7574
CodecUtil.writeIndexHeader(
7675
out, CODEC_NAME, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
7776
out.writeVInt(termIndexInterval);
7877
out.writeVInt(PackedInts.VERSION_CURRENT);
7978
out.writeVInt(BLOCKSIZE);
80-
success = true;
81-
} finally {
82-
if (!success) {
83-
IOUtils.closeWhileHandlingException(out);
84-
}
79+
} catch (Throwable t) {
80+
IOUtils.closeWhileSuppressingExceptions(t, out);
81+
throw t;
8582
}
8683
}
8784

@@ -207,8 +204,7 @@ public void finish(long termsFilePointer) throws IOException {
207204
@Override
208205
public void close() throws IOException {
209206
if (out != null) {
210-
boolean success = false;
211-
try {
207+
try (IndexOutput _ = out) {
212208
final long dirStart = out.getFilePointer();
213209
final int fieldCount = fields.size();
214210

@@ -234,13 +230,7 @@ public void close() throws IOException {
234230
}
235231
writeTrailer(dirStart);
236232
CodecUtil.writeFooter(out);
237-
success = true;
238233
} finally {
239-
if (success) {
240-
IOUtils.close(out);
241-
} else {
242-
IOUtils.closeWhileHandlingException(out);
243-
}
244234
out = null;
245235
}
246236
}

lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/VariableGapTermsIndexWriter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ public VariableGapTermsIndexWriter(SegmentWriteState state, IndexTermSelector po
187187
IndexFileNames.segmentFileName(
188188
state.segmentInfo.name, state.segmentSuffix, TERMS_INDEX_EXTENSION);
189189

190-
boolean success = false;
191190
try {
192191
metaOut = state.directory.createOutput(metaFileName, state.context);
193192
out = state.directory.createOutput(indexFileName, state.context);
@@ -199,11 +198,9 @@ public VariableGapTermsIndexWriter(SegmentWriteState state, IndexTermSelector po
199198
state.segmentSuffix);
200199
CodecUtil.writeIndexHeader(
201200
out, CODEC_NAME, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
202-
success = true;
203-
} finally {
204-
if (!success) {
205-
IOUtils.closeWhileHandlingException(this);
206-
}
201+
} catch (Throwable t) {
202+
IOUtils.closeWhileSuppressingExceptions(t, this);
203+
throw t;
207204
}
208205
}
209206

lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/BlockTreeOrdsPostingsFormat.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,23 @@ public String toString() {
6969
public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException {
7070
PostingsWriterBase postingsWriter = new Lucene103PostingsWriter(state);
7171

72-
boolean success = false;
7372
try {
74-
FieldsConsumer ret =
75-
new OrdsBlockTreeTermsWriter(state, postingsWriter, minTermBlockSize, maxTermBlockSize);
76-
success = true;
77-
return ret;
78-
} finally {
79-
if (!success) {
80-
IOUtils.closeWhileHandlingException(postingsWriter);
81-
}
73+
return new OrdsBlockTreeTermsWriter(
74+
state, postingsWriter, minTermBlockSize, maxTermBlockSize);
75+
} catch (Throwable t) {
76+
IOUtils.closeWhileSuppressingExceptions(t, postingsWriter);
77+
throw t;
8278
}
8379
}
8480

8581
@Override
8682
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
8783
PostingsReaderBase postingsReader = new Lucene103PostingsReader(state);
88-
boolean success = false;
8984
try {
90-
FieldsProducer ret = new OrdsBlockTreeTermsReader(postingsReader, state);
91-
success = true;
92-
return ret;
93-
} finally {
94-
if (!success) {
95-
IOUtils.closeWhileHandlingException(postingsReader);
96-
}
85+
return new OrdsBlockTreeTermsReader(postingsReader, state);
86+
} catch (Throwable t) {
87+
IOUtils.closeWhileSuppressingExceptions(t, postingsReader);
88+
throw t;
9789
}
9890
}
9991
}

lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsReader.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public OrdsBlockTreeTermsReader(PostingsReaderBase postingsReader, SegmentReadSt
6363
state.segmentInfo.name, state.segmentSuffix, OrdsBlockTreeTermsWriter.TERMS_EXTENSION);
6464
in = state.directory.openInput(termsFile, state.context);
6565

66-
boolean success = false;
6766
IndexInput indexIn = null;
6867

6968
try {
@@ -173,13 +172,9 @@ public OrdsBlockTreeTermsReader(PostingsReaderBase postingsReader, SegmentReadSt
173172
}
174173
}
175174
indexIn.close();
176-
177-
success = true;
178-
} finally {
179-
if (!success) {
180-
// this.close() will close in:
181-
IOUtils.closeWhileHandlingException(indexIn, this);
182-
}
175+
} catch (Throwable t) {
176+
IOUtils.closeWhileSuppressingExceptions(t, indexIn, this);
177+
throw t;
183178
}
184179
}
185180

lucene/codecs/src/java/org/apache/lucene/codecs/blocktreeords/OrdsBlockTreeTermsWriter.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ public OrdsBlockTreeTermsWriter(
178178
IndexFileNames.segmentFileName(
179179
state.segmentInfo.name, state.segmentSuffix, TERMS_EXTENSION);
180180
out = state.directory.createOutput(termsFileName, state.context);
181-
boolean success = false;
182181
IndexOutput indexOut = null;
183182
try {
184183
fieldInfos = state.fieldInfos;
@@ -204,11 +203,9 @@ public OrdsBlockTreeTermsWriter(
204203
// System.out.println("BTW.init seg=" + state.segmentName);
205204

206205
postingsWriter.init(out, state); // have consumer write its format/header
207-
success = true;
208-
} finally {
209-
if (!success) {
210-
IOUtils.closeWhileHandlingException(out, indexOut);
211-
}
206+
} catch (Throwable t) {
207+
IOUtils.closeWhileSuppressingExceptions(t, out, indexOut);
208+
throw t;
212209
}
213210
this.indexOut = indexOut;
214211
}
@@ -928,9 +925,9 @@ public void close() throws IOException {
928925
}
929926
closed = true;
930927

931-
boolean success = false;
932-
try {
933-
928+
try (out;
929+
indexOut;
930+
postingsWriter) {
934931
final long dirStart = out.getFilePointer();
935932
final long indexDirStart = indexOut.getFilePointer();
936933

@@ -960,13 +957,6 @@ public void close() throws IOException {
960957
CodecUtil.writeFooter(out);
961958
indexOut.writeLong(indexDirStart);
962959
CodecUtil.writeFooter(indexOut);
963-
success = true;
964-
} finally {
965-
if (success) {
966-
IOUtils.close(out, indexOut, postingsWriter);
967-
} else {
968-
IOUtils.closeWhileHandlingException(out, indexOut, postingsWriter);
969-
}
970960
}
971961
}
972962

lucene/codecs/src/java/org/apache/lucene/codecs/bloom/BloomFilteringPostingsFormat.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,8 @@ public BloomFilteredFieldsProducer(SegmentReadState state) throws IOException {
151151
String bloomFileName =
152152
IndexFileNames.segmentFileName(
153153
state.segmentInfo.name, state.segmentSuffix, BLOOM_EXTENSION);
154-
ChecksumIndexInput bloomIn = null;
155-
boolean success = false;
156-
try {
157-
bloomIn = state.directory.openChecksumInput(bloomFileName);
154+
155+
try (ChecksumIndexInput bloomIn = state.directory.openChecksumInput(bloomFileName)) {
158156
CodecUtil.checkIndexHeader(
159157
bloomIn,
160158
BLOOM_CODEC_NAME,
@@ -176,12 +174,9 @@ public BloomFilteredFieldsProducer(SegmentReadState state) throws IOException {
176174
bloomsByFieldName.put(fieldInfo.name, bloom);
177175
}
178176
CodecUtil.checkFooter(bloomIn);
179-
IOUtils.close(bloomIn);
180-
success = true;
181-
} finally {
182-
if (!success) {
183-
IOUtils.closeWhileHandlingException(bloomIn, delegateFieldsProducer);
184-
}
177+
} catch (Throwable t) {
178+
IOUtils.closeWhileSuppressingExceptions(t, delegateFieldsProducer);
179+
throw t;
185180
}
186181
}
187182

lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTPostingsFormat.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,22 @@ public String toString() {
4343
public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException {
4444
PostingsWriterBase postingsWriter = new Lucene103PostingsWriter(state);
4545

46-
boolean success = false;
4746
try {
48-
FieldsConsumer ret = new FSTTermsWriter(state, postingsWriter);
49-
success = true;
50-
return ret;
51-
} finally {
52-
if (!success) {
53-
IOUtils.closeWhileHandlingException(postingsWriter);
54-
}
47+
return new FSTTermsWriter(state, postingsWriter);
48+
} catch (Throwable t) {
49+
IOUtils.closeWhileSuppressingExceptions(t, postingsWriter);
50+
throw t;
5551
}
5652
}
5753

5854
@Override
5955
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
6056
PostingsReaderBase postingsReader = new Lucene103PostingsReader(state);
61-
boolean success = false;
6257
try {
63-
FieldsProducer ret = new FSTTermsReader(state, postingsReader);
64-
success = true;
65-
return ret;
66-
} finally {
67-
if (!success) {
68-
IOUtils.closeWhileHandlingException(postingsReader);
69-
}
58+
return new FSTTermsReader(state, postingsReader);
59+
} catch (Throwable t) {
60+
IOUtils.closeWhileSuppressingExceptions(t, postingsReader);
61+
throw t;
7062
}
7163
}
7264
}

0 commit comments

Comments
 (0)