Search in sources :

Example 36 with FacetLabel

use of org.apache.lucene.facet.taxonomy.FacetLabel in project lucene-solr by apache.

the class TestDirectoryTaxonomyReader method testOpenIfChangedMergedSegment.

@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();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) LogByteSizeMergePolicy(org.apache.lucene.index.LogByteSizeMergePolicy) IndexWriter(org.apache.lucene.index.IndexWriter) TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

Example 37 with FacetLabel

use of org.apache.lucene.facet.taxonomy.FacetLabel in project lucene-solr by apache.

the class TestDirectoryTaxonomyReader method testOpenIfChangedResult.

@Test
public void testOpenIfChangedResult() throws Exception {
    Directory dir = null;
    DirectoryTaxonomyWriter ltw = null;
    DirectoryTaxonomyReader ltr = null;
    try {
        dir = newDirectory();
        ltw = new DirectoryTaxonomyWriter(dir);
        ltw.addCategory(new FacetLabel("a"));
        ltw.commit();
        ltr = new DirectoryTaxonomyReader(dir);
        assertNull("Nothing has changed", TaxonomyReader.openIfChanged(ltr));
        ltw.addCategory(new FacetLabel("b"));
        ltw.commit();
        DirectoryTaxonomyReader newtr = TaxonomyReader.openIfChanged(ltr);
        assertNotNull("changes were committed", newtr);
        assertNull("Nothing has changed", TaxonomyReader.openIfChanged(newtr));
        newtr.close();
    } finally {
        IOUtils.close(ltw, ltr, dir);
    }
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 38 with FacetLabel

use of org.apache.lucene.facet.taxonomy.FacetLabel in project lucene-solr by apache.

the class TestDirectoryTaxonomyReader method testOpenIfChangedNoChangesButSegmentMerges.

@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();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) LogByteSizeMergePolicy(org.apache.lucene.index.LogByteSizeMergePolicy) IndexWriter(org.apache.lucene.index.IndexWriter) TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

Example 39 with FacetLabel

use of org.apache.lucene.facet.taxonomy.FacetLabel in project lucene-solr by apache.

the class TestDirectoryTaxonomyWriter method testCommit.

@Test
public void testCommit() throws Exception {
    // Verifies that nothing is committed to the underlying Directory, if
    // commit() wasn't called.
    Directory dir = newDirectory();
    DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);
    assertFalse(DirectoryReader.indexExists(dir));
    // first commit, so that an index will be created
    ltw.commit();
    ltw.addCategory(new FacetLabel("a"));
    IndexReader r = DirectoryReader.open(dir);
    assertEquals("No categories should have been committed to the underlying directory", 1, r.numDocs());
    r.close();
    ltw.close();
    dir.close();
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) IndexReader(org.apache.lucene.index.IndexReader) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 40 with FacetLabel

use of org.apache.lucene.facet.taxonomy.FacetLabel in project lucene-solr by apache.

the class TestDirectoryTaxonomyWriter method testPrepareCommitNoEmptyCommits.

@Test
public void testPrepareCommitNoEmptyCommits() throws Exception {
    // LUCENE-4972: DTW used to create empty commits even if no changes were made
    Directory dir = newDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir);
    taxoWriter.addCategory(new FacetLabel("a"));
    taxoWriter.prepareCommit();
    taxoWriter.commit();
    long gen1 = SegmentInfos.getLastCommitGeneration(dir);
    taxoWriter.prepareCommit();
    taxoWriter.commit();
    long gen2 = SegmentInfos.getLastCommitGeneration(dir);
    assertEquals("empty commit should not have changed the index", gen1, gen2);
    taxoWriter.close();
    dir.close();
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Aggregations

FacetLabel (org.apache.lucene.facet.taxonomy.FacetLabel)43 Directory (org.apache.lucene.store.Directory)32 Test (org.junit.Test)25 RAMDirectory (org.apache.lucene.store.RAMDirectory)13 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)7 MemoryOrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 IOException (java.io.IOException)5 Random (java.util.Random)5 DiskOrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.DiskOrdinalMap)5 OrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 Document (org.apache.lucene.document.Document)3