refactor: optimize data acquisition from previous to next posts in a post (#5659)

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.15.x

#### What this PR does / why we need it:
优化文章上一篇下一篇的查询方式避免瞬时内存占用过高

#### Does this PR introduce a user-facing change?
```release-note
优化文章上一篇下一篇的查询方式避免瞬时内存占用过高
```
pull/5685/head^2
guqing 2024-04-11 16:24:11 +08:00 committed by GitHub
parent 34febb4d5d
commit 3ef1461c32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 27 deletions

View File

@ -1,5 +1,8 @@
package run.halo.app.theme.finders.impl;
import static run.halo.app.extension.index.query.QueryFactory.and;
import static run.halo.app.extension.index.query.QueryFactory.equal;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -104,27 +107,25 @@ public class PostFinderImpl implements PostFinder {
@Override
public Mono<NavigationPostVo> cursor(String currentName) {
return postPredicateResolver.getListOptions()
.flatMapMany(postListOption ->
client.listAll(Post.class, postListOption, defaultSort())
)
.map(post -> post.getMetadata().getName())
.collectList()
.flatMap(postNames -> Mono.just(NavigationPostVo.builder())
.flatMap(builder -> getByName(currentName)
.doOnNext(builder::current)
.thenReturn(builder)
)
.flatMap(builder -> {
var previousNextPair = findPostNavigation(postNames, currentName);
String previousPostName = previousNextPair.prev();
String nextPostName = previousNextPair.next();
return fetchByName(previousPostName)
.doOnNext(builder::previous)
.then(fetchByName(nextPostName))
.doOnNext(builder::next)
.thenReturn(builder);
})
.map(NavigationPostVo.NavigationPostVoBuilder::build))
.flatMap(postListOption -> {
var postNames = client.indexedQueryEngine()
.retrieve(Post.GVK, postListOption,
PageRequestImpl.ofSize(0).withSort(defaultSort())
)
.getItems();
var previousNextPair = findPostNavigation(postNames, currentName);
String previousPostName = previousNextPair.prev();
String nextPostName = previousNextPair.next();
var builder = NavigationPostVo.builder();
var currentMono = getByName(currentName)
.doOnNext(builder::current);
var prevMono = fetchByName(previousPostName)
.doOnNext(builder::previous);
var nextMono = fetchByName(nextPostName)
.doOnNext(builder::next);
return Mono.when(currentMono, prevMono, nextMono)
.then(Mono.fromSupplier(builder::build));
})
.defaultIfEmpty(NavigationPostVo.empty());
}
@ -142,8 +143,7 @@ public class PostFinderImpl implements PostFinder {
String categoryName) {
var fieldQuery = QueryFactory.all();
if (StringUtils.isNotBlank(categoryName)) {
fieldQuery =
QueryFactory.and(fieldQuery, QueryFactory.equal("spec.categories", categoryName));
fieldQuery = and(fieldQuery, equal("spec.categories", categoryName));
}
var listOptions = new ListOptions();
listOptions.setFieldSelector(FieldSelector.of(fieldQuery));
@ -154,8 +154,7 @@ public class PostFinderImpl implements PostFinder {
public Mono<ListResult<ListedPostVo>> listByTag(Integer page, Integer size, String tag) {
var fieldQuery = QueryFactory.all();
if (StringUtils.isNotBlank(tag)) {
fieldQuery =
QueryFactory.and(fieldQuery, QueryFactory.equal("spec.tags", tag));
fieldQuery = and(fieldQuery, equal("spec.tags", tag));
}
var listOptions = new ListOptions();
listOptions.setFieldSelector(FieldSelector.of(fieldQuery));
@ -166,8 +165,7 @@ public class PostFinderImpl implements PostFinder {
public Mono<ListResult<ListedPostVo>> listByOwner(Integer page, Integer size, String owner) {
var fieldQuery = QueryFactory.all();
if (StringUtils.isNotBlank(owner)) {
fieldQuery =
QueryFactory.and(fieldQuery, QueryFactory.equal("spec.owner", owner));
fieldQuery = and(fieldQuery, equal("spec.owner", owner));
}
var listOptions = new ListOptions();
listOptions.setFieldSelector(FieldSelector.of(fieldQuery));