You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by ma...@apache.org on 2023/02/10 08:24:20 UTC

[james-project] branch master updated: More Extractors fixes (#1430)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 46ed946fae More Extractors fixes (#1430)
46ed946fae is described below

commit 46ed946fae16fa33761197899ef9b7a8a5b3d5a4
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Fri Feb 10 08:24:15 2023 +0000

    More Extractors fixes (#1430)
    
    * Avoid double conversion of Duration to/from millis
    
    * Perform IOs into Mono and not synchronously at method call
---
 .../james/mailbox/tika/CachingTextExtractor.java    | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/CachingTextExtractor.java b/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/CachingTextExtractor.java
index c553cbef63..f7309d6360 100644
--- a/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/CachingTextExtractor.java
+++ b/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/CachingTextExtractor.java
@@ -20,7 +20,6 @@
 package org.apache.james.mailbox.tika;
 
 import java.io.ByteArrayInputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.time.Duration;
 import java.util.Optional;
@@ -40,6 +39,7 @@ import com.github.benmanes.caffeine.cache.Weigher;
 import com.google.common.hash.Hashing;
 
 import reactor.core.publisher.Mono;
+import reactor.core.scheduler.Schedulers;
 
 public class CachingTextExtractor implements TextExtractor {
     private final TextExtractor underlying;
@@ -60,7 +60,7 @@ public class CachingTextExtractor implements TextExtractor {
             (key, parsedContent) -> computeWeight(parsedContent);
 
         cache = Caffeine.newBuilder()
-            .expireAfterAccess(Duration.ofMillis(cacheEvictionPeriod.toMillis()))
+            .expireAfterAccess(cacheEvictionPeriod)
             .maximumWeight(cacheWeightInBytes)
             .weigher(weigher)
             .evictionListener(removalListener)
@@ -111,14 +111,15 @@ public class CachingTextExtractor implements TextExtractor {
 
     @Override
     public Mono<ParsedContent> extractContentReactive(InputStream inputStream, ContentType contentType) {
-        try {
-            byte[] bytes = IOUtils.toByteArray(inputStream);
-            String key = Hashing.sha256().hashBytes(bytes).toString();
-
-            return Mono.fromFuture(cache.get(key, (a, b) -> retrieveAndUpdateWeight(bytes, contentType).toFuture()));
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
+        return Mono
+                .fromCallable(() -> IOUtils.toByteArray(inputStream))
+                .subscribeOn(Schedulers.boundedElastic())
+                .flatMap(bytes ->
+                    Mono.fromCallable(() -> Hashing.sha256().hashBytes(bytes).toString())
+                        .subscribeOn(Schedulers.parallel())
+                        .publishOn(Schedulers.boundedElastic())
+                        .flatMap(key -> Mono.fromFuture(cache.get(key, (a, b) -> retrieveAndUpdateWeight(bytes, contentType).toFuture())))
+                );
     }
 
     @Override


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