LUCENE-4633: DirectoryTaxonomyWriter.replaceTaxonomy should refresh its reader

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1422495 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2012-12-16 10:10:31 +00:00
parent d26d4e05d3
commit ee4ad72737
3 changed files with 18 additions and 6 deletions

View File

@ -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.

View File

@ -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 return

View File

@ -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 one
taxoWriter.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);