-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: 232 refactor get apicourses 지연시간 줄이기 #234
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "232-refactor-get-apicourses-\uC9C0\uC5F0\uC2DC\uAC04-\uC904\uC774\uAE30"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,9 @@ | |
| import java.time.LocalDateTime; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.stream.Collectors; | ||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
@@ -127,4 +129,12 @@ public int getClearRank(UserCourse userCourse) { | |
| public long getUserCourseCountByUserCourseState(UserCourseState userCourseState) { | ||
| return userCourseRepository.countByUserCourseState(userCourseState); | ||
| } | ||
|
|
||
| public Map<Long, UserCourseState> getLatestUserCourseStates(User user, List<Long> courseIds) { | ||
| List<UserCourse> latestUserCourses = | ||
| userCourseRepository.findLatestUserCoursesByUserAndCourseIds(user, courseIds); | ||
|
|
||
| return latestUserCourses.stream() | ||
| .collect(Collectors.toMap(uc -> uc.getCourse().getId(), UserCourse::getUserCourseState)); | ||
| } | ||
|
Comment on lines
+133
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 빈 목록 대비 및 중복 키 안전성 보강 필요 (빈
다음과 같이 조치 제안드립니다. @Service
@AllArgsConstructor
public class UserCourseService {
@@
- public Map<Long, UserCourseState> getLatestUserCourseStates(User user, List<Long> courseIds) {
+ @Transactional(readOnly = true)
+ public Map<Long, UserCourseState> getLatestUserCourseStates(User user, List<Long> courseIds) {
+ if (courseIds == null || courseIds.isEmpty()) {
+ return java.util.Collections.emptyMap();
+ }
List<UserCourse> latestUserCourses =
userCourseRepository.findLatestUserCoursesByUserAndCourseIds(user, courseIds);
- return latestUserCourses.stream()
- .collect(Collectors.toMap(uc -> uc.getCourse().getId(), UserCourse::getUserCourseState));
+ return latestUserCourses.stream()
+ .collect(
+ Collectors.toMap(
+ uc -> uc.getCourse().getId(),
+ UserCourse::getUserCourseState,
+ (left, right) -> left // 레포 타이브레이커 적용 전 임시 병합
+ ));
}부가 제안(선택): 레포 쿼리를 코스 ID와 상태만 프로젝션으로 가져오면(예: 🤖 Prompt for AI Agents |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
타이브레이커 없는 상관 서브쿼리 → 중복 행 가능성;
toMap충돌/비결정성 위험createdAt = MAX(createdAt)만으로는 동률 시 다건 반환 가능성이 있습니다. 서비스의toMap병합으로 임시 방어하더라도, 근본적으로 쿼리에서 단일 행이 선택되도록 타이브레이커를 넣어주세요. 일반적으로 단조 증가 PK(id)를 사용하는 것이 간단하고 DB에 독립적입니다.보완 제안:
IN :courseIds는 빈 리스트에서 예외가 나므로, 상위 서비스에서 빈 리스트 조기 리턴(본 리뷰의 Service 코멘트)으로 방지하세요.(user_id, course_id, id)또는(user_id, course_id, created_at)복합 인덱스를 고려하세요(아래 참조).인덱스 제안:
ALTER TABLE user_course ADD INDEX idx_user_course_latest (user_id, course_id, id);📝 Committable suggestion
🤖 Prompt for AI Agents