aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-09-16 23:12:24 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2013-09-16 23:12:24 +0200
commitee1d65b5c6b4a8bc15b14235ede5e701cad57e21 (patch)
tree73bfbb5f998b78a84bba4bd04bab908edff3d5de /sonar-core
parent238f455cc9f4922880a806d437294155db1e8d0c (diff)
downloadsonarqube-ee1d65b5c6b4a8bc15b14235ede5e701cad57e21.tar.gz
sonarqube-ee1d65b5c6b4a8bc15b14235ede5e701cad57e21.zip
SONAR-4676 Speed-up insertion of new issues in database
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java50
1 files changed, 38 insertions, 12 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java
index c8f657e5666..5570c7118ce 100644
--- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java
+++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java
@@ -19,6 +19,7 @@
*/
package org.sonar.core.issue.db;
+import com.google.common.collect.Lists;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.IssueComment;
@@ -32,6 +33,7 @@ import org.sonar.core.persistence.MyBatis;
import java.util.Arrays;
import java.util.Date;
+import java.util.List;
/**
* Save issues into database. It is executed :
@@ -58,29 +60,36 @@ public abstract class IssueStorage {
}
public void save(Iterable<DefaultIssue> issues) {
- // Batch session can not be used. It does not return the number of updated rows,
+ // Batch session can not be used for updates. It does not return the number of updated rows,
// required for detecting conflicts.
- SqlSession session = mybatis.openSession();
- int count = 0;
- IssueMapper issueMapper = session.getMapper(IssueMapper.class);
- IssueChangeMapper issueChangeMapper = session.getMapper(IssueChangeMapper.class);
Date now = new Date();
+ List<DefaultIssue> toBeUpdated = batchInsert(issues, now);
+ update(toBeUpdated, now);
+ }
+
+ private List<DefaultIssue> batchInsert(Iterable<DefaultIssue> issues, Date now) {
+ List<DefaultIssue> toBeUpdated = Lists.newArrayList();
+ SqlSession batchSession = mybatis.openBatchSession();
+ int count = 0;
+ IssueMapper issueMapper = batchSession.getMapper(IssueMapper.class);
+ IssueChangeMapper issueChangeMapper = batchSession.getMapper(IssueChangeMapper.class);
try {
for (DefaultIssue issue : issues) {
if (issue.isNew()) {
insert(issueMapper, now, issue);
+ insertChanges(issueChangeMapper, issue);
+ if (count++ > BatchSession.MAX_BATCH_SIZE) {
+ batchSession.commit();
+ }
} else if (issue.isChanged()) {
- update(issueMapper, now, issue);
- }
- insertChanges(issueChangeMapper, issue);
- if (count++> BatchSession.MAX_BATCH_SIZE) {
- session.commit();
+ toBeUpdated.add(issue);
}
}
- session.commit();
+ batchSession.commit();
} finally {
- MyBatis.closeQuietly(session);
+ MyBatis.closeQuietly(batchSession);
}
+ return toBeUpdated;
}
private void insert(IssueMapper issueMapper, Date now, DefaultIssue issue) {
@@ -91,6 +100,23 @@ public abstract class IssueStorage {
issueMapper.insert(dto);
}
+ private void update(List<DefaultIssue> toBeUpdated, Date now) {
+ if (!toBeUpdated.isEmpty()) {
+ SqlSession session = mybatis.openSession();
+ try {
+ IssueMapper issueMapper = session.getMapper(IssueMapper.class);
+ IssueChangeMapper issueChangeMapper = session.getMapper(IssueChangeMapper.class);
+ for (DefaultIssue issue : toBeUpdated) {
+ update(issueMapper, now, issue);
+ insertChanges(issueChangeMapper, issue);
+ }
+ session.commit();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+ }
+
private void update(IssueMapper issueMapper, Date now, DefaultIssue issue) {
IssueDto dto = IssueDto.toDtoForUpdate(issue, now);
if (Issue.STATUS_CLOSED.equals(issue.status()) || issue.selectedAt() == null) {