Apache Lucene open-source search software
LUCENE-4633: DirectoryTaxonomyWriter.replaceTaxonomy should refresh its reader
Shai Ereraee4ad727375c
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 27ad0249c61..2815a072145 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -291,6 +291,10 @@ Bug Fixes* LUCENE-4608: Handle large number of requested fragments better.(Martijn van Groningen)+* LUCENE-4633: DirectoryTaxonomyWriter.replaceTaxonomy did not refresh its+ internal reader, which could cause an existing category to be added twice.+ (Shai Erera)+Changes in Runtime Behavior* LUCENE-4586: Change default ResultMode of FacetRequest to PER_NODE_IN_TREE.diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.javaindex f8be8c78682..16bf5206a46 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java@@ -1027,6 +1027,8 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {indexWriter.deleteAll();indexWriter.addIndexes(taxoDir);shouldRefreshReaderManager = true;+ initReaderManager(); // ensure that it's initialized+ refreshReaderManager();nextID = indexWriter.maxDoc();// need to clear the cache, so that addCategory won't accidentally returndiff --git a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.javaindex f696b6e0737..1beeb8ce8c2 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java@@ -9,6 +9,7 @@ import java.util.concurrent.atomic.AtomicInteger;import org.apache.lucene.facet.taxonomy.CategoryPath;import org.apache.lucene.facet.taxonomy.TaxonomyReader;+import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap;import org.apache.lucene.facet.taxonomy.writercache.TaxonomyWriterCache;import org.apache.lucene.facet.taxonomy.writercache.cl2o.Cl2oTaxonomyWriterCache;import org.apache.lucene.facet.taxonomy.writercache.lru.LruTaxonomyWriterCache;@@ -316,12 +317,12 @@ public class TestDirectoryTaxonomyWriter extends LuceneTestCase {public void testReplaceTaxonomy() throws Exception {Directory input = newDirectory();DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(input);- taxoWriter.addCategory(new CategoryPath("a"));+ int ordA = taxoWriter.addCategory(new CategoryPath("a"));taxoWriter.close();Directory dir = newDirectory();taxoWriter = new DirectoryTaxonomyWriter(dir);- int ordinal = taxoWriter.addCategory(new CategoryPath("b"));+ int ordB = taxoWriter.addCategory(new CategoryPath("b"));taxoWriter.addCategory(new CategoryPath("c"));taxoWriter.commit();@@ -330,11 +331,16 @@ public class TestDirectoryTaxonomyWriter extends LuceneTestCase {// replace the taxonomy with the input onetaxoWriter.replaceTaxonomy(input);+ // LUCENE-4633: make sure that category "a" is not added again in any case+ taxoWriter.addTaxonomy(input, new MemoryOrdinalMap());+ assertEquals("no categories should have been added", 2, taxoWriter.getSize()); // root + 'a'+ assertEquals("category 'a' received new ordinal?", ordA, taxoWriter.addCategory(new CategoryPath("a")));+// add the same category again -- it should not receive the same ordinal !- int newOrdinal = taxoWriter.addCategory(new CategoryPath("b"));- assertNotSame("new ordinal cannot be the original ordinal", ordinal, newOrdinal);- assertEquals("ordinal should have been 2 since only one category was added by replaceTaxonomy", 2, newOrdinal);-+ int newOrdB = taxoWriter.addCategory(new CategoryPath("b"));+ assertNotSame("new ordinal cannot be the original ordinal", ordB, newOrdB);+ assertEquals("ordinal should have been 2 since only one category was added by replaceTaxonomy", 2, newOrdB);+taxoWriter.close();long newEpoch = getEpoch(dir);