diff --git a/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationDao.java b/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationDao.java index 94d7ef81c731..a53017ea7b10 100644 --- a/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationDao.java +++ b/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationDao.java @@ -19,46 +19,37 @@ */ package org.sonar.db.duplication; +import com.google.common.base.Function; import java.util.Collection; import java.util.List; -import org.apache.ibatis.session.SqlSession; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.sonar.db.Dao; +import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; -import org.sonar.db.MyBatis; public class DuplicationDao implements Dao { - private final MyBatis mybatis; - - public DuplicationDao(MyBatis mybatis) { - this.mybatis = mybatis; - } - - public List selectCandidates(int resourceSnapshotId, Integer lastSnapshotId, String language) { - SqlSession session = mybatis.openSession(false); - try { - DuplicationMapper mapper = session.getMapper(DuplicationMapper.class); - return mapper.selectCandidates(resourceSnapshotId, lastSnapshotId, language); - } finally { - MyBatis.closeQuietly(session); - } + /** + * @param projectSnapshotId snapshot id of the project from the previous analysis (islast=true) + */ + public List selectCandidates(final DbSession session, @Nullable final Long projectSnapshotId, final String language, Collection hashes) { + return DatabaseUtils.executeLargeInputs(hashes, new Function, List>() { + @Override + public List apply(@Nonnull List partition) { + return session.getMapper(DuplicationMapper.class).selectCandidates(projectSnapshotId, language, partition); + } + }); } /** * Insert rows in the table DUPLICATIONS_INDEX. * Note that generated ids are not returned. */ - public void insert(Collection units) { - DbSession session = mybatis.openSession(true); - try { - DuplicationMapper mapper = session.getMapper(DuplicationMapper.class); - for (DuplicationUnitDto unit : units) { - mapper.batchInsert(unit); - } - session.commit(); - - } finally { - MyBatis.closeQuietly(session); + public void insert(DbSession session, Collection units) { + DuplicationMapper mapper = session.getMapper(DuplicationMapper.class); + for (DuplicationUnitDto unit : units) { + mapper.batchInsert(unit); } } diff --git a/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationMapper.java b/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationMapper.java index fe366d9ffc09..284a08487bc1 100644 --- a/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationMapper.java @@ -19,15 +19,17 @@ */ package org.sonar.db.duplication; +import java.util.Collection; import java.util.List; +import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; public interface DuplicationMapper { List selectCandidates( - @Param("resource_snapshot_id") int resourceSnapshotId, - @Param("last_project_snapshot_id") Integer lastSnapshotId, - @Param("language") String language); + @Nullable @Param("projectSnapshotId") Long projectSnapshotId, + @Param("language") String language, + @Param("hashes") Collection hashes); void batchInsert(DuplicationUnitDto unit); diff --git a/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationUnitDto.java b/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationUnitDto.java index 443782a63bda..d45e5b7b2913 100644 --- a/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationUnitDto.java +++ b/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationUnitDto.java @@ -19,97 +19,85 @@ */ package org.sonar.db.duplication; -/** - * A simple DTO (Data Transfer Object) class that provides the mapping of data to a table. - */ public final class DuplicationUnitDto { - private Long id; - private Integer snapshotId; - private Integer projectSnapshotId; + private long id; + private long snapshotId; + private long projectSnapshotId; private String hash; private int indexInFile; private int startLine; private int endLine; - private String resourceKey; - - public DuplicationUnitDto() { - } + // Return by join + private String componentKey; - public DuplicationUnitDto(Integer projectSnapshotId, Integer snapshotId, String hash, Integer indexInFile, Integer startLine, Integer endLine) { - this.projectSnapshotId = projectSnapshotId; - this.snapshotId = snapshotId; - this.hash = hash; - this.indexInFile = indexInFile; - this.startLine = startLine; - this.endLine = endLine; - } - - public Long getId() { + public long getId() { return id; } - public DuplicationUnitDto setId(Long id) { + public DuplicationUnitDto setId(long id) { this.id = id; return this; } - public Integer getSnapshotId() { + public long getSnapshotId() { return snapshotId; } - public void setSnapshotId(Integer snapshotId) { + public DuplicationUnitDto setSnapshotId(long snapshotId) { this.snapshotId = snapshotId; + return this; } - public Integer getProjectSnapshotId() { + public long getProjectSnapshotId() { return projectSnapshotId; } - public void setProjectSnapshotId(Integer projectSnapshotId) { + public DuplicationUnitDto setProjectSnapshotId(long projectSnapshotId) { this.projectSnapshotId = projectSnapshotId; + return this; } public String getHash() { return hash; } - public void setHash(String hash) { + public DuplicationUnitDto setHash(String hash) { this.hash = hash; + return this; } public int getIndexInFile() { return indexInFile; } - public void setIndexInFile(int indexInFile) { + public DuplicationUnitDto setIndexInFile(int indexInFile) { this.indexInFile = indexInFile; + return this; } public int getStartLine() { return startLine; } - public void setStartLine(int startLine) { + public DuplicationUnitDto setStartLine(int startLine) { this.startLine = startLine; + return this; } public int getEndLine() { return endLine; } - public void setEndLine(int endLine) { + public DuplicationUnitDto setEndLine(int endLine) { this.endLine = endLine; + return this; } - public String getResourceKey() { - return resourceKey; - } - - public void setResourceKey(String resourceKey) { - this.resourceKey = resourceKey; + public String getComponentKey() { + return componentKey; } } diff --git a/sonar-db/src/main/resources/org/sonar/db/duplication/DuplicationMapper.xml b/sonar-db/src/main/resources/org/sonar/db/duplication/DuplicationMapper.xml index 66212b3b2054..916eba5ad968 100644 --- a/sonar-db/src/main/resources/org/sonar/db/duplication/DuplicationMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/duplication/DuplicationMapper.xml @@ -4,22 +4,30 @@ INSERT INTO duplications_index (snapshot_id, project_snapshot_id, hash, index_in_file, start_line, end_line) VALUES (#{snapshotId}, #{projectSnapshotId}, #{hash}, #{indexInFile}, #{startLine}, #{endLine}) + diff --git a/sonar-db/src/test/java/org/sonar/db/duplication/DuplicationDaoTest.java b/sonar-db/src/test/java/org/sonar/db/duplication/DuplicationDaoTest.java index 9d4180ad9768..7936524d8987 100644 --- a/sonar-db/src/test/java/org/sonar/db/duplication/DuplicationDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/duplication/DuplicationDaoTest.java @@ -19,17 +19,17 @@ */ package org.sonar.db.duplication; -import java.util.Arrays; import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.test.DbTests; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class DuplicationDaoTest { @@ -37,34 +37,45 @@ public class DuplicationDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); + DbSession dbSession = db.getSession(); + DuplicationDao dao = db.getDbClient().duplicationDao(); @Test - public void shouldGetByHash() { - db.prepareDbUnit(getClass(), "shouldGetByHash.xml"); + public void select_candidates() { + db.prepareDbUnit(getClass(), "select_candidates.xml"); + dbSession.commit(); - List blocks = dao.selectCandidates(10, 7, "java"); - assertThat(blocks.size(), is(1)); + List blocks = dao.selectCandidates(dbSession, 7L, "java", singletonList("aa")); + assertThat(blocks).hasSize(1); DuplicationUnitDto block = blocks.get(0); - assertThat("block resourceId", block.getResourceKey(), is("bar-last")); - assertThat("block hash", block.getHash(), is("aa")); - assertThat("block index in file", block.getIndexInFile(), is(0)); - assertThat("block start line", block.getStartLine(), is(1)); - assertThat("block end line", block.getEndLine(), is(2)); + assertThat(block.getComponentKey()).isEqualTo("bar-last"); + assertThat(block.getHash()).isEqualTo("aa"); + assertThat(block.getIndexInFile()).isEqualTo(0); + assertThat(block.getStartLine()).isEqualTo(1); + assertThat(block.getEndLine()).isEqualTo(2); // check null for lastSnapshotId - blocks = dao.selectCandidates(10, null, "java"); - assertThat(blocks.size(), is(2)); + blocks = dao.selectCandidates(dbSession, null, "java", singletonList("aa")); + assertThat(blocks).hasSize(2); } @Test - public void shouldInsert() { - db.prepareDbUnit(getClass(), "shouldInsert.xml"); + public void insert() { + db.prepareDbUnit(getClass(), "insert.xml"); + dbSession.commit(); - dao.insert(Arrays.asList(new DuplicationUnitDto(1, 2, "bb", 0, 1, 2))); + dao.insert(dbSession, singletonList(new DuplicationUnitDto() + .setProjectSnapshotId(1) + .setSnapshotId(2) + .setHash("bb") + .setIndexInFile(0) + .setStartLine(1) + .setEndLine(2))); + dbSession.commit(); - db.assertDbUnit(getClass(), "shouldInsert-result.xml", "duplications_index"); + db.assertDbUnit(getClass(), "insert-result.xml", "duplications_index"); } } diff --git a/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/shouldInsert-result.xml b/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/insert-result.xml similarity index 100% rename from sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/shouldInsert-result.xml rename to sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/insert-result.xml diff --git a/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/shouldInsert.xml b/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/insert.xml similarity index 100% rename from sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/shouldInsert.xml rename to sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/insert.xml diff --git a/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/shouldGetByHash.xml b/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/select_candidates.xml similarity index 55% rename from sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/shouldGetByHash.xml rename to sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/select_candidates.xml index 37efb6149f01..7b4ddcef84d1 100644 --- a/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/shouldGetByHash.xml +++ b/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/select_candidates.xml @@ -1,27 +1,27 @@ - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - +