]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15828 Refactor `ProjectDao#selectApplicationsByKeys` to use `executeLargeInputs...
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>
Wed, 26 Jan 2022 10:20:27 +0000 (11:20 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 26 Jan 2022 20:02:58 +0000 (20:02 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java
server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java

index 796c61b99e458296e86014e07919e248c828bf87..b88b86cdc2746d5fff6f162b9503bf48b3803afe 100644 (file)
@@ -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<ProjectDto> selectProjects(DbSession session) {
index e89b99f3571c6024bee9b3916f0a15da90fc4caa..e263312af301400d6e5021f2dc68bdd46471d246 100644 (file)
  */
 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<ProjectDto> 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<String> applicationsId = new HashSet<>();
+
+    List<Tuple> 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<ProjectDto> 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("");