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 rc...@apache.org on 2020/06/30 07:59:32 UTC

[james-project] 03/03: JAMES-3277 Don't read outbox mailbox for each message in SetMessagesUpdateProcessor

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

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

commit 63287bf14d47dee4544ed2034b1b49226e159605
Author: RĂ©mi Kowalski <rk...@linagora.com>
AuthorDate: Wed Jun 24 16:53:15 2020 +0200

    JAMES-3277 Don't read outbox mailbox for each message in SetMessagesUpdateProcessor
---
 server/protocols/jmap-draft/pom.xml                |  4 ++
 .../draft/methods/SetMessagesUpdateProcessor.java  | 46 +++++++++++++++-------
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/server/protocols/jmap-draft/pom.xml b/server/protocols/jmap-draft/pom.xml
index ad8e7cc..9cd06bf 100644
--- a/server/protocols/jmap-draft/pom.xml
+++ b/server/protocols/jmap-draft/pom.xml
@@ -211,6 +211,10 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>io.vavr</groupId>
+            <artifactId>vavr</artifactId>
+        </dependency>
+        <dependency>
             <groupId>javax.inject</groupId>
             <artifactId>javax.inject</artifactId>
         </dependency>
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/SetMessagesUpdateProcessor.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/SetMessagesUpdateProcessor.java
index 35ba365..edbe6ee 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/SetMessagesUpdateProcessor.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/methods/SetMessagesUpdateProcessor.java
@@ -74,6 +74,8 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 
+import io.vavr.control.Try;
+
 public class SetMessagesUpdateProcessor implements SetMessagesProcessor {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(SetMessagesUpdateProcessor.class);
@@ -109,24 +111,41 @@ public class SetMessagesUpdateProcessor implements SetMessagesProcessor {
     }
 
     @Override
-    public SetMessagesResponse process(SetMessagesRequest request,  MailboxSession mailboxSession) {
+    public SetMessagesResponse process(SetMessagesRequest request, MailboxSession mailboxSession) {
         TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + "SetMessagesUpdateProcessor");
-
         SetMessagesResponse.Builder responseBuilder = SetMessagesResponse.builder();
+        Try.ofCallable(() -> listMailboxIdsForRole(mailboxSession, Role.OUTBOX))
+            .map(outboxes -> {
+                prepareResponse(request, mailboxSession, responseBuilder, outboxes);
+                return null;
+            })
+            .onFailure(e -> request.buildUpdatePatches(updatePatchConverter)
+                .forEach((id, patch) -> prepareResponseIfCantReadOutboxes(responseBuilder, e, id, patch)));
+
+        timeMetric.stopAndPublish();
+        return responseBuilder.build();
+    }
+
+    private void prepareResponseIfCantReadOutboxes(SetMessagesResponse.Builder responseBuilder, Throwable e, MessageId id, UpdateMessagePatch patch) {
+        if (patch.isValid()) {
+            handleMessageUpdateException(id, responseBuilder, e);
+        } else {
+            handleInvalidRequest(responseBuilder, id, patch.getValidationErrors(), patch);
+        }
+    }
+
+    private void prepareResponse(SetMessagesRequest request, MailboxSession mailboxSession, SetMessagesResponse.Builder responseBuilder, Set<MailboxId> outboxes) {
         request.buildUpdatePatches(updatePatchConverter).forEach((id, patch) -> {
                 if (patch.isValid()) {
-                    update(id, patch, mailboxSession, responseBuilder);
+                    update(outboxes, id, patch, mailboxSession, responseBuilder);
                 } else {
                     handleInvalidRequest(responseBuilder, id, patch.getValidationErrors(), patch);
                 }
             }
         );
-
-        timeMetric.stopAndPublish();
-        return responseBuilder.build();
     }
 
-    private void update(MessageId messageId, UpdateMessagePatch updateMessagePatch, MailboxSession mailboxSession,
+    private void update(Set<MailboxId> outboxes, MessageId messageId, UpdateMessagePatch updateMessagePatch, MailboxSession mailboxSession,
                         SetMessagesResponse.Builder builder) {
         try {
             List<MessageResult> messages = messageIdManager.getMessage(messageId, FetchGroup.MINIMAL, mailboxSession);
@@ -144,7 +163,7 @@ public class SetMessagesUpdateProcessor implements SetMessagesProcessor {
                 } else {
                     builder.updated(ImmutableList.of(messageId));
                 }
-                sendMessageWhenOutboxInTargetMailboxIds(messageId, updateMessagePatch, mailboxSession, builder);
+                sendMessageWhenOutboxInTargetMailboxIds(outboxes, messageId, updateMessagePatch, mailboxSession, builder);
             }
         } catch (DraftMessageMailboxUpdateException e) {
             handleDraftMessageMailboxUpdateException(messageId, builder, e);
@@ -173,8 +192,8 @@ public class SetMessagesUpdateProcessor implements SetMessagesProcessor {
         }
     }
 
-    private void sendMessageWhenOutboxInTargetMailboxIds(MessageId messageId, UpdateMessagePatch updateMessagePatch, MailboxSession mailboxSession, SetMessagesResponse.Builder builder) throws MailboxException, MessagingException, IOException {
-        if (isTargetingOutbox(mailboxSession, listTargetMailboxIds(updateMessagePatch))) {
+    private void sendMessageWhenOutboxInTargetMailboxIds(Set<MailboxId> outboxes, MessageId messageId, UpdateMessagePatch updateMessagePatch, MailboxSession mailboxSession, SetMessagesResponse.Builder builder) throws MailboxException, MessagingException, IOException {
+        if (isTargetingOutbox(outboxes, listTargetMailboxIds(updateMessagePatch))) {
             Optional<MessageResult> maybeMessageToSend =
                 messageIdManager.getMessage(messageId, FetchGroup.FULL_CONTENT, mailboxSession)
                     .stream()
@@ -270,8 +289,7 @@ public class SetMessagesUpdateProcessor implements SetMessagesProcessor {
             .collect(Guavate.toImmutableSet());
     }
 
-    private boolean isTargetingOutbox(MailboxSession mailboxSession, Set<MailboxId> targetMailboxIds) throws MailboxException {
-        Set<MailboxId> outboxes = listMailboxIdsForRole(mailboxSession, Role.OUTBOX);
+    private boolean isTargetingOutbox(Set<MailboxId> outboxes, Set<MailboxId> targetMailboxIds) throws MailboxException {
         if (outboxes.isEmpty()) {
             throw new MailboxNotFoundException("At least one outbox should be accessible");
         }
@@ -329,8 +347,8 @@ public class SetMessagesUpdateProcessor implements SetMessagesProcessor {
     }
 
     private SetMessagesResponse.Builder handleMessageUpdateException(MessageId messageId,
-                                              SetMessagesResponse.Builder builder,
-                                              Exception e) {
+                                                                     SetMessagesResponse.Builder builder,
+                                                                     Throwable e) {
         LOGGER.error("An error occurred when updating a message", e);
         return builder.notUpdated(ImmutableMap.of(messageId, SetError.builder()
                 .type(SetError.Type.ERROR)


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