From d9af1cfbdcf81792104d8dc7b30fe8e039ffea7e Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Mon, 29 Jan 2018 10:08:37 +0100 Subject: [PATCH] SONAR-10313 Optimize number of SQL requests on SearchAction --- .../sonar/server/issue/ws/SearchAction.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java index a0a9b33a0ab..d70760a5cbb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java @@ -25,6 +25,7 @@ import com.google.common.collect.Lists; import java.util.Arrays; import java.util.Collection; import java.util.EnumSet; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -63,6 +64,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Iterables.concat; import static com.google.common.collect.Sets.newHashSet; import static java.lang.String.format; +import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static org.sonar.api.utils.Paging.forPageIndex; import static org.sonar.server.es.SearchOptions.MAX_LIMIT; @@ -369,7 +371,7 @@ public class SearchAction implements IssuesWsAction { // Must be done after loading of data as the "hidden" facet "debt" // can be used to get total debt. facets = reorderFacets(facets, options.getFacets()); - replaceRuleIdsByRuleKeys(facets); + replaceRuleIdsByRuleKeys(facets, data.getRules() == null ? emptyList() : data.getRules()); // FIXME allow long in Paging Paging paging = forPageIndex(options.getPage()).withPageSize(options.getLimit()).andTotal((int) result.getHits().getTotalHits()); @@ -377,18 +379,29 @@ public class SearchAction implements IssuesWsAction { return searchResponseFormat.formatSearch(additionalFields, data, paging, facets); } - private void replaceRuleIdsByRuleKeys(@Nullable Facets facets) { + private void replaceRuleIdsByRuleKeys(@Nullable Facets facets, List alreadyLoadedRules) { if (facets == null || facets.get(PARAM_RULES) == null) { return; } + // The facet for PARAM_RULES contains the id of the rule as the key // We need to update the key to be a RuleKey LinkedHashMap rulesFacet = facets.get(PARAM_RULES); try (DbSession dbSession = dbClient.openSession(false)) { - Map idToRuleKey = dbClient.ruleDao() - .selectDefinitionByIds(dbSession, Collections2.transform(rulesFacet.keySet(), Integer::parseInt)) - .stream() + Set ruleKeysToLoad = new HashSet<>(); + ruleKeysToLoad.addAll(rulesFacet.keySet()); + ruleKeysToLoad.removeAll( + alreadyLoadedRules + .stream() + .map(r -> r.getKey().toString()) + .collect(Collectors.toList())); + + Set requiredRules = new HashSet<>(); + requiredRules.addAll(alreadyLoadedRules); + requiredRules.addAll(dbClient.ruleDao().selectDefinitionByIds(dbSession, Collections2.transform(ruleKeysToLoad, Integer::parseInt))); + + Map idToRuleKey = requiredRules.stream() .collect(Collectors.toMap(RuleDefinitionDto::getId, RuleDefinitionDto::getKey)); LinkedHashMap newRulesFacet = new LinkedHashMap<>(); -- 2.39.5