diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2021-07-27 16:10:33 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-08-04 20:08:14 +0000 |
commit | bc4d1696a5998adcadb03c64e21aff4a467a1ab1 (patch) | |
tree | 7dd07d23f17e805db73dd60a961b05d210fdf53c /server/sonar-server-common | |
parent | c63120be2e7c090ca42d120c756e703e7cc9eaa1 (diff) | |
download | sonarqube-bc4d1696a5998adcadb03c64e21aff4a467a1ab1.tar.gz sonarqube-bc4d1696a5998adcadb03c64e21aff4a467a1ab1.zip |
SONAR-15237 Improve startup performance of the web process
Diffstat (limited to 'server/sonar-server-common')
3 files changed, 47 insertions, 18 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java index 36e4b71fe67..3d025d16620 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java @@ -42,9 +42,6 @@ import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleParamDto; import org.sonar.markdown.Markdown; -import static com.google.common.collect.Lists.newArrayList; -import static java.util.Optional.empty; - /** * Will be removed in the future. */ @@ -61,12 +58,25 @@ public class DefaultRuleFinder implements ServerRuleFinder { @Override public Optional<RuleDefinitionDto> findDtoByKey(RuleKey key) { try (DbSession dbSession = dbClient.openSession(false)) { - Optional<RuleDefinitionDto> rule = ruleDao.selectDefinitionByKey(dbSession, key); - if (rule.isPresent() && rule.get().getStatus() != RuleStatus.REMOVED) { - return rule; - } else { - return empty(); - } + return ruleDao.selectDefinitionByKey(dbSession, key) + .filter(r -> r.getStatus() != RuleStatus.REMOVED); + } + } + + @Override + public Optional<RuleDefinitionDto> findDtoByUuid(String uuid) { + try (DbSession dbSession = dbClient.openSession(false)) { + return ruleDao.selectDefinitionByUuid(uuid, dbSession) + .filter(r -> r.getStatus() != RuleStatus.REMOVED); + } + } + + @Override + public Collection<RuleDefinitionDto> findAll() { + try (DbSession dbSession = dbClient.openSession(false)) { + List<RuleDefinitionDto> list = new ArrayList<>(); + ruleDao.selectEnabled(dbSession, r -> list.add(r.getResultObject())); + return list; } } @@ -150,7 +160,7 @@ public class DefaultRuleFinder implements ServerRuleFinder { } } - List<org.sonar.api.rules.RuleParam> apiParams = newArrayList(); + List<org.sonar.api.rules.RuleParam> apiParams = new ArrayList<>(); for (RuleParamDto param : params) { apiParams.add(new org.sonar.api.rules.RuleParam(apiRule, param.getName(), param.getDescription(), param.getType()) .setDefaultValue(param.getDefaultValue())); diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/ServerRuleFinder.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/ServerRuleFinder.java index 9f390ad0f46..06737ddfcca 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/ServerRuleFinder.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/ServerRuleFinder.java @@ -19,6 +19,7 @@ */ package org.sonar.server.rule; +import java.util.Collection; import java.util.Optional; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleFinder; @@ -26,4 +27,9 @@ import org.sonar.db.rule.RuleDefinitionDto; public interface ServerRuleFinder extends RuleFinder { Optional<RuleDefinitionDto> findDtoByKey(RuleKey key); + + Optional<RuleDefinitionDto> findDtoByUuid(String uuid); + + Collection<RuleDefinitionDto> findAll(); + } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java index f02e599db11..9943549f96a 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java @@ -22,6 +22,7 @@ package org.sonar.server.rule; import com.google.common.collect.ImmutableSet; import org.junit.Before; import org.junit.Test; +import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleQuery; @@ -41,10 +42,10 @@ public class DefaultRuleFinderTest { @org.junit.Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - private DbClient dbClient = dbTester.getDbClient(); - private DbSession session = dbTester.getSession(); + private final DbClient dbClient = dbTester.getDbClient(); + private final DbSession session = dbTester.getSession(); - private RuleDto rule1 = new RuleDto() + private final RuleDto rule1 = new RuleDto() .setName("Check Header") .setConfigKey("Checker/Treewalker/HeaderCheck") .setRuleKey("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck") @@ -53,7 +54,7 @@ public class DefaultRuleFinderTest { .setScope(Scope.MAIN) .setStatus(RuleStatus.READY); - private RuleDto rule2 = new RuleDto() + private final RuleDto rule2 = new RuleDto() .setName("Disabled checked") .setConfigKey("Checker/Treewalker/DisabledCheck") .setRuleKey("DisabledCheck") @@ -62,7 +63,7 @@ public class DefaultRuleFinderTest { .setScope(Scope.MAIN) .setStatus(RuleStatus.REMOVED); - private RuleDto rule3 = new RuleDto() + private final RuleDto rule3 = new RuleDto() .setName("Check Annotation") .setConfigKey("Checker/Treewalker/AnnotationUseStyleCheck") .setRuleKey("com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck") @@ -71,7 +72,7 @@ public class DefaultRuleFinderTest { .setScope(Scope.MAIN) .setStatus(RuleStatus.READY); - private RuleDto rule4 = new RuleDto() + private final RuleDto rule4 = new RuleDto() .setName("Call Super First") .setConfigKey("rulesets/android.xml/CallSuperFirst") .setRuleKey("CallSuperFirst") @@ -80,7 +81,7 @@ public class DefaultRuleFinderTest { .setScope(Scope.MAIN) .setStatus(RuleStatus.READY); - private DefaultRuleFinder underTest = new DefaultRuleFinder(dbClient); + private final DefaultRuleFinder underTest = new DefaultRuleFinder(dbClient); @Before public void setup() { @@ -108,19 +109,31 @@ public class DefaultRuleFinderTest { // find_all_enabled assertThat(underTest.findAll(RuleQuery.create())).extracting("ruleKey").containsOnly(rule1.getKey(), rule3.getKey(), rule4.getKey()); - assertThat(underTest.findAll(RuleQuery.create())).hasSize(3); + + // find_all + assertThat(underTest.findAll()).extracting("ruleKey").containsOnly(rule1.getKey().rule(), rule3.getKey().rule(), rule4.getKey().rule()); // do_not_find_disabled_rules assertThat(underTest.findByKey("checkstyle", "DisabledCheck")).isNull(); // do_not_find_unknown_rules assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"))).isEmpty(); + + assertThat(underTest.findDtoByKey(RuleKey.of("pmd", "CallSuperFirst")).get().getUuid()).isEqualTo(rule4.getUuid()); + assertThat(underTest.findDtoByUuid(rule4.getUuid())).isPresent(); + } + + @Test + public void should_fail_find() { + assertThat(underTest.findDtoByKey(RuleKey.of("pmd", "unknown"))).isEmpty(); + assertThat(underTest.findDtoByUuid("unknown")).isEmpty(); } @Test public void find_all_not_include_removed_rule() { // rule 3 is REMOVED assertThat(underTest.findAll(RuleQuery.create())).extracting("ruleKey").containsOnly(rule1.getKey(), rule3.getKey(), rule4.getKey()); + assertThat(underTest.findAll()).extracting("ruleKey").containsOnly(rule1.getKey().rule(), rule3.getKey().rule(), rule4.getKey().rule()); } @Test |