You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2022/08/09 10:06:56 UTC

[GitHub] [james-project] ottoka commented on a diff in pull request #1111: JAMES-3799 Optimize memory requirements of SimpleMessageSearchIndex

ottoka commented on code in PR #1111:
URL: https://github.com/apache/james-project/pull/1111#discussion_r941151820


##########
mailbox/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java:
##########
@@ -113,6 +114,67 @@ private static UidCriterion findConjugatedUidCriterion(List<Criterion> crits) {
         return null;
     }
     
+    /**
+     * Walks down the query tree's conjunctions to find the highest necessary mail fetch type.
+     * @param crits - list of Criterion to search from
+     * @return required fetch type - metadata, headers, or full
+     */
+    private static FetchType getFetchTypeForCriteria(List<Criterion> crits) {
+        FetchType maximum = FetchType.METADATA;
+        for (Criterion crit : crits) {
+            if (crit instanceof ConjunctionCriterion) {
+                return getFetchTypeForCriteria(((ConjunctionCriterion) crit)
+                    .getCriteria());
+            } else {
+                maximum = maxFetchType(maximum, getFetchTypeForCriterion(crit));
+            }
+        }
+        return maximum;
+    }
+
+    private static FetchType getFetchTypeForCriterion(Criterion crit) {
+        if (crit instanceof SearchQuery.AllCriterion || crit instanceof SearchQuery.TextCriterion) {
+            return FetchType.FULL;
+        } else
+        if (crit instanceof SearchQuery.HeaderCriterion || crit instanceof SearchQuery.MimeMessageIDCriterion) {
+            return FetchType.HEADERS;
+        } else {
+            return FetchType.METADATA;
+        }
+    }
+
+    /**
+     * Searches a list of query sort options for the highest necessary mail fetch type.
+     * @param sorts - list of Sort to search
+     * @return required fetch type - metadata or headers
+     */
+    private static FetchType getFetchTypeForSorts(List<SearchQuery.Sort> sorts) {
+        return sorts.stream()
+            .map(SimpleMessageSearchIndex::getFetchTypeForSort)
+            .reduce(FetchType.METADATA, SimpleMessageSearchIndex::maxFetchType);
+    }
+
+    private static FetchType getFetchTypeForSort(SearchQuery.Sort sort) {
+        switch (sort.getSortClause()) {
+            case Arrival:
+            case Size:
+            case Uid:
+            case Id:
+                return FetchType.METADATA;
+            case MailboxCc:
+            case MailboxFrom:
+            case MailboxTo:
+            case BaseSubject:
+            case SentDate:
+            default:
+                return FetchType.HEADERS;
+        }
+    }
+
+    private static FetchType maxFetchType(FetchType a, FetchType b) {
+        return ArrayUtils.indexOf(FetchType.values(), a) >= ArrayUtils.indexOf(FetchType.values(), b) ? a : b;
+    }

Review Comment:
   I noticed Java Enums are actually Comparable based on their ordinal, so I will use that instead. I believe ordinal based on declaration order should be standard across Java versions. But it won't hurt to add a test four our specific use case as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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