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 2020/05/05 07:51:05 UTC

[james-project] 06/07: JAMES-3155 Reactify quotaRoot retrieval

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 cdde0fc58fa1d4c85780282b6b43e3de0a494e85
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Mon Apr 27 13:51:36 2020 +0700

    JAMES-3155 Reactify quotaRoot retrieval
---
 .../james/mailbox/quota/QuotaRootResolver.java     |  3 +++
 .../store/quota/DefaultUserQuotaRootResolver.java  | 17 ++++++++----
 .../store/quota/ListeningCurrentQuotaUpdater.java  | 31 +++++++++-------------
 .../quota/DefaultUserQuotaRootResolverTest.java    |  3 ++-
 .../quota/ListeningCurrentQuotaUpdaterTest.java    |  8 +++---
 5 files changed, 34 insertions(+), 28 deletions(-)

diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java b/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java
index 258a870..13e1099 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/quota/QuotaRootResolver.java
@@ -27,6 +27,7 @@ import org.apache.james.mailbox.model.Mailbox;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.QuotaRoot;
+import org.reactivestreams.Publisher;
 
 public interface QuotaRootResolver extends QuotaRootDeserializer {
 
@@ -40,5 +41,7 @@ public interface QuotaRootResolver extends QuotaRootDeserializer {
 
     QuotaRoot getQuotaRoot(MailboxId mailboxId) throws MailboxException;
 
+    Publisher<QuotaRoot> getQuotaRootReactive(MailboxId mailboxId);
+
     List<Mailbox> retrieveAssociatedMailboxes(QuotaRoot quotaRoot, MailboxSession mailboxSession) throws MailboxException;
 }
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java
index 6ea1f88..552e155 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolver.java
@@ -41,6 +41,8 @@ import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 
+import reactor.core.publisher.Mono;
+
 public class DefaultUserQuotaRootResolver implements UserQuotaRootResolver {
 
     public static class DefaultQuotaRootDeserializer implements QuotaRootDeserializer {
@@ -93,13 +95,18 @@ public class DefaultUserQuotaRootResolver implements UserQuotaRootResolver {
 
     @Override
     public QuotaRoot getQuotaRoot(MailboxId mailboxId) throws MailboxException {
+        return getQuotaRootReactive(mailboxId).block();
+    }
+
+    @Override
+    public Mono<QuotaRoot> getQuotaRootReactive(MailboxId mailboxId) {
         MailboxSession session = sessionProvider.createSystemSession(Username.of("DefaultUserQuotaRootResolver"));
-        Username username = factory.getMailboxMapper(session)
-            .findMailboxById(mailboxId)
-            .generateAssociatedPath()
-            .getUser();
 
-        return forUser(username);
+        return factory.getMailboxMapper(session)
+            .findMailboxByIdReactive(mailboxId)
+            .map(Mailbox::generateAssociatedPath)
+            .map(MailboxPath::getUser)
+            .map(this::forUser);
     }
 
     @Override
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 f564242..c5d96da 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
@@ -32,7 +32,6 @@ import org.apache.james.mailbox.events.EventBus;
 import org.apache.james.mailbox.events.Group;
 import org.apache.james.mailbox.events.MailboxListener;
 import org.apache.james.mailbox.events.RegistrationKey;
-import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.Quota;
 import org.apache.james.mailbox.model.QuotaOperation;
 import org.apache.james.mailbox.model.QuotaRoot;
@@ -81,23 +80,19 @@ public class ListeningCurrentQuotaUpdater implements MailboxListener.ReactiveGro
 
     @Override
     public Publisher<Void> reactiveEvent(Event event) {
-        try {
-            if (event instanceof Added) {
-                Added addedEvent = (Added) event;
-                QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(addedEvent.getMailboxId());
-                return handleAddedEvent(addedEvent, quotaRoot);
-            } else if (event instanceof Expunged) {
-                Expunged expungedEvent = (Expunged) event;
-                QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(expungedEvent.getMailboxId());
-                return handleExpungedEvent(expungedEvent, quotaRoot);
-            } else if (event instanceof MailboxDeletion) {
-                MailboxDeletion mailboxDeletionEvent = (MailboxDeletion) event;
-                return handleMailboxDeletionEvent(mailboxDeletionEvent);
-            }
-            return Mono.empty();
-        } catch (MailboxException e) {
-            return Mono.error(e);
+        if (event instanceof Added) {
+            Added addedEvent = (Added) event;
+            return Mono.from(quotaRootResolver.getQuotaRootReactive(addedEvent.getMailboxId()))
+                .flatMap(quotaRoot -> handleAddedEvent(addedEvent, quotaRoot));
+        } else if (event instanceof Expunged) {
+            Expunged expungedEvent = (Expunged) event;
+            return Mono.from(quotaRootResolver.getQuotaRootReactive(expungedEvent.getMailboxId()))
+                .flatMap(quotaRoot -> handleExpungedEvent(expungedEvent, quotaRoot));
+        } else if (event instanceof MailboxDeletion) {
+            MailboxDeletion mailboxDeletionEvent = (MailboxDeletion) event;
+            return handleMailboxDeletionEvent(mailboxDeletionEvent);
         }
+        return Mono.empty();
     }
 
     private Mono<Void> handleExpungedEvent(Expunged expunged, QuotaRoot quotaRoot) {
@@ -152,7 +147,7 @@ public class ListeningCurrentQuotaUpdater implements MailboxListener.ReactiveGro
             .sum();
     }
 
-    private Mono<Void> handleMailboxDeletionEvent(MailboxDeletion mailboxDeletionEvent) throws MailboxException {
+    private Mono<Void> handleMailboxDeletionEvent(MailboxDeletion mailboxDeletionEvent) {
         boolean mailboxContainedMessages = mailboxDeletionEvent.getDeletedMessageCount().asLong() > 0;
         if (mailboxContainedMessages) {
             return Mono.from(currentQuotaManager.decrease(new QuotaOperation(mailboxDeletionEvent.getQuotaRoot(),
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolverTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolverTest.java
index 36cb715..b022655 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolverTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/DefaultUserQuotaRootResolverTest.java
@@ -43,6 +43,7 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
 
 class DefaultUserQuotaRootResolverTest {
 
@@ -108,7 +109,7 @@ class DefaultUserQuotaRootResolverTest {
     void getQuotaRootShouldReturnUserValueWhenCalledWithMailboxId() throws Exception {
         MailboxMapper mockedMapper = mock(MailboxMapper.class);
         when(mockedFactory.getMailboxMapper(any())).thenReturn(mockedMapper);
-        when(mockedMapper.findMailboxById(MAILBOX_ID)).thenReturn(MAILBOX);
+        when(mockedMapper.findMailboxByIdReactive(MAILBOX_ID)).thenReturn(Mono.just(MAILBOX));
 
         assertThat(testee.getQuotaRoot(MAILBOX_ID)).isEqualTo(QUOTA_ROOT);
     }
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java
index d775052..de679fc 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java
@@ -96,7 +96,7 @@ class ListeningCurrentQuotaUpdaterTest {
         when(added.getMetaData(MessageUid.of(38))).thenReturn(new MessageMetaData(MessageUid.of(38), ModSeq.first(),new Flags(), SIZE, new Date(), new DefaultMessageId()));
         when(added.getUids()).thenReturn(Lists.newArrayList(MessageUid.of(36), MessageUid.of(38)));
         when(added.getUsername()).thenReturn(USERNAME_BENWA);
-        when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID))).thenReturn(QUOTA_ROOT);
+        when(mockedQuotaRootResolver.getQuotaRootReactive(eq(MAILBOX_ID))).thenReturn(Mono.just(QUOTA_ROOT));
         when(mockedCurrentQuotaManager.increase(QUOTA)).thenAnswer(any -> Mono.empty());
 
         testee.event(added);
@@ -112,7 +112,7 @@ class ListeningCurrentQuotaUpdaterTest {
         when(expunged.getUids()).thenReturn(Lists.newArrayList(MessageUid.of(36), MessageUid.of(38)));
         when(expunged.getMailboxId()).thenReturn(MAILBOX_ID);
         when(expunged.getUsername()).thenReturn(USERNAME_BENWA);
-        when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID))).thenReturn(QUOTA_ROOT);
+        when(mockedQuotaRootResolver.getQuotaRootReactive(eq(MAILBOX_ID))).thenReturn(Mono.just(QUOTA_ROOT));
         when(mockedCurrentQuotaManager.decrease(QUOTA)).thenAnswer(any -> Mono.empty());
 
         testee.event(expunged);
@@ -126,7 +126,7 @@ class ListeningCurrentQuotaUpdaterTest {
         when(expunged.getUids()).thenReturn(Lists.<MessageUid>newArrayList());
         when(expunged.getMailboxId()).thenReturn(MAILBOX_ID);
         when(expunged.getUsername()).thenReturn(USERNAME_BENWA);
-        when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID))).thenReturn(QUOTA_ROOT);
+        when(mockedQuotaRootResolver.getQuotaRootReactive(eq(MAILBOX_ID))).thenReturn(Mono.just(QUOTA_ROOT));
 
         testee.event(expunged);
 
@@ -139,7 +139,7 @@ class ListeningCurrentQuotaUpdaterTest {
         when(added.getUids()).thenReturn(Lists.<MessageUid>newArrayList());
         when(added.getMailboxId()).thenReturn(MAILBOX_ID);
         when(added.getUsername()).thenReturn(USERNAME_BENWA);
-        when(mockedQuotaRootResolver.getQuotaRoot(eq(MAILBOX_ID))).thenReturn(QUOTA_ROOT);
+        when(mockedQuotaRootResolver.getQuotaRootReactive(eq(MAILBOX_ID))).thenReturn(Mono.just(QUOTA_ROOT));
 
         testee.event(added);
 


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