Example usage for java.nio.file StandardCopyOption ATOMIC_MOVE

List of usage examples for java.nio.file StandardCopyOption ATOMIC_MOVE

Introduction

In this page you can find the example usage for java.nio.file StandardCopyOption ATOMIC_MOVE.

Prototype

StandardCopyOption ATOMIC_MOVE

To view the source code for java.nio.file StandardCopyOption ATOMIC_MOVE.

Click Source Link

Document

Move the file as an atomic file system operation.

Usage

From source file:org.apache.solr.handler.IndexFetcher.java

/**
 * The tlog files are moved from the tmp dir to the tlog dir as an atomic filesystem operation.
 * A backup of the old directory is maintained. If the directory move fails, it will try to revert back the original
 * tlog directory.//from w  w w.  j ava2  s. c  om
 */
private boolean copyTmpTlogFiles2Tlog(File tmpTlogDir) {
    Path tlogDir = FileSystems.getDefault().getPath(solrCore.getUpdateHandler().getUpdateLog().getLogDir());
    Path backupTlogDir = FileSystems.getDefault().getPath(tlogDir.getParent().toAbsolutePath().toString(),
            tmpTlogDir.getName());

    try {
        Files.move(tlogDir, backupTlogDir, StandardCopyOption.ATOMIC_MOVE);
    } catch (IOException e) {
        SolrException.log(LOG, "Unable to rename: " + tlogDir + " to: " + backupTlogDir, e);
        return false;
    }

    Path src = FileSystems.getDefault().getPath(backupTlogDir.toAbsolutePath().toString(),
            tmpTlogDir.getName());
    try {
        Files.move(src, tlogDir, StandardCopyOption.ATOMIC_MOVE);
    } catch (IOException e) {
        SolrException.log(LOG, "Unable to rename: " + src + " to: " + tlogDir, e);

        // In case of error, try to revert back the original tlog directory
        try {
            Files.move(backupTlogDir, tlogDir, StandardCopyOption.ATOMIC_MOVE);
        } catch (IOException e2) {
            // bad, we were not able to revert back the original tlog directory
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                    "Unable to rename: " + backupTlogDir + " to: " + tlogDir);
        }

        return false;
    }

    return true;
}