perf: 零部件懒加载性能优化

This commit is contained in:
2024-08-20 11:07:25 +08:00
parent f937c95be0
commit a9e2a104cf
@@ -14,6 +14,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -192,12 +193,14 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
@Override
public List<PartName> queryList(String nodeName) {
// 查询所有节点
// 查询所有节点并建立父节点映射
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
List<PartName> allNodes = list(queryWrapper);
Map<String, PartName> nodeMap = allNodes.stream()
.collect(Collectors.toMap(PartName::getCode, node -> node));
// 通过 nodeName 过滤符合条件的节点
List<PartName> matchedNodes = allNodes.stream()
List<PartName> matchedNodes = allNodes.parallelStream()
.filter(node -> node.getName() != null && node.getName().contains(nodeName))
.collect(Collectors.toList());
@@ -206,52 +209,47 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
}
// 用于存储一级节点
Set<PartName> rootNodes = new HashSet<>();
Set<PartName> rootNodes = ConcurrentHashMap.newKeySet();
// 查找每个匹配节点的一级父节点
for (PartName matchedNode : matchedNodes) {
// 并行查找每个匹配节点的一级父节点
matchedNodes.parallelStream().forEach(matchedNode -> {
PartName currentNode = matchedNode;
while (currentNode != null && !currentNode.getParentCode().equals("Part")) {
// 查找当前节点的父节点
String parentCode = currentNode.getParentCode();
currentNode = allNodes.stream()
.filter(node -> node.getCode().equals(parentCode))
.findFirst()
.orElse(null);
while (currentNode != null && !"Part".equals(currentNode.getParentCode())) {
currentNode = nodeMap.get(currentNode.getParentCode());
}
// 添加一级节点到结果集
if (currentNode != null) {
rootNodes.add(currentNode);
}
}
});
// 设置叶子标志的逻辑调整
for (PartName node : rootNodes) {
Set<String> descendantCodes = getAllDescendantCodes(node.getCode(), allNodes); // 获取所有子孙节点的ID
boolean isLeaf = true; // 默认设置为叶子节点
// 并行设置叶子标志
rootNodes.parallelStream().forEach(node -> {
Set<String> descendantCodes = getAllDescendantCodes(node.getCode(), nodeMap);
boolean isLeaf = matchedNodes.stream()
.noneMatch(matchedNode -> descendantCodes.contains(matchedNode.getCode()));
node.setLeafFlag(isLeaf);
});
for (PartName matchedNode : matchedNodes) {
if (descendantCodes.contains(matchedNode.getCode())) {
isLeaf = false;
break; // 如果在子孙节点中找到匹配节点,当前根节点不是叶子节点
}
}
node.setLeafFlag(isLeaf); // 设置叶子标志
}
return new ArrayList<>(rootNodes);
// 按名字排序
return rootNodes.stream()
.sorted(Comparator.comparing(PartName::getName))
.collect(Collectors.toList());
}
// 方法:获取给定节点ID的所有子孙节点ID
private Set<String> getAllDescendantCodes(String parentCode, List<PartName> allNodes) {
// 优化后的获取子孙节点ID方法,使用节点映射避免重复计算
private Set<String> getAllDescendantCodes(String parentCode, Map<String, PartName> nodeMap) {
Set<String> descendantCodes = new HashSet<>();
List<PartName> directChildren = allNodes.stream()
.filter(node -> node.getParentCode().equals(parentCode))
.collect(Collectors.toList());
Deque<String> stack = new ArrayDeque<>();
stack.push(parentCode);
for (PartName child : directChildren) {
descendantCodes.add(child.getCode());
descendantCodes.addAll(getAllDescendantCodes(child.getCode(), allNodes)); // 递归获取子孙节点
while (!stack.isEmpty()) {
String currentCode = stack.pop();
nodeMap.values().parallelStream()
.filter(node -> node.getParentCode().equals(currentCode))
.forEach(child -> {
descendantCodes.add(child.getCode());
stack.push(child.getCode());
});
}
return descendantCodes;
@@ -259,12 +257,14 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
@Override
public List<PartName> getChild(String parentCode, String nodeName) {
// 查询所有节点
// 查询所有节点并建立父节点映射
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
List<PartName> allNodes = list(queryWrapper);
Map<String, PartName> nodeMap = allNodes.stream()
.collect(Collectors.toMap(PartName::getCode, node -> node));
// 通过 nodeName 过滤符合条件的节点
List<PartName> matchedNodes = allNodes.stream()
List<PartName> matchedNodes = allNodes.parallelStream()
.filter(node -> node.getName() != null && node.getName().contains(nodeName))
.collect(Collectors.toList());
@@ -273,34 +273,31 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
}
// 用于存储一级节点
Set<PartName> rootNodes = new HashSet<>();
Set<PartName> rootNodes = ConcurrentHashMap.newKeySet();
// 查找每个匹配节点的一级父节点
for (PartName matchedNode : matchedNodes) {
// 并行查找每个匹配节点的一级父节点
matchedNodes.parallelStream().forEach(matchedNode -> {
PartName currentNode = matchedNode;
while (currentNode != null && !currentNode.getParentCode().equals(parentCode)) {
// 查找当前节点的父节点
String pCode = currentNode.getParentCode();
currentNode = allNodes.stream()
.filter(node -> node.getCode().equals(pCode))
.findFirst()
.orElse(null);
while (currentNode != null && !parentCode.equals(currentNode.getParentCode())) {
currentNode = nodeMap.get(currentNode.getParentCode());
}
// 添加一级节点到结果集
if (currentNode != null) {
rootNodes.add(currentNode);
}
}
// 设置叶子标志
Set<String> rootNodeCodes = rootNodes.stream().map(PartName::getCode).collect(Collectors.toSet());
rootNodes.forEach(node -> {
node.setLeafFlag(matchedNodes.stream()
.noneMatch(n -> (rootNodeCodes.contains(n.getParentCode()) &&
n.getParentCode().equals(node.getCode()))));
});
return new ArrayList<>(rootNodes);
// 并行设置叶子标志
rootNodes.parallelStream().forEach(node -> {
boolean isLeaf = matchedNodes.stream()
.noneMatch(n -> parentCode.equals(n.getParentCode()) &&
n.getParentCode().equals(node.getCode()));
node.setLeafFlag(isLeaf);
});
// 按名字排序
return rootNodes.stream()
.sorted(Comparator.comparing(PartName::getName))
.collect(Collectors.toList());
}
}