Search in sources :

Example 16 with FacetLabel

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

the class TestDirectoryTaxonomyReader method testCloseAfterIncRef.

@Test
public void testCloseAfterIncRef() throws Exception {
    Directory dir = newDirectory();
    DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
    ltw.addCategory(new FacetLabel("a"));
    ltw.close();
    DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
    ltr.incRef();
    ltr.close();
    // should not fail as we incRef() before close
    ltr.getSize();
    ltr.decRef();
    dir.close();
}
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 17 with FacetLabel

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

the class TestDirectoryTaxonomyReader method testOpenIfChangedReuse.

@Test
public void testOpenIfChangedReuse() throws Exception {
    // test the reuse of data from the old DTR instance
    for (boolean nrt : new boolean[] { false, true }) {
        Directory dir = newDirectory();
        DirectoryTaxonomyWriter writer = new DirectoryTaxonomyWriter(dir);
        FacetLabel cp_a = new FacetLabel("a");
        writer.addCategory(cp_a);
        if (!nrt)
            writer.commit();
        DirectoryTaxonomyReader r1 = nrt ? new DirectoryTaxonomyReader(writer) : new DirectoryTaxonomyReader(dir);
        // fill r1's caches
        assertEquals(1, r1.getOrdinal(cp_a));
        assertEquals(cp_a, r1.getPath(1));
        FacetLabel cp_b = new FacetLabel("b");
        writer.addCategory(cp_b);
        if (!nrt)
            writer.commit();
        DirectoryTaxonomyReader r2 = TaxonomyReader.openIfChanged(r1);
        assertNotNull(r2);
        // add r2's categories to the caches
        assertEquals(2, r2.getOrdinal(cp_b));
        assertEquals(cp_b, r2.getPath(2));
        // check that r1 doesn't see cp_b
        assertEquals(TaxonomyReader.INVALID_ORDINAL, r1.getOrdinal(cp_b));
        assertNull(r1.getPath(2));
        r1.close();
        r2.close();
        writer.close();
        dir.close();
    }
}
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 18 with FacetLabel

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

the class TestDirectoryTaxonomyReader method testCloseTwice.

@Test
public void testCloseTwice() throws Exception {
    Directory dir = newDirectory();
    DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
    ltw.addCategory(new FacetLabel("a"));
    ltw.close();
    DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
    ltr.close();
    // no exception should be thrown
    ltr.close();
    dir.close();
}
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 19 with FacetLabel

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

the class TestDirectoryTaxonomyReader method testOpenIfChangedReuseAfterRecreate.

@Test
public void testOpenIfChangedReuseAfterRecreate() throws Exception {
    // tests that if the taxonomy is recreated, no data is reused from the previous taxonomy
    Directory dir = newDirectory();
    DirectoryTaxonomyWriter writer = new DirectoryTaxonomyWriter(dir);
    FacetLabel cp_a = new FacetLabel("a");
    writer.addCategory(cp_a);
    writer.close();
    DirectoryTaxonomyReader r1 = new DirectoryTaxonomyReader(dir);
    // fill r1's caches
    assertEquals(1, r1.getOrdinal(cp_a));
    assertEquals(cp_a, r1.getPath(1));
    // now recreate, add a different category
    writer = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE);
    FacetLabel cp_b = new FacetLabel("b");
    writer.addCategory(cp_b);
    writer.close();
    DirectoryTaxonomyReader r2 = TaxonomyReader.openIfChanged(r1);
    assertNotNull(r2);
    // fill r2's caches
    assertEquals(1, r2.getOrdinal(cp_b));
    assertEquals(cp_b, r2.getPath(1));
    // check that r1 doesn't see cp_b
    assertEquals(TaxonomyReader.INVALID_ORDINAL, r1.getOrdinal(cp_b));
    assertEquals(cp_a, r1.getPath(1));
    // check that r2 doesn't see cp_a
    assertEquals(TaxonomyReader.INVALID_ORDINAL, r2.getOrdinal(cp_a));
    assertEquals(cp_b, r2.getPath(1));
    r2.close();
    r1.close();
    dir.close();
}
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 20 with FacetLabel

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

the class TestDirectoryTaxonomyReader method testOpenIfChangedAndRefCount.

@Test
public void testOpenIfChangedAndRefCount() throws Exception {
    // no need for random directories here
    Directory dir = new RAMDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir);
    taxoWriter.addCategory(new FacetLabel("a"));
    taxoWriter.commit();
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(dir);
    assertEquals("wrong refCount", 1, taxoReader.getRefCount());
    taxoReader.incRef();
    assertEquals("wrong refCount", 2, taxoReader.getRefCount());
    taxoWriter.addCategory(new FacetLabel("a", "b"));
    taxoWriter.commit();
    TaxonomyReader newtr = TaxonomyReader.openIfChanged(taxoReader);
    assertNotNull(newtr);
    taxoReader.close();
    taxoReader = newtr;
    assertEquals("wrong refCount", 1, taxoReader.getRefCount());
    taxoWriter.close();
    taxoReader.close();
    dir.close();
}
Also used : TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) 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