You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentIndexImpl.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.measure.live;
  21. import java.util.Collection;
  22. import java.util.Comparator;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.LinkedList;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.component.ComponentDto;
  32. import static java.util.Collections.emptyList;
  33. public class ComponentIndexImpl implements ComponentIndex {
  34. private final DbClient dbClient;
  35. private ComponentDto branchComponent;
  36. private List<ComponentDto> sortedComponentsToRoot;
  37. private Map<String, List<ComponentDto>> children;
  38. public ComponentIndexImpl(DbClient dbClient) {
  39. this.dbClient = dbClient;
  40. }
  41. /**
  42. * Loads all the components required for the calculation of the new values of the live measures based on what components were modified:
  43. * - All components between the touched components and the roots of their component trees
  44. * - All immediate children of those components
  45. */
  46. public void load(DbSession dbSession, List<ComponentDto> touchedComponents) {
  47. sortedComponentsToRoot = loadTreeOfComponents(dbSession, touchedComponents);
  48. branchComponent = findBranchComponent(sortedComponentsToRoot);
  49. children = new HashMap<>();
  50. List<ComponentDto> childComponents = loadChildren(dbSession, branchComponent.uuid(), sortedComponentsToRoot);
  51. for (ComponentDto c : childComponents) {
  52. List<String> uuidPathAsList = c.getUuidPathAsList();
  53. String parentUuid = uuidPathAsList.get(uuidPathAsList.size() - 1);
  54. children.computeIfAbsent(parentUuid, uuid -> new LinkedList<>()).add(c);
  55. }
  56. }
  57. private static ComponentDto findBranchComponent(Collection<ComponentDto> components) {
  58. return components.stream().filter(ComponentDto::isRootProject).findFirst()
  59. .orElseThrow(() -> new IllegalStateException("No project found in " + components));
  60. }
  61. private List<ComponentDto> loadChildren(DbSession dbSession, String branchUuid, Collection<ComponentDto> components) {
  62. return dbClient.componentDao().selectChildren(dbSession, branchUuid, components);
  63. }
  64. private List<ComponentDto> loadTreeOfComponents(DbSession dbSession, List<ComponentDto> touchedComponents) {
  65. Set<String> componentUuids = new HashSet<>();
  66. for (ComponentDto component : touchedComponents) {
  67. componentUuids.add(component.uuid());
  68. // ancestors, excluding self
  69. componentUuids.addAll(component.getUuidPathAsList());
  70. }
  71. return dbClient.componentDao().selectByUuids(dbSession, componentUuids).stream()
  72. .sorted(Comparator.comparing(ComponentDto::getUuidPath).reversed())
  73. .toList();
  74. }
  75. @Override
  76. public List<ComponentDto> getChildren(ComponentDto component) {
  77. return children.getOrDefault(component.uuid(), emptyList());
  78. }
  79. @Override
  80. public Set<String> getAllUuids() {
  81. Set<String> all = new HashSet<>();
  82. sortedComponentsToRoot.forEach(c -> all.add(c.uuid()));
  83. for (Collection<ComponentDto> l : children.values()) {
  84. for (ComponentDto c : l) {
  85. all.add(c.uuid());
  86. }
  87. }
  88. return all;
  89. }
  90. @Override
  91. public List<ComponentDto> getSortedTree() {
  92. return sortedComponentsToRoot;
  93. }
  94. @Override public ComponentDto getBranch() {
  95. return branchComponent;
  96. }
  97. }