From 9e5c6a45c9ce59dee8f6f82b31ee1edf22331195 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Thu, 17 Nov 2022 16:04:21 +0800 Subject: [PATCH] fix: failed to build due to wrong type (#2712) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core /milestone 2.0 #### What this PR does / why we need it: 修复上一个 PR 没有拉取最新代码合并后导致无法构建的问题 #### Special notes for your reviewer: /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../halo/app/search/IndicesServiceImpl.java | 22 +++++++++++-------- .../app/search/post/PostEventListener.java | 22 +++++++++---------- .../halo/app/theme/finders/PostFinder.java | 3 +++ .../theme/finders/impl/PostFinderImpl.java | 6 +++++ 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/main/java/run/halo/app/search/IndicesServiceImpl.java b/src/main/java/run/halo/app/search/IndicesServiceImpl.java index 21b91ce9b..de8ab11e7 100644 --- a/src/main/java/run/halo/app/search/IndicesServiceImpl.java +++ b/src/main/java/run/halo/app/search/IndicesServiceImpl.java @@ -2,14 +2,13 @@ package run.halo.app.search; import org.springframework.stereotype.Service; import reactor.core.Exceptions; -import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.scheduler.Schedulers; import run.halo.app.core.extension.Post; import run.halo.app.plugin.extensionpoint.ExtensionGetter; import run.halo.app.search.post.PostDoc; import run.halo.app.search.post.PostSearchService; import run.halo.app.theme.finders.PostFinder; +import run.halo.app.theme.finders.vo.PostVo; @Service public class IndicesServiceImpl implements IndicesService { @@ -25,13 +24,18 @@ public class IndicesServiceImpl implements IndicesService { @Override public Mono rebuildPostIndices() { return extensionGetter.getEnabledExtension(PostSearchService.class) - // TODO Optimize listing posts with non-blocking. - .flatMap(searchService -> Flux.fromStream(() -> postFinder.list(0, 0) - .stream() - .filter(post -> Post.isPublished(post.getMetadata())) - .peek(post -> postFinder.content(post.getMetadata().getName())) - .map(PostDoc::from)) - .subscribeOn(Schedulers.boundedElastic()) + .flatMap(searchService -> postFinder.listAll() + .filter(post -> Post.isPublished(post.getMetadata())) + .flatMap(listedPostVo -> { + PostVo postVo = PostVo.from(listedPostVo); + return postFinder.content(postVo.getMetadata().getName()) + .map(content -> { + postVo.setContent(content); + return postVo; + }) + .defaultIfEmpty(postVo); + }) + .map(PostDoc::from) .limitRate(100) .buffer(100) .doOnNext(postDocs -> { diff --git a/src/main/java/run/halo/app/search/post/PostEventListener.java b/src/main/java/run/halo/app/search/post/PostEventListener.java index 439a1b964..233a1ff9d 100644 --- a/src/main/java/run/halo/app/search/post/PostEventListener.java +++ b/src/main/java/run/halo/app/search/post/PostEventListener.java @@ -29,18 +29,18 @@ public class PostEventListener { @Async @EventListener(PostPublishedEvent.class) public void handlePostPublished(PostPublishedEvent publishedEvent) throws InterruptedException { - var postVo = postFinder.getByName(publishedEvent.getPostName()); - var postDoc = PostDoc.from(postVo); - var latch = new CountDownLatch(1); - extensionGetter.getEnabledExtension(PostSearchService.class) - .doOnNext(searchService -> { - try { - searchService.addDocuments(List.of(postDoc)); - } catch (Exception e) { - throw Exceptions.propagate(e); - } - }) + postFinder.getByName(publishedEvent.getPostName()) + .map(PostDoc::from) + .flatMap(postDoc -> extensionGetter.getEnabledExtension(PostSearchService.class) + .doOnNext(searchService -> { + try { + searchService.addDocuments(List.of(postDoc)); + } catch (Exception e) { + throw Exceptions.propagate(e); + } + }) + ) .doFinally(signalType -> latch.countDown()) .subscribe(); latch.await(); diff --git a/src/main/java/run/halo/app/theme/finders/PostFinder.java b/src/main/java/run/halo/app/theme/finders/PostFinder.java index ddc6cdcfb..a3e8a266f 100644 --- a/src/main/java/run/halo/app/theme/finders/PostFinder.java +++ b/src/main/java/run/halo/app/theme/finders/PostFinder.java @@ -1,6 +1,7 @@ package run.halo.app.theme.finders; import org.springframework.lang.Nullable; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import run.halo.app.core.extension.Post; import run.halo.app.extension.ListResult; @@ -24,6 +25,8 @@ public interface PostFinder { Mono cursor(String current); + Flux listAll(); + Mono> list(@Nullable Integer page, @Nullable Integer size); Mono> listByCategory(@Nullable Integer page, @Nullable Integer size, diff --git a/src/main/java/run/halo/app/theme/finders/impl/PostFinderImpl.java b/src/main/java/run/halo/app/theme/finders/impl/PostFinderImpl.java index 6c7225358..a59a6fe54 100644 --- a/src/main/java/run/halo/app/theme/finders/impl/PostFinderImpl.java +++ b/src/main/java/run/halo/app/theme/finders/impl/PostFinderImpl.java @@ -124,6 +124,12 @@ public class PostFinderImpl implements PostFinder { .defaultIfEmpty(NavigationPostVo.empty()); } + @Override + public Flux listAll() { + return client.list(Post.class, FIXED_PREDICATE, defaultComparator()) + .flatMap(this::getListedPostVo); + } + static Pair postPreviousNextPair(List postNames, String currentName) { FixedSizeSlidingWindow window = new FixedSizeSlidingWindow<>(3);