From e95eeb8b75b2c7ba247664ee918884465d3ab26b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cv=5Fkkhuang=E2=80=9D?= <“420895376@qq.com”> Date: Thu, 9 Jan 2025 14:17:38 +0800 Subject: [PATCH] Optimize interface logic --- .../impl/DefaultLabelManagerPersistence.java | 55 ++++++++----------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/linkis-computation-governance/linkis-manager/linkis-manager-persistence/src/main/java/org/apache/linkis/manager/persistence/impl/DefaultLabelManagerPersistence.java b/linkis-computation-governance/linkis-manager/linkis-manager-persistence/src/main/java/org/apache/linkis/manager/persistence/impl/DefaultLabelManagerPersistence.java index 5e515f589b..74774c8aad 100644 --- a/linkis-computation-governance/linkis-manager/linkis-manager-persistence/src/main/java/org/apache/linkis/manager/persistence/impl/DefaultLabelManagerPersistence.java +++ b/linkis-computation-governance/linkis-manager/linkis-manager-persistence/src/main/java/org/apache/linkis/manager/persistence/impl/DefaultLabelManagerPersistence.java @@ -327,40 +327,29 @@ public Map> getNodeRelationsByLabels( @Override public Map> getLabelRelationsByServiceInstance( - List serviceInstances) { + List serviceInstances) { if (CollectionUtils.isEmpty(serviceInstances)) return Collections.emptyMap(); - try { - List> nodeRelationsByLabels = - labelManagerMapper.listLabelRelationByServiceInstance(serviceInstances); - List> arrays = - new ArrayList>(); - for (Map nodeRelationsByLabel : nodeRelationsByLabels) { - Optional instanceOption = - serviceInstances.stream() - .filter( - serviceInstance1 -> - serviceInstance1 - .getInstance() - .equalsIgnoreCase(String.valueOf(nodeRelationsByLabel.get("instance")))) - .findFirst(); - PersistenceLabel persistenceLabel = new PersistenceLabel(); - BeanUtils.populate(persistenceLabel, nodeRelationsByLabel); - PersistenceUtils.setValue(persistenceLabel); - instanceOption.ifPresent( - serviceInstance -> arrays.add(new Tunple(serviceInstance, persistenceLabel))); - } - return arrays.stream() - .collect(Collectors.groupingBy(Tunple::getKey)) - .entrySet() - .stream() - .collect( - Collectors.toMap( - Map.Entry::getKey, - f -> f.getValue().stream().map(Tunple::getValue).collect(Collectors.toList()))); - } catch (InvocationTargetException | IllegalAccessException e) { - throw new PersistenceWarnException( - BEANUTILS_POPULATE_FAILED.getErrorCode(), BEANUTILS_POPULATE_FAILED.getErrorDesc(), e); - } + Map> resultMap = new HashMap<>(); + List> nodeRelationsByLabels = + labelManagerMapper.listLabelRelationByServiceInstance(serviceInstances); + Map>> groupByInstanceMap = nodeRelationsByLabels.stream() + .collect(Collectors.groupingBy(nodeRelationsByLabel -> nodeRelationsByLabel.get("instance").toString())); + serviceInstances.stream().filter(serviceInstance -> groupByInstanceMap.containsKey(serviceInstance.getInstance())).forEach(serviceInstance -> { + List persistenceLabelList = new ArrayList<>(); + groupByInstanceMap.get(serviceInstance.getInstance()).forEach(map -> { + try { + PersistenceLabel persistenceLabel = new PersistenceLabel(); + BeanUtils.populate(persistenceLabel, map); + PersistenceUtils.setValue(persistenceLabel); + persistenceLabelList.add(persistenceLabel); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new PersistenceWarnException( + BEANUTILS_POPULATE_FAILED.getErrorCode(), BEANUTILS_POPULATE_FAILED.getErrorDesc(), e); + } + }); + resultMap.put(serviceInstance, persistenceLabelList); + }); + return resultMap; } @Override