From 9fbd2446ec7b6010f254ad547949e51a48c9bede Mon Sep 17 00:00:00 2001 From: Klaudio Sinani Date: Wed, 26 Jan 2022 11:20:27 +0100 Subject: [PATCH] SONAR-15828 Refactor `ProjectDao#selectApplicationsByKeys` to use `executeLargeInputs` method --- .../java/org/sonar/db/project/ProjectDao.java | 5 ++- .../org/sonar/db/project/ProjectDaoTest.java | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java index 796c61b99e4..b88b86cdc27 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java @@ -27,6 +27,8 @@ import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DbSession; +import static org.sonar.db.DatabaseUtils.executeLargeInputs; + public class ProjectDao implements Dao { private final System2 system2; @@ -65,7 +67,8 @@ public class ProjectDao implements Dao { if (keys.isEmpty()) { return Collections.emptyList(); } - return mapper(session).selectApplicationsByKeys(keys); + + return executeLargeInputs(keys, partition -> mapper(session).selectApplicationsByKeys(partition)); } public List selectProjects(DbSession session) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java index e89b99f3571..e263312af30 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java @@ -19,12 +19,16 @@ */ package org.sonar.db.project; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; import javax.annotation.Nullable; +import org.assertj.core.groups.Tuple; import org.junit.Rule; import org.junit.Test; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; @@ -99,6 +103,34 @@ public class ProjectDaoTest { tuple("projectName_p1", "projectKee_o2_p1", "uuid_o2_p1", "desc_p1", "tag1,tag2", false)); } + @Test + public void select_applications_by_keys() { + List applications = new ArrayList<>(); + + for (int i = 0; i < 1500; i++) { + ProjectDto project = db.components().insertPrivateProjectDto(); + ProjectDto app = db.components().insertPrivateApplicationDto(); + db.components().addApplicationProject(app, project); + applications.add(i, app); + } + + Set applicationsId = new HashSet<>(); + + List applicationsData = applications + .stream() + .map(x -> { + applicationsId.add(x.getKey()); + return tuple(x.getKey(), x.getName(), x.getUuid(), x.getDescription(), x.isPrivate()); + }) + .collect(Collectors.toList()); + + List selectedApplications = projectDao.selectApplicationsByKeys(db.getSession(), applicationsId); + + assertThat(selectedApplications) + .extracting(ProjectDto::getKey, ProjectDto::getName, ProjectDto::getUuid, ProjectDto::getDescription, ProjectDto::isPrivate) + .containsExactlyInAnyOrderElementsOf(applicationsData); + } + @Test public void update_tags() { ProjectDto dto1 = createProject("o1", "p1").setTagsString(""); -- 2.39.5