You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/05/31 01:53:52 UTC

[13/14] james-project git commit: MAILBOX-338 Responsibility for weight metric update should belong to the loader

MAILBOX-338 Responsibility for weight metric update should belong to the loader

Not to the weigher


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/2d94eb5f
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/2d94eb5f
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/2d94eb5f

Branch: refs/heads/master
Commit: 2d94eb5f602082ed63e3f2866147df44c0c8aab4
Parents: f63e5d8
Author: benwa <bt...@linagora.com>
Authored: Wed May 30 09:14:20 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu May 31 08:53:16 2018 +0700

----------------------------------------------------------------------
 .../mailbox/tika/CachingTextExtractor.java      | 22 +++++++++++---------
 1 file changed, 12 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2d94eb5f/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/CachingTextExtractor.java
----------------------------------------------------------------------
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 b0bae1f..c06128f 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
@@ -39,6 +39,7 @@ import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.RemovalListener;
 import com.google.common.cache.Weigher;
+import com.google.common.util.concurrent.UncheckedExecutionException;
 
 public class CachingTextExtractor implements TextExtractor {
     private final TextExtractor underlying;
@@ -51,14 +52,10 @@ public class CachingTextExtractor implements TextExtractor {
         this.weightMetric = metricFactory.generate("textExtractor.cache.weight");
 
         Weigher<String, ParsedContent> weigher =
-            (key, parsedContent) -> {
-                int size = getSize(parsedContent);
-                weightMetric.add(size);
-                return size;
-            };
+            (key, parsedContent) -> computeWeight(parsedContent);
         RemovalListener<String, ParsedContent> removalListener =
             notification -> Optional.ofNullable(notification.getValue())
-                .map(this::getSize)
+                .map(this::computeWeight)
                 .ifPresent(weightMetric::remove);
 
         this.cache = CacheBuilder.<String, String>newBuilder()
@@ -99,7 +96,7 @@ public class CachingTextExtractor implements TextExtractor {
                 cache::size);
     }
 
-    private int getSize(ParsedContent parsedContent) {
+    private int computeWeight(ParsedContent parsedContent) {
         return parsedContent.getTextualContent()
             .map(String::length)
             .map(this::utf16LengthToBytesCount)
@@ -116,13 +113,18 @@ public class CachingTextExtractor implements TextExtractor {
         String key = DigestUtils.sha256Hex(bytes);
 
         try {
-            return cache.get(key,
-                () -> underlying.extractContent(new ByteArrayInputStream(bytes), contentType));
-        } catch (ExecutionException e) {
+            return cache.get(key, () -> retrieveAndUpdateWeight(bytes, contentType));
+        } catch (ExecutionException | UncheckedExecutionException e) {
             throw unwrap(e);
         }
     }
 
+    private ParsedContent retrieveAndUpdateWeight(byte[] bytes, String contentType) throws Exception {
+        ParsedContent parsedContent = underlying.extractContent(new ByteArrayInputStream(bytes), contentType);
+        weightMetric.add(computeWeight(parsedContent));
+        return parsedContent;
+    }
+
     private Exception unwrap(Exception e) {
         return Optional.ofNullable(e.getCause())
             .filter(throwable -> throwable instanceof Exception)


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