Author: norman Date: Fri Jul 29 13:18:17 2011 New Revision: 1152209 URL: http://svn.apache.org/viewvc?rev=1152209&view=rev Log: Fix batch fetching of messages
Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java (with props) Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java Modified: 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=1152209&r1=1152208&r2=1152209&view=diff ============================================================================== --- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java (original) +++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageResultIterator.java Fri Jul 29 13:18:17 2011 @@ -109,10 +109,9 @@ public class StoreMessageResultIterator< @Override public boolean hasNext() { - if (next == null || !next.hasNext()) { + if (!done && (next == null || !next.hasNext())) { try { MessageRange range; - switch (type) { default: case ALL: @@ -120,7 +119,6 @@ public class StoreMessageResultIterator< break; case FROM: range = MessageRange.from(from); - break; case ONE: range = MessageRange.one(from); @@ -131,13 +129,16 @@ public class StoreMessageResultIterator< } next = mapper.findInMailbox(mailbox, range, ftype, batchSize); + if (!next.hasNext()) { + done = true; + } } catch (MailboxException e) { this.exception = e; done = true; } return !done; } else { - return true; + return !done; } } @@ -156,6 +157,9 @@ public class StoreMessageResultIterator< // move the start UID behind the last fetched message UID if needed if (!next.hasNext()) { from = result.getUid() + 1; + if (result.getUid() >= to || from > to) { + done = true; + } } return result; } Added: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java?rev=1152209&view=auto ============================================================================== --- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java (added) +++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java Fri Jul 29 13:18:17 2011 @@ -0,0 +1,184 @@ +/**************************************************************** + * 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.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.mail.Flags; +import javax.mail.util.SharedByteArrayInputStream; + +import junit.framework.Assert; + +import org.apache.james.mailbox.MailboxException; +import org.apache.james.mailbox.MessageMetaData; +import org.apache.james.mailbox.MessageRange; +import org.apache.james.mailbox.MessageResult; +import org.apache.james.mailbox.MessageResult.FetchGroup; +import org.apache.james.mailbox.UpdatedFlags; +import org.apache.james.mailbox.store.mail.MessageMapper; +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.model.impl.PropertyBuilder; +import org.apache.james.mailbox.store.mail.model.impl.SimpleMessage; +import org.junit.Test; + +public class StoreMessageResultIteratorTest { + + @Test + public void testBatching() { + MessageRange range = MessageRange.range(1, 10); + int batchSize = 3; + StoreMessageResultIterator<Long> it = new StoreMessageResultIterator<Long>(new MessageMapper<Long>() { + + @Override + public void endRequest() { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T execute(Transaction<T> transaction) + throws MailboxException { + throw new UnsupportedOperationException(); + } + + @Override + public Iterator<Message<Long>> findInMailbox( + Mailbox<Long> mailbox, + MessageRange set, + org.apache.james.mailbox.store.mail.MessageMapper.FetchType type, + int limit) throws MailboxException { + long start = set.getUidFrom(); + long end = set.getUidTo(); + long calcEnd = start + limit; + if (calcEnd > end) { + calcEnd = end; + } + + List<Message<Long>> messages = new ArrayList<Message<Long>>(); + long i = start; + while ( i < calcEnd ) { + long uid = i; + SimpleMessage<Long> m = new SimpleMessage<Long>(null, 0, 0, new SharedByteArrayInputStream("".getBytes()), new Flags(), new PropertyBuilder(), 1L); + m.setUid(uid); + messages.add(m); + i++; + } + return messages.iterator(); + } + + @Override + public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox( + Mailbox<Long> mailbox, MessageRange set) + throws MailboxException { + throw new UnsupportedOperationException(); + + } + + @Override + public long countMessagesInMailbox(Mailbox<Long> mailbox) + throws MailboxException { + throw new UnsupportedOperationException(); + + } + + @Override + public long countUnseenMessagesInMailbox(Mailbox<Long> mailbox) + throws MailboxException { + throw new UnsupportedOperationException(); + } + + @Override + public void delete(Mailbox<Long> mailbox, Message<Long> message) + throws MailboxException { + throw new UnsupportedOperationException(); + } + + @Override + public Long findFirstUnseenMessageUid(Mailbox<Long> mailbox) + throws MailboxException { + throw new UnsupportedOperationException(); + } + + @Override + public List<Long> findRecentMessageUidsInMailbox( + Mailbox<Long> mailbox) throws MailboxException { + throw new UnsupportedOperationException(); + + } + + @Override + public MessageMetaData add(Mailbox<Long> mailbox, + Message<Long> message) throws MailboxException { + throw new UnsupportedOperationException(); + } + + @Override + public Iterator<UpdatedFlags> updateFlags(Mailbox<Long> mailbox, + Flags flags, boolean value, boolean replace, + MessageRange set) throws MailboxException { + throw new UnsupportedOperationException(); + } + + @Override + public MessageMetaData copy(Mailbox<Long> mailbox, + Message<Long> original) throws MailboxException { + throw new UnsupportedOperationException(); + + } + + @Override + public long getLastUid(Mailbox<Long> mailbox) + throws MailboxException { + throw new UnsupportedOperationException(); + } + + @Override + public long getHighestModSeq(Mailbox<Long> mailbox) + throws MailboxException { + throw new UnsupportedOperationException(); + } + + }, null, range, batchSize, new FetchGroup() { + + @Override + public Set<PartContentDescriptor> getPartContentDescriptors() { + return null; + } + + @Override + public int content() { + return FetchGroup.MINIMAL; + } + }); + + long i = 1; + while(it.hasNext()) { + MessageResult r = it.next(); + Assert.assertEquals(i++, r.getUid()); + } + Assert.assertEquals(10, i); + + } + +} Propchange: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org