From ee1d65b5c6b4a8bc15b14235ede5e701cad57e21 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 16 Sep 2013 23:12:24 +0200 Subject: [PATCH] SONAR-4676 Speed-up insertion of new issues in database --- .../org/sonar/core/issue/db/IssueStorage.java | 50 ++++++++++++++----- 1 file 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 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 toBeUpdated = batchInsert(issues, now); + update(toBeUpdated, now); + } + + private List batchInsert(Iterable issues, Date now) { + List 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 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) { -- 2.39.5