From d2391429829abedd6de5a21856b3131aa7a26f1a Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 12 May 2016 18:07:54 +0200 Subject: [PATCH] SONAR-7400 Do not load active rules from index in rules WS --- .../server/rule/ws/ActiveRuleCompleter.java | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java index 88608db09c8..e8c26af0b45 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java @@ -24,6 +24,7 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.collect.Multimap; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -53,6 +54,7 @@ import org.sonarqube.ws.Rules.SearchResponse; import org.sonarqube.ws.Rules.ShowResponse; import static com.google.common.base.Strings.nullToEmpty; +import static com.google.common.collect.FluentIterable.from; import static com.google.common.collect.Sets.newHashSet; import static java.util.Collections.singletonList; @@ -88,10 +90,11 @@ public class ActiveRuleCompleter { if (profileKey != null) { // Load details of active rules on the selected profile List activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profileKey); + Map activeRuleByRuleKey = from(activeRuleDtos).uniqueIndex(ActiveRuleToRuleKey.INSTANCE); ListMultimap activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos); for (RuleDto rule : rules) { - ActiveRule activeRule = loader.getActiveRule(ActiveRuleKey.of(profileKey, rule.getKey())); + ActiveRuleDto activeRule = activeRuleByRuleKey.get(rule.getKey()); if (activeRule != null) { qProfileKeys = writeActiveRules(rule.getKey(), singletonList(activeRule), activeRuleParamsByActiveRuleKey, activesBuilder); } @@ -99,11 +102,10 @@ public class ActiveRuleCompleter { } else { // Load details of all active rules List activeRuleDtos = dbClient.activeRuleDao().selectByRuleIds(dbSession, Lists.transform(rules, RuleDtoFunctions.toId())); + Multimap activeRulesByRuleKey = from(activeRuleDtos).index(ActiveRuleToRuleKey.INSTANCE); ListMultimap activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos); - for (RuleDto rule : rules) { - List activeRules = loader.findActiveRulesByRule(rule.getKey()); - qProfileKeys = writeActiveRules(rule.getKey(), activeRules, activeRuleParamsByActiveRuleKey, activesBuilder); + qProfileKeys = writeActiveRules(rule.getKey(), activeRulesByRuleKey.get(rule.getKey()), activeRuleParamsByActiveRuleKey, activesBuilder); } } @@ -111,13 +113,13 @@ public class ActiveRuleCompleter { return qProfileKeys; } - private static Collection writeActiveRules(RuleKey ruleKey, Collection activeRules, + private static Collection writeActiveRules(RuleKey ruleKey, Collection activeRules, ListMultimap activeRuleParamsByActiveRuleKey, Rules.Actives.Builder activesBuilder) { Collection qProfileKeys = newHashSet(); Rules.ActiveList.Builder activeRulesListResponse = Rules.ActiveList.newBuilder(); - for (ActiveRule activeRule : activeRules) { - activeRulesListResponse.addActiveList(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.key()))); - qProfileKeys.add(activeRule.key().qProfile()); + for (ActiveRuleDto activeRule : activeRules) { + activeRulesListResponse.addActiveList(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.getKey()))); + qProfileKeys.add(activeRule.getKey().qProfile()); } activesBuilder .getMutableActives() @@ -141,30 +143,30 @@ public class ActiveRuleCompleter { } void completeShow(DbSession dbSession, RuleDto rule, ShowResponse.Builder response) { - List activeRules = loader.findActiveRulesByRule(rule.getKey()); - List activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE)); + List activeRuleDtos = dbClient.activeRuleDao().selectByRule(dbSession, rule); Map activeRuleIdsByKey = new HashMap<>(); for (ActiveRuleDto activeRuleDto : activeRuleDtos) { activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey()); } List activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDtoToId.INSTANCE)); - ListMultimap activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRules.size(), 10); + ListMultimap activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRuleDtos.size(), 10); for (ActiveRuleParamDto activeRuleParamDto : activeRuleParamDtos) { ActiveRuleKey activeRuleKey = activeRuleIdsByKey.get(activeRuleParamDto.getActiveRuleId()); activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParamDto); } - for (ActiveRule activeRule : activeRules) { - response.addActives(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.key()))); + for (ActiveRuleDto activeRule : activeRuleDtos) { + response.addActives(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.getKey()))); } } - private static Rules.Active buildActiveRuleResponse(ActiveRule activeRule, List parameters) { + private static Rules.Active buildActiveRuleResponse(ActiveRuleDto activeRule, List parameters) { Rules.Active.Builder activeRuleResponse = Rules.Active.newBuilder(); - activeRuleResponse.setQProfile(activeRule.key().qProfile()); - activeRuleResponse.setInherit(activeRule.inheritance().toString()); - activeRuleResponse.setSeverity(activeRule.severity()); + activeRuleResponse.setQProfile(activeRule.getKey().qProfile()); + String inheritance = activeRule.getInheritance(); + activeRuleResponse.setInherit(inheritance != null ? inheritance : ActiveRule.Inheritance.NONE.name()); + activeRuleResponse.setSeverity(activeRule.getSeverityString()); Rules.Active.Param.Builder paramBuilder = Rules.Active.Param.newBuilder(); for (ActiveRuleParamDto parameter : parameters) { activeRuleResponse.addParams(paramBuilder.clear() @@ -224,13 +226,12 @@ public class ActiveRuleCompleter { profilesResponse.put(profile.getKey(), profileResponse.build()); } - private enum ActiveRuleToKey implements Function { + private enum ActiveRuleToRuleKey implements Function { INSTANCE; @Override - public ActiveRuleKey apply(@Nonnull ActiveRule input) { - return input.key(); + public RuleKey apply(@Nonnull ActiveRuleDto input) { + return input.getKey().ruleKey(); } } - } -- 2.39.5