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 52903bf9acf41fc4d0cc7799e81418ec92656a53
Author: Benoit TELLIER <btell...@linagora.com>
AuthorDate: Thu Dec 12 22:58:11 2024 +0100

    [PERF] Avoid triggering quota updates on message move
    
    Prior to this we were triggering an update for the added message and the 
removed one,
    which causes serialisation deserialization, quota updates, and OpenSearch 
updates.
---
 .../store/quota/ListeningCurrentQuotaUpdater.java  | 34 ++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
index 8d3aa1fb09..27e6acccb5 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
@@ -19,6 +19,7 @@
 package org.apache.james.mailbox.store.quota;
 
 import java.time.Instant;
+import java.util.List;
 
 import jakarta.inject.Inject;
 
@@ -36,6 +37,8 @@ import org.apache.james.mailbox.events.MailboxEvents.Expunged;
 import org.apache.james.mailbox.events.MailboxEvents.MailboxAdded;
 import org.apache.james.mailbox.events.MailboxEvents.MailboxDeletion;
 import org.apache.james.mailbox.events.MailboxEvents.MetaDataHoldingEvent;
+import org.apache.james.mailbox.events.MessageMoveEvent;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.QuotaOperation;
 import org.apache.james.mailbox.model.QuotaRoot;
 import org.apache.james.mailbox.quota.CurrentQuotaManager;
@@ -102,6 +105,37 @@ public class ListeningCurrentQuotaUpdater implements 
EventListener.ReactiveGroup
         return Mono.empty();
     }
 
+    @Override
+    public Publisher<Void> reactiveEvent(List<Event> event) {
+        Mono<Boolean> isAMove = event.stream()
+            .filter(MessageMoveEvent.class::isInstance)
+            .map(MessageMoveEvent.class::cast)
+            .findAny()
+            .map(move -> {
+                boolean moveAsSingleMailbox = 
move.getMessageMoves().addedMailboxIds().size() == 
move.getMessageMoves().removedMailboxIds().size()
+                    && move.getMessageMoves().addedMailboxIds().size() == 1;
+                if (moveAsSingleMailbox) {
+                    return bothMailboxesBelongToSameRoot(move);
+                }
+                return Mono.just(false);
+            }).orElse(Mono.just(false));
+
+        return isAMove.flatMap(b -> {
+            if (b) {
+                return Mono.empty();
+            }
+            return 
Mono.from(ReactiveGroupEventListener.super.reactiveEvent(event));
+        });
+    }
+
+    private Mono<Boolean> bothMailboxesBelongToSameRoot(MessageMoveEvent move) 
{
+        MailboxId mailboxId1 = 
move.getMessageMoves().addedMailboxIds().iterator().next();
+        MailboxId mailboxId2 = 
move.getMessageMoves().removedMailboxIds().iterator().next();
+        return Mono.from(quotaRootResolver.getQuotaRootReactive(mailboxId1))
+            
.zipWith(Mono.from(quotaRootResolver.getQuotaRootReactive(mailboxId2)))
+            .map(roots -> roots.getT1().equals(roots.getT2()));
+    }
+
     private Mono<Void> handleExpungedEvent(Expunged expunged, QuotaRoot 
quotaRoot) {
         return computeQuotaOperation(expunged, quotaRoot)
             .flatMap(quotaOperation ->


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

Reply via email to