diff options
Diffstat (limited to 'server/sonar-db-dao/src/main')
9 files changed, 53 insertions, 156 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index 84223ad785c..bdbccd5491e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -91,7 +91,6 @@ import org.sonar.db.issue.NewCodeReferenceIssueDto; import org.sonar.db.issue.PrIssueDto; import org.sonar.db.measure.LargestBranchNclocDto; import org.sonar.db.measure.MeasureMapper; -import org.sonar.db.measure.ProjectLocDistributionDto; import org.sonar.db.measure.ProjectMeasureDto; import org.sonar.db.measure.ProjectMeasureMapper; import org.sonar.db.metric.MetricMapper; @@ -256,7 +255,6 @@ public class MyBatis { confBuilder.loadAlias("AnalysisPropertyValuePerProject", AnalysisPropertyValuePerProject.class); confBuilder.loadAlias("ProjectAlmKeyAndProject", ProjectAlmKeyAndProject.class); confBuilder.loadAlias("PrAndBranchCountByProjectDto", PrBranchAnalyzedLanguageCountByProjectDto.class); - confBuilder.loadAlias("ProjectLocDistribution", ProjectLocDistributionDto.class); confBuilder.loadAlias("PurgeableAnalysis", PurgeableAnalysisDto.class); confBuilder.loadAlias("PushEvent", PushEventDto.class); confBuilder.loadAlias("QualityGateCondition", QualityGateConditionDto.class); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java index 0e7eb4bbb3c..dd06c36bebf 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java @@ -206,4 +206,12 @@ public class BranchDao implements Dao { long yesterday = ZonedDateTime.now(ZoneId.systemDefault()).minusDays(1).toInstant().toEpochMilli(); return mapper(dbSession).selectBranchMeasuresWithCaycMetric(yesterday); } + + public List<BranchDto> selectMainBranches(DbSession dbSession) { + return mapper(dbSession).selectMainBranches(); + } + + public List<BranchDto> selectMainBranchesAssociatedToDefaultQualityProfile(DbSession dbSession) { + return mapper(dbSession).selectMainBranchesAssociatedToDefaultQualityProfile(); + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java index 864e2b61642..ae4dde26dae 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java @@ -77,4 +77,8 @@ public interface BranchMapper { List<BranchDto> selectMainBranchesByProjectUuids(@Param("projectUuids") Collection<String> projectUuids); List<BranchMeasuresDto> selectBranchMeasuresWithCaycMetric(long yesterday); + + List<BranchDto> selectMainBranches(); + + List<BranchDto> selectMainBranchesAssociatedToDefaultQualityProfile(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureDao.java index ab619fcb46f..7777008ae45 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureDao.java @@ -19,6 +19,7 @@ */ package org.sonar.db.measure; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -32,7 +33,7 @@ import org.sonar.db.component.ComponentDto; import static java.util.Collections.singletonList; import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; -import static org.sonar.db.DatabaseUtils.executeLargeInputs; +import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; public class MeasureDao implements Dao { @@ -91,35 +92,33 @@ public class MeasureDao implements Dao { return Optional.empty(); } + /** + * Get the measures of the specified components and metrics. + * This method will scroll through the results and remove the non-requested metrics, to avoid loading all the measures in memory. + */ public List<MeasureDto> selectByComponentUuidsAndMetricKeys(DbSession dbSession, Collection<String> largeComponentUuids, Collection<String> metricKeys) { if (largeComponentUuids.isEmpty() || metricKeys.isEmpty()) { return Collections.emptyList(); } - return executeLargeInputs( - largeComponentUuids, - componentUuids -> mapper(dbSession).selectByComponentUuids(componentUuids)).stream() - .map(measureDto -> { + List<MeasureDto> result = new ArrayList<>(); + executeLargeInputsWithoutOutput(largeComponentUuids, + componentUuids -> mapper(dbSession).scrollSelectByComponentUuids(componentUuids, resultContext -> { + MeasureDto measureDto = resultContext.getResultObject(); measureDto.getMetricValues().entrySet().removeIf(entry -> !metricKeys.contains(entry.getKey())); - return measureDto; - }) - .filter(measureDto -> !measureDto.getMetricValues().isEmpty()) - .toList(); - } + if (!measureDto.getMetricValues().isEmpty()) { + result.add(measureDto); + } + })); - public void scrollSelectByComponentUuid(DbSession dbSession, String componentUuid, ResultHandler<MeasureDto> handler) { - mapper(dbSession).scrollSelectByComponentUuid(componentUuid, handler); + return result; } public Set<MeasureHash> selectMeasureHashesForBranch(DbSession dbSession, String branchUuid) { return mapper(dbSession).selectMeasureHashesForBranch(branchUuid); } - public List<MeasureDto> selectBranchMeasuresForProject(DbSession dbSession, String projectUuid) { - return mapper(dbSession).selectBranchMeasuresForProject(projectUuid); - } - public void selectTreeByQuery(DbSession dbSession, ComponentDto baseComponent, MeasureTreeQuery query, ResultHandler<MeasureDto> resultHandler) { if (query.returnsEmpty()) { @@ -128,20 +127,8 @@ public class MeasureDao implements Dao { mapper(dbSession).selectTreeByQuery(query, baseComponent.uuid(), query.getUuidPath(baseComponent), resultHandler); } - public List<ProjectMainBranchMeasureDto> selectAllForProjectMainBranches(DbSession dbSession) { - return mapper(dbSession).selectAllForProjectMainBranches(); - } - - public List<ProjectMainBranchMeasureDto> selectAllForProjectMainBranchesAssociatedToDefaultQualityProfile(DbSession dbSession) { - return mapper(dbSession).selectAllForProjectMainBranchesAssociatedToDefaultQualityProfile(); - } - - public List<MeasureDto> selectAllForMainBranches(DbSession dbSession) { - return mapper(dbSession).selectAllForMainBranches(); - } - - public long findNclocOfBiggestBranchForProject(DbSession dbSession, String projectUuid) { - List<MeasureDto> branchMeasures = mapper(dbSession).selectBranchMeasuresForProject(projectUuid); + public long findNclocOfBiggestBranch(DbSession dbSession, Collection<String> branchUuids) { + List<MeasureDto> branchMeasures = selectByComponentUuidsAndMetricKeys(dbSession, branchUuids, List.of(NCLOC_KEY)); long maxncloc = 0; for (MeasureDto measure : branchMeasures) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureMapper.java index b6baf817e2a..3e7c19441f1 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureMapper.java @@ -37,21 +37,13 @@ public interface MeasureMapper { List<MeasureDto> selectByComponentUuids(@Param("componentUuids") Collection<String> componentUuids); - void scrollSelectByComponentUuid(@Param("componentUuid") String componentUuid, ResultHandler<MeasureDto> handler); + void scrollSelectByComponentUuids(@Param("componentUuids") Collection<String> componentUuids, ResultHandler<MeasureDto> handler); Set<MeasureHash> selectMeasureHashesForBranch(@Param("branchUuid") String branchUuid); - List<MeasureDto> selectBranchMeasuresForProject(@Param("projectUuid") String projectUuid); - void selectTreeByQuery( @Param("query") MeasureTreeQuery measureQuery, @Param("baseUuid") String baseUuid, @Param("baseUuidPath") String baseUuidPath, ResultHandler<MeasureDto> resultHandler); - - List<ProjectMainBranchMeasureDto> selectAllForProjectMainBranches(); - - List<ProjectMainBranchMeasureDto> selectAllForProjectMainBranchesAssociatedToDefaultQualityProfile(); - - List<MeasureDto> selectAllForMainBranches(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectLocDistributionDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectLocDistributionDto.java deleted file mode 100644 index 0f091369a00..00000000000 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectLocDistributionDto.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db.measure; - -/** - * Loc distribution per language for the largest branch in a project. - */ -public record ProjectLocDistributionDto(String projectUuid, String branchUuid, String locDistribution) { - -} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMainBranchMeasureDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMainBranchMeasureDto.java deleted file mode 100644 index 22aa5db4d6d..00000000000 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMainBranchMeasureDto.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2024 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db.measure; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import java.util.Map; -import java.util.TreeMap; - -public class ProjectMainBranchMeasureDto { - - private static final Gson GSON = new Gson(); - - private String projectUuid; - private Map<String, Object> metricValues = new TreeMap<>(); - - public ProjectMainBranchMeasureDto() { - // empty constructor - } - - public String getProjectUuid() { - return projectUuid; - } - - public Map<String, Object> getMetricValues() { - return metricValues; - } - - // used by MyBatis mapper - public void setJsonValue(String jsonValue) { - metricValues = GSON.fromJson(jsonValue, new TypeToken<TreeMap<String, Object>>() { - }.getType()); - } -} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml index b2b3993d2bf..f96a116d7c1 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml @@ -304,4 +304,22 @@ from dual </select> + <select id="selectMainBranches" resultType="org.sonar.db.component.BranchDto"> + select <include refid="columns"/> + from project_branches pb + inner join projects p on p.uuid = pb.project_uuid + where + p.qualifier = 'TRK' + and pb.is_main = ${_true} + </select> + + <select id="selectMainBranchesAssociatedToDefaultQualityProfile" resultType="org.sonar.db.component.BranchDto"> + select <include refid="columns"/> + from project_branches pb + inner join projects p on p.uuid = pb.project_uuid + where + pb.is_main = ${_true} + and p.uuid not in (select project_uuid from project_qprofiles) + </select> + </mapper> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/MeasureMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/MeasureMapper.xml index 118ef27e034..d014a3c4a67 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/MeasureMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/MeasureMapper.xml @@ -47,13 +47,16 @@ </foreach> </select> - <select id="scrollSelectByComponentUuid" resultType="org.sonar.db.measure.MeasureDto" fetchSize="${_scrollFetchSize}" + <select id="scrollSelectByComponentUuids" parameterType="map" resultType="org.sonar.db.measure.MeasureDto" fetchSize="10" resultSetType="FORWARD_ONLY"> select <include refid="columns"/> from measures m where - m.component_uuid = #{componentUuid, jdbcType=VARCHAR} + m.component_uuid in + <foreach item="componentUuid" collection="componentUuids" open="(" separator="," close=")"> + #{componentUuid, jdbcType=VARCHAR} + </foreach> </select> <select id="selectMeasureHashesForBranch" resultType="org.sonar.db.measure.MeasureHash"> @@ -62,15 +65,6 @@ where branch_uuid = #{branchUuid, jdbcType=VARCHAR} </select> - <select id="selectBranchMeasuresForProject" parameterType="map" resultType="org.sonar.db.measure.MeasureDto"> - select - <include refid="columns"/> - from measures m - inner join project_branches b on b.uuid = m.component_uuid - inner join projects p on p.uuid = b.project_uuid and p.qualifier = 'TRK' - where b.project_uuid = #{projectUuid,jdbcType=VARCHAR} - </select> - <select id="selectTreeByQuery" parameterType="map" resultType="org.sonar.db.measure.MeasureDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY"> select <include refid="columns"/> from measures m @@ -116,30 +110,4 @@ </where> </select> - <select id="selectAllForProjectMainBranches" resultType="org.sonar.db.measure.ProjectMainBranchMeasureDto"> - select p.uuid as projectUuid, m.json_value as jsonValue - from measures m - inner join project_branches pb on pb.uuid = m.component_uuid - inner join projects p on p.uuid = pb.project_uuid - where - p.qualifier = 'TRK' - and pb.is_main = ${_true} - </select> - - <select id="selectAllForProjectMainBranchesAssociatedToDefaultQualityProfile" resultType="org.sonar.db.measure.ProjectMainBranchMeasureDto"> - select p.uuid as projectUuid, m.json_value as jsonValue - from measures m - inner join project_branches pb on pb.uuid = m.component_uuid - inner join projects p on p.uuid = pb.project_uuid - where - pb.is_main = ${_true} - and p.uuid not in (select project_uuid from project_qprofiles) - </select> - - <select id="selectAllForMainBranches" resultType="org.sonar.db.measure.MeasureDto"> - select <include refid="columns"/> from measures m - inner join project_branches pb on pb.uuid = m.component_uuid and pb.is_main = ${_true} - inner join projects p on p.uuid = pb.project_uuid and p.qualifier = 'TRK' - </select> - </mapper> |