Search in sources :

Example 1 with OpenMode

use of org.apache.lucene.index.IndexWriterConfig.OpenMode in project lucene-solr by apache.

the class TestDirectoryTaxonomyReader method testOpenIfChangedManySegments.

@Test
public void testOpenIfChangedManySegments() throws Exception {
    // test openIfChanged() when the taxonomy contains many segments
    Directory dir = newDirectory();
    DirectoryTaxonomyWriter writer = new DirectoryTaxonomyWriter(dir) {

        @Override
        protected IndexWriterConfig createIndexWriterConfig(OpenMode openMode) {
            IndexWriterConfig conf = super.createIndexWriterConfig(openMode);
            LogMergePolicy lmp = (LogMergePolicy) conf.getMergePolicy();
            lmp.setMergeFactor(2);
            return conf;
        }
    };
    TaxonomyReader reader = new DirectoryTaxonomyReader(writer);
    int numRounds = random().nextInt(10) + 10;
    // one for root
    int numCategories = 1;
    for (int i = 0; i < numRounds; i++) {
        int numCats = random().nextInt(4) + 1;
        for (int j = 0; j < numCats; j++) {
            writer.addCategory(new FacetLabel(Integer.toString(i), Integer.toString(j)));
        }
        numCategories += numCats + 1;
        TaxonomyReader newtr = TaxonomyReader.openIfChanged(reader);
        assertNotNull(newtr);
        reader.close();
        reader = newtr;
        // assert categories
        assertEquals(numCategories, reader.getSize());
        int roundOrdinal = reader.getOrdinal(new FacetLabel(Integer.toString(i)));
        int[] parents = reader.getParallelTaxonomyArrays().parents();
        // round's parent is root
        assertEquals(0, parents[roundOrdinal]);
        for (int j = 0; j < numCats; j++) {
            int ord = reader.getOrdinal(new FacetLabel(Integer.toString(i), Integer.toString(j)));
            // round's parent is root
            assertEquals(roundOrdinal, parents[ord]);
        }
    }
    reader.close();
    writer.close();
    dir.close();
}
Also used : TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) LogMergePolicy(org.apache.lucene.index.LogMergePolicy) OpenMode(org.apache.lucene.index.IndexWriterConfig.OpenMode) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

Example 2 with OpenMode

use of org.apache.lucene.index.IndexWriterConfig.OpenMode in project lucene-solr by apache.

the class TestIndexWriterOnOldIndex method testOpenModeAndCreatedVersion.

public void testOpenModeAndCreatedVersion() throws IOException {
    InputStream resource = getClass().getResourceAsStream("index.single-empty-doc.630.zip");
    assertNotNull(resource);
    Path path = createTempDir();
    TestUtil.unzip(resource, path);
    Directory dir = newFSDirectory(path);
    for (OpenMode openMode : OpenMode.values()) {
        Directory tmpDir = newDirectory(dir);
        assertEquals(6, /** 6.3.0 */
        SegmentInfos.readLatestCommit(tmpDir).getIndexCreatedVersionMajor());
        IndexWriter w = new IndexWriter(tmpDir, newIndexWriterConfig().setOpenMode(openMode));
        w.commit();
        w.close();
        switch(openMode) {
            case CREATE:
                assertEquals(Version.LATEST.major, SegmentInfos.readLatestCommit(tmpDir).getIndexCreatedVersionMajor());
                break;
            default:
                assertEquals(6, /** 6.3.0 */
                SegmentInfos.readLatestCommit(tmpDir).getIndexCreatedVersionMajor());
        }
        tmpDir.close();
    }
    dir.close();
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) OpenMode(org.apache.lucene.index.IndexWriterConfig.OpenMode) Directory(org.apache.lucene.store.Directory)

Aggregations

OpenMode (org.apache.lucene.index.IndexWriterConfig.OpenMode)2 Directory (org.apache.lucene.store.Directory)2 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 FacetLabel (org.apache.lucene.facet.taxonomy.FacetLabel)1 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 LogMergePolicy (org.apache.lucene.index.LogMergePolicy)1 RAMDirectory (org.apache.lucene.store.RAMDirectory)1 Test (org.junit.Test)1