#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.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<RevCommit> commits = new ArrayList<>();
git.log().setSkip(commitsFrom).setMaxCount(commitsTo - commitsFrom).call().forEach(commits::add);
List<Commit> list = new ArrayList<>();
List<Commit> list = new CopyOnWriteArrayList<>();
RevCommit prevCommit = null;
LOG.debug("Start analyse {} commits", commits.size());
LOG.debug(" {} to {} commits", commitsFrom, commitsTo);
List<Future<Commit>> 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;