Skip to content

Apache Lucene open-source search software

LUCENE-5242: DirectoryTaxonomyWriter.replaceTaxonomy did not fully reset its state

Shai Erera84f6e500b359
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 61fe78a002a..092ccb996bd 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -85,6 +85,10 @@ Bug Fixes
* LUCENE-4998: Fixed a few places to pass IOContext.READONCE instead
of IOContext.READ (Shikhar Bhushan via Mike McCandless)
+* LUCENE-5242: DirectoryTaxonomyWriter.replaceTaxonomy did not fully reset
+ its state, which could result in exceptions being thrown, as well as
+ incorrect ordinals returned from getParent. (Shai Erera)
+
API Changes:
* LUCENE-5222: Add SortField.needsScores(). Previously it was not possible
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.java
index 88cc95c2603..a269f5ccb92 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
@@ -981,12 +981,14 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {
initReaderManager(); // ensure that it's initialized
refreshReaderManager();
nextID = indexWriter.maxDoc();
+ taxoArrays = null; // must nullify so that it's re-computed next time it's needed
// need to clear the cache, so that addCategory won't accidentally return
// old categories that are in the cache.
cache.clear();
cacheIsComplete = false;
shouldFillCache = true;
+ cacheMisses.set(0);
// update indexEpoch as a taxonomy replace is just like it has be recreated
++indexEpoch;
diff --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.java
index fd515eb0cd6..5bfcb0abfba 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
@@ -469,4 +469,27 @@ public class TestDirectoryTaxonomyWriter extends FacetTestCase {
IOUtils.close(indexDir, taxoDir);
}
+
+ @Test
+ public void testReplaceTaxoWithLargeTaxonomy() throws Exception {
+ Directory srcTaxoDir = newDirectory(), targetTaxoDir = newDirectory();
+
+ // build source, large, taxonomy
+ DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(srcTaxoDir);
+ int ord = taxoWriter.addCategory(new CategoryPath("A/1/1/1/1/1/1", '/'));
+ taxoWriter.close();
+
+ taxoWriter = new DirectoryTaxonomyWriter(targetTaxoDir);
+ int ordinal = taxoWriter.addCategory(new CategoryPath("B/1", '/'));
+ assertEquals(1, taxoWriter.getParent(ordinal)); // call getParent to initialize taxoArrays
+ taxoWriter.commit();
+
+ taxoWriter.replaceTaxonomy(srcTaxoDir);
+ assertEquals(ord - 1, taxoWriter.getParent(ord));
+ taxoWriter.close();
+
+ srcTaxoDir.close();
+ targetTaxoDir.close();
+ }
+
}