james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r634041 - in /james/server/trunk: core-library/src/main/java/org/apache/james/mailboxmanager/ torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/ torque-mailboxmanager-function/src/test/java/org/apache/james...
Date Wed, 05 Mar 2008 21:54:05 GMT
Author: rdonkin
Date: Wed Mar  5 13:54:03 2008
New Revision: 634041

URL: http://svn.apache.org/viewvc?rev=634041&view=rev
Log:
More searches

Modified:
    james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java
    james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java
    james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java

Modified: james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java
URL: http://svn.apache.org/viewvc/james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java?rev=634041&r1=634040&r2=634041&view=diff
==============================================================================
--- james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java
(original)
+++ james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java
Wed Mar  5 13:54:03 2008
@@ -407,6 +407,16 @@
         }
         
         /**
+         * Is the given value in this range?
+         * @param value value to be tested
+         * @return true if the value is in range, 
+         * false otherwise
+         */
+        public boolean isIn(long value) {
+            return lowValue <= value && highValue >= value;
+        }
+        
+        /**
          * @see java.lang.Object#hashCode()
          */
         //@Override

Modified: james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java
URL: http://svn.apache.org/viewvc/james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java?rev=634041&r1=634040&r2=634041&view=diff
==============================================================================
--- james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java
(original)
+++ james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java
Wed Mar  5 13:54:03 2008
@@ -27,7 +27,11 @@
 import java.util.Locale;
 import java.util.TimeZone;
 
+import javax.mail.Flags;
+
 import org.apache.james.mailboxmanager.SearchQuery;
+import org.apache.james.mailboxmanager.SearchQuery.NumericRange;
+import org.apache.james.mailboxmanager.torque.om.MessageFlags;
 import org.apache.james.mailboxmanager.torque.om.MessageHeader;
 import org.apache.james.mailboxmanager.torque.om.MessageRow;
 import org.apache.james.mime4j.field.datetime.DateTime;
@@ -48,8 +52,106 @@
             result = matches((SearchQuery.SizeCriterion) criterion, row);
         } else if (criterion instanceof SearchQuery.HeaderCriterion) {
             result = matches((SearchQuery.HeaderCriterion) criterion, row);
+        } else if (criterion instanceof SearchQuery.UidCriterion) {
+            result = matches((SearchQuery.UidCriterion) criterion, row);
+        } else if (criterion instanceof SearchQuery.FlagCriterion) {
+            result = matches((SearchQuery.FlagCriterion) criterion, row);
+        } else if (criterion instanceof SearchQuery.AllCriterion) {
+            result = true;
+        } else if (criterion instanceof SearchQuery.ConjunctionCriterion) {
+            result = matches((SearchQuery.ConjunctionCriterion) criterion, row);
         } else {
             throw new UnsupportedSearchException();
+        }
+        return result;
+    }
+    
+    private static boolean matches(SearchQuery.ConjunctionCriterion criterion, MessageRow
row) throws TorqueException {
+        final int type = criterion.getType();
+        final List criteria = criterion.getCriteria();
+        switch (type) {
+            case SearchQuery.ConjunctionCriterion.NOR: return nor(criteria, row);
+            case SearchQuery.ConjunctionCriterion.OR: return or(criteria, row);
+            case SearchQuery.ConjunctionCriterion.AND: return and(criteria, row);
+            default: return false;
+        }
+    }
+    
+    private static boolean and(final List criteria, final MessageRow row) throws TorqueException
{
+        boolean result = true;
+        for (Iterator it = criteria.iterator(); it.hasNext();) {
+            final SearchQuery.Criterion criterion = (SearchQuery.Criterion) it.next();
+            final boolean matches = matches(criterion, row);
+            if (!matches) {
+                result = false;
+                break;
+            }
+        }
+        return result;
+    }
+    
+    private static boolean or(final List criteria, final MessageRow row) throws TorqueException
{
+        boolean result = false;
+        for (Iterator it = criteria.iterator(); it.hasNext();) {
+            final SearchQuery.Criterion criterion = (SearchQuery.Criterion) it.next();
+            final boolean matches = matches(criterion, row);
+            if (matches) {
+                result = true;
+                break;
+            }
+        }
+        return result;
+    }
+    
+    private static boolean nor(final List criteria, final MessageRow row) throws TorqueException
{
+        boolean result = true;
+        for (Iterator it = criteria.iterator(); it.hasNext();) {
+            final SearchQuery.Criterion criterion = (SearchQuery.Criterion) it.next();
+            final boolean matches = matches(criterion, row);
+            if (matches) {
+                result = false;
+                break;
+            }
+        }
+        return result;
+    }
+
+    private static boolean matches(SearchQuery.FlagCriterion criterion, MessageRow row) throws
TorqueException {
+        final SearchQuery.BooleanOperator operator = criterion.getOperator();
+        final boolean isSet = operator.isSet();
+        final Flags.Flag flag = criterion.getFlag();
+        final MessageFlags messageFlags = row.getMessageFlags();
+        final boolean result;
+        if (flag == Flags.Flag.ANSWERED) {
+            result = isSet == messageFlags.getAnswered();
+        } else if (flag == Flags.Flag.SEEN) {
+            result = isSet == messageFlags.getSeen(); 
+        } else if (flag == Flags.Flag.DRAFT) {
+            result = isSet == messageFlags.getDraft(); 
+        } else if (flag == Flags.Flag.FLAGGED) {
+            result = isSet == messageFlags.getFlagged();
+        } else if (flag == Flags.Flag.RECENT) {
+            result = isSet == messageFlags.getRecent();
+        } else if (flag == Flags.Flag.DELETED) {
+            result = isSet == messageFlags.getDeleted();
+        } else {
+            result = false;
+        }
+        return result;
+    }
+    
+    private static boolean matches(SearchQuery.UidCriterion criterion, MessageRow row) throws
TorqueException {
+        final SearchQuery.InOperator operator = criterion.getOperator();
+        final NumericRange[] ranges = operator.getRange();
+        final long uid = row.getUid();
+        final int length = ranges.length;
+        boolean result = false;
+        for (int i = 0; i < length; i++) {
+            final NumericRange numericRange = ranges[i];
+            if (numericRange.isIn(uid)) {
+                result = true;
+                break;
+            }
         }
         return result;
     }

Modified: james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java?rev=634041&r1=634040&r2=634041&view=diff
==============================================================================
--- james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java
(original)
+++ james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java
Wed Mar  5 13:54:03 2008
@@ -21,7 +21,10 @@
 
 import java.util.Date;
 
+import javax.mail.Flags;
+
 import org.apache.james.mailboxmanager.SearchQuery;
+import org.apache.james.mailboxmanager.torque.om.MessageFlags;
 import org.apache.james.mailboxmanager.torque.om.MessageHeader;
 import org.apache.james.mailboxmanager.torque.om.MessageRow;
 import org.apache.mailet.RFC2822Headers;
@@ -236,6 +239,188 @@
         addHeader(SUBJECT_FIELD.toUpperCase(), TEXT);
         assertFalse(SearchUtils.matches(SearchQuery.headerExists(DATE_FIELD), row));
         assertTrue(SearchUtils.matches(SearchQuery.headerExists(SUBJECT_FIELD), row));
+    }
+    
+    public void testShouldMatchUidRange() throws Exception {
+        row.setPrimaryKey(1, 1729);
+        assertFalse(SearchUtils.matches(SearchQuery.uid(range(1, 1)), row));
+        assertFalse(SearchUtils.matches(SearchQuery.uid(range(1728, 1728)), row));
+        assertTrue(SearchUtils.matches(SearchQuery.uid(range(1729, 1729)), row));
+        assertFalse(SearchUtils.matches(SearchQuery.uid(range(1730, 1730)), row));
+        assertFalse(SearchUtils.matches(SearchQuery.uid(range(1, 1728)), row));
+        assertTrue(SearchUtils.matches(SearchQuery.uid(range(1, 1729)), row));
+        assertTrue(SearchUtils.matches(SearchQuery.uid(range(1729, 1800)), row));
+        assertFalse(SearchUtils.matches(SearchQuery.uid(range(1730, Long.MAX_VALUE)), row));
+        assertFalse(SearchUtils.matches(SearchQuery.uid(range(1730, Long.MAX_VALUE, 1, 1728)),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.uid(range(1730, Long.MAX_VALUE, 1, 1729)),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.uid(range(1, 1728, 1800, 1810)), row));
+        assertTrue(SearchUtils.matches(SearchQuery.uid(range(1, 1, 1729, 1729)), row));
+        assertFalse(SearchUtils.matches(SearchQuery.uid(range(1, 1, 1800, 1800)), row));
+    }
+    
+    public void testShouldMatchSeenFlagSet() throws Exception {
+        setFlags(true, false, false, false, false, false);
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchAnsweredFlagSet() throws Exception {
+        setFlags(false, false, true, false, false, false);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.FLAGGED), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchFlaggedFlagSet() throws Exception {
+        setFlags(false, true, false, false, false, false);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.SEEN), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchDraftFlagSet() throws Exception {
+        setFlags(false, false, false, true, false, false);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.ANSWERED), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchDeletedFlagSet() throws Exception {
+        setFlags(false, false, false, false, true, false);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DRAFT), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchSeenRecentSet() throws Exception {
+        setFlags(false, false, false, false, false, true);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.DELETED), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsSet(Flags.Flag.RECENT), row));
+    }
+
+    public void testShouldMatchSeenFlagUnSet() throws Exception {
+        setFlags(false, true, true, true, true, true);
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchAnsweredFlagUnSet() throws Exception {
+        setFlags(true, true, false, true, true, true);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.FLAGGED), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchFlaggedFlagUnSet() throws Exception {
+        setFlags(true, false, true, true, true, true);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.SEEN), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchDraftFlagUnSet() throws Exception {
+        setFlags(true, true, true, false, true, true);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.ANSWERED), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchDeletedFlagUnSet() throws Exception {
+        setFlags(true, true, true, true, false, true);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DRAFT), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DELETED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchSeenRecentUnSet() throws Exception {
+        setFlags(true, true, true, true, true, false);
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.SEEN), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.FLAGGED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.ANSWERED), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DRAFT), row));
+        assertFalse(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.DELETED), row));
+        assertTrue(SearchUtils.matches(SearchQuery.flagIsUnSet(Flags.Flag.RECENT), row));
+    }
+    
+    public void testShouldMatchAll() throws Exception {
+        assertTrue(SearchUtils.matches(SearchQuery.all(), row));
+    }
+    
+    public void testShouldMatchNot() throws Exception {
+        assertFalse(SearchUtils.matches(SearchQuery.not(SearchQuery.all()), row));
+        assertTrue(SearchUtils.matches(SearchQuery.not(SearchQuery.headerExists(DATE_FIELD)),
row));
+    }
+    
+    public void testShouldMatchOr() throws Exception {
+        assertTrue(SearchUtils.matches(SearchQuery.or(SearchQuery.all(), SearchQuery.headerExists(DATE_FIELD)),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.or(SearchQuery.headerExists(DATE_FIELD),
SearchQuery.all()), row));
+        assertFalse(SearchUtils.matches(SearchQuery.or(SearchQuery.headerExists(DATE_FIELD),
SearchQuery.headerExists(DATE_FIELD)), row));
+        assertTrue(SearchUtils.matches(SearchQuery.or(SearchQuery.all(), SearchQuery.all()),
row));
+    }
+    
+    public void testShouldMatchAnd() throws Exception {
+        assertFalse(SearchUtils.matches(SearchQuery.and(SearchQuery.all(), SearchQuery.headerExists(DATE_FIELD)),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.and(SearchQuery.headerExists(DATE_FIELD),
SearchQuery.all()), row));
+        assertFalse(SearchUtils.matches(SearchQuery.and(SearchQuery.headerExists(DATE_FIELD),
SearchQuery.headerExists(DATE_FIELD)), row));
+        assertTrue(SearchUtils.matches(SearchQuery.and(SearchQuery.all(), SearchQuery.all()),
row));
+    }
+    
+    private void setFlags(boolean seen, boolean flagged, boolean answered, boolean draft,

+            boolean deleted, boolean recent) throws TorqueException {
+        final MessageFlags messageFlags = new MessageFlags();
+        messageFlags.setSeen(seen);
+        messageFlags.setFlagged(flagged);
+        messageFlags.setAnswered(answered);
+        messageFlags.setDraft(draft);
+        messageFlags.setDeleted(deleted);
+        messageFlags.setRecent(recent);
+        row.addMessageFlags(messageFlags);
+    }
+    
+    private SearchQuery.NumericRange[] range(long low, long high) {
+        SearchQuery.NumericRange[] results = {new SearchQuery.NumericRange(low, high)};
+        return results;
+    }
+    
+    private SearchQuery.NumericRange[] range(long lowOne, long highOne, long lowTwo, long
highTwo) {
+        SearchQuery.NumericRange[] results = {new SearchQuery.NumericRange(lowOne, highOne),
new SearchQuery.NumericRange(lowTwo, highTwo)};
+        return results;
     }
     
     private void addHeader(String fieldName, String value) throws TorqueException {



---------------------------------------------------------------------
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