JAMES-1874 Optimize GetMailboxes: do not search all mailboxes when a list of 
IDs is given

It's a rework from a commit of Antoine, but includes:

  - Factorized code
  - No need to filter by mailbox as this filtering is already handled (gain ~ 
3ms)


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

Branch: refs/heads/master
Commit: 0054b41a68fb0a36f2a30bc132e826b318d7357c
Parents: 3bdf918
Author: Antoine Duprat <adup...@linagora.com>
Authored: Thu Feb 2 09:57:03 2017 +0100
Committer: Benoit Tellier <btell...@linagora.com>
Committed: Tue Feb 7 08:57:45 2017 +0700

----------------------------------------------------------------------
 .../james/jmap/methods/GetMailboxesMethod.java  | 35 ++++++++++----------
 1 file changed, 18 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0054b41a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
index 446336f..d926d21 100644
--- 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
+++ 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMailboxesMethod.java
@@ -19,10 +19,10 @@
 
 package org.apache.james.jmap.methods;
 
+import java.util.Comparator;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
-import java.util.function.Predicate;
 import java.util.stream.Stream;
 
 import javax.inject.Inject;
@@ -89,29 +89,30 @@ public class GetMailboxesMethod implements Method {
     private GetMailboxesResponse getMailboxesResponse(GetMailboxesRequest 
mailboxesRequest, MailboxSession mailboxSession) {
         GetMailboxesResponse.Builder builder = GetMailboxesResponse.builder();
         try {
-            retrieveUserMailboxes(mailboxSession)
-                .stream()
-                .map(MailboxMetaData::getPath)
-                .map(mailboxPath -> 
mailboxFactory.fromMailboxPath(mailboxPath, mailboxSession))
+            Optional<ImmutableList<MailboxId>> mailboxIds = 
mailboxesRequest.getIds();
+            retrieveMailboxIds(mailboxIds, mailboxSession)
+                .map(mailboxId -> mailboxFactory.fromMailboxId(mailboxId, 
mailboxSession))
                 .filter(Optional::isPresent)
                 .map(Optional::get)
-                .filter(filterMailboxesById(mailboxesRequest.getIds()))
-                .sorted((m1, m2) -> 
m1.getSortOrder().compareTo(m2.getSortOrder()))
-                .forEach(mailbox -> builder.add(mailbox));
+                .sorted(Comparator.comparing(Mailbox::getSortOrder))
+                .forEach(builder::add);
             return builder.build();
         } catch (MailboxException e) {
             throw Throwables.propagate(e);
         }
     }
 
-    private Predicate<? super Mailbox> 
filterMailboxesById(Optional<ImmutableList<MailboxId>> ids) {
-        return (mailbox -> ids.map(list -> 
list.contains(mailbox.getId())).orElse(true));
-    }
-
-    private List<MailboxMetaData> retrieveUserMailboxes(MailboxSession 
session) throws MailboxException {
-        return mailboxManager.search(
-                MailboxQuery.builder(session).privateUserMailboxes().build(),
-                session);
+    private Stream<MailboxId> 
retrieveMailboxIds(Optional<ImmutableList<MailboxId>> mailboxIds, 
MailboxSession mailboxSession) throws MailboxException {
+        if (mailboxIds.isPresent()) {
+            return mailboxIds.get()
+                .stream();
+        } else {
+            List<MailboxMetaData> userMailboxes = mailboxManager.search(
+                
MailboxQuery.builder(mailboxSession).privateUserMailboxes().build(),
+                mailboxSession);
+            return userMailboxes
+                .stream()
+                .map(MailboxMetaData::getId);
+        }
     }
-
 }


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

Reply via email to