优化代码提交

This commit is contained in:
lilin 2025-07-07 11:59:28 +08:00
parent a7ab178032
commit fc27c0f571

View File

@ -3173,37 +3173,83 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
}
// ================ 2. 过滤并处理符合条件的记录 ================
List<TsFiles> filteredRecords = records.stream()
List<TsFiles> filteredRecords = records.parallelStream() // 使用并行流提高处理效率根据场景选择
.filter(tsFile -> {
try {
if("FOLDER".equals(tsFile.getIsFile())){
return false;
}
// 计算本地文件MD5
File localFile = new File(filePathConfig.getValue() + tsFile.getWorkPath(), tsFile.getFileName());
String localMD5 = calculateMD5Data(new FileInputStream(localFile));
// 计算MinIO文件MD5
String minioMD5 = getMinioMD5Data(bucketConfig.getValue(), tsFile.getBackupPath(), tsFile.getFileName(),storageSource);
// 路径处理
// tsFile.setWorkPath(processingPath(tsFile.getWorkPath(), tsFile.getNodeId()));
// tsFile.setBackupPath(processingPath(tsFile.getBackupPath(), tsFile.getNodeId()));
// 设置MD5字段即使不满足条件也保留字段
tsFile.setLocatMd5(localMD5);
tsFile.setMinioMd5(minioMD5);
// 返回是否满足过滤条件
return StringUtils.isNoneEmpty(localMD5, minioMD5) && !localMD5.equals(minioMD5);
} catch (Exception e) {
LOGGER.error("MD5计算失败: {}", tsFile.getFileName(), e);
return false;
if ("FOLDER".equals(tsFile.getIsFile())) {
return false; // 跳过文件夹
}
String localMD5 = null;
String minioMD5 = null;
try (InputStream localStream = new FileInputStream( // 自动关闭资源
new File(filePathConfig.getValue() + tsFile.getWorkPath(), tsFile.getFileName())
)) {
// 计算本地文件MD5
localMD5 = calculateMD5Data(localStream);
} catch (Exception e) {
LOGGER.error("本地文件MD5计算失败: {} | 路径: {}",
tsFile.getFileName(),
filePathConfig.getValue() + tsFile.getWorkPath(),
e
);
}
try {
// 计算MinIO文件MD5
minioMD5 = getMinioMD5Data(
bucketConfig.getValue(),
tsFile.getBackupPath(),
tsFile.getFileName(),
storageSource
);
} catch (Exception e) {
LOGGER.error("MinIO文件MD5获取失败: {} | 路径: {}/{}",
tsFile.getFileName(),
bucketConfig.getValue(),
tsFile.getBackupPath(),
e
);
}
// 设置MD5字段无论是否成功
tsFile.setLocatMd5(localMD5);
tsFile.setMinioMd5(minioMD5);
// 仅当两者都成功计算且不相等时保留
return StringUtils.isNoneEmpty(localMD5, minioMD5)
&& !localMD5.equals(minioMD5);
})
.collect(Collectors.toList());
// List<TsFiles> filteredRecords = records.stream()
// .filter(tsFile -> {
// try {
// if("FOLDER".equals(tsFile.getIsFile())){
// return false;
// }
// // 计算本地文件MD5
// File localFile = new File(filePathConfig.getValue() + tsFile.getWorkPath(), tsFile.getFileName());
// String localMD5 = calculateMD5Data(new FileInputStream(localFile));
//
// // 计算MinIO文件MD5
//
// String minioMD5 = getMinioMD5Data(bucketConfig.getValue(), tsFile.getBackupPath(), tsFile.getFileName(),storageSource);
//
// // 路径处理
// // tsFile.setWorkPath(processingPath(tsFile.getWorkPath(), tsFile.getNodeId()));
// // tsFile.setBackupPath(processingPath(tsFile.getBackupPath(), tsFile.getNodeId()));
//
// // 设置MD5字段即使不满足条件也保留字段
// tsFile.setLocatMd5(localMD5);
// tsFile.setMinioMd5(minioMD5);
//
// // 返回是否满足过滤条件
// return StringUtils.isNoneEmpty(localMD5, minioMD5) && !localMD5.equals(minioMD5);
// } catch (Exception e) {
// LOGGER.error("MD5计算失败: {}", tsFile.getFileName(), e);
// return false;
// }
// })
// .collect(Collectors.toList());
return filteredRecords;
} catch (Exception e) {
LOGGER.error("未知异常:{}", e.getMessage(), e);
@ -4905,6 +4951,10 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
queryWrapper.eq("task_id", taskId);
}
queryWrapper.isNotNull("work_path").ne("work_path", "");
//文件还是文件夹
queryWrapper.orderByDesc("is_file");
//文件名称
queryWrapper.orderByAsc("file_name");
List<TsFiles> allNodes = tsFilesMapper.selectList(queryWrapper);
@ -4970,6 +5020,11 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
}
queryWrapper.isNotNull("backup_path").ne("backup_path", "");
//文件还是文件夹
queryWrapper.orderByDesc("is_file");
//文件名称
queryWrapper.orderByAsc("file_name");
// queryWrapper.eq("parent_id", "00");
List<TsFiles> allNodes = tsFilesMapper.selectList(queryWrapper);
// 2. 双树构建容器