From f1476c52e6284b0b4371251a1b6d98cd82db8e48 Mon Sep 17 00:00:00 2001 From: takezoe Date: Sun, 6 Oct 2013 18:30:43 +0900 Subject: [PATCH] (refs #121)Optimize push performance for a lot of commit. --- src/main/scala/service/ActivityService.scala | 8 +++- .../scala/servlet/GitRepositoryServlet.scala | 42 +++++++++++++------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main/scala/service/ActivityService.scala b/src/main/scala/service/ActivityService.scala index 50d0b0d4c..b6074887f 100644 --- a/src/main/scala/service/ActivityService.scala +++ b/src/main/scala/service/ActivityService.scala @@ -141,7 +141,13 @@ trait ActivityService { def insertCommitId(userName: String, repositoryName: String, commitId: String) = { CommitLog insert (userName, repositoryName, commitId) } - + + def insertAllCommitIds(userName: String, repositoryName: String, commitIds: List[String]) = + CommitLog insertAll (commitIds.map(commitId => (userName, repositoryName, commitId)): _*) + + def getAllCommitIds(userName: String, repositoryName: String): List[String] = + Query(CommitLog).filter(_.byRepository(userName, repositoryName)).map(_.commitId).list + def existsCommitId(userName: String, repositoryName: String, commitId: String): Boolean = Query(CommitLog).filter(_.byPrimaryKey(userName, repositoryName, commitId)).firstOption.isDefined diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index f7b217244..5718e46bd 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -15,6 +15,7 @@ import util.Implicits._ import service._ import WebHookService._ import org.eclipse.jgit.api.Git +import util.JGitUtil.CommitInfo /** * Provides Git repository via HTTP. @@ -87,19 +88,26 @@ class CommitLogHook(owner: String, repository: String, userName: String, baseURL val commits = JGitUtil.getCommitLog(git, command.getOldId.name, command.getNewId.name) val refName = command.getRefName.split("/") - // apply issue comment - val newCommits = commits.flatMap { commit => - if(!existsCommitId(owner, repository, commit.id)){ - insertCommitId(owner, repository, commit.id) - "(^|\\W)#(\\d+)(\\W|$)".r.findAllIn(commit.fullMessage).matchData.foreach { matchData => - val issueId = matchData.group(2) - if(getAccountByUserName(commit.committer).isDefined && getIssue(owner, repository, issueId).isDefined){ - createComment(owner, repository, commit.committer, issueId.toInt, commit.fullMessage, "commit") - } + // Extract new commit and apply issue comment + val newCommits = if(commits.size > 1000){ + val existIds = getAllCommitIds(owner, repository) + commits.flatMap { commit => + optionIf(!existIds.contains(commit.id)){ + createIssueComment(commit) + Some(commit) } - Some(commit) - } else None - }.toList + } + } else { + commits.flatMap { commit => + optionIf(!existsCommitId(owner, repository, commit.id)){ + createIssueComment(commit) + Some(commit) + } + } + } + + // batch insert all new commit id + insertAllCommitIds(owner, repository, newCommits.map(_.id)) // record activity if(refName(1) == "heads"){ @@ -132,4 +140,14 @@ class CommitLogHook(owner: String, repository: String, userName: String, baseURL // update repository last modified time. updateLastActivityDate(owner, repository) } + + private def createIssueComment(commit: CommitInfo) = { + "(^|\\W)#(\\d+)(\\W|$)".r.findAllIn(commit.fullMessage).matchData.foreach { matchData => + val issueId = matchData.group(2) + if(getAccountByUserName(commit.committer).isDefined && getIssue(owner, repository, issueId).isDefined){ + createComment(owner, repository, commit.committer, issueId.toInt, commit.fullMessage, "commit") + } + } + } + }