@Test public void testOpenIfChangedNoChangesButSegmentMerges() throws Exception { // test openIfChanged() when the taxonomy hasn't really changed, but segments // were merged. The NRT reader will be reopened, and ParentArray used to assert // that the new reader contains more ordinals than were given from the old // TaxReader version Directory dir = newDirectory(); // hold onto IW to forceMerge // note how we don't close it, since DTW will close it. final IndexWriter iw = new IndexWriter( dir, new IndexWriterConfig(new MockAnalyzer(random())) .setMergePolicy(new LogByteSizeMergePolicy())); DirectoryTaxonomyWriter writer = new DirectoryTaxonomyWriter(dir) { @Override protected IndexWriter openIndexWriter(Directory directory, IndexWriterConfig config) throws IOException { return iw; } }; // add a category so that the following DTR open will cause a flush and // a new segment will be created writer.addCategory(new FacetLabel("a")); TaxonomyReader reader = new DirectoryTaxonomyReader(writer); assertEquals(2, reader.getSize()); assertEquals(2, reader.getParallelTaxonomyArrays().parents().length); // merge all the segments so that NRT reader thinks there's a change iw.forceMerge(1); // now calling openIfChanged should trip on the wrong assert in ParetArray's ctor TaxonomyReader newtr = TaxonomyReader.openIfChanged(reader); assertNotNull(newtr); reader.close(); reader = newtr; assertEquals(2, reader.getSize()); assertEquals(2, reader.getParallelTaxonomyArrays().parents().length); reader.close(); writer.close(); dir.close(); }
@Test public void testOpenIfChangedMergedSegment() throws Exception { // test openIfChanged() when all index segments were merged - used to be // a bug in ParentArray, caught by testOpenIfChangedManySegments - only // this test is not random Directory dir = newDirectory(); // hold onto IW to forceMerge // note how we don't close it, since DTW will close it. final IndexWriter iw = new IndexWriter( dir, new IndexWriterConfig(new MockAnalyzer(random())) .setMergePolicy(new LogByteSizeMergePolicy())); DirectoryTaxonomyWriter writer = new DirectoryTaxonomyWriter(dir) { @Override protected IndexWriter openIndexWriter(Directory directory, IndexWriterConfig config) throws IOException { return iw; } }; TaxonomyReader reader = new DirectoryTaxonomyReader(writer); assertEquals(1, reader.getSize()); assertEquals(1, reader.getParallelTaxonomyArrays().parents().length); // add category and call forceMerge -- this should flush IW and merge segments down to 1 // in ParentArray.initFromReader, this used to fail assuming there are no parents. writer.addCategory(new FacetLabel("1")); iw.forceMerge(1); // now calling openIfChanged should trip on the bug TaxonomyReader newtr = TaxonomyReader.openIfChanged(reader); assertNotNull(newtr); reader.close(); reader = newtr; assertEquals(2, reader.getSize()); assertEquals(2, reader.getParallelTaxonomyArrays().parents().length); reader.close(); writer.close(); dir.close(); }
@Test public void testOpenIfChangedManySegments() throws Exception { // test openIfChanged() when the taxonomy contains many segments Directory dir = newDirectory(); DirectoryTaxonomyWriter writer = new DirectoryTaxonomyWriter(dir) { @Override protected IndexWriterConfig createIndexWriterConfig(OpenMode openMode) { IndexWriterConfig conf = super.createIndexWriterConfig(openMode); LogMergePolicy lmp = (LogMergePolicy) conf.getMergePolicy(); lmp.setMergeFactor(2); return conf; } }; TaxonomyReader reader = new DirectoryTaxonomyReader(writer); int numRounds = random().nextInt(10) + 10; int numCategories = 1; // one for root for (int i = 0; i < numRounds; i++) { int numCats = random().nextInt(4) + 1; for (int j = 0; j < numCats; j++) { writer.addCategory(new FacetLabel(Integer.toString(i), Integer.toString(j))); } numCategories += numCats + 1 /* one for round-parent */; TaxonomyReader newtr = TaxonomyReader.openIfChanged(reader); assertNotNull(newtr); reader.close(); reader = newtr; // assert categories assertEquals(numCategories, reader.getSize()); int roundOrdinal = reader.getOrdinal(new FacetLabel(Integer.toString(i))); int[] parents = reader.getParallelTaxonomyArrays().parents(); assertEquals(0, parents[roundOrdinal]); // round's parent is root for (int j = 0; j < numCats; j++) { int ord = reader.getOrdinal(new FacetLabel(Integer.toString(i), Integer.toString(j))); assertEquals(roundOrdinal, parents[ord]); // round's parent is root } } reader.close(); writer.close(); dir.close(); }