Оптимизация взаимодействия дискуссий с БД

This commit is contained in:
Struchkov Mark 2022-12-20 22:05:18 +03:00
parent b350e18a5c
commit 35e206a7e2
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
9 changed files with 54 additions and 39 deletions

View File

@ -44,7 +44,7 @@ public class Discussion {
joinColumns = @JoinColumn(name = "discussion_id"),
inverseJoinColumns = @JoinColumn(name = "merge_request_id")
)
private MergeRequest mergeRequest;
private MergeRequestForDiscussion mergeRequest;
@OneToMany(
mappedBy = "discussion",

View File

@ -4,11 +4,12 @@ import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
@ -19,27 +20,30 @@ import javax.persistence.Table;
@Entity
@Table(name = "merge_request")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class MergeRequestMini {
public class MergeRequestForDiscussion {
/**
* Идентификатор
*/
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@EqualsAndHashCode.Include
private Long id;
/**
* Адрес ПР
*/
@Column(name = "two_id")
private Long twoId;
@Column(name = "project_id")
private Long projectId;
@Column(name = "title")
private String title;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "author_id")
private Person author;
@Column(name = "web_url")
private String webUrl;
/**
* Автор ПР
*/
@Column(name = "author_id")
private Long author;
}

View File

@ -3,6 +3,7 @@ package dev.struchkov.bot.gitlab.context.repository;
import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr;
import dev.struchkov.bot.gitlab.context.domain.MergeRequestState;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion;
import lombok.NonNull;
import java.util.List;
@ -17,7 +18,7 @@ public interface MergeRequestRepository {
Optional<MergeRequest> findById(Long mergeRequestId);
List<MergeRequest> findAll();
List<MergeRequestForDiscussion> findAllForDiscussion();
List<MergeRequest> findAllById(Set<Long> mergeRequestIds);

View File

@ -4,6 +4,7 @@ import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr;
import dev.struchkov.bot.gitlab.context.domain.MergeRequestState;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion;
import lombok.NonNull;
import java.util.List;
@ -25,14 +26,12 @@ public interface MergeRequestsService {
*/
Set<IdAndStatusPr> getAllId(Set<MergeRequestState> statuses);
List<MergeRequest> getAll();
List<MergeRequestForDiscussion> getAllForDiscussion();
ExistContainer<MergeRequest, Long> existsById(@NonNull Set<Long> mergeRequestIds);
List<MergeRequest> createAll(List<MergeRequest> newMergeRequests);
void deleteAllById(@NonNull Set<Long> mergeRequestIds);
List<MergeRequest> getAllByReviewerId(@NonNull Long personId);
void cleanOld();

View File

@ -3,7 +3,7 @@ package dev.struchkov.bot.gitlab.core.service.impl;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.PersonInformation;
import dev.struchkov.bot.gitlab.context.domain.entity.Discussion;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion;
import dev.struchkov.bot.gitlab.context.domain.entity.Note;
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify;
@ -88,7 +88,7 @@ public class DiscussionServiceImpl implements DiscussionService {
final Note firstNote = discussion.getFirstNote();
final List<Note> notes = discussion.getNotes();
final MergeRequest mergeRequest = discussion.getMergeRequest();
final MergeRequestForDiscussion mergeRequest = discussion.getMergeRequest();
final DiscussionNewNotify.DiscussionNewNotifyBuilder notifyBuilder = DiscussionNewNotify.builder()
.mrName(mergeRequest.getTitle())
.authorName(firstNote.getAuthor().getName())
@ -117,6 +117,7 @@ public class DiscussionServiceImpl implements DiscussionService {
}
@Override
@Transactional
public Discussion update(@NonNull Discussion discussion) {
final Discussion oldDiscussion = repository.findById(discussion.getId())
.orElseThrow(notFoundException("Дискуссия не найдена"));
@ -212,7 +213,7 @@ public class DiscussionServiceImpl implements DiscussionService {
private void updateTask(Note note, Note oldNote) {
if (isResolved(note, oldNote)) {
final MergeRequest mergeRequest = oldNote.getDiscussion().getMergeRequest();
final MergeRequestForDiscussion mergeRequest = oldNote.getDiscussion().getMergeRequest();
final List<Discussion> discussions = getAllByMergeRequestId(mergeRequest.getId())
.stream()
.filter(discussion -> Objects.nonNull(discussion.getResponsible()))
@ -247,7 +248,7 @@ public class DiscussionServiceImpl implements DiscussionService {
public void answer(@NonNull String discussionId, @NonNull String text) {
final Discussion discussion = repository.findById(discussionId)
.orElseThrow(notFoundException("Дисскусия {0} не найдена", discussionId));
final MergeRequest mergeRequest = discussion.getMergeRequest();
final MergeRequestForDiscussion mergeRequest = discussion.getMergeRequest();
final Long projectId = mergeRequest.getProjectId();
final String requestUrl = MessageFormat.format(gitlabProperty.getNewNoteUrl(), projectId, mergeRequest.getTwoId(), discussion.getId(), text);

View File

@ -8,6 +8,7 @@ import dev.struchkov.bot.gitlab.context.domain.PersonInformation;
import dev.struchkov.bot.gitlab.context.domain.ReviewerChanged;
import dev.struchkov.bot.gitlab.context.domain.entity.Discussion;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion;
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import dev.struchkov.bot.gitlab.context.domain.entity.Project;
import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.ConflictPrNotify;
@ -212,8 +213,8 @@ public class MergeRequestsServiceImpl implements MergeRequestsService {
}
@Override
public List<MergeRequest> getAll() {
return repository.findAll();
public List<MergeRequestForDiscussion> getAllForDiscussion() {
return repository.findAllForDiscussion();
}
@Override
@ -237,12 +238,6 @@ public class MergeRequestsServiceImpl implements MergeRequestsService {
.toList();
}
@Override
@Transactional
public void deleteAllById(@NonNull Set<Long> mergeRequestIds) {
repository.deleteByIds(mergeRequestIds);
}
@Override
public List<MergeRequest> getAllByReviewerId(@NonNull Long personId) {
return repository.findAllByReviewerId(personId);

View File

@ -2,7 +2,7 @@ package dev.struchkov.bot.gitlab.core.service.parser;
import dev.struchkov.bot.gitlab.context.domain.ExistContainer;
import dev.struchkov.bot.gitlab.context.domain.entity.Discussion;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion;
import dev.struchkov.bot.gitlab.context.domain.entity.Note;
import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import dev.struchkov.bot.gitlab.context.service.DiscussionService;
@ -73,16 +73,16 @@ public class DiscussionParser {
*/
public void scanNewDiscussion() {
log.debug("Старт обработки новых дискуссий");
final List<MergeRequest> mergeRequests = mergeRequestsService.getAll();
final List<MergeRequestForDiscussion> mergeRequests = mergeRequestsService.getAllForDiscussion();
for (MergeRequest mergeRequest : mergeRequests) {
for (MergeRequestForDiscussion mergeRequest : mergeRequests) {
processingMergeRequest(mergeRequest);
}
log.debug("Конец обработки новых дискуссий");
}
private void processingMergeRequest(MergeRequest mergeRequest) {
private void processingMergeRequest(MergeRequestForDiscussion mergeRequest) {
int page = 1;
List<DiscussionJson> discussionJson = getDiscussionJson(mergeRequest, page);
@ -94,7 +94,7 @@ public class DiscussionParser {
}
}
private void createNewDiscussion(List<DiscussionJson> discussionJson, MergeRequest mergeRequest) {
private void createNewDiscussion(List<DiscussionJson> discussionJson, MergeRequestForDiscussion mergeRequest) {
final Set<String> discussionIds = discussionJson.stream()
.map(DiscussionJson::getId)
.collect(Collectors.toUnmodifiableSet());
@ -200,14 +200,14 @@ public class DiscussionParser {
);
}
private List<DiscussionJson> getDiscussionJson(MergeRequest mergeRequest, int page) {
private List<DiscussionJson> getDiscussionJson(MergeRequestForDiscussion mergeRequest, int page) {
return HttpParse.request(MessageFormat.format(gitlabProperty.getDiscussionsUrl(), mergeRequest.getProjectId(), mergeRequest.getTwoId(), page))
.header(ACCEPT)
.header(H_PRIVATE_TOKEN, personProperty.getToken())
.executeList(DiscussionJson.class);
}
private Consumer<Note> createNoteLink(MergeRequest mergeRequest) {
private Consumer<Note> createNoteLink(MergeRequestForDiscussion mergeRequest) {
return note -> {
final String url = MessageFormat.format(
gitlabProperty.getNoteUrl(),

View File

@ -3,7 +3,9 @@ package dev.struchkov.bot.gitlab.data.impl;
import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr;
import dev.struchkov.bot.gitlab.context.domain.MergeRequestState;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion;
import dev.struchkov.bot.gitlab.context.repository.MergeRequestRepository;
import dev.struchkov.bot.gitlab.data.jpa.MergeRequestForDiscussionJpaRepository;
import dev.struchkov.bot.gitlab.data.jpa.MergeRequestJpaRepository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@ -18,6 +20,7 @@ import java.util.Set;
public class MergeRequestRepositoryImpl implements MergeRequestRepository {
private final MergeRequestJpaRepository jpaRepository;
private final MergeRequestForDiscussionJpaRepository forDiscussionJpaRepository;
@Override
public Set<IdAndStatusPr> findAllIdByStateIn(@NonNull Set<MergeRequestState> statuses) {
@ -35,8 +38,8 @@ public class MergeRequestRepositoryImpl implements MergeRequestRepository {
}
@Override
public List<MergeRequest> findAll() {
return jpaRepository.findAll();
public List<MergeRequestForDiscussion> findAllForDiscussion() {
return forDiscussionJpaRepository.findAll();
}
@Override

View File

@ -0,0 +1,12 @@
package dev.struchkov.bot.gitlab.data.jpa;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
/**
* @author upagge [31.01.2020]
*/
public interface MergeRequestForDiscussionJpaRepository extends JpaRepositoryImplementation<MergeRequestForDiscussion, Long> {
}