Conversation
Walkthrough폴더 응답 DTO에 Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant FolderService
participant FolderItemRepository
participant FolderResponse as FolderResponse Factory
Client->>FolderService: getFolders()
FolderService->>FolderService: 폴더 목록 조회
loop 각 폴더별
FolderService->>FolderItemRepository: countByFolderFolderId(folderId)
FolderItemRepository-->>FolderService: itemCount (Long)
FolderService->>FolderResponse: from(folder, itemCount)
FolderResponse-->>FolderService: FolderResponse with itemCount
end
FolderService-->>Client: List<FolderResponse> (itemCount 포함)
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 📋 리뷰 의견좋은 점:
주의할 점:
전체적으로는 간단하고 명확한 변경으로, 리뷰 부담이 적습니다! 👍 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In
`@src/main/java/com/finsight/finsight/domain/storage/domain/service/FolderService.java`:
- Around line 35-43: getFolders in FolderService triggers an N+1 by calling
FolderItemRepository.countByFolderFolderId for each FolderEntity; add a bulk
counting query to FolderItemRepository (e.g. a method like countByFolderIds that
returns folderId->count for a list of IDs) and change FolderService.getFolders
to: fetch folders, collect their folderIds, call the new bulk count method once,
map counts into a lookup, then stream folders and call
FolderResponse.from(folder, itemCountLookup.get(folder.getFolderId())); this
reduces queries to two and avoids the per-folder count calls.
| public List<FolderResponse> getFolders(Long userId, FolderType folderType) { | ||
| List<FolderEntity> folders = folderRepository.findByUserUserIdAndFolderTypeOrderBySortOrderAsc(userId, folderType); | ||
| return folders.stream() | ||
| .map(FolderResponse::from) | ||
| .map(folder -> { | ||
| long itemCount = folderItemRepository.countByFolderFolderId(folder.getFolderId()); | ||
| return FolderResponse.from(folder, itemCount); | ||
| }) | ||
| .toList(); | ||
| } |
There was a problem hiding this comment.
현재 getFolders에서 폴더 리스트를 조회한 후, 각 폴더마다 countByFolderFolderId를 개별 호출하고 있습니다. 사용자가 폴더를 10개 가지고 있다면, 폴더 목록 조회 1회 + 카운트 조회 10회 = 총 11번의 쿼리가 실행됩니다. updateFolderOrder (Line 141)도 getFolders를 호출하므로 동일한 문제가 전파됩니다.
권장 해결 방법: 한 번의 쿼리로 모든 폴더의 아이템 개수를 한꺼번에 가져오는 방식으로 개선할 수 있습니다.
🔧 제안하는 수정 방법
1단계: FolderItemRepository에 벌크 카운트 쿼리 추가
`@Query`("SELECT fi.folder.folderId, COUNT(fi) FROM FolderItemEntity fi WHERE fi.folder.folderId IN :folderIds GROUP BY fi.folder.folderId")
List<Object[]> countByFolderIds(`@Param`("folderIds") List<Long> folderIds);2단계: FolderService.getFolders 수정
public List<FolderResponse> getFolders(Long userId, FolderType folderType) {
List<FolderEntity> folders = folderRepository.findByUserUserIdAndFolderTypeOrderBySortOrderAsc(userId, folderType);
+
+ List<Long> folderIds = folders.stream()
+ .map(FolderEntity::getFolderId)
+ .toList();
+
+ Map<Long, Long> itemCountMap = folderItemRepository.countByFolderIds(folderIds).stream()
+ .collect(Collectors.toMap(
+ row -> (Long) row[0],
+ row -> (Long) row[1]
+ ));
+
return folders.stream()
- .map(folder -> {
- long itemCount = folderItemRepository.countByFolderFolderId(folder.getFolderId());
- return FolderResponse.from(folder, itemCount);
- })
+ .map(folder -> FolderResponse.from(folder, itemCountMap.getOrDefault(folder.getFolderId(), 0L)))
.toList();
}이렇게 하면 폴더 개수와 관계없이 항상 2번의 쿼리로 처리됩니다.
As per coding guidelines, "JPA 사용 시 N+1 문제나 불필요한 쿼리가 발생하지 않는지 체크한다."
🤖 Prompt for AI Agents
In
`@src/main/java/com/finsight/finsight/domain/storage/domain/service/FolderService.java`
around lines 35 - 43, getFolders in FolderService triggers an N+1 by calling
FolderItemRepository.countByFolderFolderId for each FolderEntity; add a bulk
counting query to FolderItemRepository (e.g. a method like countByFolderIds that
returns folderId->count for a list of IDs) and change FolderService.getFolders
to: fetch folders, collect their folderIds, call the new bulk count method once,
map counts into a lookup, then stream folders and call
FolderResponse.from(folder, itemCountLookup.get(folder.getFolderId())); this
reduces queries to two and avoids the per-folder count calls.
📍 PR 타입 (하나 이상 선택)
❗️ 관련 이슈 링크
Close #72
📌 개요
사용자 폴더 조회 응답에 저장된 뉴스, 단어 개수 추가
🔁 변경 사항
Folder Response itemCount 필드 추가
Folder Repository 메서드 ( countByFolderFolderId ) 추가
Folder Service 응답 코드 수정
📸 스크린샷
👀 기타 더 이야기해볼 점
✅ 체크 리스트
Summary by CodeRabbit
릴리스 노트