james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1151938 - in /james/mailbox/trunk: api/src/main/java/org/apache/james/mailbox/ jcr/src/main/java/org/apache/james/mailbox/jcr/mail/ jpa/src/main/java/org/apache/james/mailbox/jpa/mail/ maildir/src/main/java/org/apache/james/mailbox/maildir...
Date Thu, 28 Jul 2011 17:48:12 GMT
Author: norman
Date: Thu Jul 28 17:48:10 2011
New Revision: 1151938

URL: http://svn.apache.org/viewvc?rev=1151938&view=rev
Log:
Refactor batch fetching of messages to use a special Iterator for it. This also includes to remove the batchsize param from the MessageRange as this does not belong in there. See MAILBOX-113

Added:
    james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResultIterator.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java
Removed:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/ResultIterator.java
Modified:
    james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageManager.java
    james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java
    james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java
    james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java
    james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java
    james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractMessageMapper.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningQuotaManager.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java

Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageManager.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageManager.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageManager.java Thu Jul 28 17:48:10 2011
@@ -138,27 +138,14 @@ public interface MessageManager {
      * @param set
      * @param fetchGroup
      *            data to fetch
+     * @param the batchSize to use to fetch the Messages in batches. The implementation may just ignore if it can't optimize the call with batches
      * @param mailboxSession
      *            not null
      * @return MessageResult with the fields defined by FetchGroup
      * @throws MailboxException
      */
-    Iterator<MessageResult> getMessages(MessageRange set, FetchGroup fetchGroup, MailboxSession mailboxSession) throws MailboxException;
+    MessageResultIterator getMessages(MessageRange set, FetchGroup fetchGroup, int batchSize, MailboxSession mailboxSession) throws MailboxException;
 
-    /**
-     * Gets messages in the given range.
-     * 
-     * @param set
-     * @param fetchGroup
-     *            data to fetch
-     * @param mailboxSession
-     *            not null
-     * @param messageCallback
-     *            data callback
-     * @return MessageResult with the fields defined by FetchGroup
-     * @throws MailboxException
-     */
-    void getMessages(MessageRange set, FetchGroup fetchGroup, MailboxSession mailboxSession, MessageCallback messageCallback) throws MailboxException;
 
     /**
      * Gets current meta data for the mailbox.<br>
@@ -178,13 +165,6 @@ public interface MessageManager {
     MetaData getMetaData(boolean resetRecent, MailboxSession mailboxSession, MessageManager.MetaData.FetchGroup fetchGroup) throws MailboxException;
 
     /**
-     * Message batch read callback method
-     */
-    public interface MessageCallback {
-        void onMessages(final Iterator<MessageResult> it) throws MailboxException;
-    }
-
-    /**
      * Meta data about the current state of the mailbox.
      */
     public interface MetaData {

Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageRange.java Thu Jul 28 17:48:10 2011
@@ -45,8 +45,6 @@ public class MessageRange implements Ite
 
     private static final int NOT_A_UID = -1;
 
-    // return all rows at ones
-    private static final int UNLIMITED_BATCH = 0;
 
     /**
      * Constructs a range consisting of a single message only.
@@ -56,19 +54,7 @@ public class MessageRange implements Ite
      * @return not null
      */
     public static MessageRange one(long uid) {
-        final MessageRange result = new MessageRange(Type.ONE, uid, uid, UNLIMITED_BATCH);
-        return result;
-    }
-
-    /**
-     * Constructs a range consisting of all messages.
-     * 
-     * @param batchSize
-     *            return max batchSize rows in chunk
-     * @return not null
-     */
-    public static MessageRange all(int batchSize) {
-        final MessageRange result = new MessageRange(Type.ALL, NOT_A_UID, NOT_A_UID, batchSize);
+        final MessageRange result = new MessageRange(Type.ONE, uid, uid);
         return result;
     }
 
@@ -78,7 +64,8 @@ public class MessageRange implements Ite
      * @return not null
      */
     public static MessageRange all() {
-        return all(UNLIMITED_BATCH);
+        final MessageRange result = new MessageRange(Type.ALL, NOT_A_UID, NOT_A_UID);
+        return result;
     }
 
     /**
@@ -89,11 +76,9 @@ public class MessageRange implements Ite
      *            first message UID
      * @param to
      *            last message UID
-     * @param batchSize
-     *            return max batchSize rows in chunk
      * @return not null
      */
-    public static MessageRange range(long from, long to, int batchSize) {
+    public static MessageRange range(long from, long to) {
         final MessageRange result;
         if (to == Long.MAX_VALUE || to < from) {
             to = NOT_A_UID;
@@ -102,49 +87,24 @@ public class MessageRange implements Ite
             // from and to is the same so no need to construct a real range
             result = one(from);
         } else {
-            result = new MessageRange(Type.RANGE, from, to, batchSize);
+            result = new MessageRange(Type.RANGE, from, to);
         }
         return result;
     }
 
-    /**
-     * Constructs an inclusive ranges of messages. The parameters will be
-     * checked and {@link #from(long)} used where appropriate.
-     * 
-     * @param from
-     *            first message UID
-     * @param to
-     *            last message UID
-     * @return not null
-     */
-    public static MessageRange range(long from, long to) {
-        return range(from, to, UNLIMITED_BATCH);
-    }
 
     /**
      * Constructs an inclusive, open ended range of messages.
      * 
      * @param from
      *            first message UID in range
-     * @param batchSize
-     *            return max batchSize rows in chunk
      * @return not null
      */
-    public static MessageRange from(long from, int batchSize) {
-        final MessageRange result = new MessageRange(Type.FROM, from, NOT_A_UID, batchSize);
+    public static MessageRange from(long from) {
+        final MessageRange result = new MessageRange(Type.FROM, from, NOT_A_UID);
         return result;
     }
 
-    /**
-     * Constructs an inclusive, open ended range of messages.
-     * 
-     * @param from
-     *            first message UID in range
-     * @return not null
-     */
-    public static MessageRange from(long from) {
-        return from(from, UNLIMITED_BATCH);
-    }
 
     private final Type type;
 
@@ -152,14 +112,11 @@ public class MessageRange implements Ite
 
     private final long uidTo;
 
-    private final int batchSize;
-
-    protected MessageRange(final Type type, final long uidFrom, final long uidTo, final int batchSize) {
+    protected MessageRange(final Type type, final long uidFrom, final long uidTo) {
         super();
         this.type = type;
         this.uidFrom = uidFrom;
         this.uidTo = uidTo;
-        this.batchSize = batchSize;
     }
 
     public Type getType() {
@@ -174,9 +131,6 @@ public class MessageRange implements Ite
         return uidTo;
     }
 
-    public int getBatchSize() {
-        return batchSize;
-    }
 
     /**
      * Return true if the uid is within the range
@@ -207,15 +161,9 @@ public class MessageRange implements Ite
     }
 
     public String toString() {
-        return "TYPE: " + type + " UID: " + uidFrom + ":" + uidTo + (batchSize > 0 ? " BATCH: " + batchSize : "");
+        return "TYPE: " + type + " UID: " + uidFrom + ":" + uidTo;
     }
 
-    public MessageRange getUnlimitedRange() {
-        if (this.batchSize == UNLIMITED_BATCH)
-            return this;
-        else
-            return new MessageRange(type, uidFrom, uidTo, UNLIMITED_BATCH);
-    }
     
     /**
      * Converts the given {@link Collection} of uids to a {@link List} of {@link MessageRange} instances

Added: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResultIterator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResultIterator.java?rev=1151938&view=auto
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResultIterator.java (added)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MessageResultIterator.java Thu Jul 28 17:48:10 2011
@@ -0,0 +1,42 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox;
+
+import java.util.Iterator;
+
+/**
+ * A special {@link Iterator} which allows to access the {@link MailboxException} if one was thrown while try to lazy fetch the {@link MessageResult}'s in batches
+ * 
+ *
+ */
+public interface MessageResultIterator extends Iterator<MessageResult> {
+
+    /**
+     * This method should get called after the {@link #hasNext()} method returns
+     * <code>false</code>.
+     * 
+     * If it does not return <code>null</code> an error was thrown before while
+     * try to lazy fetch th next batch of {@link MessageResult}'s
+     * 
+     * @return exception
+     */
+    public MailboxException getException();
+
+}

Modified: james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java (original)
+++ james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java Thu Jul 28 17:48:10 2011
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -254,43 +255,34 @@ public class JCRMessageMapper extends Ab
     }
 
 
+
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
      */
-    public void findInMailbox(Mailbox<String> mailbox, MessageRange set, FetchType fType, MessageCallback<String> callback) throws MailboxException {
+    public Iterator<Message<String>> findInMailbox(Mailbox<String> mailbox, MessageRange set, FetchType fType, int max) throws MailboxException {
         try {
             List<Message<String>> results;
             long from = set.getUidFrom();
             final long to = set.getUidTo();
-            final int batchSize = set.getBatchSize();
             final Type type = set.getType();
 
-            do {
-                switch (type) {
-                default:
-                case ALL:
-                    results = findMessagesInMailbox(mailbox, batchSize);
-                    break;
-                case FROM:
-                    results = findMessagesInMailboxAfterUID(mailbox, from, batchSize);
-                    break;
-                case ONE:
-                    results = findMessageInMailboxWithUID(mailbox, from);
-                    break;
-                case RANGE:
-                    results = findMessagesInMailboxBetweenUIDs(mailbox, from, to, batchSize);
-                    break;
-                }
-
-                if (results.size() > 0) {
-                    callback.onMessages(results);
-
-                    // move the start UID behind the last fetched message UID
-                    from = results.get(results.size() - 1).getUid() + 1;
+            switch (type) {
+            default:
+            case ALL:
+                results = findMessagesInMailbox(mailbox, max);
+                break;
+            case FROM:
+                results = findMessagesInMailboxAfterUID(mailbox, from, max);
+                break;
+            case ONE:
+                results = findMessageInMailboxWithUID(mailbox, from);
+                break;
+            case RANGE:
+                results = findMessagesInMailboxBetweenUIDs(mailbox, from, to, max);
+                break;
                 }
-
-            } while (results.size() > 0 && batchSize > 0);
+            return results.iterator();
         } catch (RepositoryException e) {
             throw new MailboxException("Unable to search MessageRange " + set + " in mailbox " + mailbox, e);
         }

Modified: james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java (original)
+++ james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java Thu Jul 28 17:48:10 2011
@@ -19,6 +19,7 @@
 package org.apache.james.mailbox.jpa.mail;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -119,44 +120,34 @@ public class JPAMessageMapper extends Ab
 
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
      */
-    public void findInMailbox(Mailbox<Long> mailbox, MessageRange set, FetchType fType, MessageCallback<Long> callback) throws MailboxException {
+    public Iterator<Message<Long>> findInMailbox(Mailbox<Long> mailbox, MessageRange set, FetchType fType, int max) throws MailboxException {
         try {
             List<Message<Long>> results;
             long from = set.getUidFrom();
             final long to = set.getUidTo();
-            final int batchSize = set.getBatchSize();
             final Type type = set.getType();
 
-            // when batch is specified fetch data in chunks and send back in
-            // batches
-            do {
-                switch (type) {
-                default:
-                case ALL:
-                    results = findMessagesInMailbox(mailbox, batchSize);
-                    break;
-                case FROM:
-                    results = findMessagesInMailboxAfterUID(mailbox, from, batchSize);
-                    break;
-                case ONE:
-                    results = findMessagesInMailboxWithUID(mailbox, from);
-                    break;
-                case RANGE:
-                    results = findMessagesInMailboxBetweenUIDs(mailbox, from, to, batchSize);
-                    break;
-                }
-
-                if (results.size() > 0) {
-                    callback.onMessages(results);
-
-                    // move the start UID behind the last fetched message UID
-                    from = results.get(results.size() - 1).getUid() + 1;
-                }
-
-            } while (results.size() > 0 && batchSize > 0);
+			switch (type) {
+			default:
+			case ALL:
+				results = findMessagesInMailbox(mailbox, max);
+				break;
+			case FROM:
+				results = findMessagesInMailboxAfterUID(mailbox, from, max);
+				break;
+			case ONE:
+				results = findMessagesInMailboxWithUID(mailbox, from);
+				break;
+			case RANGE:
+				results = findMessagesInMailboxBetweenUIDs(mailbox, from, to,
+						max);
+				break;
+			}
 
+			return results.iterator();
+               
         } catch (PersistenceException e) {
             throw new MailboxException("Search of MessageRange " + set + " failed in mailbox " + mailbox, e);
         }

Modified: james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java (original)
+++ james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java Thu Jul 28 17:48:10 2011
@@ -112,40 +112,31 @@ public class MaildirMessageMapper extend
 
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
      */
-    public void findInMailbox(Mailbox<Integer> mailbox, MessageRange set, FetchType fType, MessageCallback<Integer> callback)
+    public Iterator<Message<Integer>> findInMailbox(Mailbox<Integer> mailbox, MessageRange set, FetchType fType, int max)
     throws MailboxException {
         final List<Message<Integer>> results;
         final long from = set.getUidFrom();
         final long to = set.getUidTo();
-        final int batchSize = set.getBatchSize(); 
         final Type type = set.getType();
         switch (type) {
         default:
         case ALL:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, null, 0, -1);
+            results = findMessagesInMailboxBetweenUIDs(mailbox, null, 0, -1, max);
             break;
         case FROM:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, null, from, -1);
+            results = findMessagesInMailboxBetweenUIDs(mailbox, null, from, -1, max);
             break;
         case ONE:
             results = findMessageInMailboxWithUID(mailbox, from);
             break;
         case RANGE:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, null, from, to);
+            results = findMessagesInMailboxBetweenUIDs(mailbox, null, from, to, max);
             break;       
         }
-        
-        if (batchSize > 0) {
-            int i = 0;
-            while (i * batchSize < results.size()) {
-                callback.onMessages(results.subList(i * batchSize, (i + 1) * batchSize < results.size() ? (i + 1) * batchSize : results.size()));
-                i++;
-            }
-        } else {
-            callback.onMessages(results);
-        }
+        return results.iterator();
+       
     }
 
     private List<Message<Integer>> findMessageInMailboxWithUID(Mailbox<Integer> mailbox, long uid)
@@ -166,8 +157,9 @@ public class MaildirMessageMapper extend
     }
 
     private List<Message<Integer>> findMessagesInMailboxBetweenUIDs(Mailbox<Integer> mailbox,
-            FilenameFilter filter, long from, long to) throws MailboxException {
+            FilenameFilter filter, long from, long to, int max) throws MailboxException {
         MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
+        int cur = 0;
         SortedMap<Long, MaildirMessageName> uidMap = null;
         try {
             if (filter != null)
@@ -178,6 +170,10 @@ public class MaildirMessageMapper extend
             ArrayList<Message<Integer>> messages = new ArrayList<Message<Integer>>();
             for (Entry<Long, MaildirMessageName> entry : uidMap.entrySet()) {
                 messages.add(new MaildirMessage(mailbox, entry.getKey(), entry.getValue()));
+                if (max != -1) {
+                	cur++;
+                	if (cur >= max) break;
+                }
             }
             return messages;
         } catch (IOException e) {
@@ -388,69 +384,64 @@ public class MaildirMessageMapper extend
         final List<UpdatedFlags> updatedFlags = new ArrayList<UpdatedFlags>();
         final MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
 
-        findInMailbox(mailbox, set, FetchType.Metadata, new MessageCallback<Integer>() {
+        Iterator<Message<Integer>> it = findInMailbox(mailbox, set, FetchType.Metadata, -1);
+        while(it.hasNext()) {
+        	final Message<Integer> member = it.next();
+            Flags originalFlags = member.createFlags();
+            if (replace) {
+                member.setFlags(flags);
+            } else {
+                Flags current = member.createFlags();
+                if (value) {
+                    current.add(flags);
+                } else {
+                    current.remove(flags);
+                }
+                member.setFlags(current);
+            }
+            Flags newFlags = member.createFlags();
 
-            public void onMessages(List<Message<Integer>> members) throws MailboxException {
-                for (final Message<Integer> member : members) {
-                    Flags originalFlags = member.createFlags();
-                    if (replace) {
-                        member.setFlags(flags);
+            try {
+                MaildirMessageName messageName = folder.getMessageNameByUid(mailboxSession, member.getUid());
+                if (messageName != null) {
+                    File messageFile = messageName.getFile();
+                    // System.out.println("save existing " + message +
+                    // " as " + messageFile.getName());
+                    messageName.setFlags(member.createFlags());
+                    // this automatically moves messages from new to cur if
+                    // needed
+                    String newMessageName = messageName.getFullName();
+
+                    File newMessageFile;
+                    
+                    // See MAILBOX-57
+                    if (newFlags.contains(Flag.RECENT)) {
+                        // message is recent so save it in the new folder
+                        newMessageFile = new File(folder.getNewFolder(), newMessageName);
                     } else {
-                        Flags current = member.createFlags();
-                        if (value) {
-                            current.add(flags);
-                        } else {
-                            current.remove(flags);
-                        }
-                        member.setFlags(current);
+                        newMessageFile = new File(folder.getCurFolder(), newMessageName);
                     }
-                    Flags newFlags = member.createFlags();
+                    long modSeq;
+                    // if the flags don't have change we should not try to move the file
+                    if (newMessageFile.equals(messageFile) == false) {
+                        FileUtils.moveFile(messageFile, newMessageFile );
+                        modSeq = newMessageFile.lastModified();
 
-                    try {
-                        MaildirMessageName messageName = folder.getMessageNameByUid(mailboxSession, member.getUid());
-                        if (messageName != null) {
-                            File messageFile = messageName.getFile();
-                            // System.out.println("save existing " + message +
-                            // " as " + messageFile.getName());
-                            messageName.setFlags(member.createFlags());
-                            // this automatically moves messages from new to cur if
-                            // needed
-                            String newMessageName = messageName.getFullName();
-
-                            File newMessageFile;
-                        
-                            // See MAILBOX-57
-                            if (newFlags.contains(Flag.RECENT)) {
-                                // message is recent so save it in the new folder
-                                newMessageFile = new File(folder.getNewFolder(), newMessageName);
-                            } else {
-                                newMessageFile = new File(folder.getCurFolder(), newMessageName);
-                            }
-                            long modSeq;
-                            // if the flags don't have change we should not try to move the file
-                            if (newMessageFile.equals(messageFile) == false) {
-                                FileUtils.moveFile(messageFile, newMessageFile );
-                                modSeq = newMessageFile.lastModified();
-
-                            } else {
-                                modSeq = messageFile.lastModified();
-                            } 
-                            member.setModSeq(modSeq);
-                        
-                            updatedFlags.add(new UpdatedFlags(member.getUid(), modSeq, originalFlags, newFlags));
-
-                            long uid = member.getUid();
-                            folder.update(mailboxSession, uid, newMessageName);
-                        }
-                    } catch (IOException e) {
-                        throw new MailboxException("Failure while save Message " + member + " in Mailbox " + mailbox, e);
-                    }
+                    } else {
+                        modSeq = messageFile.lastModified();
+                    } 
+                    member.setModSeq(modSeq);
+                    
+                    updatedFlags.add(new UpdatedFlags(member.getUid(), modSeq, originalFlags, newFlags));
 
+                    long uid = member.getUid();
+                    folder.update(mailboxSession, uid, newMessageName);
                 }
-
+            } catch (IOException e) {
+                throw new MailboxException("Failure while save Message " + member + " in Mailbox " + mailbox, e);
             }
-        });
-        
+
+        }
         return updatedFlags.iterator();       
         
     }
@@ -468,13 +459,13 @@ public class MaildirMessageMapper extend
             results = findMessagesInMailbox(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, -1);
             break;
         case FROM:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, -1);
+            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, -1, -1);
             break;
         case ONE:
             results = findDeletedMessageInMailboxWithUID(mailbox, from);
             break;
         case RANGE:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, to);
+            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, to, -1);
             break;       
         }
         Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();

Modified: james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java (original)
+++ james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java Thu Jul 28 17:48:10 2011
@@ -91,13 +91,13 @@ public class InMemoryMessageMapper exten
         getMembershipByUidForMailbox(mailbox).remove(message.getUid());
     }
 
+
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(java.lang.Object, org.apache.james.mailbox.MessageRange)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
      */
-    public void findInMailbox(Mailbox<Long> mailbox, MessageRange set, FetchType ftype, MessageCallback<Long> callback) throws MailboxException {
-        final List<Message<Long>> results;
-        final int batchSize = set.getBatchSize();
+    public Iterator<Message<Long>> findInMailbox(Mailbox<Long> mailbox, MessageRange set, FetchType ftype, int max) throws MailboxException {
+        List<Message<Long>> results;
         final MessageRange.Type type = set.getType();
         switch (type) {
             case ALL:
@@ -133,15 +133,10 @@ public class InMemoryMessageMapper exten
         }
         Collections.sort(results);
         
-        if(batchSize > 0) {
-	        int i = 0;
-	        while(i*batchSize < results.size()) {
-	        	callback.onMessages(results.subList(i*batchSize, (i+1)*batchSize < results.size() ? (i+1)*batchSize : results.size()));
-	        	i++;
-	        }
-        } else {
-        	callback.onMessages(results);
+        if (results.size() > max) {
+        	results = results.subList(0, max -1);
         }
+        return results.iterator();
     }
     
 
@@ -246,20 +241,15 @@ public class InMemoryMessageMapper exten
     public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(final Mailbox<Long> mailbox, MessageRange set) throws MailboxException {
         final Map<Long, MessageMetaData> filteredResult = new HashMap<Long, MessageMetaData>();
 
-        findInMailbox(mailbox, set, FetchType.Metadata, new MessageCallback<Long>() {
-
-            public void onMessages(List<Message<Long>> results) throws MailboxException {
-                for (final Iterator<Message<Long>> it = results.iterator(); it.hasNext();) {
-                    Message<Long> member = it.next();
-                    if (member.isDeleted()) {
-                        filteredResult.put(member.getUid(), new SimpleMessageMetaData(member));
+        Iterator<Message<Long>> it = findInMailbox(mailbox, set, FetchType.Metadata, -1);
+        while(it.hasNext()) {
+            Message<Long> member = it.next();
+            if (member.isDeleted()) {
+                filteredResult.put(member.getUid(), new SimpleMessageMetaData(member));
 
-                        delete(mailbox, member);
-                    }
-                }
+                delete(mailbox, member);
             }
-        });
-
+        }
         return filteredResult;
     }
 }

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java Thu Jul 28 17:48:10 2011
@@ -45,7 +45,7 @@ import org.apache.james.mailbox.MailboxS
 import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.MessageMetaData;
 import org.apache.james.mailbox.MessageRange;
-import org.apache.james.mailbox.MessageResult;
+import org.apache.james.mailbox.MessageResultIterator;
 import org.apache.james.mailbox.ReadOnlyException;
 import org.apache.james.mailbox.SearchQuery;
 import org.apache.james.mailbox.UpdatedFlags;
@@ -544,93 +544,18 @@ public class StoreMessageManager<Id> imp
 
 
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.mailbox.Mailbox#getMessages(org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.MessageResult.FetchGroup, org.apache.james.mailbox.MailboxSession)
-     */
-    public Iterator<MessageResult> getMessages(final MessageRange set, FetchGroup fetchGroup, MailboxSession mailboxSession) throws MailboxException {
-
-        class InterceptingCallback implements MessageCallback {
-            Iterator<MessageResult> iterator;
-
-            public void onMessages(Iterator<MessageResult> it) throws MailboxException {
-                iterator = it;
-            }
-
-            public Iterator<MessageResult> getIterator() {
-                if (iterator == null) {
-                    iterator = new ResultIterator<Id>(null, null);
-                }
-                return iterator;
-            }
-        }
-
-        // if we are intercepting callback - let's make it effective
-        MessageRange nonBatchedSet = set.getUnlimitedRange();
-
-        // intercepting callback
-        InterceptingCallback callback = new InterceptingCallback();
-        this.getMessages(nonBatchedSet, fetchGroup, mailboxSession, callback);
 
-        return callback.getIterator();
-    }
-   
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.MessageManager#getMessages(org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.MessageResult.FetchGroup, org.apache.james.mailbox.MailboxSession, int, org.apache.james.mailbox.MessageManager.MessageCallback)
-     */
-    public void getMessages(MessageRange set, final FetchGroup fetchGroup, MailboxSession mailboxSession, final MessageCallback messageCallback) throws MailboxException {
-
-        mapperFactory.getMessageMapper(mailboxSession).findInMailbox(getMailboxEntity(), set, getFetchType(fetchGroup), new org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback<Id>() {
-            public void onMessages(List<Message<Id>> rows) throws MailboxException {
-                messageCallback.onMessages(new ResultIterator<Id>(rows.iterator(), fetchGroup));
-            }
-        });
-    }
-    
-    /**
-     * Use the passed {@link FetchGroup} and calculate the right {@link FetchType} for it
-     * 
-     * @param group
-     * @return fetchType
+     * @see org.apache.james.mailbox.MessageManager#getMessages(org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.MessageResult.FetchGroup, int, org.apache.james.mailbox.MailboxSession)
      */
-    protected static final FetchType getFetchType(FetchGroup group) {
-        int content = group.content();
-        boolean headers = false;
-        boolean body = false;
-        boolean full = false;
-
-        if ((content & FetchGroup.HEADERS) > 0) {
-            headers = true;
-            content -= FetchGroup.HEADERS;
-        }
-        if ((content & FetchGroup.BODY_CONTENT) > 0) {
-            body = true;
-            content -= FetchGroup.BODY_CONTENT;
-        }
-
-        if ((content & FetchGroup.FULL_CONTENT) > 0) {
-            full = true;
-            content -= FetchGroup.FULL_CONTENT;
-        }
-
-        if ((content & FetchGroup.MIME_DESCRIPTOR) > 0) {
-            // If we need the mimedescriptor we MAY need the full content later too. 
-            // This gives us no other choice then request it
-            full = true;
-            content -= FetchGroup.MIME_DESCRIPTOR;
-        }
-        if (full || (body && headers)) {
-            return FetchType.Full;
-        } else if (body) {
-            return FetchType.Body;
-        } else if (headers) {
-            return FetchType.Headers;
-        } else {
-            return FetchType.Metadata;
-        }
+    public MessageResultIterator getMessages(final MessageRange set, FetchGroup fetchGroup, int batchSize, MailboxSession mailboxSession) throws MailboxException {
+        final MessageMapper<Id> messageMapper = mapperFactory.getMessageMapper(mailboxSession);
+        return new StoreMessageResultIterator<Id>(messageMapper, mailbox, set, batchSize, fetchGroup);
     }
 
+ 
+
     /**
      * Return a List which holds all uids of recent messages and optional reset
      * the recent flag on the messages for the uids
@@ -694,21 +619,21 @@ public class StoreMessageManager<Id> imp
     }
 
 
-    private Iterator<MessageMetaData> copy(final List<Message<Id>> originalRows, final MailboxSession session) throws MailboxException {
+    private Iterator<MessageMetaData> copy(final Iterator<Message<Id>> originalRows, final MailboxSession session) throws MailboxException {
         try {
             final List<MessageMetaData> copiedRows = new ArrayList<MessageMetaData>();
             final MessageMapper<Id> messageMapper = mapperFactory.getMessageMapper(session);
 
-            for (final Message<Id> originalMessage:originalRows) {
-               MessageMetaData data = messageMapper.execute(new Mapper.Transaction<MessageMetaData>() {
+            while(originalRows.hasNext()) {
+				final Message<Id> originalMessage = originalRows.next();
+				MessageMetaData data = messageMapper.execute(new Mapper.Transaction<MessageMetaData>() {
+					public MessageMetaData run() throws MailboxException {
+					    return messageMapper.copy(getMailboxEntity(), originalMessage);
 
-                    public MessageMetaData run() throws MailboxException {
-                        return messageMapper.copy(getMailboxEntity(), originalMessage);
-                        
-                    }
-                    
-                });
-               copiedRows.add(data);
+					}
+
+				});
+				copiedRows.add(data);
             }
             return copiedRows.iterator();
         } catch (MailboxException e) {
@@ -725,16 +650,13 @@ public class StoreMessageManager<Id> imp
             MessageMapper<Id> messageMapper = mapperFactory.getMessageMapper(session);
 
             final Map<Long, MessageMetaData> copiedMessages = new HashMap<Long, MessageMetaData>();
-            messageMapper.findInMailbox(getMailboxEntity(), set, FetchType.Full, new org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback<Id>() {
-
-                public void onMessages(List<Message<Id>> originalRows) throws MailboxException {
-                    Iterator<MessageMetaData> ids = to.copy(originalRows, session);
-                    while (ids.hasNext()) {
-                        MessageMetaData data = ids.next();
-                        copiedMessages.put(data.getUid(), data);
-                    }
-                }
-            });
+            Iterator<Message<Id>> originalRows = messageMapper.findInMailbox(mailbox, set, FetchType.Full, -1);
+            Iterator<MessageMetaData> ids = to.copy(originalRows, session);
+            while (ids.hasNext()) {
+                MessageMetaData data = ids.next();
+                copiedMessages.put(data.getUid(), data);
+            }
+            
             return copiedMessages;
 
         } catch (MailboxException e) {

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java?rev=1151938&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java Thu Jul 28 17:48:10 2011
@@ -0,0 +1,262 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mailbox.store;
+
+import java.util.Date;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import javax.mail.Flags;
+
+import org.apache.james.mailbox.Content;
+import org.apache.james.mailbox.MailboxException;
+import org.apache.james.mailbox.MessageRange;
+import org.apache.james.mailbox.MimeDescriptor;
+import org.apache.james.mailbox.MessageRange.Type;
+import org.apache.james.mailbox.MessageResult;
+import org.apache.james.mailbox.MessageResult.FetchGroup;
+import org.apache.james.mailbox.MessageResultIterator;
+import org.apache.james.mailbox.store.mail.MessageMapper;
+import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
+import org.apache.james.mailbox.store.mail.model.Mailbox;
+import org.apache.james.mailbox.store.mail.model.Message;
+
+public class StoreMessageResultIterator<Id> implements MessageResultIterator {
+
+    private Iterator<Message<Id>> next = null;
+    private MailboxException exception;
+    private Mailbox<Id> mailbox;
+    private FetchGroup group;
+    private long from;
+    private long to;
+    private int batchSize;
+    private Type type;
+    private boolean done = false;
+    private MessageMapper<Id> mapper;
+    private FetchType ftype;
+
+    public StoreMessageResultIterator(MessageMapper<Id> mapper, Mailbox<Id> mailbox, MessageRange range, int batchSize, org.apache.james.mailbox.MessageResult.FetchGroup group) {
+        this.mailbox = mailbox;
+        this.group = group;
+        this.mapper = mapper;
+        this.from = range.getUidFrom();
+        this.to = range.getUidTo();
+        this.batchSize = batchSize;
+        this.type = range.getType();
+        this.ftype = getFetchType(group);
+    }
+
+    /**
+     * Use the passed {@link FetchGroup} and calculate the right
+     * {@link FetchType} for it
+     * 
+     * @param group
+     * @return fetchType
+     */
+    private static final FetchType getFetchType(FetchGroup group) {
+        int content = group.content();
+        boolean headers = false;
+        boolean body = false;
+        boolean full = false;
+
+        if ((content & FetchGroup.HEADERS) > 0) {
+            headers = true;
+            content -= FetchGroup.HEADERS;
+        }
+        if ((content & FetchGroup.BODY_CONTENT) > 0) {
+            body = true;
+            content -= FetchGroup.BODY_CONTENT;
+        }
+
+        if ((content & FetchGroup.FULL_CONTENT) > 0) {
+            full = true;
+            content -= FetchGroup.FULL_CONTENT;
+        }
+
+        if ((content & FetchGroup.MIME_DESCRIPTOR) > 0) {
+            // If we need the mimedescriptor we MAY need the full content later
+            // too.
+            // This gives us no other choice then request it
+            full = true;
+            content -= FetchGroup.MIME_DESCRIPTOR;
+        }
+        if (full || (body && headers)) {
+            return FetchType.Full;
+        } else if (body) {
+            return FetchType.Body;
+        } else if (headers) {
+            return FetchType.Headers;
+        } else {
+            return FetchType.Metadata;
+        }
+    }
+
+    @Override
+    public boolean hasNext() {
+        if (next == null || !next.hasNext()) {
+            try {
+                MessageRange range;
+
+                switch (type) {
+                default:
+                case ALL:
+                    range = MessageRange.all();
+                    break;
+                case FROM:
+                    range = MessageRange.from(from);
+
+                    break;
+                case ONE:
+                    range = MessageRange.one(from);
+                    break;
+                case RANGE:
+                    range = MessageRange.range(from, to);
+                    break;
+                }
+
+                next = mapper.findInMailbox(mailbox, range, ftype, batchSize);
+            } catch (MailboxException e) {
+                this.exception = e;
+                done = true;
+            }
+            return !done;
+        } else {
+            return true;
+        }
+    }
+
+    @Override
+    public MessageResult next() {
+        if (hasNext()) {
+            final Message<Id> message = next.next();
+            MessageResult result;
+            try {
+
+                result = ResultUtils.loadMessageResult(message, group);
+            } catch (MailboxException e) {
+                result = new UnloadedMessageResult<Id>(message, e);
+            }
+
+            // move the start UID behind the last fetched message UID if needed
+            if (!next.hasNext()) {
+                from = result.getUid() + 1;
+            }
+            return result;
+        }
+        throw new NoSuchElementException();
+    }
+
+    @Override
+    public void remove() {
+        throw new UnsupportedOperationException("Read only");
+    }
+
+    @Override
+    public MailboxException getException() {
+        return exception;
+    }
+
+    private static final class UnloadedMessageResult<Id> implements MessageResult {
+        private final MailboxException exception;
+
+        private final Date internalDate;
+
+        private final long size;
+
+        private final long uid;
+
+        private final Flags flags;
+
+        private long modSeq = -1;
+
+        public UnloadedMessageResult(final Message<Id> message, final MailboxException exception) {
+            super();
+            internalDate = message.getInternalDate();
+            size = message.getFullContentOctets();
+            uid = message.getUid();
+            flags = message.createFlags();
+            modSeq = message.getModSeq();
+            this.exception = exception;
+        }
+
+        public Flags getFlags() {
+            return flags;
+        }
+
+        public Content getFullContent() throws MailboxException {
+            throw exception;
+        }
+
+        public Date getInternalDate() {
+            return internalDate;
+        }
+
+        public Content getBody() throws MailboxException {
+            throw exception;
+        }
+
+        public long getSize() {
+            return size;
+        }
+
+        public long getUid() {
+            return uid;
+        }
+
+        public Iterator<Header> headers() throws MailboxException {
+            throw exception;
+        }
+
+        public int compareTo(MessageResult that) {
+            // Java 1.5 return (int) Math.signum(uid - that.getUid());
+            long diff = uid - that.getUid();
+            return (int) diff == 0 ? 0 : diff > 0 ? 1 : -1;
+        }
+
+        public Content getFullContent(MimePath path) throws MailboxException {
+            throw exception;
+        }
+
+        public Iterator<Header> iterateHeaders(MimePath path) throws MailboxException {
+            throw exception;
+        }
+
+        public Iterator<Header> iterateMimeHeaders(MimePath path) throws MailboxException {
+            throw exception;
+        }
+
+        public Content getBody(MimePath path) throws MailboxException {
+            throw exception;
+        }
+
+        public Content getMimeBody(MimePath path) throws MailboxException {
+            throw exception;
+        }
+
+        public MimeDescriptor getMimeDescriptor() throws MailboxException {
+            throw exception;
+        }
+
+        public long getModSeq() {
+            return modSeq;
+        }
+
+    }
+
+}

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractMessageMapper.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractMessageMapper.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/AbstractMessageMapper.java Thu Jul 28 17:48:10 2011
@@ -77,47 +77,42 @@ public abstract class AbstractMessageMap
      */
     public Iterator<UpdatedFlags> updateFlags(final Mailbox<Id> mailbox, final Flags flags, final boolean value, final boolean replace, final MessageRange set) throws MailboxException {
         final List<UpdatedFlags> updatedFlags = new ArrayList<UpdatedFlags>();
-        findInMailbox(mailbox, set, FetchType.Metadata, new MessageCallback<Id>() {
-
-            public void onMessages(List<Message<Id>> members) throws MailboxException {
-                
-                long modSeq = -1;
-                if (members.isEmpty() == false) {
-                    // if a mailbox does not support mod-sequences the provider may be null
-                    if (modSeqProvider != null) {
-                        modSeq = modSeqProvider.nextModSeq(mailboxSession, mailbox);
-                    }
-                }
-                for (final Message<Id> member : members) {
-                    Flags originalFlags = member.createFlags();
-                    if (replace) {
-                        member.setFlags(flags);
-                    } else {
-                        Flags current = member.createFlags();
-                        if (value) {
-                            current.add(flags);
-                        } else {
-                            current.remove(flags);
-                        }
-                        member.setFlags(current);
-                    }
-                    Flags newFlags = member.createFlags();
-                    if (UpdatedFlags.flagsChanged(originalFlags, newFlags)) {
-                        // increase the mod-seq as we changed the flags
-                        member.setModSeq(modSeq);
-                        save(mailbox, member);
-                    }
-
-                    
-                    UpdatedFlags uFlags = new UpdatedFlags(member.getUid(), member.getModSeq(), originalFlags, newFlags);
-                    
-                    updatedFlags.add(uFlags);
-                    
-
+        Iterator<Message<Id>> messages = findInMailbox(mailbox, set, FetchType.Metadata, -1);
+        
+        long modSeq = -1;
+        if (messages.hasNext() == false) {
+            // if a mailbox does not support mod-sequences the provider may be null
+            if (modSeqProvider != null) {
+                modSeq = modSeqProvider.nextModSeq(mailboxSession, mailbox);
+            }
+        }
+        while(messages.hasNext()) {
+        	final Message<Id> member = messages.next();
+            Flags originalFlags = member.createFlags();
+            if (replace) {
+                member.setFlags(flags);
+            } else {
+                Flags current = member.createFlags();
+                if (value) {
+                    current.add(flags);
+                } else {
+                    current.remove(flags);
                 }
-               
+                member.setFlags(current);
+            }
+            Flags newFlags = member.createFlags();
+            if (UpdatedFlags.flagsChanged(originalFlags, newFlags)) {
+                // increase the mod-seq as we changed the flags
+                member.setModSeq(modSeq);
+                save(mailbox, member);
             }
-        });
+
+            
+            UpdatedFlags uFlags = new UpdatedFlags(member.getUid(), member.getModSeq(), originalFlags, newFlags);
+            
+            updatedFlags.add(uFlags);
+            
+        }
 
         return updatedFlags.iterator();
 

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java Thu Jul 28 17:48:10 2011
@@ -46,10 +46,10 @@ public interface MessageMapper<Id> exten
      * @param mailbox The mailbox to search
      * @param set message range for batch processing
      * @param type
-     * @param callback callback object 
+     * @param limt the maximal limit of returned {@link Message}'s. Use -1 to set no limit
      * @throws MailboxException
      */
-    void findInMailbox(Mailbox<Id> mailbox, MessageRange set, FetchType type, MessageCallback<Id> callback)
+    Iterator<Message<Id>> findInMailbox(Mailbox<Id> mailbox, MessageRange set, FetchType type, int limit)
             throws MailboxException;
 
     /**
@@ -173,18 +173,6 @@ public interface MessageMapper<Id> exten
      */
     long getHighestModSeq(Mailbox<Id> mailbox) throws MailboxException;
     
-
-    public interface MessageCallback<Id> {
-        
-        /**
-         * Callback which get executed for a {@link List} of {@link Message}'s
-         * 
-         * @param list
-         * @throws MailboxException
-         */
-        void onMessages(List<Message<Id>> list) throws MailboxException;
-    }
-    
     /**
      * Specify what data needs to get filled in a {@link Message} before returning it
      * 

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningQuotaManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningQuotaManager.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningQuotaManager.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningQuotaManager.java Thu Jul 28 17:48:10 2011
@@ -34,8 +34,8 @@ import org.apache.james.mailbox.store.Ma
 import org.apache.james.mailbox.store.StoreMailboxManager;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
+import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
-import org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback;
 
 /**
  * {@link QuotaManager} which will keep track of quota by listing for {@link Event}'s.
@@ -93,26 +93,22 @@ public abstract class ListeningQuotaMana
     public Quota getStorageQuota(MailboxSession session) throws MailboxException {
         long max = getMaxStorage(session);
         if (max != Quota.UNLIMITED || calculateWhenUnlimited) {
-            String id = session.getUser().getUserName();
+            MessageMapper mapper = factory.getMessageMapper(session);
+        	String id = session.getUser().getUserName();
             AtomicLong size = sizes.get(id);
+            
             if (size == null) {
                 final AtomicLong mSizes = new AtomicLong(0);
                 List<Mailbox> mailboxes = factory.getMailboxMapper(session).findMailboxWithPathLike(new MailboxPath(session.getPersonalSpace(), id, "%"));
                 for (int i = 0; i < mailboxes.size(); i++) {
-                    factory.getMessageMapper(session).findInMailbox(mailboxes.get(i), MessageRange.all(), FetchType.Metadata, new MessageCallback<Object>() {
-                        long messageSizes = 0;
-
-                        @Override
-                        public void onMessages(List<Message<Object>> list) throws MailboxException {
-                            for (int i = 0; i < list.size(); i++) {
-                                messageSizes += list.get(i).getFullContentOctets();
-                            }
-                            mSizes.set(mSizes.get() + messageSizes);
-                        }
-                    });
+                	long messageSizes = 0;
+                    Iterator<Message>  messages = mapper.findInMailbox(mailboxes.get(i), MessageRange.all(), FetchType.Metadata, -1);
+                    
+                    while(messages.hasNext()) {
+                        messageSizes +=  messages.next().getFullContentOctets();
+                    }
+                    mSizes.set(mSizes.get() + messageSizes);
                 }
-                
-                
 
                 AtomicLong s = sizes.putIfAbsent(id, mSizes);
                 if (s != null) {

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/LazyMessageSearchIndex.java Thu Jul 28 17:48:10 2011
@@ -19,7 +19,6 @@
 package org.apache.james.mailbox.store.search;
 
 import java.util.Iterator;
-import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.mail.Flags;
@@ -29,7 +28,6 @@ import org.apache.james.mailbox.MailboxS
 import org.apache.james.mailbox.MessageRange;
 import org.apache.james.mailbox.SearchQuery;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
-import org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
 
@@ -84,22 +82,16 @@ public class LazyMessageSearchIndex<Id> 
                 done = oldDone;
             }
             synchronized (done) {
-                getFactory().getMessageMapper(session).findInMailbox(mailbox, MessageRange.all(), FetchType.Full, new MessageCallback<Id>() {
-
-                    @Override
-                    public void onMessages(List<Message<Id>> list) throws MailboxException {
-                        for (int i = 0; i < list.size(); i++) {
-                            final Message<Id> message = list.get(i);
-
-                            try {
-                                add(session, mailbox, message);
-                            } catch (MailboxException e) {
-                                session.getLog().debug("Unable to index message " + message.getUid() + " in mailbox " + mailbox.getName(), e);
-                            }
-
-                        }
+                Iterator<Message<Id>> messages = getFactory().getMessageMapper(session).findInMailbox(mailbox, MessageRange.all(), FetchType.Full, -1);
+                while(messages.hasNext()) {
+                    final Message<Id> message = messages.next();
+                    try {
+                        add(session, mailbox, message);
+                    } catch (MailboxException e) {
+                        session.getLog().debug("Unable to index message " + message.getUid() + " in mailbox " + mailbox.getName(), e);
                     }
-                });
+
+                }
             }
         }
        

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/ListeningMessageSearchIndex.java Thu Jul 28 17:48:10 2011
@@ -33,7 +33,6 @@ import org.apache.james.mailbox.store.Ma
 import org.apache.james.mailbox.store.MailboxEventDispatcher.FlagsUpdatedImpl;
 import org.apache.james.mailbox.store.MailboxEventDispatcher.MailboxDeletionImpl;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
-import org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback;
 import org.apache.james.mailbox.store.mail.MessageMapperFactory;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
@@ -81,20 +80,15 @@ public abstract class ListeningMessageSe
 
                     while (uids.hasNext()) {
                         long next = uids.next();
-                        factory.getMessageMapper(session).findInMailbox(mailbox, MessageRange.one(next), FetchType.Full, new MessageCallback<Id>() {
-
-                            @Override
-                            public void onMessages(List<Message<Id>> list) throws MailboxException {
-                                for (int i = 0; i < list.size(); i++) {
-                                    Message<Id> message = list.get(i);
-                                    try {
-                                        add(session, mailbox, message);
-                                    } catch (MailboxException e) {
-                                        session.getLog().debug("Unable to index message " + message.getUid() + " for mailbox " + mailbox, e);
-                                    }
-                                }
+                        Iterator<Message<Id>> messages = factory.getMessageMapper(session).findInMailbox(mailbox, MessageRange.one(next), FetchType.Full, -1);
+                        while(messages.hasNext()) {
+                            Message<Id> message = messages.next();
+                            try {
+                                add(session, mailbox, message);
+                            } catch (MailboxException e) {
+                                session.getLog().debug("Unable to index message " + message.getUid() + " for mailbox " + mailbox, e);
                             }
-                        });
+                        }
 
                     }
                 } else if (event instanceof ExpungedImpl) {

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java?rev=1151938&r1=1151937&r2=1151938&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndex.java Thu Jul 28 17:48:10 2011
@@ -32,7 +32,6 @@ import org.apache.james.mailbox.SearchQu
 import org.apache.james.mailbox.SearchQuery.UidCriterion;
 import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
-import org.apache.james.mailbox.store.mail.MessageMapper.MessageCallback;
 import org.apache.james.mailbox.store.mail.MessageMapperFactory;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
@@ -65,17 +64,14 @@ public class SimpleMessageSearchIndex<Id
             NumericRange[] ranges = uidCrit.getOperator().getRange();
             for (int i = 0; i < ranges.length; i++) {
                 NumericRange r = ranges[i];
-                mapper.findInMailbox(mailbox, MessageRange.range(r.getLowValue(), r.getHighValue()), FetchType.Metadata, new MessageCallback<Id>() {
-
-                    public void onMessages(List<Message<Id>> list) throws MailboxException {
-                        for (int i = 0; i < list.size(); i++) {
-                            long uid = list.get(i).getUid();
-                            if (uids.contains(uid) == false) {
-                                uids.add(uid);
-                            }
-                        }
+                Iterator<Message<Id>> messages = mapper.findInMailbox(mailbox, MessageRange.range(r.getLowValue(), r.getHighValue()), FetchType.Metadata, -1);
+                
+                while(messages.hasNext()) {
+                    long uid = messages.next().getUid();
+                    if (uids.contains(uid) == false) {
+                        uids.add(uid);
                     }
-                });
+                }
             }
             Collections.sort(uids);
             return uids.iterator();
@@ -85,17 +81,13 @@ public class SimpleMessageSearchIndex<Id
             
             final List<Message<Id>> hits = new ArrayList<Message<Id>>();
 
-            mapper.findInMailbox(mailbox, MessageRange.all(), FetchType.Full, new MessageCallback<Id>() {
-
-                public void onMessages(List<Message<Id>> list) throws MailboxException {
-                    for (int i = 0; i < list.size(); i++) {
-                        Message<Id> m = list.get(i);
-                        if (hits.contains(m) == false) {
-                            hits.add(m);
-                        }
-                    }
+            Iterator<Message<Id>> messages = mapper.findInMailbox(mailbox, MessageRange.all(), FetchType.Full, -1);
+            while(messages.hasNext()) {
+            	Message<Id> m = messages.next();
+                if (hits.contains(m) == false) {
+                    hits.add(m);
                 }
-            });
+            }
             Collections.sort(hits);
             
             Iterator<Message<?>> it = new Iterator<Message<?>>() {



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