Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

템플릿 생성, 수정 쿼리 성능 개선 #715

Merged
merged 6 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/src/main/java/codezap/tag/domain/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;

import codezap.global.auditing.BaseTimeEntity;
import lombok.AccessLevel;
Expand All @@ -18,6 +20,7 @@
@AllArgsConstructor
@Getter
@EqualsAndHashCode(of = "id", callSuper = false)
@Table(indexes = @Index(name = "idx_tag_name", columnList = "name"))
public class Tag extends BaseTimeEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface TagRepository {

Optional<Tag> findByName(String name);

List<String> findNameByNamesIn(List<String> names);
List<Tag> findByNameIn(List<String> names);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다음에 테스트 추가해주세요~!!


Tag save(Tag tag);

Expand Down
24 changes: 11 additions & 13 deletions backend/src/main/java/codezap/tag/service/TagService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codezap.tag.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
Expand All @@ -25,25 +26,22 @@ public class TagService {

@Transactional
public void createTags(Template template, List<String> tagNames) {
List<String> existingTags = tagRepository.findNameByNamesIn(tagNames);
templateTagRepository.saveAll(
existingTags.stream()
.map(tagRepository::fetchByName)
.map(tag -> new TemplateTag(template, tag))
.toList()
);
List<Tag> existingTags = new ArrayList<>(tagRepository.findByNameIn(tagNames));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

방어적 복사를 하신건가요? 이유가 궁금 ~
나중에 얘기해봐요 왜냐면 전 다 안했거든요 ~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existingTags 에 새로운 원소를 추가하는 로직이 존재해요.
findByNameIn 의 return 값이 UnmodifiableList 라서 수정 가능한 리스트로 변경해 주는 로직입니다!

List<String> existNames = existingTags.stream()
.map(Tag::getName)
.toList();
Comment on lines +29 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나중에 repository로 부터 name만 받도록 수정해서 커버링 인덱스를 사용해봐요 ~


List<Tag> newTags = tagRepository.saveAll(
tagNames.stream()
.filter(tagName -> !existingTags.contains(tagName))
.filter(name -> !existNames.contains(name))
.map(Tag::new)
.toList()
);
templateTagRepository.saveAll(
newTags.stream()
.map(tag -> new TemplateTag(template, tag))
.toList()
);
existingTags.addAll(newTags);

for (Tag existingTag : existingTags) {
templateTagRepository.save(new TemplateTag(template, existingTag));
}
}

public List<Tag> findAllByTemplate(Template template) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;
Expand Down Expand Up @@ -36,12 +37,12 @@ private static class TemplateTagId implements Serializable {
@EmbeddedId
private TemplateTagId id;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("templateId")
@JoinColumn(name = "template_id")
private Template template;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("tagId")
@JoinColumn(name = "tag_id")
private Tag tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX idx_tag_name ON tag(name);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -90,17 +89,4 @@ void findByNameFailByNotExistsId() {
assertThat(actual).isEmpty();
}
}

@Test
@DisplayName("이름 목록 조회 성공 : 이름 목록에 포함된 모든 태그를 반환한다.")
void findNameByNamesIn() {
tagRepository.save(new Tag("태그1"));
tagRepository.save(new Tag("태그2"));
tagRepository.save(new Tag("태그3"));

List<String> actual = tagRepository.findNameByNamesIn(List.of("태그1", "태그3", "태그4"));

assertThat(actual).hasSize(2)
.containsExactly("태그1", "태그3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ public Tag fetchById(Long id) {
.orElseThrow(() -> new CodeZapException(HttpStatus.NOT_FOUND, "식별자 " + id + "에 해당하는 태그가 존재하지 않습니다."));
}

@Override
public List<String> findNameByNamesIn(List<String> names) {
return names.stream()
.filter(this::existsByName)
.toList();
}

private boolean existsByName(String name) {
return tags.stream().anyMatch(tag -> Objects.equals(tag.getName(), name));
}

@Override
public Tag fetchByName(String name) {
return findByName(name).orElseThrow(
Expand All @@ -52,6 +41,11 @@ public Optional<Tag> findByName(String name) {
return tags.stream().filter(tag -> Objects.equals(tag.getName(), name)).findFirst();
}

@Override
public List<Tag> findByNameIn(List<String> names) {
return List.of();
}

@Override
public <S extends Tag> List<S> saveAll(Iterable<S> entities) {
entities.forEach(this::save);
Expand Down