#27 -- add multithreading

This commit is contained in:
Anton Romanov 2021-04-30 11:27:36 +04:00
parent ccde75abc6
commit 9ab0d9cee0

View File

@ -45,6 +45,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -59,6 +60,7 @@ public class GitRepositoryService {
@Value("${extractor.custom-projects-dir}") @Value("${extractor.custom-projects-dir}")
private String customProjectsDir; private String customProjectsDir;
private final ExecutorService executorService = Executors.newFixedThreadPool(4); private final ExecutorService executorService = Executors.newFixedThreadPool(4);
private final ExecutorService executorServiceCommits = Executors.newFixedThreadPool(4);
private final StructuralUnitService structuralUnitService; private final StructuralUnitService structuralUnitService;
@ -109,22 +111,34 @@ public class GitRepositoryService {
List<RevCommit> commits = new ArrayList<>(); List<RevCommit> commits = new ArrayList<>();
git.log().setSkip(commitsFrom).setMaxCount(commitsTo - commitsFrom).call().forEach(commits::add); git.log().setSkip(commitsFrom).setMaxCount(commitsTo - commitsFrom).call().forEach(commits::add);
List<Commit> list = new ArrayList<>(); List<Commit> list = new CopyOnWriteArrayList<>();
RevCommit prevCommit = null; RevCommit prevCommit = null;
LOG.debug("Start analyse {} commits", commits.size()); LOG.debug("Start analyse {} commits", commits.size());
LOG.debug(" {} to {} commits", commitsFrom, commitsTo); LOG.debug(" {} to {} commits", commitsFrom, commitsTo);
List<Future<Commit>> futureCommits = new ArrayList<>();
for (RevCommit revCommit : commits) { for (RevCommit revCommit : commits) {
Commit commit = new Commit( final RevCommit forPrevCommit = prevCommit;
revCommit.getFullMessage(), futureCommits.add(executorServiceCommits.submit(() -> {
new Author(revCommit.getAuthorIdent().getName()), Commit commit = new Commit(
Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())), revCommit.getFullMessage(),
revCommit.getName()); new Author(revCommit.getAuthorIdent().getName()),
if (prevCommit != null) { Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())),
commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo)); revCommit.getName());
} if (forPrevCommit != null) {
list.add(commit); commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, forPrevCommit, localRepo));
}
list.add(commit);
return commit;
}));
prevCommit = revCommit; prevCommit = revCommit;
} }
futureCommits.forEach(fc -> {
try {
fc.get();
} catch (Exception e) {
LOG.debug(e.getMessage());
}
});
git.close(); git.close();
localRepo.close(); localRepo.close();
return list; return list;