Skip to content

Commit b85f030

Browse files
authored
Remove even more boolean success flags (#15134)
1 parent c67f8d3 commit b85f030

File tree

15 files changed

+182
-269
lines changed

15 files changed

+182
-269
lines changed

lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/SortingStrategy.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ public EntrySupplier finishAndSort() throws IOException {
8787
ByteSequencesReader reader =
8888
new ByteSequencesReader(tempDir.openChecksumInput(sortedFile), sortedFile);
8989
return new EntrySupplier() {
90-
boolean success = false;
91-
9290
@Override
9391
public int wordCount() {
9492
return wordCount;
@@ -98,7 +96,6 @@ public int wordCount() {
9896
public String next() throws IOException {
9997
BytesRef scratch = reader.next();
10098
if (scratch == null) {
101-
success = true;
10299
return null;
103100
}
104101
return scratch.utf8ToString();
@@ -107,11 +104,7 @@ public String next() throws IOException {
107104
@Override
108105
public void close() throws IOException {
109106
reader.close();
110-
if (success) {
111-
tempDir.deleteFile(sortedFile);
112-
} else {
113-
IOUtils.deleteFilesIgnoringExceptions(tempDir, sortedFile);
114-
}
107+
tempDir.deleteFile(sortedFile);
115108
}
116109
};
117110
}

lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushQueue.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,16 @@ final class DocumentsWriterFlushQueue {
3838
synchronized FlushTicket addTicket(Supplier<FlushTicket> ticketSupplier) throws IOException {
3939
// first inc the ticket count - freeze opens a window for #anyChanges to fail
4040
incTickets();
41-
boolean success = false;
4241
try {
4342
FlushTicket ticket = ticketSupplier.get();
4443
if (ticket != null) {
4544
// no need to publish anything if we don't have any frozen updates
4645
queue.add(ticket);
47-
success = true;
4846
}
4947
return ticket;
50-
} finally {
51-
if (!success) {
52-
decTickets();
53-
}
48+
} catch (Throwable t) {
49+
decTickets();
50+
throw t;
5451
}
5552
}
5653

lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.java

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -74,80 +74,78 @@ final class SegmentCoreReaders {
7474
SegmentCoreReaders(Directory dir, SegmentCommitInfo si, IOContext context) throws IOException {
7575

7676
final Codec codec = si.info.getCodec();
77-
final Directory
78-
cfsDir; // confusing name: if (cfs) it's the cfsdir, otherwise it's the segment's directory.
79-
boolean success = false;
77+
// confusing name: if (cfs) it's the cfsdir, otherwise it's the segment's directory:
78+
final Directory cfsDir;
8079

8180
try {
82-
if (si.info.getUseCompoundFile()) {
83-
cfsDir = cfsReader = codec.compoundFormat().getCompoundReader(dir, si.info);
84-
} else {
85-
cfsReader = null;
86-
cfsDir = dir;
87-
}
81+
try {
82+
if (si.info.getUseCompoundFile()) {
83+
cfsDir = cfsReader = codec.compoundFormat().getCompoundReader(dir, si.info);
84+
} else {
85+
cfsReader = null;
86+
cfsDir = dir;
87+
}
8888

89-
segment = si.info.name;
89+
segment = si.info.name;
9090

91-
coreFieldInfos = codec.fieldInfosFormat().read(cfsDir, si.info, "", context);
91+
coreFieldInfos = codec.fieldInfosFormat().read(cfsDir, si.info, "", context);
9292

93-
final SegmentReadState segmentReadState =
94-
new SegmentReadState(cfsDir, si.info, coreFieldInfos, context);
95-
if (coreFieldInfos.hasPostings()) {
96-
final PostingsFormat format = codec.postingsFormat();
97-
// Ask codec for its Fields
98-
fields = format.fieldsProducer(segmentReadState);
99-
assert fields != null;
100-
} else {
101-
fields = null;
102-
}
103-
// ask codec for its Norms:
104-
// TODO: since we don't write any norms file if there are no norms,
105-
// kinda jaky to assume the codec handles the case of no norms file at all gracefully?!
106-
107-
if (coreFieldInfos.hasNorms()) {
108-
normsProducer = codec.normsFormat().normsProducer(segmentReadState);
109-
assert normsProducer != null;
110-
} else {
111-
normsProducer = null;
112-
}
113-
114-
fieldsReaderOrig =
115-
si.info
116-
.getCodec()
117-
.storedFieldsFormat()
118-
.fieldsReader(cfsDir, si.info, coreFieldInfos, context);
93+
final SegmentReadState segmentReadState =
94+
new SegmentReadState(cfsDir, si.info, coreFieldInfos, context);
95+
if (coreFieldInfos.hasPostings()) {
96+
final PostingsFormat format = codec.postingsFormat();
97+
// Ask codec for its Fields
98+
fields = format.fieldsProducer(segmentReadState);
99+
assert fields != null;
100+
} else {
101+
fields = null;
102+
}
103+
// ask codec for its Norms:
104+
// TODO: since we don't write any norms file if there are no norms,
105+
// kinda jaky to assume the codec handles the case of no norms file at all gracefully?!
106+
107+
if (coreFieldInfos.hasNorms()) {
108+
normsProducer = codec.normsFormat().normsProducer(segmentReadState);
109+
assert normsProducer != null;
110+
} else {
111+
normsProducer = null;
112+
}
119113

120-
if (coreFieldInfos.hasTermVectors()) { // open term vector files only as needed
121-
termVectorsReaderOrig =
114+
fieldsReaderOrig =
122115
si.info
123116
.getCodec()
124-
.termVectorsFormat()
125-
.vectorsReader(cfsDir, si.info, coreFieldInfos, context);
126-
} else {
127-
termVectorsReaderOrig = null;
128-
}
129-
130-
if (coreFieldInfos.hasPointValues()) {
131-
pointsReader = codec.pointsFormat().fieldsReader(segmentReadState);
132-
} else {
133-
pointsReader = null;
134-
}
117+
.storedFieldsFormat()
118+
.fieldsReader(cfsDir, si.info, coreFieldInfos, context);
119+
120+
if (coreFieldInfos.hasTermVectors()) { // open term vector files only as needed
121+
termVectorsReaderOrig =
122+
si.info
123+
.getCodec()
124+
.termVectorsFormat()
125+
.vectorsReader(cfsDir, si.info, coreFieldInfos, context);
126+
} else {
127+
termVectorsReaderOrig = null;
128+
}
135129

136-
if (coreFieldInfos.hasVectorValues()) {
137-
knnVectorsReader = codec.knnVectorsFormat().fieldsReader(segmentReadState);
138-
} else {
139-
knnVectorsReader = null;
140-
}
130+
if (coreFieldInfos.hasPointValues()) {
131+
pointsReader = codec.pointsFormat().fieldsReader(segmentReadState);
132+
} else {
133+
pointsReader = null;
134+
}
141135

142-
success = true;
143-
} catch (EOFException | FileNotFoundException e) {
144-
throw new CorruptIndexException("Problem reading index from " + dir, dir.toString(), e);
145-
} catch (NoSuchFileException e) {
146-
throw new CorruptIndexException("Problem reading index.", e.getFile(), e);
147-
} finally {
148-
if (!success) {
149-
decRef();
136+
if (coreFieldInfos.hasVectorValues()) {
137+
knnVectorsReader = codec.knnVectorsFormat().fieldsReader(segmentReadState);
138+
} else {
139+
knnVectorsReader = null;
140+
}
141+
} catch (EOFException | FileNotFoundException e) {
142+
throw new CorruptIndexException("Problem reading index from " + dir, dir.toString(), e);
143+
} catch (NoSuchFileException e) {
144+
throw new CorruptIndexException("Problem reading index.", e.getFile(), e);
150145
}
146+
} catch (Throwable t) {
147+
decRef();
148+
throw t;
151149
}
152150
}
153151

lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.lucene.store;
1818

19+
import java.io.Closeable;
1920
import java.io.FileNotFoundException;
2021
import java.io.IOException;
2122
import java.io.UncheckedIOException;
@@ -249,11 +250,9 @@ public IndexOutput createTempOutput(String prefix, String suffix, IOContext cont
249250
if (VERBOSE) {
250251
System.out.println("nrtdir.createTempOutput prefix=" + prefix + " suffix=" + suffix);
251252
}
252-
Set<String> toDelete = new HashSet<>();
253-
254253
// This is very ugly/messy/dangerous (can in some disastrous case maybe create too many temp
255254
// files), but I don't know of a cleaner way:
256-
boolean success = false;
255+
Set<String> toDelete = new HashSet<>();
257256

258257
Directory first;
259258
Directory second;
@@ -266,7 +265,7 @@ public IndexOutput createTempOutput(String prefix, String suffix, IOContext cont
266265
}
267266

268267
IndexOutput out = null;
269-
try {
268+
try (Closeable _ = () -> IOUtils.deleteFiles(first, toDelete)) {
270269
while (true) {
271270
out = first.createTempOutput(prefix, suffix, context);
272271
String name = out.getName();
@@ -275,17 +274,12 @@ public IndexOutput createTempOutput(String prefix, String suffix, IOContext cont
275274
out.close();
276275
} else {
277276
toDelete.remove(name);
278-
success = true;
279277
break;
280278
}
281279
}
282-
} finally {
283-
if (success) {
284-
IOUtils.deleteFiles(first, toDelete);
285-
} else {
286-
IOUtils.closeWhileHandlingException(out);
287-
IOUtils.deleteFilesIgnoringExceptions(first, toDelete);
288-
}
280+
} catch (Throwable t) {
281+
IOUtils.closeWhileSuppressingExceptions(t, out);
282+
throw t;
289283
}
290284

291285
return out;

lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,13 @@ public final void decRef() throws IOException {
155155
ensureOpen();
156156
final int rc = refCount.decrementAndGet();
157157
if (rc == 0) {
158-
boolean success = false;
159158
try {
160159
doClose();
161160
closed = true;
162-
success = true;
163-
} finally {
164-
if (!success) {
165-
// Put reference back on failure
166-
refCount.incrementAndGet();
167-
}
161+
} catch (Throwable t) {
162+
// Put reference back on failure
163+
refCount.incrementAndGet();
164+
throw t;
168165
}
169166
} else if (rc < 0) {
170167
throw new IllegalStateException(

lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ protected DirectoryTaxonomyReader doOpenIfChanged() throws IOException {
188188
}
189189

190190
// check if the taxonomy was recreated
191-
boolean success = false;
192191
try {
193192
boolean recreated = false;
194193
if (taxoWriter == null) {
@@ -222,12 +221,10 @@ protected DirectoryTaxonomyReader doOpenIfChanged() throws IOException {
222221
new DirectoryTaxonomyReader(r2, taxoWriter, ordinalCache, categoryCache, taxoArrays);
223222
}
224223

225-
success = true;
226224
return newTaxoReader;
227-
} finally {
228-
if (!success) {
229-
IOUtils.closeWhileHandlingException(r2);
230-
}
225+
} catch (Throwable t) {
226+
IOUtils.closeWhileSuppressingExceptions(t, r2);
227+
throw t;
231228
}
232229
}
233230

lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestAlwaysRefreshDirectoryTaxonomyReader.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ private class AlwaysRefreshDirectoryTaxonomyReader extends DirectoryTaxonomyRead
167167

168168
@Override
169169
protected DirectoryTaxonomyReader doOpenIfChanged() throws IOException {
170-
boolean success = false;
171-
172170
// the getInternalIndexReader() function performs the ensureOpen() check
173171
final DirectoryReader reader = DirectoryReader.openIfChanged(super.getInternalIndexReader());
174172
if (reader == null) {
@@ -181,14 +179,10 @@ protected DirectoryTaxonomyReader doOpenIfChanged() throws IOException {
181179
// Returning a AlwaysRefreshDirectoryTaxonomyReader ensures that the recreated taxonomy
182180
// reader also uses the overridden doOpenIfChanged
183181
// method (that always recomputes values).
184-
final AlwaysRefreshDirectoryTaxonomyReader newTaxonomyReader =
185-
new AlwaysRefreshDirectoryTaxonomyReader(reader);
186-
success = true;
187-
return newTaxonomyReader;
188-
} finally {
189-
if (!success) {
190-
IOUtils.closeWhileHandlingException(reader);
191-
}
182+
return new AlwaysRefreshDirectoryTaxonomyReader(reader);
183+
} catch (Throwable t) {
184+
IOUtils.closeWhileSuppressingExceptions(t, reader);
185+
throw t;
192186
}
193187
}
194188
}

lucene/replicator/src/test/org/apache/lucene/replicator/nrt/TestSimpleServer.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public ClientHandler(ServerSocket ss, Node node, Socket socket) {
8585

8686
@Override
8787
public void run() {
88-
boolean success = false;
89-
try {
88+
try (socket) {
9089
// node.message("using stream buffer size=" + bufferSize);
9190
InputStream is = new BufferedInputStream(socket.getInputStream(), bufferSize);
9291
DataInput in = new InputStreamDataInput(is);
@@ -104,30 +103,18 @@ public void run() {
104103
if (VERBOSE_CONNECTIONS) {
105104
node.message("bos.flush done");
106105
}
107-
108-
success = true;
109106
} catch (Throwable t) {
110107
if (t instanceof SocketException == false
111108
&& t instanceof NodeCommunicationException == false) {
112109
node.message("unexpected exception handling client connection; now failing test:");
113110
t.printStackTrace(System.out);
114-
IOUtils.closeWhileHandlingException(ss);
111+
IOUtils.closeWhileSuppressingExceptions(t, ss);
115112
// Test should fail with this:
116113
throw new RuntimeException(t);
117114
} else {
118115
node.message("exception handling client connection; ignoring:");
119116
t.printStackTrace(System.out);
120117
}
121-
} finally {
122-
if (success) {
123-
try {
124-
IOUtils.close(socket);
125-
} catch (IOException ioe) {
126-
throw new RuntimeException(ioe);
127-
}
128-
} else {
129-
IOUtils.closeWhileHandlingException(socket);
130-
}
131118
}
132119
if (VERBOSE_CONNECTIONS) {
133120
node.message("socket.close done");

lucene/suggest/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public BytesRef next() throws IOException {
7878
if (done) {
7979
return null;
8080
}
81-
boolean success = false;
8281
BytesRef result;
8382
try {
8483
String line;
@@ -90,11 +89,9 @@ public BytesRef next() throws IOException {
9089
IOUtils.close(in);
9190
result = null;
9291
}
93-
success = true;
94-
} finally {
95-
if (!success) {
96-
IOUtils.closeWhileHandlingException(in);
97-
}
92+
} catch (Throwable t) {
93+
IOUtils.closeWhileSuppressingExceptions(t, in);
94+
throw t;
9895
}
9996
return result;
10097
}

0 commit comments

Comments
 (0)