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 2019/01/24 03:03:53 UTC

[2/8] james-project git commit: MAILBOX-374 Slightly improve ListeningCurrentQuotaUpdater code quality

MAILBOX-374 Slightly improve ListeningCurrentQuotaUpdater code quality


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

Branch: refs/heads/master
Commit: b9266899a141a72a0c8e44c05a5dcf0d03856974
Parents: ac3d520
Author: Benoit Tellier <bt...@linagora.com>
Authored: Tue Jan 22 11:47:42 2019 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Thu Jan 24 09:46:03 2019 +0700

----------------------------------------------------------------------
 .../quota/ListeningCurrentQuotaUpdater.java     | 31 ++++++++------------
 1 file changed, 13 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b9266899/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
----------------------------------------------------------------------
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 57cee5a..cf6cace 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,13 +19,11 @@
 package org.apache.james.mailbox.store.quota;
 
 import java.time.Instant;
-import java.util.Collection;
 
 import javax.inject.Inject;
 
 import org.apache.james.mailbox.Event;
 import org.apache.james.mailbox.MailboxListener;
-import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.events.EventBus;
 import org.apache.james.mailbox.events.Group;
 import org.apache.james.mailbox.events.RegistrationKey;
@@ -78,16 +76,11 @@ public class ListeningCurrentQuotaUpdater implements MailboxListener.GroupMailbo
     }
 
     private void handleExpungedEvent(Expunged expunged, QuotaRoot quotaRoot) throws MailboxException {
-        long addedSize = 0;
-        long addedCount = 0;
-        Collection<MessageUid> uids = expunged.getUids();
-        for (MessageUid uid : uids) {
-            addedSize += expunged.getMetaData(uid).getSize();
-            addedCount++;
-        }
+        long expungedSize = totalSize(expunged);
+        long expungedCount = (long) expunged.getUids().size();
         // Expunge event can contain no data (expunge performed while no messages marked \Deleted)
-        if (addedCount != 0 && addedSize != 0) {
-            currentQuotaManager.decrease(quotaRoot, addedCount, addedSize);
+        if (expungedCount != 0 && expungedSize != 0) {
+            currentQuotaManager.decrease(quotaRoot, expungedCount, expungedSize);
         }
         eventBus.dispatch(
             EventFactory.quotaUpdated()
@@ -103,13 +96,8 @@ public class ListeningCurrentQuotaUpdater implements MailboxListener.GroupMailbo
     }
 
     private void handleAddedEvent(Added added, QuotaRoot quotaRoot) throws MailboxException {
-        long addedSize = 0;
-        long addedCount = 0;
-        Collection<MessageUid> uids = added.getUids();
-        for (MessageUid uid : uids) {
-            addedSize += added.getMetaData(uid).getSize();
-            addedCount++;
-        }
+        long addedSize = totalSize(added);
+        long addedCount = (long) added.getUids().size();
         if (addedCount != 0 && addedSize != 0) {
             currentQuotaManager.increase(quotaRoot, addedCount, addedSize);
         }
@@ -126,6 +114,13 @@ public class ListeningCurrentQuotaUpdater implements MailboxListener.GroupMailbo
             .block();
     }
 
+    private long totalSize(MetaDataHoldingEvent metaDataHoldingEvent) {
+        return metaDataHoldingEvent.getUids()
+            .stream()
+            .mapToLong(uid -> metaDataHoldingEvent.getMetaData(uid).getSize())
+            .sum();
+    }
+
     private void handleMailboxDeletionEvent(MailboxDeletion mailboxDeletionEvent) throws MailboxException {
         boolean mailboxContainedMessages = mailboxDeletionEvent.getDeletedMessageCount().asLong() > 0;
         if (mailboxContainedMessages) {


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