This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
The following commit(s) were added to refs/heads/master by this push: new e437b0b JAMES-3140 Parse Cassandra Cache configuration e437b0b is described below commit e437b0b215b09b74033ffdd70d61a0eacca6e4a9 Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Thu May 7 15:43:08 2020 +0700 JAMES-3140 Parse Cassandra Cache configuration --- .../cache/CassandraCacheConfiguration.java | 62 +++++++++++++++++++++- .../cache/CassandraCacheConfigurationTest.java | 35 +++++++++++- 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CassandraCacheConfiguration.java b/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CassandraCacheConfiguration.java index 85be6ad..04ed434 100644 --- a/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CassandraCacheConfiguration.java +++ b/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CassandraCacheConfiguration.java @@ -20,8 +20,14 @@ package org.apache.james.blob.cassandra.cache; import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.util.Objects; import java.util.Optional; +import org.apache.commons.configuration2.Configuration; +import org.apache.james.util.DurationParser; +import org.apache.james.util.SizeFormat; + import com.google.common.base.Preconditions; public class CassandraCacheConfiguration { @@ -38,7 +44,7 @@ public class CassandraCacheConfiguration { public Builder timeOut(Duration timeout) { Preconditions.checkNotNull(timeout, "'Read timeout' must not to be null"); - Preconditions.checkArgument(timeout.getSeconds() > 0, "'Read timeout' needs to be positive"); + Preconditions.checkArgument(timeout.toMillis() > 0, "'Read timeout' needs to be positive"); Preconditions.checkArgument(timeout.getSeconds() <= MAX_READ_TIMEOUT.getSeconds(), "'Read timeout' needs to be less than %s sec", MAX_READ_TIMEOUT.getSeconds()); @@ -63,6 +69,21 @@ public class CassandraCacheConfiguration { return this; } + public Builder ttl(Optional<Duration> ttl) { + ttl.ifPresent(this::ttl); + return this; + } + + public Builder timeOut(Optional<Duration> timeOut) { + timeOut.ifPresent(this::timeOut); + return this; + } + + public Builder sizeThresholdInBytes(Optional<Integer> sizeThresholdInBytes) { + sizeThresholdInBytes.ifPresent(this::sizeThresholdInBytes); + return this; + } + public CassandraCacheConfiguration build() { return new CassandraCacheConfiguration( readTimeout.orElse(DEFAULT_READ_TIMEOUT), @@ -71,6 +92,28 @@ public class CassandraCacheConfiguration { } } + public static final CassandraCacheConfiguration DEFAULT = builder().build(); + + public static Builder builder() { + return new Builder(); + } + + public static CassandraCacheConfiguration from(Configuration configuration) { + Optional<Duration> ttl = Optional.ofNullable(configuration.getString("cache.cassandra.ttl", null)) + .map(value -> DurationParser.parse(value, ChronoUnit.SECONDS)); + Optional<Duration> timeOut = Optional.ofNullable(configuration.getString("cache.cassandra.timeout", null)) + .map(value -> DurationParser.parse(value, ChronoUnit.SECONDS)); + Optional<Integer> sizeThreshold = Optional.ofNullable(configuration.getString("cache.sizeThresholdInBytes", null)) + .map(SizeFormat::parseAsByteCount) + .map(Math::toIntExact); + + return builder() + .ttl(ttl) + .timeOut(timeOut) + .sizeThresholdInBytes(sizeThreshold) + .build(); + } + private final Duration readTimeOut; private final int sizeThresholdInBytes; private final Duration ttl; @@ -92,4 +135,21 @@ public class CassandraCacheConfiguration { public int getSizeThresholdInBytes() { return sizeThresholdInBytes; } + + @Override + public final boolean equals(Object o) { + if (o instanceof CassandraCacheConfiguration) { + CassandraCacheConfiguration that = (CassandraCacheConfiguration) o; + + return Objects.equals(this.sizeThresholdInBytes, that.sizeThresholdInBytes) + && Objects.equals(this.readTimeOut, that.readTimeOut) + && Objects.equals(this.ttl, that.ttl); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(readTimeOut, sizeThresholdInBytes, ttl); + } } diff --git a/server/blob/blob-cassandra/src/test/java/org/apache/james/blob/cassandra/cache/CassandraCacheConfigurationTest.java b/server/blob/blob-cassandra/src/test/java/org/apache/james/blob/cassandra/cache/CassandraCacheConfigurationTest.java index 2353298..99f6ea6 100644 --- a/server/blob/blob-cassandra/src/test/java/org/apache/james/blob/cassandra/cache/CassandraCacheConfigurationTest.java +++ b/server/blob/blob-cassandra/src/test/java/org/apache/james/blob/cassandra/cache/CassandraCacheConfigurationTest.java @@ -19,12 +19,13 @@ package org.apache.james.blob.cassandra.cache; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.nio.charset.StandardCharsets; import java.time.Duration; -import java.time.temporal.ChronoUnit; +import org.apache.commons.configuration2.PropertiesConfiguration; import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.Test; @@ -82,7 +83,7 @@ public class CassandraCacheConfigurationTest { void shouldThrowWhenConfiguredNullTimeout() { assertThatThrownBy(() -> new CassandraCacheConfiguration.Builder() .sizeThresholdInBytes(DEFAULT_THRESHOLD_SIZE_IN_BYTES) - .timeOut(null) + .timeOut((Duration) null) .ttl(_1_SEC_TTL) .build()) .isInstanceOf(NullPointerException.class); @@ -127,4 +128,34 @@ public class CassandraCacheConfigurationTest { .build()) .isInstanceOf(IllegalArgumentException.class); } + + @Test + void timeOutShouldNotThrowWhenMilliSeconds() { + assertThatCode(() -> CassandraCacheConfiguration.builder().timeOut(Duration.ofMillis(50))) + .doesNotThrowAnyException(); + } + + @Test + void fromShouldReturnDefaultConfigurationWhenEmpty() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + + assertThat(CassandraCacheConfiguration.from(configuration)) + .isEqualTo(CassandraCacheConfiguration.builder().build()); + } + + @Test + void fromShouldReturnSuppliedConfiguration() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("cache.cassandra.ttl", "3 days"); + configuration.addProperty("cache.cassandra.timeout", "50 ms"); + configuration.addProperty("cache.sizeThresholdInBytes", "4 KiB"); + + assertThat(CassandraCacheConfiguration.from(configuration)) + .isEqualTo(CassandraCacheConfiguration.builder() + .ttl(Duration.ofDays(3)) + .timeOut(Duration.ofMillis(50)) + .sizeThresholdInBytes(4096) + .build()); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org