From 8115d8b7e41c38120650bac7432291c722b9a7c3 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 2 Oct 2017 10:28:58 +0200 Subject: [PATCH] SONAR-6959 Performance improvements * Avoid creation of intermediary array * SONAR-6959 Speed-up copy of custom measures in Compute Engine * Use light object to get key/uuid map of components * Avoid always formatting string * Minor refactoring * Avoid cost of hash * Ajust perf tests --- .../src/main/java/org/sonar/db/MyBatis.java | 2 + .../org/sonar/db/component/ComponentDao.java | 6 ++- .../sonar/db/component/ComponentMapper.java | 5 +++ .../sonar/db/component/KeyWithUuidDto.java | 42 +++++++++++++++++++ .../sonar/db/component/ComponentMapper.xml | 9 ++++ .../sonar/db/component/ComponentDaoTest.java | 30 +++++++++++++ .../component/ComponentImpl.java | 13 +++--- .../component/ComponentTreeBuilder.java | 6 ++- .../component/ComponentUuidFactory.java | 8 ++-- .../filemove/FileMoveDetectionStep.java | 4 +- .../issue/ComponentIssuesRepositoryImpl.java | 4 +- .../measure/MapBasedRawMeasureRepository.java | 26 +++++------- .../source/SymbolsLineReader.java | 4 +- .../step/CustomMeasuresCopyStep.java | 28 +++++++------ .../step/PersistMeasuresStep.java | 5 +-- .../component/ComponentImplTest.java | 3 +- .../step/CustomMeasuresCopyStepTest.java | 26 ++++++------ .../org/sonar/core/issue/DefaultIssue.java | 5 +-- .../ce/measure/RangeDistributionBuilder.java | 14 +++---- .../tests/performance/scanner/MemoryTest.java | 2 +- 20 files changed, 163 insertions(+), 79 deletions(-) create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/component/KeyWithUuidDto.java 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 6e7db16ec98..b8ee31f72e7 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 @@ -43,6 +43,7 @@ import org.sonar.db.component.ComponentLinkDto; import org.sonar.db.component.ComponentLinkMapper; import org.sonar.db.component.ComponentMapper; import org.sonar.db.component.FilePathWithHashDto; +import org.sonar.db.component.KeyWithUuidDto; import org.sonar.db.component.ResourceDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.component.SnapshotMapper; @@ -150,6 +151,7 @@ public class MyBatis implements Startable { confBuilder.loadAlias("DuplicationUnit", DuplicationUnitDto.class); confBuilder.loadAlias("Event", EventDto.class); confBuilder.loadAlias("FilePathWithHash", FilePathWithHashDto.class); + confBuilder.loadAlias("KeyWithUuid", KeyWithUuidDto.class); confBuilder.loadAlias("Group", GroupDto.class); confBuilder.loadAlias("GroupMembership", GroupMembershipDto.class); confBuilder.loadAlias("GroupPermission", GroupPermissionDto.class); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index e3b3b556c6b..9e11072f09e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -164,6 +164,10 @@ public class ComponentDao implements Dao { return mapper(session).selectComponentsFromProjectKeyAndScope(projectKey, null, false); } + public List selectUuidsByKeyFromProjectKey(DbSession session, String projectKey) { + return mapper(session).selectUuidsByKeyFromProjectKey(projectKey); + } + public List selectEnabledModulesFromProjectKey(DbSession session, String projectKey) { return mapper(session).selectComponentsFromProjectKeyAndScope(projectKey, Scopes.PROJECT, true); } @@ -174,7 +178,7 @@ public class ComponentDao implements Dao { public List selectByKeysAndBranch(DbSession session, Collection keys, String branch) { List dbKeys = keys.stream().map(k -> generateBranchKey(k, branch)).collect(toList()); - List allKeys = Stream.of(keys, dbKeys) .flatMap(Collection::stream) .collect(toList()); + List allKeys = Stream.of(keys, dbKeys).flatMap(Collection::stream).collect(toList()); return executeLargeInputs(allKeys, subKeys -> mapper(session).selectByKeysAndBranch(subKeys, branch)); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java index ca7da22e645..c99bab08f13 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -116,6 +116,11 @@ public interface ComponentMapper { List selectComponentsFromProjectKeyAndScope(@Param("projectKey") String projectKey, @Nullable @Param("scope") String scope, @Param(value = "excludeDisabled") boolean excludeDisabled); + /** + * Return keys and UUIDs of all components belonging to a project + */ + List selectUuidsByKeyFromProjectKey(@Param("projectKey") String projectKey); + /** * Return technical projects from a view or a sub-view */ diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/KeyWithUuidDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/KeyWithUuidDto.java new file mode 100644 index 00000000000..e1413063389 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/KeyWithUuidDto.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.component; + +import javax.annotation.concurrent.Immutable; + +@Immutable +public class KeyWithUuidDto { + + private final String kee; + private final String uuid; + + public KeyWithUuidDto(String kee, String uuid) { + this.kee = kee; + this.uuid = uuid; + } + + public String key() { + return kee; + } + + public String uuid() { + return uuid; + } +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index feaa6661866..2aaadf4d956 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -414,6 +414,15 @@ + +