]> source.dussan.org Git - sonarqube.git/commitdiff
Apply code review feedback
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 29 Jan 2015 07:39:52 +0000 (08:39 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 29 Jan 2015 14:10:06 +0000 (15:10 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/db/ComponentDao.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/PurgeRemovedViewsStep.java
server/sonar-server/src/test/java/org/sonar/server/component/db/ComponentDaoTest.java
sonar-core/src/main/java/org/sonar/core/component/db/ComponentMapper.java
sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
sonar-core/src/main/resources/org/sonar/core/component/db/ComponentMapper.xml

index 3da989879f74f0146b3c880ca7e4472f42015cb5..1630b9536fbdcd5cf8cb26214c8c962c8553e33b 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.sonar.server.component.db;
 
+import com.google.common.base.Function;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.Scopes;
@@ -103,19 +104,19 @@ public class ComponentDao extends BaseDao<ComponentMapper, ComponentDto, String>
   }
 
   public List<ComponentDto> getByUuids(final DbSession session, Collection<String> uuids) {
-    return DaoUtils.partitionSelect(uuids, new DaoUtils.Function<ComponentDto, String>() {
+    return DaoUtils.executeLargeInputs(uuids, new Function<List<String>, List<ComponentDto>>() {
       @Override
-      public List<ComponentDto> execute(List<String> partition) {
+      public List<ComponentDto> apply(List<String> partition) {
         return mapper(session).findByUuids(partition);
       }
     });
   }
 
-  public List<String> selectUuidsByUuids(final DbSession session, Collection<String> uuids) {
-    return DaoUtils.partitionSelect(uuids, new DaoUtils.Function<String, String>() {
+  public List<String> selectExistingUuids(final DbSession session, Collection<String> uuids) {
+    return DaoUtils.executeLargeInputs(uuids, new Function<List<String>, List<String>>() {
       @Override
-      public List<String> execute(List<String> partition) {
-        return mapper(session).selectUuidsByUuids(partition);
+      public List<String> apply(List<String> partition) {
+        return mapper(session).selectExistingUuids(partition);
       }
     });
   }
index 463ea6b48fc2eb2f65c665e495350e4cd476d383..4c652e5129fea89d9f29f950d2acf5bf4aee1033 100644 (file)
@@ -58,7 +58,7 @@ public class PurgeRemovedViewsStep implements ComputationStep {
     DbSession session = dbClient.openSession(false);
     try {
       Set<String> viewUuidsInIndex = newHashSet(index.findAllViewUuids());
-      Set<String> viewUuidInDb = newHashSet(dbClient.componentDao().selectUuidsByUuids(session, viewUuidsInIndex));
+      Set<String> viewUuidInDb = newHashSet(dbClient.componentDao().selectExistingUuids(session, viewUuidsInIndex));
       Set<String> viewsToRemove = Sets.difference(viewUuidsInIndex, viewUuidInDb);
       index.delete(viewsToRemove);
     } finally {
index 46b41d7fbaefc7065df71c709bf0d6a75a04ccc6..5b4157ffa0bfd3a6e2860c7ebedcfa81cd8b4224 100644 (file)
@@ -201,14 +201,14 @@ public class ComponentDaoTest extends AbstractDaoTestCase {
   }
 
   @Test
-  public void select_uuids_by_uuids() {
+  public void select_existing_uuids() {
     setupData("shared");
 
-    List<String> results = dao.selectUuidsByUuids(session, newArrayList("KLMN"));
+    List<String> results = dao.selectExistingUuids(session, newArrayList("KLMN"));
     assertThat(results).containsOnly("KLMN");
 
-    assertThat(dao.selectUuidsByUuids(session, newArrayList("KLMN", "unknown"))).hasSize(1);
-    assertThat(dao.selectUuidsByUuids(session, newArrayList("unknown"))).isEmpty();
+    assertThat(dao.selectExistingUuids(session, newArrayList("KLMN", "unknown"))).hasSize(1);
+    assertThat(dao.selectExistingUuids(session, newArrayList("unknown"))).isEmpty();
   }
 
   @Test
index b0a49a83b93409e8592498cc521a5c93e44274bd..5deb3808f10d62f4917e8757e311ae7581e60817 100644 (file)
@@ -67,7 +67,7 @@ public interface ComponentMapper {
 
   List<ComponentDto> findByUuids(@Param("uuids") Collection<String> uuids);
 
-  List<String> selectUuidsByUuids(@Param("uuids") Collection<String> uuids);
+  List<String> selectExistingUuids(@Param("uuids") Collection<String> uuids);
 
   /**
    * Return all project (PRJ/TRK) uuids
index 074384c46dc974a6cef38955290b80627710e32e..2494f802fafcc82ed087e1ad2055adf37a460f89 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.core.persistence;
 
+import com.google.common.base.Function;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import org.sonar.core.dashboard.ActiveDashboardDao;
@@ -92,21 +93,23 @@ public final class DaoUtils {
       );
   }
 
-  public static <F, T> List<F> partitionSelect(Collection<T> input, Function<F, T> select) {
+  /**
+   * Partition by 1000 elements a list of input and execute a function on each part.
+   *
+   * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)'
+   * and with MsSQL when there's more than 2000 parameters in a query
+   */
+  public static <OUTPUT, INPUT> List<OUTPUT> executeLargeInputs(Collection<INPUT> input, Function<List<INPUT>, List<OUTPUT>> function) {
     if (input.isEmpty()) {
       return Collections.emptyList();
     }
-    List<F> results = newArrayList();
-    List<List<T>> partitionList = Lists.partition(newArrayList(input), PARTITION_SIZE_FOR_ORACLE);
-    for (List<T> partition : partitionList) {
-      List<F> dtos = select.execute(partition);
-      results.addAll(dtos);
+    List<OUTPUT> results = newArrayList();
+    List<List<INPUT>> partitionList = Lists.partition(newArrayList(input), PARTITION_SIZE_FOR_ORACLE);
+    for (List<INPUT> partition : partitionList) {
+      List<OUTPUT> subResults = function.apply(partition);
+      results.addAll(subResults);
     }
     return results;
   }
 
-  public static interface Function<F, T> {
-    List<F> execute(List<T> partition);
-  }
-
 }
index d5d965d1d03d3eea0ac180975e971f91e311cdff..e7b01ed806aaf7460dd89c43bce5040f50acfdcd 100644 (file)
@@ -97,7 +97,7 @@
     </where>
   </select>
 
-  <select id="selectUuidsByUuids" parameterType="String" resultType="String">
+  <select id="selectExistingUuids" parameterType="String" resultType="String">
     select p.uuid
     from projects p
     <where>