From 9ab0d9cee0a8b73126030d6fcd3d36619ec803b1 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 30 Apr 2021 11:27:36 +0400 Subject: [PATCH] #27 -- add multithreading --- .../service/GitRepositoryService.java | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index 924180d..6f85552 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -45,6 +45,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Optional; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -59,6 +60,7 @@ public class GitRepositoryService { @Value("${extractor.custom-projects-dir}") private String customProjectsDir; private final ExecutorService executorService = Executors.newFixedThreadPool(4); + private final ExecutorService executorServiceCommits = Executors.newFixedThreadPool(4); private final StructuralUnitService structuralUnitService; @@ -109,22 +111,34 @@ public class GitRepositoryService { List commits = new ArrayList<>(); git.log().setSkip(commitsFrom).setMaxCount(commitsTo - commitsFrom).call().forEach(commits::add); - List list = new ArrayList<>(); + List list = new CopyOnWriteArrayList<>(); RevCommit prevCommit = null; LOG.debug("Start analyse {} commits", commits.size()); LOG.debug(" {} to {} commits", commitsFrom, commitsTo); + List> futureCommits = new ArrayList<>(); for (RevCommit revCommit : commits) { - Commit commit = new Commit( - revCommit.getFullMessage(), - new Author(revCommit.getAuthorIdent().getName()), - Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())), - revCommit.getName()); - if (prevCommit != null) { - commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo)); - } - list.add(commit); + final RevCommit forPrevCommit = prevCommit; + futureCommits.add(executorServiceCommits.submit(() -> { + Commit commit = new Commit( + revCommit.getFullMessage(), + new Author(revCommit.getAuthorIdent().getName()), + Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())), + revCommit.getName()); + if (forPrevCommit != null) { + commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, forPrevCommit, localRepo)); + } + list.add(commit); + return commit; + })); prevCommit = revCommit; } + futureCommits.forEach(fc -> { + try { + fc.get(); + } catch (Exception e) { + LOG.debug(e.getMessage()); + } + }); git.close(); localRepo.close(); return list;