]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3755 Revert usage of "or" in favour of "in" in SQL used to get issues by id
authorJulien Lancelot <julien.lancelot@gmail.com>
Tue, 18 Jun 2013 09:17:11 +0000 (11:17 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Tue, 18 Jun 2013 09:17:58 +0000 (11:17 +0200)
sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java
sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java
sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml
sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java

index 9cc7fa17b8bbdd38ed145ae17e4c871b5d7197a6..7b93ecfb41f923d3bd0e52918f1b1c918a2240b1 100644 (file)
@@ -21,6 +21,7 @@
 package org.sonar.core.issue.db;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.Lists;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.SqlSession;
 import org.sonar.api.BatchComponent;
@@ -34,6 +35,10 @@ import javax.annotation.Nullable;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.collect.Maps.newHashMap;
 
 /**
  * @since 3.6
@@ -113,6 +118,9 @@ public class IssueDao implements BatchComponent, ServerComponent {
     if (ids.isEmpty()) {
       return Collections.emptyList();
     }
-    return session.getMapper(IssueMapper.class).selectByIds(ids);
+    Object idsPartition = Lists.partition(newArrayList(ids), 1000);
+    Map<String, Object> params = newHashMap();
+    params.put("ids", idsPartition);
+    return session.selectList("org.sonar.core.issue.db.IssueMapper.selectByIds", params);
   }
 }
index 18d9a4b98e5f0d19176d277a866bb453c1e53561..42c9b0913403843ef332c23ea244edf1f77d9352 100644 (file)
@@ -36,8 +36,6 @@ public interface IssueMapper {
   List<IssueDto> selectIssues(@Param("query") IssueQuery query, @Param("componentRootKeys") Collection<String> componentRootKeys,
                               @Nullable @Param("userId") Integer userId, @Param("role") String role, @Param("maxResults") Integer maxResult);
 
-  List<IssueDto> selectByIds(@Param("ids") Collection<Long> ids);
-
   void insert(IssueDto issue);
 
   int update(IssueDto issue);
index 2d11f53f8287f58008ec728aba9a159a9b19098d..06fa350595e16ec29269cdf0ae5d2cdf131ba2bc 100644 (file)
     inner join projects p on p.id=i.component_id
     inner join projects root on root.id=i.root_component_id
     <where>
-      and <foreach collection="ids" open="(" close=")" item="element" index="index" separator=" or ">i.id=#{element}</foreach>
+      and
+      <foreach collection="ids" open="i.id in (" close=")" item="list" separator=") or i.id in (">
+        <foreach collection="list" item="element" separator=",">
+          #{element}
+        </foreach>
+      </foreach>
     </where>
   </select>
 
index a792176d525319940199af55937e83ef031a49a4..035fc1a636a2111adfabeb8065aab18359fba78c 100644 (file)
@@ -305,6 +305,20 @@ public class IssueDaoTest extends AbstractDaoTestCase {
     assertThat(results).hasSize(3);
   }
 
+  @Test
+  public void should_select_by_ids_with_huge_number_of_ids() {
+    setupData("shared");
+
+    List<Long> hugeNbOfIssues = newArrayList();
+    for (long i=0; i<1500; i++) {
+      hugeNbOfIssues.add(i);
+    }
+    List<IssueDto> results = dao.selectByIds(hugeNbOfIssues);
+
+    // The goal of this test is only to check that the query do no fail, not to check the number of results
+    assertThat(results).isEmpty();
+  }
+
   private List<Long> getIssueIds(List<IssueDto> issues) {
     return newArrayList(Iterables.transform(issues, new Function<IssueDto, Long>() {
       @Override