Search in sources :

Example 21 with BaseDirectoryWrapper

use of org.apache.lucene.store.BaseDirectoryWrapper in project lucene-solr by apache.

the class LuceneTestCase method wrapDirectory.

private static BaseDirectoryWrapper wrapDirectory(Random random, Directory directory, boolean bare) {
    if (rarely(random) && !bare) {
        directory = new NRTCachingDirectory(directory, random.nextDouble(), random.nextDouble());
    }
    if (bare) {
        BaseDirectoryWrapper base = new RawDirectoryWrapper(directory);
        closeAfterSuite(new CloseableDirectory(base, suiteFailureMarker));
        return base;
    } else {
        MockDirectoryWrapper mock = new MockDirectoryWrapper(random, directory);
        mock.setThrottling(TEST_THROTTLING);
        closeAfterSuite(new CloseableDirectory(mock, suiteFailureMarker));
        return mock;
    }
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) RawDirectoryWrapper(org.apache.lucene.store.RawDirectoryWrapper) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Example 22 with BaseDirectoryWrapper

use of org.apache.lucene.store.BaseDirectoryWrapper in project elasticsearch by elastic.

the class DirectoryUtilsTests method testGetLeave.

public void testGetLeave() throws IOException {
    Path file = createTempDir();
    final int iters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < iters; i++) {
        {
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(new FilterDirectory(dir) {
            }, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }
        {
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(dir, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }
        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean()), FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }
        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(new FilterDirectory(new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {
            }, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }
        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            RAMDirectory directory = DirectoryUtils.getLeaf(new FilterDirectory(new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {
            }, RAMDirectory.class, null);
            assertThat(directory, nullValue());
            dir.close();
        }
    }
}
Also used : Path(java.nio.file.Path) Set(java.util.Set) FilterDirectory(org.apache.lucene.store.FilterDirectory) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) FileSwitchDirectory(org.apache.lucene.store.FileSwitchDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Example 23 with BaseDirectoryWrapper

use of org.apache.lucene.store.BaseDirectoryWrapper in project lucene-solr by apache.

the class DataSplitterTest method assertSplit.

public static void assertSplit(LeafReader originalIndex, double testRatio, double crossValidationRatio, String... fieldNames) throws Exception {
    BaseDirectoryWrapper trainingIndex = newDirectory();
    BaseDirectoryWrapper testIndex = newDirectory();
    BaseDirectoryWrapper crossValidationIndex = newDirectory();
    try {
        DatasetSplitter datasetSplitter = new DatasetSplitter(testRatio, crossValidationRatio);
        datasetSplitter.split(originalIndex, trainingIndex, testIndex, crossValidationIndex, new MockAnalyzer(random()), true, classFieldName, fieldNames);
        assertNotNull(trainingIndex);
        assertNotNull(testIndex);
        assertNotNull(crossValidationIndex);
        DirectoryReader trainingReader = DirectoryReader.open(trainingIndex);
        assertEquals((int) (originalIndex.maxDoc() * (1d - testRatio - crossValidationRatio)), trainingReader.maxDoc(), 20);
        DirectoryReader testReader = DirectoryReader.open(testIndex);
        assertEquals((int) (originalIndex.maxDoc() * testRatio), testReader.maxDoc(), 20);
        DirectoryReader cvReader = DirectoryReader.open(crossValidationIndex);
        assertEquals((int) (originalIndex.maxDoc() * crossValidationRatio), cvReader.maxDoc(), 20);
        trainingReader.close();
        testReader.close();
        cvReader.close();
        closeQuietly(trainingReader);
        closeQuietly(testReader);
        closeQuietly(cvReader);
    } finally {
        if (trainingIndex != null) {
            trainingIndex.close();
        }
        if (testIndex != null) {
            testIndex.close();
        }
        if (crossValidationIndex != null) {
            crossValidationIndex.close();
        }
    }
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) DirectoryReader(org.apache.lucene.index.DirectoryReader) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper)

Example 24 with BaseDirectoryWrapper

use of org.apache.lucene.store.BaseDirectoryWrapper in project lucene-solr by apache.

the class TestSegmentInfos method testVersionsNoSegments.

// LUCENE-5954
public void testVersionsNoSegments() throws IOException {
    SegmentInfos sis = new SegmentInfos(Version.LATEST.major);
    BaseDirectoryWrapper dir = newDirectory();
    dir.setCheckIndexOnClose(false);
    sis.commit(dir);
    sis = SegmentInfos.readLatestCommit(dir);
    assertNull(sis.getMinSegmentLuceneVersion());
    assertEquals(Version.LATEST, sis.getCommitLuceneVersion());
    dir.close();
}
Also used : BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper)

Example 25 with BaseDirectoryWrapper

use of org.apache.lucene.store.BaseDirectoryWrapper in project lucene-solr by apache.

the class TestSegmentInfos method testVersionsOneSegment.

// LUCENE-5954
public void testVersionsOneSegment() throws IOException {
    BaseDirectoryWrapper dir = newDirectory();
    dir.setCheckIndexOnClose(false);
    byte[] id = StringHelper.randomId();
    Codec codec = Codec.getDefault();
    SegmentInfos sis = new SegmentInfos(Version.LATEST.major);
    SegmentInfo info = new SegmentInfo(dir, Version.LUCENE_7_0_0, Version.LUCENE_7_0_0, "_0", 1, false, Codec.getDefault(), Collections.<String, String>emptyMap(), id, Collections.<String, String>emptyMap(), null);
    info.setFiles(Collections.<String>emptySet());
    codec.segmentInfoFormat().write(dir, info, IOContext.DEFAULT);
    SegmentCommitInfo commitInfo = new SegmentCommitInfo(info, 0, -1, -1, -1);
    sis.add(commitInfo);
    sis.commit(dir);
    sis = SegmentInfos.readLatestCommit(dir);
    assertEquals(Version.LUCENE_7_0_0, sis.getMinSegmentLuceneVersion());
    assertEquals(Version.LATEST, sis.getCommitLuceneVersion());
    dir.close();
}
Also used : Codec(org.apache.lucene.codecs.Codec) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper)

Aggregations

BaseDirectoryWrapper (org.apache.lucene.store.BaseDirectoryWrapper)40 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)23 Document (org.apache.lucene.document.Document)18 MockDirectoryWrapper (org.apache.lucene.store.MockDirectoryWrapper)15 Path (java.nio.file.Path)8 IndexInput (org.apache.lucene.store.IndexInput)8 IndexOutput (org.apache.lucene.store.IndexOutput)8 BytesRef (org.apache.lucene.util.BytesRef)8 Field (org.apache.lucene.document.Field)7 IOException (java.io.IOException)5 FieldType (org.apache.lucene.document.FieldType)5 TextField (org.apache.lucene.document.TextField)5 Directory (org.apache.lucene.store.Directory)5 RAMDirectory (org.apache.lucene.store.RAMDirectory)4 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 Codec (org.apache.lucene.codecs.Codec)3