Skip to content

Commit

Permalink
JAMES-2032 Use FetchBatchSizes in StoreMessageResultIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
aduprat authored and chibenwa committed May 25, 2017
1 parent 5d989ae commit 1fbfb80
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
Expand Up @@ -100,7 +100,6 @@
public class StoreMailboxManager implements MailboxManager {

public static final char SQL_WILDCARD_CHAR = '%';
public static final int DEFAULT_FETCH_BATCH_SIZE = 200;

private MailboxEventDispatcher dispatcher;
private DelegatingMailboxListener delegatingListener;
Expand Down Expand Up @@ -132,7 +131,7 @@ public class StoreMailboxManager implements MailboxManager {

private QuotaUpdater quotaUpdater;

private int fetchBatchSize = DEFAULT_FETCH_BATCH_SIZE;
private FetchBatchSizes fetchBatchSizes = FetchBatchSizes.defaultValues();

private final MessageParser messageParser;
private final Factory messageIdFactory;
Expand Down Expand Up @@ -216,8 +215,8 @@ public void setMoveBatchSize(int moveBatchSize) {
this.moveBatcher = new MessageBatcher(moveBatchSize);
}

public void setFetchBatchSize(int fetchBatchSize) {
this.fetchBatchSize = fetchBatchSize;
public void setFetchBatchSizes(FetchBatchSizes fetchBatchSizes) {
this.fetchBatchSizes = fetchBatchSizes;
}


Expand Down Expand Up @@ -492,7 +491,7 @@ public MessageManager getMailbox(MailboxPath mailboxPath, MailboxSession session
session.getLog().debug("Loaded mailbox " + mailboxPath);

StoreMessageManager messageManager = createMessageManager(mailboxRow, session);
messageManager.setFetchBatchSize(fetchBatchSize);
messageManager.setFetchBatchSizes(fetchBatchSizes);
return messageManager;
}
}
Expand All @@ -516,7 +515,7 @@ public MessageManager getMailbox(MailboxId mailboxId, MailboxSession session)
session.getLog().debug("Loaded mailbox " + mailboxId.serialize());

StoreMessageManager messageManager = createMessageManager(mailboxRow, session);
messageManager.setFetchBatchSize(fetchBatchSize);
messageManager.setFetchBatchSizes(fetchBatchSizes);
return messageManager;
}

Expand Down
Expand Up @@ -169,7 +169,7 @@ public boolean apply(MessageAttachment input) {

private final Factory messageIdFactory;

private int fetchBatchSize;
private FetchBatchSizes fetchBatchSizes = FetchBatchSizes.defaultValues();

public StoreMessageManager(MailboxSessionMapperFactory mapperFactory, MessageSearchIndex index, MailboxEventDispatcher dispatcher,
MailboxPathLocker locker, Mailbox mailbox, MailboxACLResolver aclResolver, GroupMembershipResolver groupMembershipResolver,
Expand All @@ -187,8 +187,8 @@ public StoreMessageManager(MailboxSessionMapperFactory mapperFactory, MessageSea
this.messageIdFactory = messageIdFactory;
}

public void setFetchBatchSize(int fetchBatchSize) {
this.fetchBatchSize = fetchBatchSize;
public void setFetchBatchSizes(FetchBatchSizes fetchBatchSizes) {
this.fetchBatchSizes = fetchBatchSizes;
}

protected Factory getMessageIdFactory() {
Expand Down Expand Up @@ -699,7 +699,7 @@ public long getMessageCount(MailboxSession mailboxSession) throws MailboxExcepti
*/
public MessageResultIterator getMessages(MessageRange set, FetchGroup fetchGroup, MailboxSession mailboxSession) throws MailboxException {
final MessageMapper messageMapper = mapperFactory.getMessageMapper(mailboxSession);
return new StoreMessageResultIterator(messageMapper, mailbox, set, fetchBatchSize, fetchGroup);
return new StoreMessageResultIterator(messageMapper, mailbox, set, fetchBatchSizes, fetchGroup);
}

/**
Expand Down
Expand Up @@ -42,33 +42,38 @@
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.MailboxMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Objects;

public class StoreMessageResultIterator implements MessageResultIterator {

private static final Logger LOGGER = LoggerFactory.getLogger(StoreMessageResultIterator.class);

private Iterator<MailboxMessage> next = null;
private MailboxException exception;
private final Mailbox mailbox;
private final FetchGroup group;
private final MessageUid from;
private MessageUid cursor;
private final MessageUid to;
private final int batchSize;
private final FetchBatchSizes fetchBatchSizes;
private final Type type;
private final MessageMapper mapper;
private final FetchType ftype;

public StoreMessageResultIterator(MessageMapper mapper, Mailbox mailbox, MessageRange range, int batchSize, org.apache.james.mailbox.model.MessageResult.FetchGroup group) {
public StoreMessageResultIterator(MessageMapper mapper, Mailbox mailbox, MessageRange range, FetchBatchSizes fetchBatchSizes, org.apache.james.mailbox.model.MessageResult.FetchGroup group) {
this.mailbox = mailbox;
this.group = group;
this.mapper = mapper;
this.from = range.getUidFrom();
this.cursor = this.from;
this.to = range.getUidTo();
this.batchSize = batchSize;
this.fetchBatchSizes = fetchBatchSizes;
this.type = range.getType();
this.ftype = getFetchType(group);
LOGGER.debug("fetchBatchSizes used: " + fetchBatchSizes);
}

/**
Expand Down Expand Up @@ -154,7 +159,21 @@ private void readBatch() throws MailboxException {
range = MessageRange.range(cursor, to);
break;
}
next = mapper.findInMailbox(mailbox, range, ftype, batchSize);
next = mapper.findInMailbox(mailbox, range, ftype, batchSizeFromFetchType(ftype));
}

private int batchSizeFromFetchType(FetchType fetchType) {
switch (fetchType) {
case Metadata:
return fetchBatchSizes.getMetadata();
case Headers:
return fetchBatchSizes.getHeaders();
case Body:
return fetchBatchSizes.getBody();
case Full:
return fetchBatchSizes.getFull();
}
throw new RuntimeException("Unknown fetchTpe: " + fetchType);
}

@Override
Expand Down
Expand Up @@ -191,7 +191,7 @@ public Flags getApplicableFlag(Mailbox mailbox) throws MailboxException {
@Test
public void testBatching() {
MessageRange range = MessageRange.range(MessageUid.of(1), MessageUid.of(10));
int batchSize = 3;
FetchBatchSizes batchSize = FetchBatchSizes.uniqueBatchSize(3);
StoreMessageResultIterator it = new StoreMessageResultIterator(new TestMessageMapper(MessageRange.all()), null, range, batchSize, new TestFetchGroup());

assertThat(it).extracting(new Extractor<MessageResult, Long>(){
Expand All @@ -205,7 +205,7 @@ public Long extract(MessageResult input) {
@Test
public void nextShouldReturnFirstElement() {
MessageRange range = MessageUid.of(1).toRange();
int batchSize = 42;
FetchBatchSizes batchSize = FetchBatchSizes.uniqueBatchSize(42);
StoreMessageResultIterator iterator = new StoreMessageResultIterator(new TestMessageMapper(range), null, range, batchSize, new TestFetchGroup());
assertThat(iterator.next()).isNotNull();
}
Expand All @@ -214,7 +214,7 @@ public void nextShouldReturnFirstElement() {
public void nextShouldThrowWhenNoElement() {
MessageRange messages = MessageUid.of(1).toRange();
MessageRange findRange = MessageUid.of(2).toRange();
int batchSize = 42;
FetchBatchSizes batchSize = FetchBatchSizes.uniqueBatchSize(42);
StoreMessageResultIterator iterator = new StoreMessageResultIterator(new TestMessageMapper(messages), null, findRange, batchSize, new TestFetchGroup());
iterator.next();
}
Expand All @@ -223,7 +223,7 @@ public void nextShouldThrowWhenNoElement() {
public void hasNextShouldReturnFalseWhenNoElement() {
MessageRange messages = MessageUid.of(1).toRange();
MessageRange findRange = MessageUid.of(2).toRange();
int batchSize = 42;
FetchBatchSizes batchSize = FetchBatchSizes.uniqueBatchSize(42);
StoreMessageResultIterator iterator = new StoreMessageResultIterator(new TestMessageMapper(messages), null, findRange, batchSize, new TestFetchGroup());
assertThat(iterator.hasNext()).isFalse();
}
Expand Down

0 comments on commit 1fbfb80

Please sign in to comment.