From: Simon Brandhof Date: Wed, 10 May 2017 21:04:42 +0000 (+0200) Subject: SONAR-9186 fix performance hotspot when verifying authorization X-Git-Tag: 6.4-RC1~52 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=133d5af9c0d39429334a140b9ead77bda2b4fe98;p=sonarqube.git SONAR-9186 fix performance hotspot when verifying authorization by reducing the number of SQL requests. --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java index 8c4ea02e2c9..e792752cba4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java @@ -30,7 +30,6 @@ import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; import javax.annotation.Nullable; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; @@ -171,8 +170,8 @@ public class SuggestionsAction implements ComponentsWsAction { if (!recentlyBrowsedKeys.isEmpty()) { componentDtos.addAll(dbClient.componentDao().selectByKeys(dbSession, recentlyBrowsedKeys)); } - ListMultimap componentsPerQualifier = componentDtos.stream() - .filter(c -> userSession.hasComponentPermission(USER, c)) + List authorizedComponents = userSession.keepAuthorizedComponents(USER, componentDtos); + ListMultimap componentsPerQualifier = authorizedComponents.stream() .collect(MoreCollectors.index(ComponentDto::qualifier)); if (componentsPerQualifier.isEmpty()) { return newBuilder().build(); @@ -195,7 +194,7 @@ public class SuggestionsAction implements ComponentsWsAction { int totalHits = componentsPerQualifier.size(); return new ComponentHitsPerQualifier(q, hits, totalHits); })).build(); - return buildResponse(recentlyBrowsedKeys, favoriteUuids, componentsPerQualifiers, dbSession, componentDtos.stream(), skip + limit).build(); + return buildResponse(recentlyBrowsedKeys, favoriteUuids, componentsPerQualifiers, dbSession, authorizedComponents, skip + limit).build(); } } @@ -219,9 +218,9 @@ public class SuggestionsAction implements ComponentsWsAction { .flatMap(Collection::stream) .map(ComponentHit::getUuid) .collect(toSet()); - Stream componentDtoStream = dbClient.componentDao().selectByUuids(dbSession, componentUuids).stream(); + List componentDtos = dbClient.componentDao().selectByUuids(dbSession, componentUuids); Set favoriteUuids = favorites.stream().map(ComponentDto::uuid).collect(MoreCollectors.toSet(favorites.size())); - SuggestionsWsResponse.Builder searchWsResponse = buildResponse(recentlyBrowsedKeys, favoriteUuids, componentsPerQualifiers, dbSession, componentDtoStream, skip + limit); + SuggestionsWsResponse.Builder searchWsResponse = buildResponse(recentlyBrowsedKeys, favoriteUuids, componentsPerQualifiers, dbSession, componentDtos, skip + limit); getWarning(query).ifPresent(searchWsResponse::setWarning); return searchWsResponse.build(); } @@ -243,8 +242,9 @@ public class SuggestionsAction implements ComponentsWsAction { } private SuggestionsWsResponse.Builder buildResponse(Set recentlyBrowsedKeys, Set favoriteUuids, ComponentIndexResults componentsPerQualifiers, - DbSession dbSession, Stream stream, int coveredItems) { - Map componentsByUuids = stream + DbSession dbSession, List componentDtos, int coveredItems) { + + Map componentsByUuids = componentDtos.stream() .collect(MoreCollectors.uniqueIndex(ComponentDto::uuid)); Map organizationsByUuids = loadOrganizations(dbSession, componentsByUuids.values()); Map projectsByUuids = loadProjects(dbSession, componentsByUuids.values());