From: Léo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com> Date: Wed, 27 Apr 2022 14:16:24 +0000 (+0200) Subject: SONAR-16302 Replace usage of rule for indexing dto (#5853) X-Git-Tag: 9.5.0.56709~168 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=838c48d8e94d0d780582cdfb4429e5cb57615f6f;p=sonarqube.git SONAR-16302 Replace usage of rule for indexing dto (#5853) --- diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java index 95e81b68c4a..0837c3e2821 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java @@ -20,10 +20,15 @@ package org.sonar.db.rule; import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleQuery; import org.sonar.core.util.UuidFactory; @@ -33,6 +38,7 @@ import org.sonar.db.RowNotFoundException; import static com.google.common.base.Preconditions.checkNotNull; import static java.util.Collections.emptyList; +import static java.util.stream.Collectors.toMap; import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; import static org.sonar.db.DatabaseUtils.executeLargeUpdates; @@ -175,20 +181,47 @@ public class RuleDao implements Dao { .forEach(consumer)); } - public void scrollIndexingRulesByKeys(DbSession dbSession, Collection ruleUuids, Consumer consumer) { + public void selectIndexingRulesByKeys(DbSession dbSession, Collection ruleUuids, Consumer consumer) { RuleMapper mapper = mapper(dbSession); executeLargeInputsWithoutOutput(ruleUuids, - pageOfRuleUuids -> mapper - .selectIndexingRulesByUuids(pageOfRuleUuids) - .forEach(consumer)); + pageOfRuleUuids -> { + List ruleDtos = mapper.selectByUuids(pageOfRuleUuids); + processRuleDtos(ruleDtos, consumer, mapper); + }); + } + + public void selectIndexingRules(DbSession dbSession, Consumer consumer) { + RuleMapper mapper = mapper(dbSession); + executeLargeInputsWithoutOutput(mapper.selectAll(), + ruleDtos -> processRuleDtos(ruleDtos, consumer, mapper)); } - public void scrollIndexingRules(DbSession dbSession, Consumer consumer) { - mapper(dbSession).scrollIndexingRules(context -> { - RuleForIndexingDto dto = context.getResultObject(); - consumer.accept(dto); - }); + private static RuleForIndexingDto toRuleForIndexingDto(RuleDto r, Map templateDtos) { + RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(r); + if (templateDtos.containsKey(r.getTemplateUuid())) { + ruleForIndexingDto.setTemplateRuleKey(templateDtos.get(r.getTemplateUuid()).getRuleKey()); + ruleForIndexingDto.setTemplateRepository(templateDtos.get(r.getTemplateUuid()).getRepositoryKey()); + } + return ruleForIndexingDto; + } + + private static void processRuleDtos(List ruleDtos, Consumer consumer, RuleMapper mapper) { + List templateRuleUuids = ruleDtos.stream() + .map(RuleDto::getTemplateUuid) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + Map templateDtos = findTemplateDtos(mapper, templateRuleUuids); + ruleDtos.stream().map(r -> toRuleForIndexingDto(r, templateDtos)).forEach(consumer); + } + + private static Map findTemplateDtos(RuleMapper mapper, List templateRuleUuids) { + if (!templateRuleUuids.isEmpty()) { + return mapper.selectByUuids(templateRuleUuids).stream().collect(toMap(RuleDto::getUuid, Function.identity())); + }else{ + return Collections.emptyMap(); + } } private static RuleMapper mapper(DbSession session) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java index 58b88632d1a..cdc6b8d7844 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java @@ -19,8 +19,10 @@ */ package org.sonar.db.rule; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.Sets; +import java.util.Collections; import java.util.HashSet; -import java.util.List; import java.util.Optional; import java.util.Set; import javax.annotation.CheckForNull; @@ -40,9 +42,9 @@ public class RuleForIndexingDto { private Integer severity; private RuleStatus status; private boolean isTemplate; - private String systemTags; - private String tags; - private String securityStandards; + private Set systemTags; + private Set tags; + private Set securityStandards; private String templateRuleKey; private String templateRepository; private String internalKey; @@ -54,10 +56,37 @@ public class RuleForIndexingDto { private Set ruleDescriptionSectionsDtos = new HashSet<>(); + @VisibleForTesting public RuleForIndexingDto() { // nothing to do here } + public static RuleForIndexingDto fromRuleDto(RuleDto r) { + RuleForIndexingDto ruleForIndexingDto = new RuleForIndexingDto(); + ruleForIndexingDto.createdAt = r.getCreatedAt(); + ruleForIndexingDto.uuid = r.getUuid(); + ruleForIndexingDto.repository = r.getRepositoryKey(); + ruleForIndexingDto.pluginRuleKey = r.getRuleKey(); + ruleForIndexingDto.name = r.getName(); + ruleForIndexingDto.descriptionFormat = r.getDescriptionFormat(); + ruleForIndexingDto.severity = r.getSeverity(); + ruleForIndexingDto.status = r.getStatus(); + ruleForIndexingDto.isTemplate = r.isTemplate(); + ruleForIndexingDto.systemTags = Sets.newHashSet(r.getSystemTags()); + ruleForIndexingDto.tags = r.getMetadata() != null ? Sets.newHashSet(r.getMetadata().getTags()) : Collections.emptySet(); + ruleForIndexingDto.securityStandards = Sets.newHashSet(r.getSecurityStandards()); + ruleForIndexingDto.internalKey = r.getConfigKey(); + ruleForIndexingDto.language = r.getLanguage(); + ruleForIndexingDto.isExternal = r.isExternal(); + ruleForIndexingDto.type = r.getType(); + ruleForIndexingDto.createdAt = r.getCreatedAt(); + ruleForIndexingDto.updatedAt = r.getUpdatedAt(); + if (r.getRuleDescriptionSectionDtos() != null) { + ruleForIndexingDto.setRuleDescriptionSectionsDtos(Sets.newHashSet(r.getRuleDescriptionSectionDtos())); + } + return ruleForIndexingDto; + } + public String getUuid() { return uuid; } @@ -103,15 +132,15 @@ public class RuleForIndexingDto { } public Set getSystemTags() { - return RuleDefinitionDto.deserializeTagsString(systemTags); + return Collections.unmodifiableSet(systemTags); } public Set getTags() { - return RuleDefinitionDto.deserializeTagsString(tags); + return Collections.unmodifiableSet(tags); } public Set getSecurityStandards() { - return RuleDefinitionDto.deserializeSecurityStandardsString(securityStandards); + return Collections.unmodifiableSet(securityStandards); } public String getTemplateRuleKey() { @@ -160,7 +189,7 @@ public class RuleForIndexingDto { } public Set getRuleDescriptionSectionsDtos() { - return ruleDescriptionSectionsDtos; + return Collections.unmodifiableSet(ruleDescriptionSectionsDtos); } public void setRuleDescriptionSectionsDtos(Set ruleDescriptionSectionsDtos) { @@ -175,4 +204,12 @@ public class RuleForIndexingDto { public RuleDescriptionSectionDto getDefaultRuleDescriptionSectionDto() { return findExistingSectionWithSameKey(DEFAULT_KEY).orElse(null); } + + public void setTemplateRuleKey(String templateRuleKey) { + this.templateRuleKey = templateRuleKey; + } + + public void setTemplateRepository(String templateRepository) { + this.templateRepository = templateRepository; + } } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml index bef6fde79b5..bb07883486c 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml @@ -165,33 +165,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - select - r.uuid as "uuid", - r.plugin_name as "repository", - r.plugin_rule_key as "pluginRuleKey", - r.name as "name", - r.description_format as "descriptionFormat", - r.priority as "severity", - r.status as "status", - r.is_template as "isTemplate", - r.is_external as "isExternal", - r.system_tags as "systemTags", - r.security_standards as "securityStandards", - t.plugin_rule_key as "templateRuleKey", - t.plugin_name as "templateRepository", - r.plugin_config_key as "internalKey", - r.language as "language", - r.rule_type as "type", - r.created_at as "createdAt", - r.updated_at as "updatedAt", - rm.tags as "tags", - rds.uuid as "rds_uuid", - rds.kee as "rds_kee", - rds.description as "rds_description" - from rules r - left outer join rules t on t.uuid = r.template_uuid - left outer join rules_metadata rm on r.uuid = rm.rule_uuid - left outer join rule_desc_sections rds on - rds.rule_uuid = r.uuid - -