james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [16/22] james-project git commit: JAMES-1894 Fix sorting on Memory backend
Date Wed, 11 Jan 2017 16:45:43 GMT
JAMES-1894 Fix sorting on Memory backend

It should not discard messages if the sorted field is equal


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

Branch: refs/heads/master
Commit: 1732427456c16a6cc82da2eeefe0118434d4e0e3
Parents: 0ca60fb
Author: Benoit Tellier <btellier@linagora.com>
Authored: Thu Dec 22 17:00:16 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Mon Jan 9 22:01:06 2017 +0700

----------------------------------------------------------------------
 .../mailbox/store/search/MessageSearches.java   | 25 +++++++-------------
 .../search/AbstractMessageSearchIndexTest.java  | 13 ++++++++++
 2 files changed, 22 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/17324274/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java
index c2126f1..2f69adb 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java
@@ -32,9 +32,7 @@ import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
-import java.util.Set;
 import java.util.TimeZone;
-import java.util.TreeSet;
 
 import javax.mail.Flags;
 
@@ -71,6 +69,7 @@ import org.apache.james.mime4j.utils.search.MessageMatcher;
 
 import com.google.common.base.Function;
 import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
 /**
@@ -94,13 +93,13 @@ public class MessageSearches implements Iterable<SimpleMessageSearchIndex.Search
     public MessageSearches() {
     }
 
-    private Set<SimpleMessageSearchIndex.SearchResult> search() {
-        TreeSet<MailboxMessage> matched = new TreeSet<MailboxMessage>(CombinedComparator.create(query.getSorts()));
+    public Iterator<SimpleMessageSearchIndex.SearchResult> iterator() {
+        ImmutableList.Builder<MailboxMessage> builder = ImmutableList.builder();
         while (messages.hasNext()) {
             MailboxMessage m = messages.next();
             try {
                 if (isMatch(query, m)) {
-                    matched.add(m);
+                    builder.add(m);
                 }
             } catch (MailboxException e) {
                 if (session != null && session.getLog() != null) {
@@ -108,7 +107,9 @@ public class MessageSearches implements Iterable<SimpleMessageSearchIndex.Search
                 }
             }
         }
-        return FluentIterable.from(matched)
+        List<MailboxMessage> sortedResults = FluentIterable.from(builder.build())
+            .toSortedList(CombinedComparator.create(query.getSorts()));
+        return FluentIterable.from(sortedResults)
             .transform(new Function<MailboxMessage, SimpleMessageSearchIndex.SearchResult>()
{
                 @Override
                 public SimpleMessageSearchIndex.SearchResult apply(MailboxMessage input)
{
@@ -117,7 +118,8 @@ public class MessageSearches implements Iterable<SimpleMessageSearchIndex.Search
                         input.getMailboxId(),
                         input.getUid());
                 }
-            }).toSet();
+            })
+            .iterator();
     }
 
     /**
@@ -631,13 +633,4 @@ public class MessageSearches implements Iterable<SimpleMessageSearchIndex.Search
         return Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
     }
 
-    /**
-     * Return a {@link Iterator} which holds all uids which matched, sorted
-     * according to the SearchQuery
-     * 
-     */
-    public Iterator<SimpleMessageSearchIndex.SearchResult> iterator() {
-        return search().iterator();
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/17324274/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/AbstractMessageSearchIndexTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/AbstractMessageSearchIndexTest.java
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/AbstractMessageSearchIndexTest.java
index 74f8a3b..b3cd404 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/AbstractMessageSearchIndexTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/search/AbstractMessageSearchIndexTest.java
@@ -44,6 +44,7 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
 public abstract class AbstractMessageSearchIndexTest {
@@ -899,4 +900,16 @@ public abstract class AbstractMessageSearchIndexTest {
         assertThat(messageSearchIndex.search(session, mailbox, searchQuery))
             .containsExactly(m7.getUid());
     }
+
+    @Test
+    public void sortShouldNotDiscardResultWhenSearchingFieldIsIdentical() throws Exception
{
+        SearchQuery searchQuery = new SearchQuery();
+        searchQuery.andCriteria(new SearchQuery.AllCriterion());
+        searchQuery.setSorts(ImmutableList.of(new SearchQuery.Sort(SearchQuery.Sort.SortClause.Arrival)));
+
+        List<MessageId> actual = messageSearchIndex.search(session, MultimailboxesSearchQuery.from(searchQuery).build(),
LIMIT);
+
+        assertThat(actual).containsOnly(m1.getMessageId(), m2.getMessageId(), m3.getMessageId(),
m4.getMessageId(), m5.getMessageId(),
+            m6.getMessageId(), m7.getMessageId(), m8.getMessageId(), m9.getMessageId(), mOther.getMessageId());
+    }
 }


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


Mime
View raw message