From 5054224ff36177b26bcf9af21d6041cb0d1d6240 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 28 Apr 2021 11:44:27 +0400 Subject: [PATCH] fix out of memory --- .../ulstu/extractor/service/BranchService.java | 4 ++-- .../ulstu/extractor/service/CommitService.java | 18 +++++++++++++----- .../ulstu/extractor/service/IndexService.java | 4 +--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/service/BranchService.java b/src/main/java/ru/ulstu/extractor/service/BranchService.java index 0b5649b..596c6e0 100644 --- a/src/main/java/ru/ulstu/extractor/service/BranchService.java +++ b/src/main/java/ru/ulstu/extractor/service/BranchService.java @@ -42,9 +42,9 @@ public class BranchService { public Branch addCommits(Branch branch, List commits) { LOG.debug("Start add commits to {} branch with {} commits ", branch.getName(), commits.size()); - branch.getCommits().addAll(commitService.save(commits)); + commitService.save(commits); LOG.debug("Save branch {} ", branch.getName()); - branch = branchRepository.save(branch); + commitService.updateBranchId(commits, branch.getId()); return branch; } diff --git a/src/main/java/ru/ulstu/extractor/service/CommitService.java b/src/main/java/ru/ulstu/extractor/service/CommitService.java index c61b2ce..3ae5905 100644 --- a/src/main/java/ru/ulstu/extractor/service/CommitService.java +++ b/src/main/java/ru/ulstu/extractor/service/CommitService.java @@ -5,6 +5,7 @@ package ru.ulstu.extractor.service; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.repository.CommitRepository; @@ -14,16 +15,16 @@ import java.util.stream.Collectors; @Service public class CommitService { - private final GitRepositoryService gitRepositoryService; private final CommitRepository commitRepository; private final AuthorService authorService; + private final JdbcTemplate jdbcTemplate; - public CommitService(GitRepositoryService gitRepositoryService, - CommitRepository commitRepository, - AuthorService authorService) { - this.gitRepositoryService = gitRepositoryService; + public CommitService(CommitRepository commitRepository, + AuthorService authorService, + JdbcTemplate jdbcTemplate) { this.commitRepository = commitRepository; this.authorService = authorService; + this.jdbcTemplate = jdbcTemplate; } public void delete(List commitsToRemove) { @@ -41,6 +42,13 @@ public class CommitService { public void deleteWithEmptyIds() { commitRepository.deleteByBranchIsNull(); } + + public void updateBranchId(List commits, Integer branchId) { + List commitIds = commits.stream().map(c -> c.getId().toString()).collect(Collectors.toList()); + String updateQuery = "update commit set branch_id = ? where id in (%s)"; + updateQuery = String.format(updateQuery, String.join(",", commitIds)); + jdbcTemplate.update(updateQuery, branchId); + } } diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java index 8fb7d0d..3cd9644 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -10,7 +10,6 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Repository; @@ -36,7 +35,6 @@ public class IndexService { this.branchService = branchService; } - @Transactional public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { Repository repository = repositoryRepository.findByUrl(repositoryUrl); if (repository == null) { @@ -52,7 +50,7 @@ public class IndexService { List commits = gitRepositoryService.getCommits(repositoryUrl, branchName, commitsFrom, commitsTo, true); while (!commits.isEmpty()) { LOG.debug("{} commits loaded.", commits.size()); - branch = branchService.addCommits(branch, commits); + branchService.addCommits(branch, commits); LOG.debug("{} commits successfully saved. {} {}", commits.size(), repositoryUrl, branchName); commitsFrom += COMMITS_PAGE_SIZE; commitsTo += COMMITS_PAGE_SIZE;