Example usage for org.apache.cassandra.io.util FileUtils createHardLink

List of usage examples for org.apache.cassandra.io.util FileUtils createHardLink

Introduction

In this page you can find the example usage for org.apache.cassandra.io.util FileUtils createHardLink.

Prototype

public static void createHardLink(File from, File to) 

Source Link

Usage

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

License:Apache License

/**
 * Cassandra table snapshot => hard links associated elasticsearch lucene files.
 *//*ww w . j  a  va 2  s.  c o  m*/
@SuppressForbidden(reason = "File used for snapshots")
public Callable<?> getSnapshotWithoutFlushTask(String snapshotName) {
    return () -> {
        if (isIndexing()) {
            for (MappingInfo.IndexInfo indexInfo : mappingInfo.indices.values()) {
                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;
    };
}