From 23b47d8d8f585c642109988a5b07c8cf79678a26 Mon Sep 17 00:00:00 2001 From: Eric Giffon Date: Mon, 27 Jan 2025 13:33:25 +0100 Subject: SONAR-24216 Optimize querying of measures on startup indexing --- .../db/measure/ProjectMeasuresIndexerIterator.java | 102 +++++---------------- 1 file changed, 22 insertions(+), 80 deletions(-) (limited to 'server/sonar-db-dao/src/main/java/org/sonar/db') diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java index 5d65669ac66..2ce635d455a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java @@ -37,11 +37,13 @@ import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.measures.CoreMetrics; -import org.sonar.db.component.ComponentQualifiers; import org.sonar.core.metric.SoftwareQualitiesMetrics; import org.sonar.core.util.CloseableIterator; import org.sonar.db.DatabaseUtils; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; +import org.sonar.db.component.ComponentQualifiers; import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; @@ -104,36 +106,27 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator projects; private final List metrics; - private ProjectMeasuresIndexerIterator(DbSession dbSession, PreparedStatement measuresStatement, List projects, List metrics) { + private ProjectMeasuresIndexerIterator(DbSession dbSession, DbClient dbClient, PreparedStatement measuresStatement, + List projects, List metrics) { this.dbSession = dbSession; + this.dbClient = dbClient; this.measuresStatement = measuresStatement; this.projects = projects.iterator(); this.metrics = metrics; } - public static ProjectMeasuresIndexerIterator create(DbSession session, @Nullable String projectUuid) { + public static ProjectMeasuresIndexerIterator create(DbSession session, DbClient dbClient, @Nullable String projectUuid) { List projects = selectProjects(session, projectUuid); - PreparedStatement projectsStatement = createMeasuresStatement(session); - return new ProjectMeasuresIndexerIterator(session, projectsStatement, projects, selectMetrics(session)); + PreparedStatement measuresStatement = createMeasuresStatement(session); + return new ProjectMeasuresIndexerIterator(session, dbClient, measuresStatement, projects, selectMetrics(session)); } private static List selectMetrics(DbSession session) { @@ -234,14 +227,7 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator biggestBranch = project.getNcloc() - .flatMap(ncloc -> selectProjectBranchForNcloc(dbSession, projectUuid, ncloc)); - - if (biggestBranch.isPresent()) { - readNclocDistributionFromBiggestBranch(biggestBranch.get(), measures); - } - + project.getNcloc().ifPresent(ncloc -> readNclocDistributionFromBiggestBranch(projectUuid, ncloc, measures)); return measures; } catch (Exception e) { throw new IllegalStateException(String.format("Fail to execute request to select measures of project %s", projectUuid), e); @@ -285,62 +271,18 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator selectProjectBranchForNcloc(DbSession session, String projectUuid, long ncloc) { - try (PreparedStatement nclocStatement = session.getConnection().prepareStatement(SQL_BRANCH_BY_NCLOC)) { - nclocStatement.setString(1, projectUuid); - - try (ResultSet rs = nclocStatement.executeQuery()) { - return readBranchMeasures(rs, ncloc); - } - } catch (SQLException e) { - throw new IllegalStateException("Fail to execute request to select the project biggest branch", e); - } - } - - private static Optional readBranchMeasures(ResultSet rs, long ncloc) throws SQLException { - while (rs.next()) { - String jsonValue = rs.getString(JSON_VALUE_FIELD); - Map metricValues = GSON.fromJson(jsonValue, new TypeToken>() { - }.getType()); - - if (metricValues.containsKey(NCLOC_KEY)) { - Object nclocValue = metricValues.get(NCLOC_KEY); - if (nclocValue instanceof Double branchNcloc && branchNcloc == ncloc) { - return Optional.of(rs.getString(1)); + private void readNclocDistributionFromBiggestBranch(String projectUuid, long ncloc, Measures measures) { + List branchUuids = dbClient.branchDao().selectByProjectUuid(dbSession, projectUuid).stream().map(BranchDto::getUuid).toList(); + List measureDtos = dbClient.measureDao().selectByComponentUuidsAndMetricKeys(dbSession, branchUuids, + List.of(NCLOC_KEY, NCLOC_LANGUAGE_DISTRIBUTION_KEY)); + for (MeasureDto measureDto : measureDtos) { + Long branchNcloc = measureDto.getLong(NCLOC_KEY); + if (branchNcloc != null && branchNcloc == ncloc) { + String nclocLanguageDistribution = measureDto.getString(NCLOC_LANGUAGE_DISTRIBUTION_KEY); + if (nclocLanguageDistribution != null) { + measures.setNclocByLanguages(nclocLanguageDistribution); } - } - } - return Optional.empty(); - } - - private static void readNclocDistributionKey(ResultSet rs, Measures measures) throws SQLException { - String jsonValue = rs.getString(3); - Map metricValues = GSON.fromJson(jsonValue, new TypeToken>() { - }.getType()); - - if (metricValues.containsKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY)) { - Object distribution = metricValues.get(NCLOC_LANGUAGE_DISTRIBUTION_KEY); - if (distribution instanceof String stringDistribution) { - measures.setNclocByLanguages(stringDistribution); + break; } } } -- cgit v1.2.3