JAMES-2032 Introduce FetchBatchSizes
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/5d989ae1 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/5d989ae1 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/5d989ae1 Branch: refs/heads/master Commit: 5d989ae197415261426526f007de2fb53b6a003a Parents: 58f0e72 Author: Antoine Duprat <[email protected]> Authored: Thu May 18 22:30:32 2017 +0200 Committer: benwa <[email protected]> Committed: Thu May 25 09:20:49 2017 +0700 ---------------------------------------------------------------------- .../apache/james/mailbox/store/BatchSizes.java | 185 ++++++++++++++ .../james/mailbox/store/BatchSizesTest.java | 253 +++++++++++++++++++ 2 files changed, 438 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/5d989ae1/mailbox/store/src/main/java/org/apache/james/mailbox/store/BatchSizes.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/BatchSizes.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/BatchSizes.java new file mode 100644 index 0000000..8b079b7 --- /dev/null +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/BatchSizes.java @@ -0,0 +1,185 @@ +/**************************************************************** + * 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 com.google.common.base.MoreObjects; +import com.google.common.base.Objects; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; + +public class BatchSizes { + + public static final int DEFAULT_BATCH_SIZE = 200; + + public static BatchSizes defaultValues() { + return new Builder().build(); + } + + public static BatchSizes uniqueBatchSize(int batchSize) { + return new Builder() + .fetchMetadata(batchSize) + .fetchHeaders(batchSize) + .fetchBody(batchSize) + .fetchFull(batchSize) + .copyBatchSize(batchSize) + .moveBatchSize(batchSize) + .build(); + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private Optional<Integer> fetchMetadata; + private Optional<Integer> fetchHeaders; + private Optional<Integer> fetchBody; + private Optional<Integer> fetchFull; + private Optional<Integer> copyBatchSize; + private Optional<Integer> moveBatchSize; + + private Builder() { + fetchMetadata = Optional.absent(); + fetchHeaders = Optional.absent(); + fetchBody = Optional.absent(); + fetchFull = Optional.absent(); + copyBatchSize = Optional.absent(); + moveBatchSize = Optional.absent(); + } + + public Builder fetchMetadata(int batchSize) { + Preconditions.checkArgument(batchSize > 0, "'fetchMetadata' must be greater than zero"); + this.fetchMetadata = Optional.of(batchSize); + return this; + } + + public Builder fetchHeaders(int batchSize) { + Preconditions.checkArgument(batchSize > 0, "'fetchHeaders' must be greater than zero"); + this.fetchHeaders = Optional.of(batchSize); + return this; + } + + public Builder fetchBody(int batchSize) { + Preconditions.checkArgument(batchSize > 0, "'fetchBody' must be greater than zero"); + this.fetchBody = Optional.of(batchSize); + return this; + } + + public Builder fetchFull(int batchSize) { + Preconditions.checkArgument(batchSize > 0, "'fetchFull' must be greater than zero"); + this.fetchFull = Optional.of(batchSize); + return this; + } + + public Builder copyBatchSize(int batchSize) { + Preconditions.checkArgument(batchSize > 0, "'copyBatchSize' must be greater than zero"); + this.copyBatchSize = Optional.of(batchSize); + return this; + } + + public Builder moveBatchSize(int batchSize) { + Preconditions.checkArgument(batchSize > 0, "'moveBatchSize' must be greater than zero"); + this.moveBatchSize = Optional.of(batchSize); + return this; + } + + public BatchSizes build() { + return new BatchSizes( + fetchMetadata.or(DEFAULT_BATCH_SIZE), + fetchHeaders.or(DEFAULT_BATCH_SIZE), + fetchBody.or(DEFAULT_BATCH_SIZE), + fetchFull.or(DEFAULT_BATCH_SIZE), + copyBatchSize.or(DEFAULT_BATCH_SIZE), + moveBatchSize.or(DEFAULT_BATCH_SIZE)); + } + } + + private final int fetchMetadata; + private final int fetchHeaders; + private final int fetchBody; + private final int fetchFull; + private final int copyBatchSize; + private final int moveBatchSize; + + private BatchSizes(int fetchMetadata, int fetchHeaders, int fetchBody, int fetchFull, int copyBatchSize, int moveBatchSize) { + this.fetchMetadata = fetchMetadata; + this.fetchHeaders = fetchHeaders; + this.fetchBody = fetchBody; + this.fetchFull = fetchFull; + this.copyBatchSize = copyBatchSize; + this.moveBatchSize = moveBatchSize; + } + + public int getFetchMetadata() { + return fetchMetadata; + } + + public int getFetchHeaders() { + return fetchHeaders; + } + + public int getFetchBody() { + return fetchBody; + } + + public int getFetchFull() { + return fetchFull; + } + + public int getCopyBatchSize() { + return copyBatchSize; + } + + public int getMoveBatchSize() { + return moveBatchSize; + } + + @Override + public final boolean equals(Object obj) { + if (obj instanceof BatchSizes) { + BatchSizes other = (BatchSizes) obj; + return Objects.equal(this.fetchMetadata, other.fetchMetadata) + && Objects.equal(this.fetchHeaders, other.fetchHeaders) + && Objects.equal(this.fetchBody, other.fetchBody) + && Objects.equal(this.fetchFull, other.fetchFull) + && Objects.equal(this.copyBatchSize, other.copyBatchSize) + && Objects.equal(this.moveBatchSize, other.moveBatchSize); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hashCode(this.fetchMetadata, this.fetchHeaders, this.fetchBody, this.fetchFull, this.copyBatchSize, this.moveBatchSize); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(BatchSizes.class) + .add("fetchMetadata", fetchMetadata) + .add("fetchHeaders", fetchHeaders) + .add("fetchBody", fetchBody) + .add("fetchFull", fetchFull) + .add("copyBatchSize", copyBatchSize) + .add("moveBatchSize", moveBatchSize) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/5d989ae1/mailbox/store/src/test/java/org/apache/james/mailbox/store/BatchSizesTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/BatchSizesTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/BatchSizesTest.java new file mode 100644 index 0000000..275387f --- /dev/null +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/BatchSizesTest.java @@ -0,0 +1,253 @@ +/**************************************************************** + * 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 org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class BatchSizesTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void shouldRespectJavaBeanContract() { + EqualsVerifier.forClass(BatchSizes.class).verify(); + } + + @Test + public void defaultValuesShouldReturnDefaultForEachParameters() { + BatchSizes batchSizes = BatchSizes.defaultValues(); + assertThat(batchSizes.getFetchMetadata()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + assertThat(batchSizes.getFetchHeaders()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + assertThat(batchSizes.getFetchBody()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + assertThat(batchSizes.getFetchFull()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + assertThat(batchSizes.getCopyBatchSize()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + assertThat(batchSizes.getMoveBatchSize()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + } + + @Test + public void uniqueBatchSizeShouldSetTheSameValueToAllAttributes() { + int batchSize = 10; + BatchSizes batchSizes = BatchSizes.uniqueBatchSize(batchSize); + assertThat(batchSizes.getFetchMetadata()).isEqualTo(batchSize); + assertThat(batchSizes.getFetchHeaders()).isEqualTo(batchSize); + assertThat(batchSizes.getFetchBody()).isEqualTo(batchSize); + assertThat(batchSizes.getFetchFull()).isEqualTo(batchSize); + assertThat(batchSizes.getCopyBatchSize()).isEqualTo(batchSize); + assertThat(batchSizes.getMoveBatchSize()).isEqualTo(batchSize); + } + + @Test + public void fetchMetadataShouldThrowWhenNegative() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .fetchMetadata(-1); + } + + @Test + public void fetchMetadataShouldThrowWhenZero() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .fetchMetadata(0); + } + + @Test + public void fetchHeadersShouldThrowWhenNegative() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .fetchHeaders(-1); + } + + @Test + public void fetchHeadersShouldThrowWhenZero() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .fetchHeaders(0); + } + + @Test + public void fetchBodyShouldThrowWhenNegative() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .fetchBody(-1); + } + + @Test + public void fetchBodyShouldThrowWhenZero() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .fetchBody(0); + } + + @Test + public void fetchFullShouldThrowWhenNegative() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .fetchFull(-1); + } + + @Test + public void fetchFullShouldThrowWhenZero() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .fetchFull(0); + } + + @Test + public void copyBatchSizeShouldThrowWhenNegative() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .copyBatchSize(-1); + } + + @Test + public void copyBatchSizeShouldThrowWhenZero() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .copyBatchSize(0); + } + + @Test + public void moveBatchSizeShouldThrowWhenNegative() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .moveBatchSize(-1); + } + + @Test + public void moveBatchSizeShouldThrowWhenZero() { + expectedException.expect(IllegalArgumentException.class); + + BatchSizes.builder() + .moveBatchSize(0); + } + + @Test + public void buildShouldSetDefaultValueToFetchMetadataWhenNotGiven() { + BatchSizes batchSizes = BatchSizes.builder() + .build(); + assertThat(batchSizes.getFetchMetadata()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + } + + @Test + public void buildShouldSetValueToFetchMetadataWhenGiven() { + int expected = 123; + BatchSizes batchSizes = BatchSizes.builder() + .fetchMetadata(expected) + .build(); + assertThat(batchSizes.getFetchMetadata()).isEqualTo(expected); + } + + @Test + public void buildShouldSetDefaultValueToFetchHeadersWhenNotGiven() { + BatchSizes batchSizes = BatchSizes.builder() + .build(); + assertThat(batchSizes.getFetchHeaders()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + } + + @Test + public void buildShouldSetValueToFetchHeadersWhenGiven() { + int expected = 123; + BatchSizes batchSizes = BatchSizes.builder() + .fetchHeaders(expected) + .build(); + assertThat(batchSizes.getFetchHeaders()).isEqualTo(expected); + } + + @Test + public void buildShouldSetDefaultValueToFetchBodyWhenNotGiven() { + BatchSizes batchSizes = BatchSizes.builder() + .build(); + assertThat(batchSizes.getFetchBody()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + } + + @Test + public void buildShouldSetValueToFetchBodyWhenGiven() { + int expected = 123; + BatchSizes batchSizes = BatchSizes.builder() + .fetchBody(expected) + .build(); + assertThat(batchSizes.getFetchBody()).isEqualTo(expected); + } + + @Test + public void buildShouldSetDefaultValueToFetchFullWhenNotGiven() { + BatchSizes batchSizes = BatchSizes.builder() + .build(); + assertThat(batchSizes.getFetchFull()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + } + + @Test + public void buildShouldSetValueToFetchFullWhenGiven() { + int expected = 123; + BatchSizes batchSizes = BatchSizes.builder() + .fetchFull(expected) + .build(); + assertThat(batchSizes.getFetchFull()).isEqualTo(expected); + } + + @Test + public void buildShouldSetDefaultValueToCopyBatchSizeWhenNotGiven() { + BatchSizes batchSizes = BatchSizes.builder() + .build(); + assertThat(batchSizes.getCopyBatchSize()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + } + + @Test + public void buildShouldSetValueToCopyBatchSizeWhenGiven() { + int expected = 123; + BatchSizes batchSizes = BatchSizes.builder() + .copyBatchSize(expected) + .build(); + assertThat(batchSizes.getCopyBatchSize()).isEqualTo(expected); + } + + @Test + public void buildShouldSetDefaultValueToMoveBatchSizeWhenNotGiven() { + BatchSizes batchSizes = BatchSizes.builder() + .build(); + assertThat(batchSizes.getMoveBatchSize()).isEqualTo(BatchSizes.DEFAULT_BATCH_SIZE); + } + + @Test + public void buildShouldSetValueToMoveBatchSizeWhenGiven() { + int expected = 123; + BatchSizes batchSizes = BatchSizes.builder() + .moveBatchSize(expected) + .build(); + assertThat(batchSizes.getMoveBatchSize()).isEqualTo(expected); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
