From: Julien Lancelot Date: Tue, 18 Jun 2013 09:17:11 +0000 (+0200) Subject: SONAR-3755 Revert usage of "or" in favour of "in" in SQL used to get issues by id X-Git-Tag: 3.7~460 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c1903b898f440e042a65555bed7a09324fbf3f33;p=sonarqube.git SONAR-3755 Revert usage of "or" in favour of "in" in SQL used to get issues by id --- diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java index 9cc7fa17b8b..7b93ecfb41f 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java @@ -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 params = newHashMap(); + params.put("ids", idsPartition); + return session.selectList("org.sonar.core.issue.db.IssueMapper.selectByIds", params); } } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java index 18d9a4b98e5..42c9b091340 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java @@ -36,8 +36,6 @@ public interface IssueMapper { List selectIssues(@Param("query") IssueQuery query, @Param("componentRootKeys") Collection componentRootKeys, @Nullable @Param("userId") Integer userId, @Param("role") String role, @Param("maxResults") Integer maxResult); - List selectByIds(@Param("ids") Collection ids); - void insert(IssueDto issue); int update(IssueDto issue); diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml index 2d11f53f828..06fa350595e 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml @@ -192,7 +192,12 @@ inner join projects p on p.id=i.component_id inner join projects root on root.id=i.root_component_id - and i.id=#{element} + and + + + #{element} + + diff --git a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java index a792176d525..035fc1a636a 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java @@ -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 hugeNbOfIssues = newArrayList(); + for (long i=0; i<1500; i++) { + hugeNbOfIssues.add(i); + } + List 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 getIssueIds(List issues) { return newArrayList(Iterables.transform(issues, new Function() { @Override