-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
feat: add a random article method to the post finder #7471
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
base: main
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/milestone 2.21.x |
public Flux<ListedPostVo> random(Integer limit) { | ||
|
||
return postPredicateResolver.getListOptions() | ||
.flatMapMany(listOptions -> client.listAll(Post.class, listOptions, Sort.unsorted())) |
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.
这看起来会查询所有文章,可能会导致查询性能问题。
@halo-dev/sig-halo 请帮忙确认是否有其他效率更高的方式,如果没有,我认为不值得添加这个方法。
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.
这里确实会有性能问题。但是目前我也想不出任何有效的办法。
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.
Pull Request Overview
This PR adds a random article method to the post finder to return a randomly shuffled subset of posts.
- Implemented a new random(Integer limit) method in PostFinderImpl using a custom shuffle algorithm.
- Updated the PostFinder interface to declare the new random method.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
application/src/main/java/run/halo/app/theme/finders/impl/PostFinderImpl.java | Added random method and a helper to shuffle posts using a custom Fisher-Yates algorithm. |
application/src/main/java/run/halo/app/theme/finders/PostFinder.java | Added random method declaration in the interface. |
Random random = new Random(); | ||
for(int i= shuffledList.size() - 1; i > 0; i--) { | ||
int index = random.nextInt(i + 1); | ||
// 交换元素 | ||
Post temp = shuffledList.get(index); | ||
shuffledList.set(index, shuffledList.get(i)); | ||
shuffledList.set(i, temp); | ||
} |
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.
[nitpick] Consider using Collections.shuffle(shuffledList, random) instead of a custom shuffle loop to simplify the code and reduce potential maintenance overhead.
Random random = new Random(); | |
for(int i= shuffledList.size() - 1; i > 0; i--) { | |
int index = random.nextInt(i + 1); | |
// 交换元素 | |
Post temp = shuffledList.get(index); | |
shuffledList.set(index, shuffledList.get(i)); | |
shuffledList.set(i, temp); | |
} | |
Collections.shuffle(shuffledList, new Random()); |
Copilot uses AI. Check for mistakes.
|
/hold Hold until we find a better solution. |
What type of PR is this?
/area core
/kind feature
What this PR does / why we need it:
为 postFinder 添加 随机文章方法
Which issue(s) this PR fixes:
Fixes #7327
Does this PR introduce a user-facing change?