From effff3b1c705df6f18bce016a6da65b3cd97dbe2 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Sun, 30 Nov 2014 15:58:29 +0000 Subject: [PATCH] LUCENE-6082: remove abort() from codec apis git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1642558 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 2 ++ .../simpletext/SimpleTextSegmentInfoFormat.java | 1 - .../simpletext/SimpleTextStoredFieldsWriter.java | 10 +--------- .../simpletext/SimpleTextTermVectorsWriter.java | 10 +--------- .../org/apache/lucene/codecs/SegmentInfoFormat.java | 3 ++- .../org/apache/lucene/codecs/StoredFieldsWriter.java | 4 ---- .../org/apache/lucene/codecs/TermVectorsWriter.java | 4 ---- .../compressing/CompressingStoredFieldsWriter.java | 11 +---------- .../compressing/CompressingTermVectorsWriter.java | 11 +---------- .../codecs/lucene50/Lucene50SegmentInfoFormat.java | 7 ------- .../org/apache/lucene/index/DefaultIndexingChain.java | 6 +----- .../org/apache/lucene/index/TermVectorsConsumer.java | 2 +- .../TestCompressingStoredFieldsFormat.java | 4 +++- .../codecs/asserting/AssertingStoredFieldsFormat.java | 5 ----- .../codecs/asserting/AssertingTermVectorsFormat.java | 5 ----- .../codecs/cranky/CrankyStoredFieldsFormat.java | 8 -------- .../lucene/codecs/cranky/CrankyTermVectorsFormat.java | 8 -------- 17 files changed, 13 insertions(+), 88 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 4ceb500521e..e266c963d8c 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -227,6 +227,8 @@ API Changes * LUCENE-6068: LeafReader.fields() never returns null. (Robert Muir) +* LUCENE-6082: Remove abort() from codec apis. (Robert Muir) + Bug Fixes * LUCENE-5650: Enforce read-only access to any path outside the temporary diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSegmentInfoFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSegmentInfoFormat.java index 98a6d826fc0..2bb602a9d1c 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSegmentInfoFormat.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSegmentInfoFormat.java @@ -205,7 +205,6 @@ public class SimpleTextSegmentInfoFormat extends SegmentInfoFormat { } finally { if (!success) { IOUtils.closeWhileHandlingException(output); - IOUtils.deleteFilesIgnoringExceptions(dir, segFileName); } else { output.close(); } 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 d32cb1f650f..bad528f057a 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 @@ -70,7 +70,7 @@ public class SimpleTextStoredFieldsWriter extends StoredFieldsWriter { success = true; } finally { if (!success) { - abort(); + IOUtils.closeWhileHandlingException(this); } } } @@ -151,14 +151,6 @@ public class SimpleTextStoredFieldsWriter extends StoredFieldsWriter { } } - @Override - public void abort() { - try { - close(); - } catch (Throwable ignored) {} - IOUtils.deleteFilesIgnoringExceptions(directory, IndexFileNames.segmentFileName(segment, "", FIELDS_EXTENSION)); - } - @Override public void finish(FieldInfos fis, int numDocs) throws IOException { if (numDocsWritten != numDocs) { 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 6a5f1a0b8e3..b45d86c54c4 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 @@ -74,7 +74,7 @@ public class SimpleTextTermVectorsWriter extends TermVectorsWriter { success = true; } finally { if (!success) { - abort(); + IOUtils.closeWhileHandlingException(this); } } } @@ -163,14 +163,6 @@ public class SimpleTextTermVectorsWriter extends TermVectorsWriter { } } - @Override - public void abort() { - try { - close(); - } catch (Throwable ignored) {} - IOUtils.deleteFilesIgnoringExceptions(directory, IndexFileNames.segmentFileName(segment, "", VECTORS_EXTENSION)); - } - @Override public void finish(FieldInfos fis, int numDocs) throws IOException { if (numDocsWritten != numDocs) { diff --git a/lucene/core/src/java/org/apache/lucene/codecs/SegmentInfoFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/SegmentInfoFormat.java index 524c41a2de8..b2e6fa9759e 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/SegmentInfoFormat.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/SegmentInfoFormat.java @@ -48,7 +48,8 @@ public abstract class SegmentInfoFormat { public abstract SegmentInfo read(Directory directory, String segmentName, byte segmentID[], IOContext context) throws IOException; /** - * Write {@link SegmentInfo} data. + * Write {@link SegmentInfo} data. + * The codec must add its SegmentInfo filename(s) to {@code info} before doing i/o. * @throws IOException If an I/O error occurs */ public abstract void write(Directory dir, SegmentInfo info, IOContext ioContext) throws IOException; diff --git a/lucene/core/src/java/org/apache/lucene/codecs/StoredFieldsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/StoredFieldsWriter.java index 92c95819ba3..ce8b6b18af8 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/StoredFieldsWriter.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/StoredFieldsWriter.java @@ -60,10 +60,6 @@ public abstract class StoredFieldsWriter implements Closeable { /** Writes a single stored field. */ public abstract void writeField(FieldInfo info, StorableField field) throws IOException; - - /** Aborts writing entirely, implementation should remove - * any partially-written files, etc. */ - public abstract void abort(); /** Called before {@link #close()}, passing in the number * of documents that were written. Note that this is diff --git a/lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java index 7560556a9ba..4ae22378c26 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/TermVectorsWriter.java @@ -93,10 +93,6 @@ public abstract class TermVectorsWriter implements Closeable { /** Adds a term position and offsets */ public abstract void addPosition(int position, int startOffset, int endOffset, BytesRef payload) throws IOException; - - /** Aborts writing entirely, implementation should remove - * any partially-written files, etc. */ - public abstract void abort(); /** Called before {@link #close()}, passing in the number * of documents that were written. Note that this is diff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsWriter.java index cf1d5f4db42..d193e332940 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsWriter.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsWriter.java @@ -128,8 +128,7 @@ public final class CompressingStoredFieldsWriter extends StoredFieldsWriter { success = true; } finally { if (!success) { - IOUtils.closeWhileHandlingException(indexStream); - abort(); + IOUtils.closeWhileHandlingException(fieldsStream, indexStream, indexWriter); } } } @@ -303,14 +302,6 @@ public final class CompressingStoredFieldsWriter extends StoredFieldsWriter { } } - @Override - public void abort() { - IOUtils.closeWhileHandlingException(this); - IOUtils.deleteFilesIgnoringExceptions(directory, - IndexFileNames.segmentFileName(segment, segmentSuffix, FIELDS_EXTENSION), - IndexFileNames.segmentFileName(segment, segmentSuffix, FIELDS_INDEX_EXTENSION)); - } - @Override public void finish(FieldInfos fis, int numDocs) throws IOException { if (numBufferedDocs > 0) { diff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingTermVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingTermVectorsWriter.java index 1d941cec276..393c56e1ff0 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingTermVectorsWriter.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingTermVectorsWriter.java @@ -248,8 +248,7 @@ public final class CompressingTermVectorsWriter extends TermVectorsWriter { success = true; } finally { if (!success) { - IOUtils.closeWhileHandlingException(indexStream); - abort(); + IOUtils.closeWhileHandlingException(vectorsStream, indexStream, indexWriter); } } } @@ -264,14 +263,6 @@ public final class CompressingTermVectorsWriter extends TermVectorsWriter { } } - @Override - public void abort() { - IOUtils.closeWhileHandlingException(this); - IOUtils.deleteFilesIgnoringExceptions(directory, - IndexFileNames.segmentFileName(segment, segmentSuffix, VECTORS_EXTENSION), - IndexFileNames.segmentFileName(segment, segmentSuffix, VECTORS_INDEX_EXTENSION)); - } - @Override public void startDocument(int numVectorFields) throws IOException { curDoc = addDocData(numVectorFields); diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50SegmentInfoFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50SegmentInfoFormat.java index 794b0d0e360..8a665b619ad 100755 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50SegmentInfoFormat.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50SegmentInfoFormat.java @@ -118,7 +118,6 @@ public class Lucene50SegmentInfoFormat extends SegmentInfoFormat { final String fileName = IndexFileNames.segmentFileName(si.name, "", Lucene50SegmentInfoFormat.SI_EXTENSION); si.addFile(fileName); - boolean success = false; try (IndexOutput output = dir.createOutput(fileName, ioContext)) { CodecUtil.writeIndexHeader(output, Lucene50SegmentInfoFormat.CODEC_NAME, @@ -146,12 +145,6 @@ public class Lucene50SegmentInfoFormat extends SegmentInfoFormat { } output.writeStringSet(files); CodecUtil.writeFooter(output); - success = true; - } finally { - if (!success) { - // TODO: are we doing this outside of the tracking wrapper? why must SIWriter cleanup like this? - IOUtils.deleteFilesIgnoringExceptions(si.dir, fileName); - } } } diff --git a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java index 70e8659a252..b55e4cc2848 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java +++ b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java @@ -217,11 +217,7 @@ final class DefaultIndexingChain extends DocConsumer { @Override public void abort() { - try { - // E.g. close any open files in the stored fields writer: - storedFieldsWriter.abort(); - } catch (Throwable t) { - } + IOUtils.closeWhileHandlingException(storedFieldsWriter); try { // E.g. close any open files in the term vectors writer: diff --git a/lucene/core/src/java/org/apache/lucene/index/TermVectorsConsumer.java b/lucene/core/src/java/org/apache/lucene/index/TermVectorsConsumer.java index 36e8e184b9e..b8fa1cb49c4 100644 --- a/lucene/core/src/java/org/apache/lucene/index/TermVectorsConsumer.java +++ b/lucene/core/src/java/org/apache/lucene/index/TermVectorsConsumer.java @@ -126,7 +126,7 @@ final class TermVectorsConsumer extends TermsHash { super.abort(); } finally { if (writer != null) { - writer.abort(); + IOUtils.closeWhileHandlingException(writer); writer = null; } diff --git a/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java index b0e23baa465..e3e3c26b1dc 100644 --- a/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java +++ b/lucene/core/src/test/org/apache/lucene/codecs/compressing/TestCompressingStoredFieldsFormat.java @@ -84,6 +84,8 @@ public class TestCompressingStoredFieldsFormat extends BaseStoredFieldsFormatTes iw.commit(); } finally { + // next event will cause IW to delete the old files: we use prepareCommit just as example + iw.prepareCommit(); int counter = 0; for (String fileName : dir.listAll()) { if (fileName.endsWith(".fdt") || fileName.endsWith(".fdx")) { @@ -92,7 +94,7 @@ public class TestCompressingStoredFieldsFormat extends BaseStoredFieldsFormatTes } // Only one .fdt and one .fdx files must have been found assertEquals(2, counter); - iw.close(); + iw.rollback(); dir.close(); } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingStoredFieldsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingStoredFieldsFormat.java index 20a23d63c1f..e8beeae674b 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingStoredFieldsFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingStoredFieldsFormat.java @@ -142,11 +142,6 @@ public class AssertingStoredFieldsFormat extends StoredFieldsFormat { in.writeField(info, field); } - @Override - public void abort() { - in.abort(); - } - @Override public void finish(FieldInfos fis, int numDocs) throws IOException { assert docStatus == (numDocs > 0 ? Status.FINISHED : Status.UNDEFINED); diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingTermVectorsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingTermVectorsFormat.java index 88cf289ca59..dd84348237b 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingTermVectorsFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingTermVectorsFormat.java @@ -194,11 +194,6 @@ public class AssertingTermVectorsFormat extends TermVectorsFormat { --positionCount; } - @Override - public void abort() { - in.abort(); - } - @Override public void finish(FieldInfos fis, int numDocs) throws IOException { assert docCount == numDocs; diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyStoredFieldsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyStoredFieldsFormat.java index b7e86cae80a..105e309ea89 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyStoredFieldsFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyStoredFieldsFormat.java @@ -63,14 +63,6 @@ class CrankyStoredFieldsFormat extends StoredFieldsFormat { this.random = random; } - @Override - public void abort() { - delegate.abort(); - if (random.nextInt(100) == 0) { - throw new RuntimeException(new IOException("Fake IOException from StoredFieldsWriter.abort()")); - } - } - @Override public void finish(FieldInfos fis, int numDocs) throws IOException { if (random.nextInt(100) == 0) { diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyTermVectorsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyTermVectorsFormat.java index fff475e12a9..f0388f1f6a9 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyTermVectorsFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyTermVectorsFormat.java @@ -63,14 +63,6 @@ class CrankyTermVectorsFormat extends TermVectorsFormat { this.random = random; } - @Override - public void abort() { - delegate.abort(); - if (random.nextInt(100) == 0) { - throw new RuntimeException(new IOException("Fake IOException from TermVectorsWriter.abort()")); - } - } - @Override public int merge(MergeState mergeState) throws IOException { if (random.nextInt(100) == 0) {