-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: 채팅방 관련 n + 1 문제 개선 #477
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
Merged
Gyuhyeok99
merged 2 commits into
solid-connection:develop
from
Gyuhyeok99:refactor/458-chatroom-n-plus-one-optimization
Aug 28, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/com/example/solidconnection/chat/dto/ChatRoomData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.example.solidconnection.chat.dto; | ||
|
|
||
| import com.example.solidconnection.chat.domain.ChatMessage; | ||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public record ChatRoomData( | ||
| Map<Long, ChatMessage> latestMessages, | ||
| Map<Long, Long> unreadCounts, | ||
| Map<Long, SiteUser> partnerUsers | ||
| ) { | ||
|
|
||
| public static ChatRoomData from(List<ChatMessage> latestMessages, | ||
| List<UnreadCountDto> unreadCounts, | ||
| List<SiteUser> partnerUsers) { | ||
| return new ChatRoomData( | ||
| latestMessages.stream().collect(Collectors.toMap(msg -> msg.getChatRoom().getId(), msg -> msg)), | ||
| unreadCounts.stream().collect(Collectors.toMap(UnreadCountDto::chatRoomId, UnreadCountDto::count)), | ||
| partnerUsers.stream().collect(Collectors.toMap(SiteUser::getId, user -> user)) | ||
| ); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/example/solidconnection/chat/dto/UnreadCountDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.solidconnection.chat.dto; | ||
|
|
||
| public record UnreadCountDto( | ||
| long chatRoomId, | ||
| long count | ||
| ) { | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
💡 Verification agent
🧩 Analysis chain
최신 메시지 선정 기준을 id → createdAt로 통일해 의미적 정합성 보장.
ChatMessage의 @GeneratedValue 전략과 createdAt 세터·감사 필드 전략을 확인해 일관된 시간 정렬이 가능한지 점검해 주세요.
아래와 같이 쿼리를 createdAt 기준으로 바꾸는 것을 권장합니다.
🏁 Script executed:
Length of output: 37996
ChatMessageRepository의 최신 메시지 선별 기준을 createdAt으로 통일하기
쿼리 기준 통일
createdAt 필드 설정 확인
ZonedDateTime.now(UTC).truncatedTo(MICROS)로 자동 설정됩니다.빈 리스트 처리
동률 처리
📝 Committable suggestion
🤖 Prompt for AI Agents