From 9839ad461e0ff0828fbe68bedc5c91f581c1729a Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 2 May 2014 02:48:40 +0000 Subject: [PATCH] LUCENE-5611: lazy-init the stored fields writer like before to prevent leaks git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1591807 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/index/DefaultIndexingChain.java | 16 ++++++++++++---- .../org/apache/lucene/index/TestIndexWriter.java | 1 - 2 files changed, 12 insertions(+), 5 deletions(-) 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 646b71dc8b6..ae448485cc1 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java +++ b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java @@ -51,7 +51,8 @@ final class DefaultIndexingChain extends DocConsumer { // Writes postings and term vectors: final TermsHash termsHash; - final StoredFieldsWriter storedFieldsWriter; + // lazy init: + private StoredFieldsWriter storedFieldsWriter; private int lastStoredDocID; // NOTE: I tried using Hash Map @@ -75,12 +76,16 @@ final class DefaultIndexingChain extends DocConsumer { this.docState = docWriter.docState; this.bytesUsed = docWriter.bytesUsed; - // Writes stored fields: - storedFieldsWriter = docWriter.codec.storedFieldsFormat().fieldsWriter(docWriter.directory, docWriter.getSegmentInfo(), IOContext.DEFAULT); - TermsHash termVectorsWriter = new TermVectorsConsumer(docWriter); termsHash = new FreqProxTermsWriter(docWriter, termVectorsWriter); } + + // TODO: can we remove this lazy-init / make cleaner / do it another way...? + private void initStoredFieldsWriter() throws IOException { + if (storedFieldsWriter == null) { + storedFieldsWriter = docWriter.codec.storedFieldsFormat().fieldsWriter(docWriter.directory, docWriter.getSegmentInfo(), IOContext.DEFAULT); + } + } @Override public void flush(SegmentWriteState state) throws IOException { @@ -92,6 +97,8 @@ final class DefaultIndexingChain extends DocConsumer { writeNorms(state); writeDocValues(state); + // its possible all docs hit non-aborting exceptions... + initStoredFieldsWriter(); fillStoredFields(numDocs); storedFieldsWriter.finish(state.fieldInfos, numDocs); storedFieldsWriter.close(); @@ -246,6 +253,7 @@ final class DefaultIndexingChain extends DocConsumer { private void startStoredFields() throws IOException { boolean success = false; try { + initStoredFieldsWriter(); storedFieldsWriter.startDocument(); success = true; } finally { diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java index bc6c6d0cc2a..3b973209f1a 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -2188,7 +2188,6 @@ public class TestIndexWriter extends LuceneTestCase { IOUtils.close(reader, dir); } - @AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/LUCENE-5611") public void testIterableThrowsException2() throws IOException { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(