|
62 | 62 | import org.springframework.transaction.support.TransactionTemplate; |
63 | 63 |
|
64 | 64 | import java.sql.Timestamp; |
| 65 | +import java.util.HashSet; |
65 | 66 | import java.util.List; |
66 | 67 | import java.util.Map; |
67 | 68 | import java.util.Objects; |
@@ -487,43 +488,42 @@ public ConfigImportResult importData(final String namespace, final List<Discover |
487 | 488 | return ConfigImportResult.success(); |
488 | 489 | } |
489 | 490 | Map<String, String> discoveryHandlerIdMapping = context.getDiscoveryHandlerIdMapping(); |
490 | | - Map<String, List<DiscoveryDO>> pluginDiscoveryMap = discoveryMapper |
| 491 | + Map<String, Set<String>> pluginDiscoveryMap = discoveryMapper |
491 | 492 | .selectAllByNamespaceId(namespace) |
492 | 493 | .stream() |
493 | | - .collect(Collectors.groupingBy(DiscoveryDO::getPluginName)); |
| 494 | + .collect(Collectors.groupingBy(DiscoveryDO::getPluginName, |
| 495 | + Collectors.mapping(DiscoveryDO::getDiscoveryName, Collectors.toSet()))); |
| 496 | + Map<String, Map<String, String>> importedHandlerIdMap = Maps.newHashMap(); |
494 | 497 | int successCount = 0; |
495 | 498 | StringBuilder errorMsgBuilder = new StringBuilder(); |
496 | 499 | for (DiscoveryDTO discoveryDTO : discoveryList) { |
497 | 500 | String pluginName = discoveryDTO.getPluginName(); |
498 | 501 | String discoveryName = discoveryDTO.getName(); |
499 | | - Set<String> existDiscoveryNameSet = pluginDiscoveryMap |
500 | | - .getOrDefault(pluginName, Lists.newArrayList()) |
501 | | - .stream() |
502 | | - .map(DiscoveryDO::getDiscoveryName) |
503 | | - .collect(Collectors.toSet()); |
| 502 | + Set<String> existDiscoveryNameSet = pluginDiscoveryMap.computeIfAbsent(pluginName, key -> new HashSet<>()); |
| 503 | + Map<String, String> importedHandlerIds = importedHandlerIdMap.computeIfAbsent(pluginName, key -> Maps.newHashMap()); |
504 | 504 | if (existDiscoveryNameSet.contains(discoveryName)) { |
505 | 505 | errorMsgBuilder |
506 | 506 | .append(discoveryName) |
507 | 507 | .append(","); |
| 508 | + String importedHandlerId = importedHandlerIds.get(discoveryName); |
| 509 | + if (Objects.nonNull(importedHandlerId) && Objects.nonNull(discoveryDTO.getDiscoveryHandler())) { |
| 510 | + discoveryHandlerIdMapping.put(discoveryDTO.getDiscoveryHandler().getId(), importedHandlerId); |
| 511 | + } |
508 | 512 | continue; |
509 | 513 | } |
510 | 514 | String discoveryId = UUIDUtils.getInstance().generateShortUuid(); |
511 | 515 | discoveryDTO.setId(discoveryId); |
512 | 516 | discoveryDTO.setNamespaceId(namespace); |
513 | | - create(discoveryDTO); |
| 517 | + if (Objects.isNull(create(discoveryDTO))) { |
| 518 | + continue; |
| 519 | + } |
| 520 | + existDiscoveryNameSet.add(discoveryName); |
514 | 521 | successCount++; |
515 | 522 |
|
516 | 523 | // import discovery handler data |
517 | | - String discoveryHandlerId = null; |
518 | | - if (Objects.nonNull(discoveryDTO.getDiscoveryHandler())) { |
519 | | - DiscoveryHandlerDO discoveryHandlerDO = DiscoveryTransfer |
520 | | - .INSTANCE |
521 | | - .mapToDO(discoveryDTO.getDiscoveryHandler()); |
522 | | - discoveryHandlerDO.setDiscoveryId(discoveryId); |
523 | | - discoveryHandlerId = UUIDUtils.getInstance().generateShortUuid(); |
524 | | - discoveryHandlerIdMapping.put(discoveryHandlerDO.getId(), discoveryHandlerId); |
525 | | - discoveryHandlerDO.setId(discoveryHandlerId); |
526 | | - discoveryHandlerMapper.insertSelective(discoveryHandlerDO); |
| 524 | + String discoveryHandlerId = importDiscoveryHandler(discoveryDTO, context); |
| 525 | + if (Objects.nonNull(discoveryHandlerId)) { |
| 526 | + importedHandlerIds.put(discoveryName, discoveryHandlerId); |
527 | 527 | } |
528 | 528 |
|
529 | 529 | // import discovery rel data |
@@ -561,4 +561,17 @@ public ConfigImportResult importData(final String namespace, final List<Discover |
561 | 561 | } |
562 | 562 | return ConfigImportResult.success(successCount); |
563 | 563 | } |
| 564 | + |
| 565 | + private String importDiscoveryHandler(final DiscoveryDTO discoveryDTO, final ConfigsImportContext context) { |
| 566 | + if (Objects.isNull(discoveryDTO.getDiscoveryHandler())) { |
| 567 | + return null; |
| 568 | + } |
| 569 | + DiscoveryHandlerDO discoveryHandlerDO = DiscoveryTransfer.INSTANCE.mapToDO(discoveryDTO.getDiscoveryHandler()); |
| 570 | + discoveryHandlerDO.setDiscoveryId(discoveryDTO.getId()); |
| 571 | + String discoveryHandlerId = UUIDUtils.getInstance().generateShortUuid(); |
| 572 | + context.getDiscoveryHandlerIdMapping().put(discoveryHandlerDO.getId(), discoveryHandlerId); |
| 573 | + discoveryHandlerDO.setId(discoveryHandlerId); |
| 574 | + discoveryHandlerMapper.insertSelective(discoveryHandlerDO); |
| 575 | + return discoveryHandlerId; |
| 576 | + } |
564 | 577 | } |
0 commit comments