You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2020/11/25 05:19:59 UTC

[james-project] 05/06: [Refactoring] ElasticSearchSearcher: avoid flatMap usage for synchronous transformations

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit fb264392f656a5c77d51208d91e1d2080901bfc8
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Tue Nov 24 14:30:05 2020 +0700

    [Refactoring] ElasticSearchSearcher: avoid flatMap usage for synchronous transformations
---
 .../mailbox/elasticsearch/search/ElasticSearchSearcher.java   | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/search/ElasticSearchSearcher.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/search/ElasticSearchSearcher.java
index 7dd6509..0e74473 100644
--- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/search/ElasticSearchSearcher.java
+++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/search/ElasticSearchSearcher.java
@@ -19,6 +19,8 @@
 
 package org.apache.james.mailbox.elasticsearch.search;
 
+import static org.apache.james.util.ReactorUtils.publishIfPresent;
+
 import java.util.Collection;
 import java.util.Optional;
 
@@ -81,7 +83,8 @@ public class ElasticSearchSearcher {
         SearchRequest searchRequest = prepareSearch(mailboxIds, query, limit);
         Flux<MessageSearchIndex.SearchResult> pairStream = new ScrolledSearch(client, searchRequest)
             .searchHits()
-            .flatMap(this::extractContentFromHit);
+            .map(this::extractContentFromHit)
+            .handle(publishIfPresent());
 
         return limit.map(pairStream::take)
             .orElse(pairStream);
@@ -123,20 +126,20 @@ public class ElasticSearchSearcher {
             .orElse(size);
     }
 
-    private Flux<MessageSearchIndex.SearchResult> extractContentFromHit(SearchHit hit) {
+    private Optional<MessageSearchIndex.SearchResult> extractContentFromHit(SearchHit hit) {
         DocumentField mailboxId = hit.field(JsonMessageConstants.MAILBOX_ID);
         DocumentField uid = hit.field(JsonMessageConstants.UID);
         Optional<DocumentField> id = retrieveMessageIdField(hit);
         if (mailboxId != null && uid != null) {
             Number uidAsNumber = uid.getValue();
-            return Flux.just(
+            return Optional.of(
                 new MessageSearchIndex.SearchResult(
                     id.map(field -> messageIdFactory.fromString(field.getValue())),
                     mailboxIdFactory.fromString(mailboxId.getValue()),
                     MessageUid.of(uidAsNumber.longValue())));
         } else {
             LOGGER.warn("Can not extract UID, MessageID and/or MailboxId for search result {}", hit.getId());
-            return Flux.empty();
+            return Optional.empty();
         }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org