Search in sources :

Example 21 with FacetLabel

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

the class TestDirectoryTaxonomyReader method doTestReadRecreatedTaxonomy.

private void doTestReadRecreatedTaxonomy(Random random, boolean closeReader) throws Exception {
    Directory dir = null;
    TaxonomyWriter tw = null;
    TaxonomyReader tr = null;
    // prepare a few categories
    int n = 10;
    FacetLabel[] cp = new FacetLabel[n];
    for (int i = 0; i < n; i++) {
        cp[i] = new FacetLabel("a", Integer.toString(i));
    }
    try {
        dir = newDirectory();
        tw = new DirectoryTaxonomyWriter(dir);
        tw.addCategory(new FacetLabel("a"));
        tw.close();
        tr = new DirectoryTaxonomyReader(dir);
        int baseNumCategories = tr.getSize();
        for (int i = 0; i < n; i++) {
            int k = random.nextInt(n);
            tw = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE);
            for (int j = 0; j <= k; j++) {
                tw.addCategory(cp[j]);
            }
            tw.close();
            if (closeReader) {
                tr.close();
                tr = new DirectoryTaxonomyReader(dir);
            } else {
                TaxonomyReader newtr = TaxonomyReader.openIfChanged(tr);
                assertNotNull(newtr);
                tr.close();
                tr = newtr;
            }
            assertEquals("Wrong #categories in taxonomy (i=" + i + ", k=" + k + ")", baseNumCategories + 1 + k, tr.getSize());
        }
    } finally {
        IOUtils.close(tr, tw, dir);
    }
}
Also used : TaxonomyWriter(org.apache.lucene.facet.taxonomy.TaxonomyWriter) 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)

Example 22 with FacetLabel

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

the class TestDirectoryTaxonomyReader method testGetChildren.

@Test
public void testGetChildren() throws Exception {
    Directory dir = newDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir);
    int numCategories = atLeast(10);
    int numA = 0, numB = 0;
    Random random = random();
    // add the two categories for which we'll also add children (so asserts are simpler)
    taxoWriter.addCategory(new FacetLabel("a"));
    taxoWriter.addCategory(new FacetLabel("b"));
    for (int i = 0; i < numCategories; i++) {
        if (random.nextBoolean()) {
            taxoWriter.addCategory(new FacetLabel("a", Integer.toString(i)));
            ++numA;
        } else {
            taxoWriter.addCategory(new FacetLabel("b", Integer.toString(i)));
            ++numB;
        }
    }
    // add category with no children
    taxoWriter.addCategory(new FacetLabel("c"));
    taxoWriter.close();
    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(dir);
    // non existing category
    ChildrenIterator it = taxoReader.getChildren(taxoReader.getOrdinal(new FacetLabel("invalid")));
    assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next());
    // a category with no children
    it = taxoReader.getChildren(taxoReader.getOrdinal(new FacetLabel("c")));
    assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next());
    // arbitrary negative ordinal
    it = taxoReader.getChildren(-2);
    assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next());
    // root's children
    Set<String> roots = new HashSet<>(Arrays.asList("a", "b", "c"));
    it = taxoReader.getChildren(TaxonomyReader.ROOT_ORDINAL);
    while (!roots.isEmpty()) {
        FacetLabel root = taxoReader.getPath(it.next());
        assertEquals(1, root.length);
        assertTrue(roots.remove(root.components[0]));
    }
    assertEquals(TaxonomyReader.INVALID_ORDINAL, it.next());
    for (int i = 0; i < 2; i++) {
        FacetLabel cp = i == 0 ? new FacetLabel("a") : new FacetLabel("b");
        int ordinal = taxoReader.getOrdinal(cp);
        it = taxoReader.getChildren(ordinal);
        int numChildren = 0;
        int child;
        while ((child = it.next()) != TaxonomyReader.INVALID_ORDINAL) {
            FacetLabel path = taxoReader.getPath(child);
            assertEquals(2, path.length);
            assertEquals(path.components[0], i == 0 ? "a" : "b");
            ++numChildren;
        }
        int expected = i == 0 ? numA : numB;
        assertEquals("invalid num children", expected, numChildren);
    }
    taxoReader.close();
    dir.close();
}
Also used : Random(java.util.Random) FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) ChildrenIterator(org.apache.lucene.facet.taxonomy.TaxonomyReader.ChildrenIterator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 23 with FacetLabel

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

the class TestDirectoryTaxonomyWriter method testCommitNoEmptyCommits.

@Test
public void testCommitNoEmptyCommits() 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.commit();
    long gen1 = SegmentInfos.getLastCommitGeneration(dir);
    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)

Example 24 with FacetLabel

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

the class TestAddTaxonomy method testAddEmpty.

public void testAddEmpty() throws Exception {
    Directory dest = newDirectory();
    DirectoryTaxonomyWriter destTW = new DirectoryTaxonomyWriter(dest);
    destTW.addCategory(new FacetLabel("Author", "Rob Pike"));
    destTW.addCategory(new FacetLabel("Aardvarks", "Bob"));
    destTW.commit();
    Directory src = newDirectory();
    // create an empty taxonomy
    new DirectoryTaxonomyWriter(src).close();
    OrdinalMap map = randomOrdinalMap();
    destTW.addTaxonomy(src, map);
    destTW.close();
    validate(dest, src, map);
    IOUtils.close(dest, src);
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) DiskOrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.DiskOrdinalMap) MemoryOrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap) OrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap) Directory(org.apache.lucene.store.Directory)

Example 25 with FacetLabel

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

the class TestAddTaxonomy method dotest.

private void dotest(int ncats, final int range) throws Exception {
    final AtomicInteger numCats = new AtomicInteger(ncats);
    Directory[] dirs = new Directory[2];
    for (int i = 0; i < dirs.length; i++) {
        dirs[i] = newDirectory();
        final DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(dirs[i]);
        Thread[] addThreads = new Thread[4];
        for (int j = 0; j < addThreads.length; j++) {
            addThreads[j] = new Thread() {

                @Override
                public void run() {
                    Random random = random();
                    while (numCats.decrementAndGet() > 0) {
                        String cat = Integer.toString(random.nextInt(range));
                        try {
                            tw.addCategory(new FacetLabel("a", cat));
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            };
        }
        for (Thread t : addThreads) t.start();
        for (Thread t : addThreads) t.join();
        tw.close();
    }
    DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(dirs[0]);
    OrdinalMap map = randomOrdinalMap();
    tw.addTaxonomy(dirs[1], map);
    tw.close();
    validate(dirs[0], dirs[1], map);
    IOUtils.close(dirs);
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) IOException(java.io.IOException) DiskOrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.DiskOrdinalMap) MemoryOrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap) OrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Directory(org.apache.lucene.store.Directory)

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