Example usage for java.nio.file Files newDirectoryStream

List of usage examples for java.nio.file Files newDirectoryStream

Introduction

In this page you can find the example usage for java.nio.file Files newDirectoryStream.

Prototype

public static DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
        throws IOException 

Source Link

Document

Opens a directory, returning a DirectoryStream to iterate over the entries in the directory.

Usage

From source file:org.elassandra.index.ElasticSecondaryIndex.java

/**
 * Cassandra table snapshot => hard links associated elasticsearch lucene files.
 *///from   w  w  w  .ja  v  a 2  s  . co  m
@SuppressForbidden(reason = "File used for snapshots")
public Callable<?> getSnapshotWithoutFlushTask(String snapshotName) {
    return () -> {
        if (isIndexing()) {
            for (ImmutableMappingInfo.ImmutableIndexInfo indexInfo : mappingInfo.indices) {
                IndexShard indexShard = indexInfo.indexService.shard(0);
                if (indexShard != null && indexInfo.snapshot) {
                    if (indexShard.state() == IndexShardState.STARTED) {
                        // snapshotPath = data/elasticsearch.data/<cluster_name>/nodes/0/snapshots
                        Path snapshotPath = indexShard.shardPath().resolveSnapshot();
                        if ((Files.notExists(snapshotPath)))
                            Files.createDirectory(snapshotPath, snapshotDirPermissions);

                        // snapshotIndex = data/elasticsearch.data/<cluster_name>/nodes/0/snapshots/<index_name>
                        Path snapshotIndex = snapshotPath.resolve(indexShard.shardId().getIndex());
                        if ((Files.notExists(snapshotIndex)))
                            Files.createDirectory(snapshotIndex, snapshotDirPermissions);

                        // snapshotDir = data/elasticsearch.data/<cluster_name>/nodes/0/snapshots/<index_name>/<snapshot_name>
                        Path snapshotDir = Files.createDirectory(snapshotIndex.resolve(snapshotName),
                                snapshotDirPermissions);
                        Path indexPath = indexShard.shardPath().resolveIndex();

                        try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath,
                                "{_*.*,segments*}")) {
                            for (Path luceneFile : stream) {
                                File targetLink = new File(snapshotDir.toFile(),
                                        luceneFile.getFileName().toString());
                                FileUtils.createHardLink(luceneFile.toFile(), targetLink);
                            }
                            if (logger.isDebugEnabled())
                                logger.debug("Elasticsearch index=[{}], snapshot=[{}], path=[{}]",
                                        indexInfo.name, snapshotName, snapshotDir.toString());
                        } catch (DirectoryIteratorException ex) {
                            logger.error("Failed to retreive lucene files in {}", ex, indexPath);
                        }
                    } else {
                        if (logger.isDebugEnabled())
                            logger.debug("Cannot snapshot index=[{}], state=[{}], snapshot=[{}]",
                                    indexInfo.name, indexShard.state(), snapshotName);
                    }
                }
            }
        }
        return null;
    };
}