Example usage for org.apache.lucene.store MMapDirectory UNMAP_SUPPORTED

List of usage examples for org.apache.lucene.store MMapDirectory UNMAP_SUPPORTED

Introduction

In this page you can find the example usage for org.apache.lucene.store MMapDirectory UNMAP_SUPPORTED.

Prototype

boolean UNMAP_SUPPORTED

To view the source code for org.apache.lucene.store MMapDirectory UNMAP_SUPPORTED.

Click Source Link

Document

true, if this platform supports unmapping mmapped files.

Usage

From source file:com.b2international.index.lucene.Directories.java

License:Apache License

/**
 * Just like {@link #openFile(File)}, but allows you to also specify a custom {@link LockFactory}.
 *///  w w  w .  j  ava2 s  . c  om
public static FSDirectory openFile(final Path path, final LockFactory lockFactory) throws IOException {
    if ((Constants.WINDOWS || Constants.SUN_OS || Constants.LINUX || Constants.MAC_OS_X)
            && Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED) {

        return new MMapDirectory(path, lockFactory);
    } else if (Constants.WINDOWS) {
        return new SimpleFSDirectory(path, lockFactory);
    } else {
        return new NIOFSDirectory(path, lockFactory);
    }
}

From source file:com.novartis.pcs.ontology.service.search.OntologySearchServiceImpl.java

License:Apache License

@PostConstruct
public void start() {
    try {/*from w  w  w.  j av  a2s.com*/
        Analyzer analyzer = new TermNameAnalyzer(true);
        File indexPath = new File(path);

        if (!indexPath.exists()) {
            indexPath.mkdirs();
        }

        directory = new MMapDirectory(indexPath, new NativeFSLockFactory());
        if (MMapDirectory.UNMAP_SUPPORTED) {
            ((MMapDirectory) directory).setUseUnmap(true);
        }
        boolean indexExists = IndexReader.indexExists(directory);

        writer = new IndexWriter(directory, analyzer, new IndexWriter.MaxFieldLength(MAX_CHARS));

        if (!indexExists) {
            logger.info("Building ontology search index.");
            Collection<Term> terms = termDAO.loadAll();
            for (Term term : terms) {
                if (StatusChecker.isValid(term)) {
                    Collection<Document> docs = createDocuments(term);
                    for (Document doc : docs) {
                        writer.addDocument(doc);
                    }
                }
            }
        }
        writer.optimize();
        writer.commit();
        numberOfDocuments = writer.numDocs();
        xar = new LuceneIndexWriterXAResource(writer, this);
        reader = IndexReader.open(directory, true);
        searcher = new IndexSearcher(reader);
        rwlock = new ReentrantReadWriteLock();
    } catch (Exception e) {
        logger.log(Level.WARNING, "Failed to start Lucene term searcher", e);
        stop();
        throw new RuntimeException("Failed to start Lucene term searcher", e);
    }
}

From source file:org.elasticsearch.index.store.IndexStoreModule.java

License:Apache License

@Override
public Iterable<? extends Module> spawnModules() {
    Class<? extends Module> indexStoreModule = NioFsIndexStoreModule.class;
    // Same logic as FSDirectory#open ...
    if ((Constants.WINDOWS || Constants.SUN_OS || Constants.LINUX) && Constants.JRE_IS_64BIT
            && MMapDirectory.UNMAP_SUPPORTED) {
        indexStoreModule = MmapFsIndexStoreModule.class;
    } else if (Constants.WINDOWS) {
        indexStoreModule = SimpleFsIndexStoreModule.class;
    }//w  w w.  ja v a 2 s. co m
    String storeType = settings.get("index.store.type");
    if ("ram".equalsIgnoreCase(storeType)) {
        indexStoreModule = RamIndexStoreModule.class;
    } else if ("memory".equalsIgnoreCase(storeType)) {
        indexStoreModule = RamIndexStoreModule.class;
    } else if ("fs".equalsIgnoreCase(storeType)) {
        // nothing to set here ... (we default to fs)
    } else if ("simplefs".equalsIgnoreCase(storeType) || "simple_fs".equals(storeType)) {
        indexStoreModule = SimpleFsIndexStoreModule.class;
    } else if ("niofs".equalsIgnoreCase(storeType) || "nio_fs".equalsIgnoreCase(storeType)) {
        indexStoreModule = NioFsIndexStoreModule.class;
    } else if ("mmapfs".equalsIgnoreCase(storeType) || "mmap_fs".equalsIgnoreCase(storeType)) {
        indexStoreModule = MmapFsIndexStoreModule.class;
    } else if (storeType != null) {
        indexStoreModule = settings.getAsClass("index.store.type", indexStoreModule,
                "org.elasticsearch.index.store.", "IndexStoreModule");
    }
    return ImmutableList.of(Modules.createModule(indexStoreModule, settings));
}

From source file:org.elasticsearch.index.store.IndexStoreTests.java

License:Apache License

public void testStoreDirectory() throws IOException {
    final Path tempDir = createTempDir().resolve("foo").resolve("0");
    final IndexStoreModule.Type[] values = IndexStoreModule.Type.values();
    final IndexStoreModule.Type type = RandomPicks.randomFrom(random(), values);
    Settings settings = Settings.settingsBuilder()
            .put(IndexStoreModule.STORE_TYPE, type.name().toLowerCase(Locale.ROOT)).build();
    FsDirectoryService service = new FsDirectoryService(settings, null,
            new ShardPath(false, tempDir, tempDir, "foo", new ShardId("foo", 0)));
    try (final Directory directory = service.newFSDirectory(tempDir, NoLockFactory.INSTANCE)) {
        switch (type) {
        case NIOFS:
            assertTrue(type + " " + directory.toString(), directory instanceof NIOFSDirectory);
            break;
        case MMAPFS:
            assertTrue(type + " " + directory.toString(), directory instanceof MMapDirectory);
            break;
        case SIMPLEFS:
            assertTrue(type + " " + directory.toString(), directory instanceof SimpleFSDirectory);
            break;
        case FS://ww w  . j a va  2  s  .c o  m
        case DEFAULT:
            if (Constants.WINDOWS) {
                if (Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED) {
                    assertTrue(type + " " + directory.toString(), directory instanceof MMapDirectory);
                } else {
                    assertTrue(type + " " + directory.toString(), directory instanceof SimpleFSDirectory);
                }
            } else if (Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED) {
                assertTrue(type + " " + directory.toString(), directory instanceof FileSwitchDirectory);
            } else {
                assertTrue(type + " " + directory.toString(), directory instanceof NIOFSDirectory);
            }
            break;
        }
    }
}

From source file:org.elasticsearch.index.store.mock.MockDirectoryHelper.java

License:Apache License

public FsDirectoryService randomDirectorService(IndexStore indexStore) {
    if ((Constants.WINDOWS || Constants.SUN_OS) && Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED) {
        return new MmapFsDirectoryService(shardId, indexSettings, indexStore);
    } else if (Constants.WINDOWS) {
        return new SimpleFsDirectoryService(shardId, indexSettings, indexStore);
    }/*from  www .ja  v a2s . c  o  m*/
    switch (random.nextInt(3)) {
    case 1:
        return new MmapFsDirectoryService(shardId, indexSettings, indexStore);
    case 0:
        return new SimpleFsDirectoryService(shardId, indexSettings, indexStore);
    default:
        return new NioFsDirectoryService(shardId, indexSettings, indexStore);
    }
}

From source file:org.polymap.core.runtime.recordstore.lucene.LuceneRecordStore.java

License:Open Source License

/**
 * Creates a new store for the given filesystem directory. 
 * /*from  w  w  w .j a v  a2 s  . c  o m*/
 * @param indexDir The directory to hold the store files.
 * @param clean
 * @throws IOException
 */
public LuceneRecordStore(File indexDir, boolean clean) throws IOException {
    if (!indexDir.exists()) {
        indexDir.mkdirs();
    }

    directory = null;
    // limit mmap memory to 1GB
    // seems that bigger mmap indexes slow down performance
    long dbSize = FileUtils.sizeOfDirectory(indexDir);
    long mmapLimit = Long.parseLong(System.getProperty("org.polymap.core.LuceneRecordStore.mmaplimit", "1000"))
            * 1000000;
    if (dbSize > mmapLimit) {
        directory = Constants.WINDOWS ? new SimpleFSDirectory(indexDir, null)
                : new NIOFSDirectory(indexDir, null);
        open(clean);
    }

    // use mmap on 32bit Linux of index size < 100MB
    // more shared memory results in system stall under rare conditions
    else if (Constants.LINUX && !Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED
            && dbSize < 100 * 1024 * 1024) {
        try {
            directory = new MMapDirectory(indexDir, null);
            open(clean);
        } catch (OutOfMemoryError e) {
            log.info("Unable to mmap index: falling back to default.");
        }
    }

    // default
    if (searcher == null) {
        directory = FSDirectory.open(indexDir);
        open(clean);
    }

    log.info("Database: " + indexDir.getAbsolutePath() + "\n    size: " + FileUtils.sizeOfDirectory(indexDir)
            + "\n    using: " + directory.getClass().getSimpleName() + "\n    files in directry: "
            + Arrays.asList(directory.listAll()));
}

From source file:org.polymap.recordstore.lucene.LuceneRecordStore.java

License:Open Source License

public LuceneRecordStore(Configuration config) throws IOException {
    this.config = config;

    File indexDir = config.indexDir.get();
    if (!indexDir.exists()) {
        indexDir.mkdirs();//from  www  .  j a v  a  2  s .  com
    }

    directory = null;
    // use mmap on 32bit Linux of index size < 100MB
    // more shared memory results in system stall under rare conditions
    if (Constants.LINUX && !Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED
            && FileUtils.sizeOfDirectory(indexDir) < 100 * 1024 * 1024) {
        try {
            directory = new MMapDirectory(indexDir, null);
            open(config.clean.get());
        } catch (OutOfMemoryError e) {
            log.info("Unable to mmap index: falling back to default.");
        }
    }

    if (searcher == null) {
        directory = FSDirectory.open(indexDir);
        open(config.clean.get());
    }

    // init cache (if configured)
    CacheManager cacheManager = config.documentCache.get();
    if (cacheManager != null) {
        cache = cacheManager.createCache("LuceneRecordStore-" + hashCode(),
                new MutableConfiguration().setReadThrough(true).setCacheLoaderFactory(() -> loader));
    }

    // init ExecutorService
    executor = Optional.ofNullable(config.executor.get()).orElse(defaultExecutor);

    log.info("Database: " + indexDir.getAbsolutePath() + "\n    size: " + FileUtils.sizeOfDirectory(indexDir)
            + "\n    using: " + directory.getClass().getSimpleName() + "\n    files in directry: "
            + Arrays.asList(directory.listAll()) + "\n    cache: "
            + (cache != null ? cache.getClass().getSimpleName() : "none") + "\n    executor: "
            + executor.getClass().getSimpleName());
}