From 274ba091408eec64d7c26525b0ba77762145ce34 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 30 Aug 2013 16:26:55 +0200 Subject: [PATCH] SONAR-4518 Improve performance of rule removal during server startup --- .../server/configuration/ProfilesManager.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java index 2d3977ae4e7..2a040e860e3 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java @@ -19,6 +19,7 @@ */ package org.sonar.server.configuration; +import com.google.common.collect.Lists; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.database.DatabaseSession; @@ -30,7 +31,6 @@ import org.sonar.jpa.dao.RulesDao; import java.util.List; -import static com.google.common.collect.Lists.newArrayList; public class ProfilesManager extends BaseDao { @@ -95,26 +95,25 @@ public class ProfilesManager extends BaseDao { /** * Deactivate all active rules from profiles using a rule, then remove then. - * @param ruleId */ public void removeActivatedRules(Rule rule) { - List profiles = getSession().createQuery("FROM " + RulesProfile.class.getSimpleName()).getResultList(); - for (RulesProfile profile : profiles) { - List activeRulesToRemove = newArrayList(); - for (ActiveRule activeRule : profile.getActiveRules(true)) { - if (activeRule.getRule().equals(rule) && !activeRule.isInherited()) { - incrementProfileVersionIfNeeded(profile); - ruleDisabled(profile, activeRule, null); - for (RulesProfile child : getChildren(profile.getId())) { - deactivate(child, activeRule.getRule(), null); - } - activeRulesToRemove.add(activeRule); + List activeRules = getSession().createQuery("FROM " + ActiveRule.class.getSimpleName() + " WHERE rule=:rule").setParameter("rule", rule).getResultList(); + List activeRulesToRemove = Lists.newArrayList(); + + for (ActiveRule activeRule : activeRules) { + if (!activeRule.isInherited()) { + RulesProfile profile = activeRule.getRulesProfile(); + incrementProfileVersionIfNeeded(profile); + ruleDisabled(profile, activeRule, null); + for (RulesProfile child : getChildren(profile.getId())) { + deactivate(child, activeRule.getRule(), null); } + activeRulesToRemove.add(activeRule); } - for (ActiveRule activeRule : activeRulesToRemove) { - ActiveRule activeRuleToRemove = getSession().getSingleResult(ActiveRule.class, "id", activeRule.getId()); - removeActiveRule(profile, activeRuleToRemove); - } + } + for (ActiveRule activeRule : activeRulesToRemove) { + ActiveRule activeRuleToRemove = getSession().getSingleResult(ActiveRule.class, "id", activeRule.getId()); + removeActiveRule(activeRuleToRemove); } getSession().commit(); } @@ -202,7 +201,7 @@ public class ProfilesManager extends BaseDao { ActiveRule oldActiveRule = getSession().getEntity(ActiveRule.class, activeRuleId); if (oldActiveRule != null && oldActiveRule.doesOverride()) { ActiveRule parentActiveRule = getParentProfile(profile).getActiveRule(oldActiveRule.getRule()); - removeActiveRule(profile, oldActiveRule); + removeActiveRule(oldActiveRule); ActiveRule newActiveRule = (ActiveRule) parentActiveRule.clone(); newActiveRule.setRulesProfile(profile); newActiveRule.setInheritance(ActiveRule.INHERITED); @@ -319,7 +318,7 @@ public class ProfilesManager extends BaseDao { ActiveRule oldActiveRule = profile.getActiveRule(parentActiveRule.getRule()); if (oldActiveRule != null) { if (oldActiveRule.isInherited()) { - removeActiveRule(profile, oldActiveRule); + removeActiveRule(oldActiveRule); } else { oldActiveRule.setInheritance(ActiveRule.OVERRIDES); getSession().saveWithoutFlush(oldActiveRule); @@ -349,7 +348,7 @@ public class ProfilesManager extends BaseDao { if (activeRule != null) { if (activeRule.isInherited()) { ruleDisabled(profile, activeRule, userName); - removeActiveRule(profile, activeRule); + removeActiveRule(activeRule); } else { activeRule.setInheritance(null); getSession().saveWithoutFlush(activeRule); @@ -374,7 +373,8 @@ public class ProfilesManager extends BaseDao { "parentName", parent.getName()); } - private void removeActiveRule(RulesProfile profile, ActiveRule activeRule) { + private void removeActiveRule(ActiveRule activeRule) { + org.sonar.api.profiles.RulesProfile profile = activeRule.getRulesProfile(); profile.removeActiveRule(activeRule); getSession().removeWithoutFlush(activeRule); } -- 2.39.5