-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: 페이징 방식을 오프셋 기반에서 커서 기반으로 변경한다. #44
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
709e98f
feat: 커서 기반 페이징을 위해 dto를 수정 및 추가한다.
min429 0bc2275
feat: 커서 기반 페이징을 위해 관련 controller, service를 수정한다.
min429 4e383db
feat: 커서 기반 페이징 방식으로 쿼리를 재구성한다.
min429 c520ab5
refactor: 메소드 위치를 변경하고 중복 메소드를 추출한다.
min429 01f4701
feat: Controller 메소드 매핑 방식을 변경하고 dto에 nullable을 추가한다.(swagger에 명시)
min429 99c6d26
Merge branch 'main' into ISSUE-208
min429 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
This file contains 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 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 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 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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/dnd/accompany/domain/accompany/api/dto/FindSlicesResult.java
This file contains 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,34 @@ | ||
package com.dnd.accompany.domain.accompany.api.dto; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.SliceImpl; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
@SuperBuilder | ||
public abstract class FindSlicesResult { | ||
private final String cursor; | ||
|
||
public static <T extends FindSlicesResult> String getLastCursor(List<T> result) { | ||
return result.get(result.size() - 1).getCursor(); | ||
} | ||
|
||
public static <T extends FindSlicesResult> Slice<T> createSlice(int size, List<T> content) { | ||
boolean hasNext = content.size() > size; | ||
|
||
if (hasNext) { | ||
content.remove(content.size() - 1); | ||
} | ||
|
||
return new SliceImpl<>(content, PageRequest.of(0, size), hasNext); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/dnd/accompany/domain/accompany/api/dto/PageRequest.java
This file contains 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,42 @@ | ||
package com.dnd.accompany.domain.accompany.api.dto; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.boot.context.properties.bind.DefaultValue; | ||
import org.springframework.lang.Nullable; | ||
|
||
import com.querydsl.core.BooleanBuilder; | ||
import com.querydsl.core.types.dsl.DateTimePath; | ||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.core.types.dsl.NumberPath; | ||
import com.querydsl.core.types.dsl.NumberTemplate; | ||
import com.querydsl.core.types.dsl.StringTemplate; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
public record PageRequest( | ||
@Schema(nullable = true) String cursor, | ||
@Schema(nullable = true) Integer size | ||
) { | ||
|
||
public PageRequest { | ||
if (size == null) { | ||
size = 10; | ||
} | ||
} | ||
|
||
public static BooleanBuilder cursorCondition(String cursor, DateTimePath<LocalDateTime> updatedAt, NumberPath<Long> id) { | ||
BooleanBuilder builder = new BooleanBuilder(); | ||
|
||
if (cursor == null) return builder; | ||
|
||
StringTemplate nextCursor = Expressions.stringTemplate( | ||
"CONCAT(DATE_FORMAT({0}, '%Y%m%d%H%i%S'), LPAD(CAST({1} AS STRING), 6, '0'))", | ||
updatedAt, | ||
id | ||
); | ||
builder.and(nextCursor.lt(cursor)); | ||
|
||
return builder; | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
public record PageResponse<T>( | ||
boolean hasNext, | ||
List<T> data | ||
List<T> data, | ||
String cursor | ||
) { | ||
} |
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.
Q. updatedA을 사용하는 이유가 있을까요 ?
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.
커서 페이징은 정렬할 컬럼에 중복된 값이 존재하면 안되고, 순차적이어야 하기 때문에 updateAt 기준으로 정렬하면서 중복을 피하기 위해 boardId와 updateAt을 합쳐서 cursor를 만들었습니다.
정렬 기준이 updatedAt -> id 순서인데, 만약 cursor를 id로만 할 경우, id값을 기준으로 정렬되어있지 않기 때문에 값을 제대로 가져오지 못할 수도 있다고 판단했습니다.
예를들어, 데이터가 다음 그림과 같은경우, 만약 id만 cursor로 쓴다면 size가 1일때, id 3을 가져오고 나서 그 뒤의 값들을 읽을 수 없게 된다고 생각했습니다.
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.
생성일 기준으로 정렬하는거면, id값도 어짜피 생성일 순서랑 같을 것 같아서 cursor를 id로 해도 상관없을 것 같은데, 업데이트 날짜 기준으로 정렬할 때는 두가지 다 고려해야하지 않을까 싶네요