Search in sources :

Example 1 with SlowRAMDirectory

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

the class TestTaxonomyCombined method assertConsistentYoungestChild.

private void assertConsistentYoungestChild(final FacetLabel abPath, final int abOrd, final int abYoungChildBase1, final int abYoungChildBase2, final int retry, int numCategories) throws Exception {
    // no slowness for initialization
    SlowRAMDirectory indexDir = new SlowRAMDirectory(-1, null);
    TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
    tw.addCategory(new FacetLabel("a", "0"));
    tw.addCategory(abPath);
    tw.commit();
    final DirectoryTaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
    for (int i = 0; i < numCategories; i++) {
        final FacetLabel cp = new FacetLabel("a", "b", Integer.toString(i));
        tw.addCategory(cp);
        assertEquals("Ordinal of " + cp + " must be invalid until Taxonomy Reader was refreshed", TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(cp));
    }
    tw.close();
    final AtomicBoolean stop = new AtomicBoolean(false);
    final Throwable[] error = new Throwable[] { null };
    final int[] retrieval = { 0 };
    Thread thread = new Thread("Child Arrays Verifier") {

        @Override
        public void run() {
            setPriority(1 + getPriority());
            try {
                while (!stop.get()) {
                    int lastOrd = tr.getParallelTaxonomyArrays().parents().length - 1;
                    assertNotNull("path of last-ord " + lastOrd + " is not found!", tr.getPath(lastOrd));
                    assertChildrenArrays(tr.getParallelTaxonomyArrays(), retry, retrieval[0]++);
                    // don't starve refresh()'s CPU, which sleeps every 50 bytes for 1 ms
                    sleep(10);
                }
            } catch (Throwable e) {
                error[0] = e;
                stop.set(true);
            }
        }

        private void assertChildrenArrays(ParallelTaxonomyArrays ca, int retry, int retrieval) {
            final int abYoungChild = ca.children()[abOrd];
            assertTrue("Retry " + retry + ": retrieval: " + retrieval + ": wrong youngest child for category " + abPath + " (ord=" + abOrd + ") - must be either " + abYoungChildBase1 + " or " + abYoungChildBase2 + " but was: " + abYoungChild, abYoungChildBase1 == abYoungChild || abYoungChildBase2 == ca.children()[abOrd]);
        }
    };
    thread.start();
    // some delay for refresh
    indexDir.setSleepMillis(1);
    TaxonomyReader newTaxoReader = TaxonomyReader.openIfChanged(tr);
    if (newTaxoReader != null) {
        newTaxoReader.close();
    }
    stop.set(true);
    thread.join();
    assertNull("Unexpcted exception at retry " + retry + " retrieval " + retrieval[0] + ": \n" + stackTraceStr(error[0]), error[0]);
    tr.close();
}
Also used : DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SlowRAMDirectory(org.apache.lucene.facet.SlowRAMDirectory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SlowRAMDirectory (org.apache.lucene.facet.SlowRAMDirectory)1 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)1 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)1