Skip to content

Commit

Permalink
Optimize interface logic
Browse files Browse the repository at this point in the history
  • Loading branch information
“v_kkhuang” committed Jan 9, 2025
1 parent 8ff93f9 commit e95eeb8
Showing 1 changed file with 22 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,40 +327,29 @@ public Map<PersistenceLabel, List<ServiceInstance>> getNodeRelationsByLabels(

@Override
public Map<ServiceInstance, List<PersistenceLabel>> getLabelRelationsByServiceInstance(
List<ServiceInstance> serviceInstances) {
List<ServiceInstance> serviceInstances) {
if (CollectionUtils.isEmpty(serviceInstances)) return Collections.emptyMap();
try {
List<Map<String, Object>> nodeRelationsByLabels =
labelManagerMapper.listLabelRelationByServiceInstance(serviceInstances);
List<Tunple<ServiceInstance, PersistenceLabel>> arrays =
new ArrayList<Tunple<ServiceInstance, PersistenceLabel>>();
for (Map<String, Object> nodeRelationsByLabel : nodeRelationsByLabels) {
Optional<ServiceInstance> 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<ServiceInstance, List<PersistenceLabel>> resultMap = new HashMap<>();
List<Map<String, Object>> nodeRelationsByLabels =
labelManagerMapper.listLabelRelationByServiceInstance(serviceInstances);
Map<String, List<Map<String, Object>>> groupByInstanceMap = nodeRelationsByLabels.stream()
.collect(Collectors.groupingBy(nodeRelationsByLabel -> nodeRelationsByLabel.get("instance").toString()));
serviceInstances.stream().filter(serviceInstance -> groupByInstanceMap.containsKey(serviceInstance.getInstance())).forEach(serviceInstance -> {
List<PersistenceLabel> 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
Expand Down

0 comments on commit e95eeb8

Please sign in to comment.