Java Code Examples for

The following examples show how to use These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   Source File:    License: Apache License 2.0 6 votes vote down vote up
 * Tries to extract the leaf of the {@link Directory} if the directory is a {@link FilterDirectory} and cast
 * it to the given target class or returns the given default value, if the leaf is not assignable to the target class.
 * If the given {@link Directory} is a concrete directory it will treated as a leaf and the above applies.
public static <T extends Directory> T getLeaf(Directory dir, Class<T> targetClass, T defaultValue) {
    Directory d = dir;
    if (dir instanceof FilterDirectory) {
        d = getLeafDirectory((FilterDirectory) dir, targetClass);
    if (d instanceof FileSwitchDirectory) {
        T leaf = getLeaf(((FileSwitchDirectory) d).getPrimaryDir(), targetClass);
        if (leaf == null) {
            d = getLeaf(((FileSwitchDirectory) d).getSecondaryDir(), targetClass, defaultValue);
        } else {
            d = leaf;

    if (d != null && targetClass.isAssignableFrom(d.getClass())) {
        return targetClass.cast(d);
    } else {
        return defaultValue;
Example 2
Source Project: crate   Source File:    License: Apache License 2.0 6 votes vote down vote up
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);
        return new FileSwitchDirectory(preLoadExtensions, primary, directory, true) {
            public String[] listAll() throws IOException {
                // avoid listing twice
                return primary.listAll();
    return directory;
Example 3
Source Project: lucene-solr   Source File:    License: Apache License 2.0 5 votes vote down vote up
private static Directory newFileSwitchDirectory(Random random, Directory dir1, Directory dir2) {
  List<String> fileExtensions =
      Arrays.asList("fdt", "fdx", "tim", "tip", "si", "fnm", "pos", "dii", "dim", "nvm", "nvd", "dvm", "dvd");
  Collections.shuffle(fileExtensions, random);
  fileExtensions = fileExtensions.subList(0, 1 + random.nextInt(fileExtensions.size()));
  return new FileSwitchDirectory(new HashSet<>(fileExtensions), dir1, dir2, true);
Example 4
Source Project: lucene-solr   Source File:    License: Apache License 2.0 5 votes vote down vote up
/** If the dir is an {@link FSDirectory} or wraps one via possibly
 *  nested {@link FilterDirectory} or {@link FileSwitchDirectory},
 *  this returns {@link #spins(Path)} for the wrapped directory,
 *  else, true.
 *  @throws IOException if {@code path} does not exist.
 *  @lucene.internal */
public static boolean spins(Directory dir) throws IOException {
  dir = FilterDirectory.unwrap(dir);
  if (dir instanceof FileSwitchDirectory) {
    FileSwitchDirectory fsd = (FileSwitchDirectory) dir;
    // Spinning is contagious:
    return spins(fsd.getPrimaryDir()) || spins(fsd.getSecondaryDir());
  } else if (dir instanceof ByteBuffersDirectory) {
    return false;
  } else if (dir instanceof FSDirectory) {
    return spins(((FSDirectory) dir).getDirectory());
  } else {
    return true;
Example 5
Source Project: crate   Source File:    License: Apache License 2.0 5 votes vote down vote up
boolean useDelegate(String name) {
    String extension = FileSwitchDirectory.getExtension(name);
    switch (extension) {
        // Norms, doc values and term dictionaries are typically performance-sensitive and hot in the page
        // cache, so we use mmap, which provides better performance.
        case "nvd":
        case "dvd":
        case "tim":
        // We want to open the terms index and KD-tree index off-heap to save memory, but this only performs
        // well if using mmap.
        case "tip":
        case "dim":
        // Compound files are tricky because they store all the information for the segment. Benchmarks
        // suggested that not mapping them hurts performance.
        case "cfs":
        // MMapDirectory has special logic to read long[] arrays in little-endian order that helps speed
        // up the decoding of postings. The same logic applies to positions (.pos) of offsets (.pay) but we
        // are not mmaping them as queries that leverage positions are more costly and the decoding of postings
        // tends to be less a bottleneck.
        case "doc":
            return true;
        // Other files are either less performance-sensitive (e.g. stored field index, norms metadata)
        // or are large and have a random access pattern and mmap leads to page cache trashing
        // (e.g. stored fields and term vectors).
            return false;