use of com.baidu.hugegraph.exception.InternalException in project incubator-hugegraph-toolchain by apache.
the class FileUploadController method ensureLocationExist.
private void ensureLocationExist(String location, String connPath) {
String path = Paths.get(location, connPath).toString();
File locationDir = new File(path);
if (!locationDir.exists()) {
try {
FileUtils.forceMkdir(locationDir);
} catch (IOException e) {
throw new InternalException("failed to create location dir", e);
}
}
}
use of com.baidu.hugegraph.exception.InternalException in project incubator-hugegraph-toolchain by apache.
the class FileMappingService method deleteDiskFile.
public void deleteDiskFile(FileMapping mapping) {
File file = new File(mapping.getPath());
if (file.isDirectory()) {
log.info("Prepare to delete directory {}", file);
try {
FileUtils.forceDelete(file);
} catch (IOException e) {
throw new InternalException("Failed to delete directory " + "corresponded to the file id %s, " + "please delete it manually", e, mapping.getId());
}
} else {
File parentDir = file.getParentFile();
log.info("Prepare to delete directory {}", parentDir);
try {
FileUtils.forceDelete(parentDir);
} catch (IOException e) {
throw new InternalException("Failed to delete parent directory " + "corresponded to the file id %s, " + "please delete it manually", e, mapping.getId());
}
}
}
use of com.baidu.hugegraph.exception.InternalException in project incubator-hugegraph-toolchain by apache.
the class FileMappingService method tryMergePartFiles.
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;
}
use of com.baidu.hugegraph.exception.InternalException in project incubator-hugegraph-toolchain by apache.
the class JobManagerController method update.
@PutMapping("{id}")
public JobManager update(@PathVariable("connId") int connId, @PathVariable("id") int id, @RequestBody JobManager newEntity) {
Ex.check(newEntity.getJobName().length() <= 48, "job.manager.job-name.reached-limit");
Ex.check(newEntity.getJobName() != null, () -> Constant.COMMON_NAME_PATTERN.matcher(newEntity.getJobName()).matches(), "job.manager.job-name.unmatch-regex");
Ex.check(!StringUtils.isEmpty(newEntity.getJobRemarks()), () -> Constant.COMMON_REMARK_PATTERN.matcher(newEntity.getJobRemarks()).matches(), "job.manager.job-remarks.unmatch-regex");
// Check exist Job Manager with this id
JobManager entity = this.service.get(id);
if (entity == null) {
throw new ExternalException("job-manager.not-exist.id", id);
}
if (!newEntity.getJobName().equals(entity.getJobName()) && this.service.getTask(newEntity.getJobName(), connId) != null) {
throw new InternalException("job.manager.job-name.repeated");
}
entity.setJobName(newEntity.getJobName());
entity.setJobRemarks(newEntity.getJobRemarks());
this.service.update(entity);
return entity;
}
Aggregations