Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IMAP-370 Factorize code between copies and moves
- Loading branch information
Showing
4 changed files
with
168 additions
and
56 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
mailbox/store/src/main/java/org/apache/james/mailbox/store/MessageBatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/**************************************************************** | ||
* 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.List; | ||
|
||
import org.apache.james.mailbox.exception.MailboxException; | ||
import org.apache.james.mailbox.model.MessageRange; | ||
import org.apache.james.mailbox.store.mail.model.MailboxId; | ||
import org.msgpack.core.Preconditions; | ||
|
||
public class MessageBatcher { | ||
|
||
public static final int NO_BATCH_SIZE = 0; | ||
|
||
public interface BatchedOperation { | ||
List<MessageRange> execute(MessageRange messageRange) throws MailboxException; | ||
} | ||
|
||
private final int moveBatchSize; | ||
|
||
public MessageBatcher(int moveBatchSize) { | ||
Preconditions.checkArgument(moveBatchSize >= NO_BATCH_SIZE); | ||
this.moveBatchSize = moveBatchSize; | ||
} | ||
|
||
public List<MessageRange> batchMessages(MessageRange set, BatchedOperation batchedOperation) throws MailboxException { | ||
if (moveBatchSize > 0) { | ||
List<MessageRange> movedRanges = new ArrayList<MessageRange>(); | ||
for (MessageRange messageRange : set.split(moveBatchSize)) { | ||
movedRanges.addAll(batchedOperation.execute(messageRange)); | ||
} | ||
return movedRanges; | ||
} else { | ||
return batchedOperation.execute(set); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
mailbox/store/src/test/java/org/apache/james/mailbox/store/MessageBatcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/**************************************************************** | ||
* 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 static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.james.mailbox.exception.MailboxException; | ||
import org.apache.james.mailbox.model.MessageRange; | ||
import org.junit.Test; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
public class MessageBatcherTest { | ||
|
||
private MessageBatcher.BatchedOperation incrementBatcher = new MessageBatcher.BatchedOperation() { | ||
@Override | ||
public List<MessageRange> execute(MessageRange messageRange) throws MailboxException { | ||
return Lists.<MessageRange>newArrayList(MessageRange.range(messageRange.getUidFrom() + 1, messageRange.getUidTo() + 1)); | ||
} | ||
}; | ||
|
||
@Test | ||
public void batchMessagesShouldWorkOnSingleRangeMode() throws Exception { | ||
MessageBatcher messageBatcher = new MessageBatcher(0); | ||
|
||
assertThat(messageBatcher.batchMessages(MessageRange.range(1, 10), incrementBatcher)).containsOnly(MessageRange.range(2, 11)); | ||
} | ||
|
||
@Test | ||
public void batchMessagesShouldWorkWithNonZeroBatchedSize() throws Exception { | ||
MessageBatcher messageBatcher = new MessageBatcher(5); | ||
|
||
assertThat(messageBatcher.batchMessages(MessageRange.range(1, 10), incrementBatcher)).containsOnly(MessageRange.range(2, 6), MessageRange.range(7, 11)); | ||
} | ||
|
||
@Test(expected = MailboxException.class) | ||
public void batchMessagesShouldPropagateExceptions() throws Exception { | ||
MessageBatcher messageBatcher = new MessageBatcher(0); | ||
|
||
messageBatcher.batchMessages(MessageRange.range(1, 10), new MessageBatcher.BatchedOperation() { | ||
public List<MessageRange> execute(MessageRange messageRange) throws MailboxException { | ||
throw new MailboxException(); | ||
} | ||
}); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void messageBatcherShouldThrowOnNegativeBatchSize() throws Exception { | ||
MessageBatcher messageBatcher = new MessageBatcher(-1); | ||
} | ||
|
||
} |