Search in sources :

Example 1 with FileSwitchDirectory

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

the class FsDirectoryService method setPreload.

private static Directory setPreload(Directory directory, Path location, LockFactory lockFactory, Set<String> preLoadExtensions) throws IOException {
    if (preLoadExtensions.isEmpty() == false && directory instanceof MMapDirectory && ((MMapDirectory) directory).getPreload() == false) {
        if (preLoadExtensions.contains("*")) {
            ((MMapDirectory) directory).setPreload(true);
            return directory;
        }
        MMapDirectory primary = new MMapDirectory(location, lockFactory);
        primary.setPreload(true);
        return new FileSwitchDirectory(preLoadExtensions, primary, directory, true) {

            @Override
            public String[] listAll() throws IOException {
                // avoid listing twice
                return primary.listAll();
            }
        };
    }
    return directory;
}
Also used : FileSwitchDirectory(org.apache.lucene.store.FileSwitchDirectory) MMapDirectory(org.apache.lucene.store.MMapDirectory)

Example 2 with FileSwitchDirectory

use of org.apache.lucene.store.FileSwitchDirectory 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 3 with FileSwitchDirectory

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

the class FsDirectoryServiceTests method doTestPreload.

private void doTestPreload(String... preload) throws IOException {
    Settings build = Settings.builder().put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "mmapfs").putArray(IndexModule.INDEX_STORE_PRE_LOAD_SETTING.getKey(), preload).build();
    IndexSettings settings = IndexSettingsModule.newIndexSettings("foo", build);
    IndexStore store = new IndexStore(settings);
    Path tempDir = createTempDir().resolve(settings.getUUID()).resolve("0");
    Files.createDirectories(tempDir);
    ShardPath path = new ShardPath(false, tempDir, tempDir, new ShardId(settings.getIndex(), 0));
    FsDirectoryService fsDirectoryService = new FsDirectoryService(settings, store, path);
    Directory directory = fsDirectoryService.newDirectory();
    assertFalse(directory instanceof SleepingLockWrapper);
    if (preload.length == 0) {
        assertTrue(directory.toString(), directory instanceof MMapDirectory);
        assertFalse(((MMapDirectory) directory).getPreload());
    } else if (Arrays.asList(preload).contains("*")) {
        assertTrue(directory.toString(), directory instanceof MMapDirectory);
        assertTrue(((MMapDirectory) directory).getPreload());
    } else {
        assertTrue(directory.toString(), directory instanceof FileSwitchDirectory);
        FileSwitchDirectory fsd = (FileSwitchDirectory) directory;
        assertTrue(fsd.getPrimaryDir() instanceof MMapDirectory);
        assertTrue(((MMapDirectory) fsd.getPrimaryDir()).getPreload());
        assertTrue(fsd.getSecondaryDir() instanceof MMapDirectory);
        assertFalse(((MMapDirectory) fsd.getSecondaryDir()).getPreload());
    }
}
Also used : ShardPath(org.elasticsearch.index.shard.ShardPath) Path(java.nio.file.Path) ShardId(org.elasticsearch.index.shard.ShardId) ShardPath(org.elasticsearch.index.shard.ShardPath) IndexSettings(org.elasticsearch.index.IndexSettings) SleepingLockWrapper(org.apache.lucene.store.SleepingLockWrapper) FileSwitchDirectory(org.apache.lucene.store.FileSwitchDirectory) MMapDirectory(org.apache.lucene.store.MMapDirectory) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) FileSwitchDirectory(org.apache.lucene.store.FileSwitchDirectory)

Aggregations

FileSwitchDirectory (org.apache.lucene.store.FileSwitchDirectory)3 Path (java.nio.file.Path)2 MMapDirectory (org.apache.lucene.store.MMapDirectory)2 Set (java.util.Set)1 BaseDirectoryWrapper (org.apache.lucene.store.BaseDirectoryWrapper)1 Directory (org.apache.lucene.store.Directory)1 FSDirectory (org.apache.lucene.store.FSDirectory)1 FilterDirectory (org.apache.lucene.store.FilterDirectory)1 RAMDirectory (org.apache.lucene.store.RAMDirectory)1 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)1 SleepingLockWrapper (org.apache.lucene.store.SleepingLockWrapper)1 Settings (org.elasticsearch.common.settings.Settings)1 IndexSettings (org.elasticsearch.index.IndexSettings)1 ShardId (org.elasticsearch.index.shard.ShardId)1 ShardPath (org.elasticsearch.index.shard.ShardPath)1