优化代码提交

This commit is contained in:
lilin 2025-07-07 13:58:40 +08:00
parent fc27c0f571
commit f03a3918e8

View File

@ -3086,37 +3086,94 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
}
// ================ 2. 过滤并处理符合条件的记录 ================
List<TsFiles> filteredRecords = tsFilesPage.getRecords().stream()
// ================ 2. 优化后的MD5比较处理 ================
List<TsFiles> filteredRecords = tsFilesPage.getRecords().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;
boolean hasError = false;
// 计算本地文件MD5带资源自动管理
String localFilePath = filePathConfig.getValue() + tsFile.getWorkPath();
File localFile = new File(localFilePath, tsFile.getFileName());
if (localFile.exists()) {
try (InputStream is = new FileInputStream(localFile)) {
localMD5 = calculateMD5Data(is);
} catch (Exception e) {
LOGGER.error("本地文件MD5计算失败: {} | 路径: {}",
tsFile.getFileName(), localFilePath, e);
hasError = true;
}
} else {
LOGGER.warn("本地文件不存在: {} | 路径: {}", tsFile.getFileName(), localFilePath);
hasError = true;
}
// 计算MinIO文件MD5
try {
minioMD5 = getMinioMD5Data(
bucketConfig.getValue(),
tsFile.getBackupPath(),
tsFile.getFileName(),
storageSource
);
} catch (Exception e) {
LOGGER.error("MinIO文件MD5获取失败: {} | Bucket: {}/{}",
tsFile.getFileName(),
bucketConfig.getValue(),
tsFile.getBackupPath(),
e);
hasError = true;
}
// 始终设置MD5字段成功为实际值失败为null
tsFile.setLocatMd5(localMD5);
tsFile.setMinioMd5(minioMD5);
// 仅当两者都成功计算且不相等时保留
return !hasError &&
StringUtils.isNoneEmpty(localMD5, minioMD5) &&
!localMD5.equals(minioMD5);
})
.collect(Collectors.toList());
// List<TsFiles> filteredRecords = tsFilesPage.getRecords().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());
// ================ 3. 构建新的分页结果 ================
Page<TsFiles> resultPage = new Page<>();
@ -3824,13 +3881,13 @@ public class TsFilesServiceImpl extends ServiceImpl<TsFilesMapper, TsFiles> impl
});
totalProcessed += batchList.size();
LOGGER.debug("已处理: {}/{}", totalProcessed, batchList.size());
// LOGGER.debug("已处理: {}/{}", totalProcessed, batchList.size());
}
// 4. 批量更新路径
long updateStart = System.currentTimeMillis();
updateBackupPaths(taskId, nodeId);
LOGGER.info("批量更新耗时: {}ms", System.currentTimeMillis() - updateStart);
// LOGGER.info("批量更新耗时: {}ms", System.currentTimeMillis() - updateStart);
// 5. 性能监控
long totalTime = System.currentTimeMillis() - startTime;