Examples of LuceneTaxonomyWriter


Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

  private void assertConsistentYoungestChild(final CategoryPath abPath,
      final int abOrd, final int abYoungChildBase1, final int abYoungChildBase2, final int retry)
      throws Exception {
    SlowRAMDirectory indexDir =  new SlowRAMDirectory(-1,null); // no slowness for intialization
    TaxonomyWriter tw = new LuceneTaxonomyWriter(indexDir);
    tw.addCategory(new CategoryPath("a", "0"));
    tw.addCategory(abPath);
    tw.commit();
   
    final TaxonomyReader tr = new LuceneTaxonomyReader(indexDir);
    for (int i=0; i < 1<<10; i++) { //1024 facets
      final CategoryPath cp = new CategoryPath("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.commit();
   
    final boolean[] stop = new boolean[] { 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[0]) {
            int lastOrd = tr.getParentArray().length-1;
            assertNotNull("path of last-ord "+lastOrd+" is not found!",tr.getPath(lastOrd));
            assertChildrenArrays(tr.getChildrenArrays(),retry,retrieval[0]++);
          }
        } catch (Throwable e) {
          error[0] = e;
          stop[0] = true;
        }
      }

      private void assertChildrenArrays(ChildrenArrays ca, int retry, int retrieval) {
        final int abYoungChild = ca.getYoungestChildArray()[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.getYoungestChildArray()[abOrd]);
      }
    };
    thread.start();
   
    indexDir.setSleepMillis(1); // some delay for refresh
    tr.refresh();
   
    stop[0] = true;
    thread.join();
    assertNull("Unexpcted exception at retry "+retry+" retrieval "+retrieval[0]+": \n"+stackTraceStr(error[0]), error[0]);
   
    tw.close();
    tr.close();
  }
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

    It also doesn't test multi-threading.
   */
  @Test
  public void testSeparateReaderAndWriter() throws Exception {
    Directory indexDir = newDirectory();
    TaxonomyWriter tw = new LuceneTaxonomyWriter(indexDir);
    tw.commit();
    TaxonomyReader tr = new LuceneTaxonomyReader(indexDir);

    int author = 1;

    // getParent() and getSize() test:
    try {
      tr.getParent(author);
      fail("Initially, getParent for "+author+" should throw exception");
    } catch (ArrayIndexOutOfBoundsException e) {
      // ok
    }
    assertEquals(1, tr.getSize()); // the empty taxonomy has size 1 (the root)
    tw.addCategory(new CategoryPath("Author"));
    try {
      tr.getParent(author);
      fail("Before commit() and refresh(), getParent for "+author+" should still throw exception");
    } catch (ArrayIndexOutOfBoundsException e) {
      // ok
    }
    assertEquals(1, tr.getSize()); // still root only...
    tr.refresh(); // this is not enough, because tw.commit() hasn't been done yet
    try {
      tr.getParent(author);
      fail("Before commit() and refresh(), getParent for "+author+" should still throw exception");
    } catch (ArrayIndexOutOfBoundsException e) {
      // ok
    }
    assertEquals(1, tr.getSize()); // still root only...
    tw.commit();
    try {
      tr.getParent(author);
      fail("Before refresh(), getParent for "+author+" should still throw exception");
    } catch (ArrayIndexOutOfBoundsException e) {
      // ok
    }
    assertEquals(1, tr.getSize()); // still root only...
    tr.refresh();
    try {
      assertEquals(TaxonomyReader.ROOT_ORDINAL, tr.getParent(author));
      // ok
    } catch (ArrayIndexOutOfBoundsException e) {
      fail("After category addition, commit() and refresh(), getParent for "+author+" should NOT throw exception");
    }
    assertEquals(2, tr.getSize()); // finally, see there are two categories

    // now, add another category, and verify that after commit and refresh
    // the parent of this category is correct (this requires the reader
    // to correctly update its prefetched parent vector), and that the
    // old information also wasn't ruined:
    tw.addCategory(new CategoryPath("Author", "Richard Dawkins"));
    int dawkins = 2;
    tw.commit();
    tr.refresh();
    assertEquals(author, tr.getParent(dawkins));
    assertEquals(TaxonomyReader.ROOT_ORDINAL, tr.getParent(author));
    assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getParent(TaxonomyReader.ROOT_ORDINAL));
    assertEquals(3, tr.getSize());
    tw.close();
    tr.close();
    indexDir.close();
  }
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

  }
 
  @Test
  public void testSeparateReaderAndWriter2() throws Exception {
    Directory indexDir = newDirectory();
    TaxonomyWriter tw = new LuceneTaxonomyWriter(indexDir);
    tw.commit();
    TaxonomyReader tr = new LuceneTaxonomyReader(indexDir);

    // Test getOrdinal():
    CategoryPath author = new CategoryPath("Author");

    assertEquals(1, tr.getSize()); // the empty taxonomy has size 1 (the root)
    assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(author));
    tw.addCategory(author);
    // before commit and refresh, no change:
    assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(author));
    assertEquals(1, tr.getSize()); // still root only...
    tr.refresh(); // this is not enough, because tw.commit() hasn't been done yet
    assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(author));
    assertEquals(1, tr.getSize()); // still root only...
    tw.commit();
    // still not enough before refresh:
    assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(author));
    assertEquals(1, tr.getSize()); // still root only...
    tr.refresh(); // finally
    assertEquals(1, tr.getOrdinal(author));
    assertEquals(2, tr.getSize()); // still root only...
    tw.close();
    tr.close();
    indexDir.close();
  }
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

   */
  @Test
  public void testWriterLock() throws Exception {
    // native fslock impl gets angry if we use it, so use RAMDirectory explicitly.
    Directory indexDir = new RAMDirectory();
    TaxonomyWriter tw = new LuceneTaxonomyWriter(indexDir);
    tw.addCategory(new CategoryPath("hi", "there"));
    tw.commit();
    // we deliberately not close the write now, and keep it open and
    // locked.
    // Verify that the writer worked:
    TaxonomyReader tr = new LuceneTaxonomyReader(indexDir);
    assertEquals(2, tr.getOrdinal(new CategoryPath("hi", "there")));
    // Try to open a second writer, with the first one locking the directory.
    // We expect to get a LockObtainFailedException.
    try {
      new LuceneTaxonomyWriter(indexDir);
      fail("should have failed to write in locked directory");
    } catch (LockObtainFailedException e) {
      // this is what we expect to happen.
    }
    // Remove the lock, and now the open should succeed, and we can
    // write to the new writer.
    LuceneTaxonomyWriter.unlock(indexDir);
    TaxonomyWriter tw2 = new LuceneTaxonomyWriter(indexDir);
    tw2.addCategory(new CategoryPath("hey"));
    tw2.close();
    // See that the writer indeed wrote:
    tr.refresh();
    assertEquals(3, tr.getOrdinal(new CategoryPath("hey")));
    tr.close();
    tw.close();
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

   * the categories themselves.
   */
  @Test
  public void testWriterCheckPaths() throws Exception {
    Directory indexDir = newDirectory();
    TaxonomyWriter tw = new LuceneTaxonomyWriter(indexDir);
    fillTaxonomyCheckPaths(tw);
    // Also check TaxonomyWriter.getSize() - see that the taxonomy's size
    // is what we expect it to be.
    assertEquals(expectedCategories.length, tw.getSize());
    tw.close();
    indexDir.close();
  }
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

   * that the reading of existing data from disk works as well.
   */
  @Test
  public void testWriterCheckPaths2() throws Exception {
    Directory indexDir = newDirectory();
    TaxonomyWriter tw = new LuceneTaxonomyWriter(indexDir);
    fillTaxonomy(tw);
    checkPaths(tw);
    fillTaxonomy(tw);
    checkPaths(tw);
    tw.close();

    tw = new LuceneTaxonomyWriter(indexDir);
    checkPaths(tw);
    fillTaxonomy(tw);
    checkPaths(tw);
    tw.close();
    indexDir.close();
  }
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

   * @throws IOException
   */
  @Test
  public void testStreamDefaultParams() throws IOException {
    Directory directory = newDirectory();
    TaxonomyWriter taxonomyWriter = new LuceneTaxonomyWriter(
        directory);
    CategoryParentsStream stream = new CategoryParentsStream(
        new CategoryAttributesStream(categoryContainer),
        taxonomyWriter, new DefaultFacetIndexingParams());

    // count the number of tokens
    int nTokens;
    for (nTokens = 0; stream.incrementToken(); nTokens++) {
    }
    // should be 6 - all categories and parents
    assertEquals("Wrong number of tokens", 6, nTokens);

    taxonomyWriter.close();
    directory.close();
  }
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

   * @throws IOException
   */
  @Test
  public void testStreamNonTopLevelParams() throws IOException {
    Directory directory = newDirectory();
    final TaxonomyWriter taxonomyWriter = new LuceneTaxonomyWriter(
        directory);
    FacetIndexingParams indexingParams = new DefaultFacetIndexingParams() {
      @Override
      protected OrdinalPolicy fixedOrdinalPolicy() {
        return new NonTopLevelOrdinalPolicy();
      }
      @Override
      protected PathPolicy fixedPathPolicy() {
        return new NonTopLevelPathPolicy();
      }
    };
   
    CategoryParentsStream stream = new CategoryParentsStream(
        new CategoryAttributesStream(categoryContainer),
        taxonomyWriter, indexingParams);

    // count the number of tokens
    int nTokens;
    for (nTokens = 0; stream.incrementToken(); nTokens++) {
    }
    /*
     * should be 4: 3 non top level ("two", "three" and "six"), and one
     * explicit top level ("four")
     */
    assertEquals("Wrong number of tokens", 4, nTokens);

    taxonomyWriter.close();
    directory.close();
  }
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

   * @throws FacetException
   */
  @Test
  public void testNoRetainableAttributes() throws IOException, FacetException {
    Directory directory = newDirectory();
    TaxonomyWriter taxonomyWriter = new LuceneTaxonomyWriter(directory);

    new CategoryParentsStream(new CategoryAttributesStream(categoryContainer),
        taxonomyWriter, new DefaultFacetIndexingParams());

    // add DummyAttribute, but do not retain, only one expected
    categoryContainer.addCategory(initialCatgeories[0], new DummyProperty());

    CategoryParentsStream stream = new CategoryParentsStream(new CategoryAttributesStream(
        categoryContainer), taxonomyWriter,
        new DefaultFacetIndexingParams());

    int nAttributes = 0;
    while (stream.incrementToken()) {
      if (stream.categoryAttribute.getProperty(DummyProperty.class) != null) {
        nAttributes++;
      }
    }
    assertEquals("Wrong number of tokens with attributes", 1, nAttributes);

    taxonomyWriter.close();
    directory.close();
  }
View Full Code Here

Examples of org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter

   * @throws FacetException
   */
  @Test
  public void testRetainableAttributes() throws IOException, FacetException {
    Directory directory = newDirectory();
    TaxonomyWriter taxonomyWriter = new LuceneTaxonomyWriter(
        directory);

    FacetIndexingParams indexingParams = new DefaultFacetIndexingParams();
    new CategoryParentsStream(new CategoryAttributesStream(
        categoryContainer), taxonomyWriter, indexingParams);

    // add DummyAttribute and retain it, three expected
    categoryContainer.clear();
    categoryContainer
        .addCategory(initialCatgeories[0], new DummyProperty());
    CategoryParentsStream stream = new CategoryParentsStream(
        new CategoryAttributesStream(categoryContainer),
        taxonomyWriter, new DefaultFacetIndexingParams());
    stream.addRetainableProperty(DummyProperty.class);

    MyCategoryListTokenizer tokenizer = new MyCategoryListTokenizer(stream,
        indexingParams);

    int nAttributes = 0;
    try {
      while (tokenizer.incrementToken()) {
        if (stream.categoryAttribute.getProperty(DummyProperty.class) != null) {
          nAttributes++;
        }
      }
    } catch (IOException e) {
      fail("Properties retained after stream closed");
    }
    assertEquals("Wrong number of tokens with attributes", 3, nAttributes);

    taxonomyWriter.close();
    directory.close();
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.