Skip to content

Commit c3b472d

Browse files
committed
⚡️ [Perf] FastAPI 클러스터링 결과 저장 로직 성능 개선
반복적인 DB저장 호출 최소화
1 parent 82947e8 commit c3b472d

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/main/java/com/going/server/global/temp/service/FastApiService.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.going.server.domain.word.entity.Word;
88
import com.going.server.domain.word.repository.WordRepository;
99
import jakarta.annotation.PostConstruct;
10+
import java.util.ArrayList;
1011
import lombok.RequiredArgsConstructor;
1112
import lombok.extern.slf4j.Slf4j;
1213
import org.springframework.beans.factory.annotation.Value;
@@ -54,52 +55,51 @@ public String callFastApi() {
5455

5556
@Profile("!test")
5657
public void setCluster() {
57-
// FastAPI 요청 데이터 (필요시 변경)
5858
Map<String, Object> requestData = Map.of("input_text", "클러스터링할 데이터 예제");
5959

60-
// FastAPI 응답 받기
6160
Map<String, Object> response = webClient.post()
62-
.uri(baseUrl + "/api/cluster") // FastAPI 클러스터링 엔드포인트
61+
.uri(baseUrl + "/api/cluster")
6362
.bodyValue(requestData)
6463
.retrieve()
6564
.bodyToMono(Map.class)
6665
.block();
6766

68-
//모든 클러스터링 결과 저장
6967
List<Map<String, Object>> clusters = (List<Map<String, Object>>) response.get("clusters");
70-
//클러스터링 결과 이미지 저장
7168
String imageUrl = response.get("image_url").toString();
7269

70+
// 병렬처리를 위한 리스트
71+
List<Cluster> clusterEntities = new ArrayList<>();
72+
List<Word> wordEntities = new ArrayList<>();
73+
List<Sentence> sentenceEntities = new ArrayList<>();
74+
7375
for (Map<String, Object> cluster : clusters) {
7476
Map<String, List<String>> wordSentences = (Map<String, List<String>>) cluster.get("word_sentences");
7577

76-
if (wordSentences.isEmpty()) continue; // 빈 클러스터 예외 처리
78+
if (wordSentences.isEmpty()) continue;
7779

78-
//첫 번째 단어를 대표 어휘로 설정
7980
String representWord = wordSentences.keySet().iterator().next();
80-
//엔티티 저장
8181
Cluster clusterEntity = Cluster.toEntity(representWord, imageUrl);
82-
//클러스터 결과 DB에 저장
83-
Cluster saveCluster = clusterRepository.save(clusterEntity);
82+
clusterEntities.add(clusterEntity);
8483

8584
for (Map.Entry<String, List<String>> entry : wordSentences.entrySet()) {
86-
//단어
87-
String word = entry.getKey();
88-
//문장들
89-
List<String> sentences = entry.getValue();
90-
//Word 엔티티 생성
91-
Word wordEntity = Word.toEntity(word, saveCluster);
92-
//DB에 저장
93-
Word saveWord = wordRepository.save(wordEntity);
94-
for (String sentence : sentences) {
95-
//Sentence 엔티티 생성
96-
Sentence sententEntity = Sentence.toEntity(sentence, saveWord);
97-
sentenceRepository.save(sententEntity);
85+
Word wordEntity = Word.toEntity(entry.getKey(), clusterEntity);
86+
wordEntities.add(wordEntity);
87+
88+
for (String sentence : entry.getValue()) {
89+
sentenceEntities.add(Sentence.toEntity(sentence, wordEntity));
9890
}
9991
}
10092
}
93+
94+
// 1. 클러스터 저장
95+
clusterRepository.saveAll(clusterEntities);
96+
97+
// 2. 클러스터 저장 후 Word에 연결된 객체들을 저장
98+
wordRepository.saveAll(wordEntities);
99+
sentenceRepository.saveAll(sentenceEntities);
101100
}
102101

102+
103103
public Word testWord(String word) {
104104
Word wordEntity = Word.toEntity(word, null);
105105
return wordRepository.save(wordEntity);

0 commit comments

Comments
 (0)