-
Notifications
You must be signed in to change notification settings - Fork 3
[Feat] #257: 커서페이징 도입 #258
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
Open
oneeee822
wants to merge
4
commits into
develop
Choose a base branch
from
feature/257
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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
Binary file not shown.
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 |
|---|---|---|
|
|
@@ -41,3 +41,4 @@ out/ | |
| load-test.js | ||
|
|
||
| .env | ||
| **/.DS_Store | ||
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
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
project/src/main/java/com/edison/project/common/response/CursorPageInfo.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,7 @@ | ||
| package com.edison.project.common.response; | ||
|
|
||
| public record CursorPageInfo( | ||
| Long nextCursorId, | ||
| boolean hasNext, | ||
| int size | ||
| ) implements Pagination {} |
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
5 changes: 5 additions & 0 deletions
5
project/src/main/java/com/edison/project/common/response/Pagination.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,5 @@ | ||
| package com.edison.project.common.response; | ||
|
|
||
| public interface Pagination { | ||
|
|
||
| } |
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
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import com.edison.project.common.exception.GeneralException; | ||
| import com.edison.project.common.response.Response; | ||
| import com.edison.project.common.response.CursorPageInfo; | ||
| import com.edison.project.common.response.PageInfo; | ||
| import com.edison.project.common.status.ErrorStatus; | ||
| import com.edison.project.common.status.SuccessStatus; | ||
|
|
@@ -25,6 +26,7 @@ | |
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
@@ -83,6 +85,32 @@ public ResponseEntity<Response> getBubblesByMember(CustomUserPrincipal userPrinc | |
| return Response.onSuccess(SuccessStatus._OK, pageInfo, bubbles); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<Response> getCursorBubblesByMember(CustomUserPrincipal userPrincipal, Long cursorId, Pageable pageable){ | ||
| Long memberId = userPrincipal.getMemberId(); | ||
| Slice<Bubble> bubbleSlice; | ||
|
|
||
| if(cursorId == null){ | ||
| bubbleSlice = bubbleRepository.findByMember_MemberIdAndIsTrashedFalseOrderByBubbleIdDesc(memberId, pageable); | ||
| } | ||
| else{ | ||
| bubbleSlice = bubbleRepository.findByMember_MemberIdAndIsTrashedFalseAndBubbleIdLessThanOrderByBubbleIdDesc(memberId, cursorId, pageable); | ||
| } | ||
|
|
||
| List<BubbleResponseDto.SyncResultDto> bubbles = bubbleSlice.getContent().stream() | ||
| .map(this::convertToBubbleResponseDto) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| Long nextCursorId = null; | ||
| if(!bubbles.isEmpty()){ | ||
| nextCursorId = bubbleSlice.getContent().get(bubbles.size()-1).getBubbleId(); | ||
| } | ||
|
|
||
| CursorPageInfo pageInfo = new CursorPageInfo(cursorId, bubbleSlice.hasNext(), pageable.getPageSize()); | ||
|
Collaborator
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. 위에서 계산한 nextCursorId가 들어가야 할 것 같습니다.
|
||
|
|
||
| return Response.onSuccess(SuccessStatus._OK, pageInfo, bubbles); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<Response> getDeletedBubbles(CustomUserPrincipal userPrincipal, Pageable pageable) { | ||
| Page<Bubble> bubblePage = bubbleRepository.findByMember_MemberIdAndIsTrashedTrue(userPrincipal.getMemberId(), pageable); | ||
|
|
@@ -343,7 +371,7 @@ public ResponseEntity<Response> vectorizeAllBubbles(CustomUserPrincipal userPrin | |
| List<Bubble> bubbles = bubbleRepository.findByMember_MemberIdAndIsTrashedFalse(member.getMemberId()); | ||
|
|
||
| if (bubbles.isEmpty()) { | ||
| return Response.onSuccess(SuccessStatus._OK, null, "No bubbles found"); | ||
| return Response.onSuccess(SuccessStatus._OK, (PageInfo)null, "No bubbles found"); | ||
| } | ||
|
|
||
| List<BubbleResponseDto.VectorizeResultDto> results = new ArrayList<>(); | ||
|
|
||
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,27 @@ | ||
| spring: | ||
| datasource: | ||
| url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;REFERENTIAL_INTEGRITY=FALSE | ||
| driver-class-name: org.h2.Driver | ||
| username: sa | ||
| password: | ||
|
|
||
| jpa: | ||
| database-platform: com.edison.project.domain.bubble.H2TestDialect | ||
| hibernate: | ||
| ddl-auto: create-drop | ||
| properties: | ||
| hibernate: | ||
| show_sql: true | ||
| format_sql: true | ||
| highlight_sql: true | ||
| sql: | ||
| init: | ||
| mode: never | ||
| AWS_ACCESS_KEY: dummy-access-key | ||
| AWS_SECRET_KEY: dummy-secret-key | ||
|
|
||
| cloud: | ||
| aws: | ||
| credentials: | ||
| access-key: dummy-access-key | ||
| secret-key: dummy-secret-key |
1 change: 0 additions & 1 deletion
1
project/src/test/java/com/edison/project/ProjectApplicationTests.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
103 changes: 103 additions & 0 deletions
103
project/src/test/java/com/edison/project/domain/bubble/BubblePaginationPerformanceTest.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,103 @@ | ||
| package com.edison.project.domain.bubble; | ||
|
|
||
| import com.edison.project.domain.bubble.repository.BubbleRepository; | ||
| import java.sql.PreparedStatement; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.springframework.jdbc.core.BatchPreparedStatementSetter; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.util.StopWatch; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Sort; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @SpringBootTest | ||
| @Transactional | ||
| @ActiveProfiles("test") | ||
| @Tag("ignore") | ||
| public class BubblePaginationPerformanceTest { | ||
|
|
||
| @Autowired | ||
| private BubbleRepository bubbleRepository; | ||
|
|
||
| @Autowired | ||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| private Long testMemberId = 1L; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| System.out.println("데이터 세팅 시작"); | ||
|
|
||
| jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY FALSE"); | ||
|
|
||
| String sql = "INSERT INTO bubble " + | ||
| "(member_id, title, content, is_deleted, is_trashed, created_at, updated_at) " + | ||
| "VALUES (?, ?, ?, ?, ?, NOW(), NOW())"; | ||
|
|
||
| jdbcTemplate.batchUpdate(sql, | ||
| new BatchPreparedStatementSetter() { | ||
| @Override | ||
| public void setValues(PreparedStatement ps, int i) throws java.sql.SQLException { | ||
| ps.setLong(1, testMemberId); | ||
| ps.setString(2, "테스트용 버블 제목 " + i); | ||
| ps.setString(3, "테스트용 버블 내용 " + i); | ||
| ps.setBoolean(4, false); | ||
| ps.setBoolean(5, false); | ||
| } | ||
| @Override | ||
| public int getBatchSize() { | ||
| return 100000; | ||
| } | ||
| }); | ||
|
|
||
| jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY TRUE"); | ||
|
|
||
| System.out.println("10만 건 데이터 세팅"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("오프셋 페이징 vs 커서 페이징 성능 비교") | ||
| void comparePaginationPerformance() { | ||
| int targetPage = 4900; // 4900번째 페이지 조회 | ||
| int size = 20; | ||
|
|
||
| // 오프셋 페이징 측정 | ||
| StopWatch offsetStopWatch = new StopWatch(); | ||
| offsetStopWatch.start(); | ||
| PageRequest offsetRequest = PageRequest.of(targetPage, size, Sort.by(Sort.Direction.DESC, "bubbleId")); | ||
|
|
||
| System.out.println("\n======================================================="); | ||
| System.out.println("[오프셋 페이징 쿼리 시작] - 쿼리 2개(데이터+COUNT)"); | ||
| System.out.println("======================================================="); | ||
|
|
||
| bubbleRepository.findByMember_MemberIdAndIsTrashedFalse(testMemberId, offsetRequest); | ||
| offsetStopWatch.stop(); | ||
|
|
||
| // 커서 페이징 측정 | ||
| // 4900 페이지의 첫 번째 항목 커서 값 임의로 계산 | ||
| Long cursorId = 2000L; | ||
|
|
||
| StopWatch cursorStopWatch = new StopWatch(); | ||
| cursorStopWatch.start(); | ||
| PageRequest cursorRequest = PageRequest.of(0, size); | ||
|
|
||
| System.out.println("\n======================================================="); | ||
| System.out.println("[커서 페이징 쿼리 시작] - 쿼리 1개"); | ||
| System.out.println("======================================================="); | ||
|
|
||
| bubbleRepository.findByMember_MemberIdAndIsTrashedFalseAndBubbleIdLessThanOrderByBubbleIdDesc( | ||
| testMemberId, cursorId, cursorRequest); | ||
| cursorStopWatch.stop(); | ||
|
|
||
| System.out.println("=== 테스트 결과 ==="); | ||
| System.out.println("오프셋 페이징 소요 시간: " + offsetStopWatch.getTotalTimeMillis() + " ms"); | ||
| System.out.println("커서 페이징 소요 시간: " + cursorStopWatch.getTotalTimeMillis() + " ms"); | ||
|
|
||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
project/src/test/java/com/edison/project/domain/bubble/H2TestDialect.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,20 @@ | ||
| package com.edison.project.domain.bubble; | ||
|
|
||
| import org.hibernate.boot.model.TypeContributions; | ||
| import org.hibernate.dialect.H2Dialect; | ||
| import org.hibernate.service.ServiceRegistry; | ||
| import org.hibernate.type.SqlTypes; | ||
| import org.hibernate.type.descriptor.sql.internal.DdlTypeImpl; | ||
|
|
||
| // 테스트 환경의 H2 DB를 위한 전용 방언(Dialect) 설정 | ||
| public class H2TestDialect extends H2Dialect { | ||
|
|
||
| @Override | ||
| public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { | ||
| super.contributeTypes(typeContributions, serviceRegistry); | ||
|
|
||
| // 핵심: H2가 모르는 VECTOR 타입이 들어오면, 무조건 "varbinary" (이진 데이터) 타입으로 속여서 테이블을 만들라고 지시합니다. | ||
| typeContributions.getTypeConfiguration().getDdlTypeRegistry() | ||
| .addDescriptor(new DdlTypeImpl(SqlTypes.VECTOR, "varbinary", this)); | ||
| } | ||
| } |
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.
오타낫더요..