use of java.nio.file.FileAlreadyExistsException in project neo4j by neo4j.
the class DumpCommandTest method shouldGiveAClearErrorIfTheArchiveAlreadyExists.
@Test
public void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws Exception {
doThrow(new FileAlreadyExistsException("the-archive-path")).when(dumper).dump(any(), any(), any());
try {
execute("foo.db");
fail("expected exception");
} catch (CommandFailed e) {
assertThat(e.getMessage(), equalTo("archive already exists: the-archive-path"));
}
}
use of java.nio.file.FileAlreadyExistsException in project neo4j by neo4j.
the class DumperTest method shouldGiveAClearErrorIfTheArchiveAlreadyExists.
@Test
public void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws IOException {
Path directory = testDirectory.directory("a-directory").toPath();
Path archive = testDirectory.file("the-archive.dump").toPath();
Files.write(archive, new byte[0]);
try {
new Dumper().dump(directory, archive, Predicates.alwaysFalse());
fail("Expected an exception");
} catch (FileAlreadyExistsException e) {
assertEquals(archive.toString(), e.getMessage());
}
}
use of java.nio.file.FileAlreadyExistsException in project neo4j by neo4j.
the class EphemeralFileSystemAbstraction method renameFile.
@Override
public void renameFile(File from, File to, CopyOption... copyOptions) throws IOException {
from = canonicalFile(from);
to = canonicalFile(to);
if (!files.containsKey(from)) {
throw new NoSuchFileException("'" + from + "' doesn't exist");
}
boolean replaceExisting = false;
for (CopyOption copyOption : copyOptions) {
replaceExisting |= copyOption == REPLACE_EXISTING;
}
if (files.containsKey(to) && !replaceExisting) {
throw new FileAlreadyExistsException("'" + to + "' already exists");
}
if (!isDirectory(to.getParentFile())) {
throw new NoSuchFileException("Target directory[" + to.getParent() + "] does not exists");
}
files.put(to, files.remove(from));
}
use of java.nio.file.FileAlreadyExistsException in project presto by prestodb.
the class ShardRecoveryManager method restoreFromBackup.
@VisibleForTesting
void restoreFromBackup(UUID shardUuid, OptionalLong shardSize) {
File storageFile = storageService.getStorageFile(shardUuid);
if (!backupStore.get().shardExists(shardUuid)) {
stats.incrementShardRecoveryBackupNotFound();
throw new PrestoException(RAPTOR_RECOVERY_ERROR, "No backup file found for shard: " + shardUuid);
}
if (storageFile.exists()) {
if (!shardSize.isPresent() || (storageFile.length() == shardSize.getAsLong())) {
return;
}
log.warn("Local shard file is corrupt. Deleting local file: %s", storageFile);
storageFile.delete();
}
// create a temporary file in the staging directory
File stagingFile = temporarySuffix(storageService.getStagingFile(shardUuid));
storageService.createParents(stagingFile);
// copy to temporary file
log.info("Copying shard %s from backup...", shardUuid);
long start = System.nanoTime();
try {
backupStore.get().restoreShard(shardUuid, stagingFile);
} catch (PrestoException e) {
stats.incrementShardRecoveryFailure();
stagingFile.delete();
throw e;
}
Duration duration = nanosSince(start);
DataSize size = succinctBytes(stagingFile.length());
DataSize rate = dataRate(size, duration).convertToMostSuccinctDataSize();
stats.addShardRecoveryDataRate(rate, size, duration);
log.info("Copied shard %s from backup in %s (%s at %s/s)", shardUuid, duration, size, rate);
// move to final location
storageService.createParents(storageFile);
try {
Files.move(stagingFile.toPath(), storageFile.toPath(), ATOMIC_MOVE);
} catch (FileAlreadyExistsException e) {
// someone else already created it (should not happen, but safe to ignore)
} catch (IOException e) {
stats.incrementShardRecoveryFailure();
throw new PrestoException(RAPTOR_RECOVERY_ERROR, "Failed to move shard: " + shardUuid, e);
} finally {
stagingFile.delete();
}
if (!storageFile.exists() || (shardSize.isPresent() && (storageFile.length() != shardSize.getAsLong()))) {
stats.incrementShardRecoveryFailure();
log.info("Files do not match after recovery. Deleting local file: " + shardUuid);
storageFile.delete();
throw new PrestoException(RAPTOR_RECOVERY_ERROR, "File not recovered correctly: " + shardUuid);
}
stats.incrementShardRecoverySuccess();
}
use of java.nio.file.FileAlreadyExistsException in project flink by apache.
the class LocalFileSystem method create.
@Override
public FSDataOutputStream create(final Path filePath, final WriteMode overwrite) throws IOException {
checkNotNull(filePath, "filePath");
if (exists(filePath) && overwrite == WriteMode.NO_OVERWRITE) {
throw new FileAlreadyExistsException("File already exists: " + filePath);
}
final Path parent = filePath.getParent();
if (parent != null && !mkdirs(parent)) {
throw new IOException("Mkdirs failed to create " + parent);
}
final File file = pathToFile(filePath);
return new LocalDataOutputStream(file);
}
Aggregations