in hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/load/FileMappingService.java [177:238]
public boolean tryMergePartFiles(String dirPath, int total) {
File dir = new File(dirPath);
File[] partFiles = dir.listFiles();
if (partFiles == null) {
throw new InternalException("The part files can't be null");
}
if (partFiles.length != total) {
return false;
}
File newFile = new File(dir.getPath() + ".all");
File destFile = new File(dir.getPath());
if (partFiles.length == 1) {
try {
// Rename file to dest file
FileUtils.moveFile(partFiles[0], newFile);
} catch (IOException e) {
log.error("Failed to rename file from {} to {}",
partFiles[0], newFile, e);
throw new InternalException("load.upload.move-file.failed", e);
}
} else {
Arrays.sort(partFiles, (o1, o2) -> {
String file1Idx = StringUtils.substringAfterLast(o1.getName(),
"-");
String file2Idx = StringUtils.substringAfterLast(o2.getName(),
"-");
Integer idx1 = Integer.valueOf(file1Idx);
Integer idx2 = Integer.valueOf(file2Idx);
return idx1.compareTo(idx2);
});
try (OutputStream os = new FileOutputStream(newFile, true)) {
for (int i = 0; i < partFiles.length; i++) {
File partFile = partFiles[i];
try (InputStream is = new FileInputStream(partFile)) {
IOUtils.copy(is, os);
} catch (IOException e) {
log.error("Failed to copy file stream from {} to {}",
partFile, newFile, e);
throw new InternalException(
"load.upload.merge-file.failed", e);
}
}
} catch (IOException e) {
log.error("Failed to copy all file-parts stream to {}",
newFile, e);
throw new InternalException("load.upload.merge-file.failed", e);
}
}
// Delete origin directory
try {
FileUtils.forceDelete(dir);
} catch (IOException e) {
log.error("Failed to force delete file {}", dir, e);
throw new InternalException("load.upload.delete-temp-dir.failed", e);
}
// Rename file to dest file
if (!newFile.renameTo(destFile)) {
throw new InternalException("load.upload.rename-file.failed");
}
return true;
}