From 23de4c514bfec96d8a2fc987441c1e7dab46588c Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Thu, 3 Nov 2011 07:41:40 +0000 Subject: [PATCH] LUCENE-3556: make DirectoryTaxonomyWriter's indexWriter private git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1196982 13f79535-47bb-0310-9956-ffa450edef68 --- .../directory/DirectoryTaxonomyWriter.java | 31 +++++++------------ .../taxonomy/directory/TestIndexClose.java | 4 +-- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java b/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java index 24b92d3d837..c462747c663 100644 --- a/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java +++ b/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java @@ -76,14 +76,13 @@ import org.apache.lucene.facet.taxonomy.writercache.lru.LruTaxonomyWriterCache; * algorithm used. *

* This class offers some hooks for extending classes to control the - * {@link IndexWriter} instance that is used. See {@link #openIndexWriter} and - * {@link #closeIndexWriter()} . + * {@link IndexWriter} instance that is used. See {@link #openIndexWriter}. * * @lucene.experimental */ public class DirectoryTaxonomyWriter implements TaxonomyWriter { - protected IndexWriter indexWriter; + private IndexWriter indexWriter; private int nextID; private char delimiter = Consts.DEFAULT_DELIMITER; private SinglePositionTokenStream parentStream = new SinglePositionTokenStream(Consts.PAYLOAD_PARENT); @@ -171,7 +170,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter { throws CorruptIndexException, LockObtainFailedException, IOException { - openIndexWriter(directory, openMode); + indexWriter = openIndexWriter(directory, openMode); reader = null; FieldType ft = new FieldType(TextField.TYPE_UNSTORED); @@ -212,8 +211,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter { * {@link org.apache.lucene.index.IndexDeletionPolicy}, different RAM size * etc.
* NOTE: the instance this method returns will be closed upon calling - * to {@link #close()}. If you wish to do something different, you should - * override {@link #closeIndexWriter()}. + * to {@link #close()}. * * @param directory * the {@link Directory} on top of which an {@link IndexWriter} @@ -221,7 +219,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter { * @param openMode * see {@link OpenMode} */ - protected void openIndexWriter(Directory directory, OpenMode openMode) + protected IndexWriter openIndexWriter(Directory directory, OpenMode openMode) throws IOException { // Make sure we use a MergePolicy which merges segments in-order and thus // keeps the doc IDs ordered as well (this is crucial for the taxonomy @@ -229,7 +227,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter { IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, new KeywordAnalyzer()).setOpenMode(openMode).setMergePolicy( new LogByteSizeMergePolicy()); - indexWriter = new IndexWriter(directory, config); + return new IndexWriter(directory, config); } // Currently overridden by a unit test that verifies that every index we open @@ -279,7 +277,11 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter { */ @Override public synchronized void close() throws CorruptIndexException, IOException { - closeIndexWriter(); + if (indexWriter != null) { + indexWriter.close(); + indexWriter = null; + } + closeResources(); } @@ -312,17 +314,6 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter { } } - /** - * A hook for extending classes to control closing the {@link IndexWriter} - * returned by {@link #openIndexWriter}. - */ - protected void closeIndexWriter() throws CorruptIndexException, IOException { - if (indexWriter != null) { - indexWriter.close(); - indexWriter = null; - } - } - /** * Look up the given category in the cache and/or the on-disk storage, * returning the category's ordinal, or a negative number in case the diff --git a/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestIndexClose.java b/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestIndexClose.java index 77c0086aa65..c912f681298 100644 --- a/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestIndexClose.java +++ b/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestIndexClose.java @@ -130,8 +130,8 @@ public class TestIndexClose extends LuceneTestCase { return new InstrumentedIndexReader(super.openReader()); } @Override - protected void openIndexWriter (Directory directory, OpenMode openMode) throws IOException { - indexWriter = new InstrumentedIndexWriter(directory, + protected IndexWriter openIndexWriter (Directory directory, OpenMode openMode) throws IOException { + return new InstrumentedIndexWriter(directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.KEYWORD, false)) .setOpenMode(openMode)); }