Search in sources :

Example 1 with OrdinalMap

use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap in project lucene-solr by apache.

the class TestAddTaxonomy method testAddToEmpty.

public void testAddToEmpty() throws Exception {
    Directory dest = newDirectory();
    Directory src = newDirectory();
    DirectoryTaxonomyWriter srcTW = new DirectoryTaxonomyWriter(src);
    srcTW.addCategory(new FacetLabel("Author", "Rob Pike"));
    srcTW.addCategory(new FacetLabel("Aardvarks", "Bob"));
    srcTW.close();
    DirectoryTaxonomyWriter destTW = new DirectoryTaxonomyWriter(dest);
    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 2 with OrdinalMap

use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap in project lucene-solr by apache.

the class TestAddTaxonomy method testSimple.

public void testSimple() throws Exception {
    Directory dest = newDirectory();
    DirectoryTaxonomyWriter tw1 = new DirectoryTaxonomyWriter(dest);
    tw1.addCategory(new FacetLabel("Author", "Mark Twain"));
    tw1.addCategory(new FacetLabel("Animals", "Dog"));
    tw1.addCategory(new FacetLabel("Author", "Rob Pike"));
    Directory src = newDirectory();
    DirectoryTaxonomyWriter tw2 = new DirectoryTaxonomyWriter(src);
    tw2.addCategory(new FacetLabel("Author", "Rob Pike"));
    tw2.addCategory(new FacetLabel("Aardvarks", "Bob"));
    tw2.close();
    OrdinalMap map = randomOrdinalMap();
    tw1.addTaxonomy(src, map);
    tw1.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 3 with OrdinalMap

use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap 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 4 with OrdinalMap

use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap 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)

Example 5 with OrdinalMap

use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap in project lucene-solr by apache.

the class TestAddTaxonomy method testConcurrency.

public void testConcurrency() throws Exception {
    // tests that addTaxonomy and addCategory work in parallel
    final int numCategories = atLeast(10000);
    // build an input taxonomy index
    Directory src = newDirectory();
    DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(src);
    for (int i = 0; i < numCategories; i++) {
        tw.addCategory(new FacetLabel("a", Integer.toString(i)));
    }
    tw.close();
    // now add the taxonomy to an empty taxonomy, while adding the categories
    // again, in parallel -- in the end, no duplicate categories should exist.
    Directory dest = newDirectory();
    final DirectoryTaxonomyWriter destTW = new DirectoryTaxonomyWriter(dest);
    Thread t = new Thread() {

        @Override
        public void run() {
            for (int i = 0; i < numCategories; i++) {
                try {
                    destTW.addCategory(new FacetLabel("a", Integer.toString(i)));
                } catch (IOException e) {
                    // shouldn't happen - if it does, let the test fail on uncaught exception.
                    throw new RuntimeException(e);
                }
            }
        }
    };
    t.start();
    OrdinalMap map = new MemoryOrdinalMap();
    destTW.addTaxonomy(src, map);
    t.join();
    destTW.close();
    // now validate
    DirectoryTaxonomyReader dtr = new DirectoryTaxonomyReader(dest);
    // +2 to account for the root category + "a"
    assertEquals(numCategories + 2, dtr.getSize());
    HashSet<FacetLabel> categories = new HashSet<>();
    for (int i = 1; i < dtr.getSize(); i++) {
        FacetLabel cat = dtr.getPath(i);
        assertTrue("category " + cat + " already existed", categories.add(cat));
    }
    dtr.close();
    IOUtils.close(src, dest);
}
Also used : MemoryOrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap) 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) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet)

Aggregations

FacetLabel (org.apache.lucene.facet.taxonomy.FacetLabel)5 DiskOrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.DiskOrdinalMap)5 MemoryOrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap)5 OrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap)5 Directory (org.apache.lucene.store.Directory)5 IOException (java.io.IOException)2 HashSet (java.util.HashSet)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1