Author: matthieu Date: Thu Oct 29 14:12:12 2015 New Revision: 1711263 URL: http://svn.apache.org/viewvc?rev=1711263&view=rev Log: JAMES-1588 Creating cassandra project
Added: james/project/trunk/backends-common/cassandra/ james/project/trunk/backends-common/cassandra/pom.xml james/project/trunk/backends-common/cassandra/src/ james/project/trunk/backends-common/cassandra/src/main/ james/project/trunk/backends-common/cassandra/src/main/java/ james/project/trunk/backends-common/cassandra/src/main/java/org/ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraConstants.java james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraUtils.java james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetry.java james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/LightweightTransactionException.java james/project/trunk/backends-common/cassandra/src/test/ james/project/trunk/backends-common/cassandra/src/test/java/ james/project/trunk/backends-common/cassandra/src/test/java/org/ james/project/trunk/backends-common/cassandra/src/test/java/org/apache/ james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/ james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/backends/ james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/ james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/utils/ james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetryTest.java Added: james/project/trunk/backends-common/cassandra/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/backends-common/cassandra/pom.xml?rev=1711263&view=auto ============================================================================== --- james/project/trunk/backends-common/cassandra/pom.xml (added) +++ james/project/trunk/backends-common/cassandra/pom.xml Thu Oct 29 14:12:12 2015 @@ -0,0 +1,200 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <artifactId>james-backends-common</artifactId> + <groupId>org.apache.james</groupId> + <version>1.8.3-SNAPSHOT</version> + </parent> + + <artifactId>apache-james-backends-cassandra</artifactId> + <name>Apache James Cassandra backend</name> + + <profiles> + <profile> + <id>disable-build-for-older-jdk</id> + <activation> + <jdk>(,1.8)</jdk> + </activation> + <build> + <plugins> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <executions> + <execution> + <id>default-jar</id> + <phase>none</phase> + </execution> + <execution> + <id>jar</id> + <phase>none</phase> + </execution> + <execution> + <id>test-jar</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <executions> + <execution> + <id>default-compile</id> + <phase>none</phase> + </execution> + <execution> + <id>default-testCompile</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <executions> + <execution> + <id>default-test</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-source-plugin</artifactId> + <executions> + <execution> + <id>attach-sources</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-install-plugin</artifactId> + <executions> + <execution> + <id>default-install</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <executions> + <execution> + <id>default-resources</id> + <phase>none</phase> + </execution> + <execution> + <id>default-testResources</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <executions> + <execution> + <id>attach-descriptor</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>build-for-jdk-8</id> + <activation> + <jdk>[1.8,)</jdk> + </activation> + <dependencies> + <dependency> + <groupId>com.datastax.cassandra</groupId> + <artifactId>cassandra-driver-core</artifactId> + <version>2.1.7</version> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.cassandraunit</groupId> + <artifactId>cassandra-unit</artifactId> + <version>2.0.2.2</version> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <archive> + <manifest> + <mainClass>fully.qualified.MainClass</mainClass> + </manifest> + </archive> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>disable-animal-sniffer</id> + <activation> + <jdk>[1.6,)</jdk> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>animal-sniffer-maven-plugin</artifactId> + <executions> + <execution> + <id>check_java_6</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> + + +</project> Added: james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraConstants.java URL: http://svn.apache.org/viewvc/james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraConstants.java?rev=1711263&view=auto ============================================================================== --- james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraConstants.java (added) +++ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraConstants.java Thu Oct 29 14:12:12 2015 @@ -0,0 +1,24 @@ +/**************************************************************** + * 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.backends.cassandra.utils; + +public interface CassandraConstants { + int LIGHTWEIGHT_TRANSACTION_APPLIED = 0; +} Added: james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraUtils.java URL: http://svn.apache.org/viewvc/james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraUtils.java?rev=1711263&view=auto ============================================================================== --- james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraUtils.java (added) +++ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/CassandraUtils.java Thu Oct 29 14:12:12 2015 @@ -0,0 +1,33 @@ +/**************************************************************** + * 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.backends.cassandra.utils; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +public class CassandraUtils { + + public static Stream<Row> convertToStream(ResultSet resultSet) { + return StreamSupport.stream(resultSet.spliterator(), true); + } + +} Added: james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetry.java URL: http://svn.apache.org/viewvc/james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetry.java?rev=1711263&view=auto ============================================================================== --- james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetry.java (added) +++ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetry.java Thu Oct 29 14:12:12 2015 @@ -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.backends.cassandra.utils; + +import com.google.common.base.Preconditions; + +import java.util.Optional; +import java.util.function.BooleanSupplier; +import java.util.stream.IntStream; + +public class FunctionRunnerWithRetry { + + @FunctionalInterface + public interface OptionalSupplier<T> { + Optional<T> getAsOptional(); + } + + private final int maxRetry; + + public FunctionRunnerWithRetry(int maxRetry) { + Preconditions.checkArgument(maxRetry > 0); + this.maxRetry = maxRetry; + } + + public void execute(BooleanSupplier functionNotifyingSuccess) throws LightweightTransactionException { + IntStream.range(0, maxRetry) + .filter((x) -> functionNotifyingSuccess.getAsBoolean()) + .findFirst() + .orElseThrow(() -> new LightweightTransactionException(maxRetry)); + } + + public <T> T executeAndRetrieveObject(OptionalSupplier<T> functionNotifyingSuccess) throws LightweightTransactionException { + return IntStream.range(0, maxRetry) + .mapToObj((x) -> functionNotifyingSuccess.getAsOptional()) + .filter(Optional::isPresent) + .findFirst() + .orElseThrow(() -> new LightweightTransactionException(maxRetry)) + .get(); + } +} Added: james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/LightweightTransactionException.java URL: http://svn.apache.org/viewvc/james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/LightweightTransactionException.java?rev=1711263&view=auto ============================================================================== --- james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/LightweightTransactionException.java (added) +++ james/project/trunk/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/utils/LightweightTransactionException.java Thu Oct 29 14:12:12 2015 @@ -0,0 +1,35 @@ +/**************************************************************** + * 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.backends.cassandra.utils; + +public class LightweightTransactionException extends Exception { + + public LightweightTransactionException(int retries) { + super(createMessage(retries)); + } + + public LightweightTransactionException(int retries, Throwable throwable) { + super(createMessage(retries), throwable); + } + + private static String createMessage(int retries) { + return "Can not execute Optional Supplier. " + retries + " retries."; + } +} Added: james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetryTest.java URL: http://svn.apache.org/viewvc/james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetryTest.java?rev=1711263&view=auto ============================================================================== --- james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetryTest.java (added) +++ james/project/trunk/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/utils/FunctionRunnerWithRetryTest.java Thu Oct 29 14:12:12 2015 @@ -0,0 +1,84 @@ +/**************************************************************** + * 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.backends.cassandra.utils; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.commons.lang.mutable.MutableInt; +import org.junit.Test; + +public class FunctionRunnerWithRetryTest { + + private final static int MAX_RETRY = 10; + + @Test(expected = IllegalArgumentException.class) + public void functionRunnerWithInvalidMaxRetryShouldFail() throws Exception { + new FunctionRunnerWithRetry(-1); + } + + @Test(expected = LightweightTransactionException.class) + public void functionRunnerShouldFailIfTransactionCanNotBePerformed() throws Exception { + final MutableInt value = new MutableInt(0); + new FunctionRunnerWithRetry(MAX_RETRY).execute( + () -> { + value.increment(); + return false; + } + ); + assertThat(value.getValue()).isEqualTo(MAX_RETRY); + } + + @Test + public void functionRunnerShouldWorkOnFirstTry() throws Exception { + final MutableInt value = new MutableInt(0); + new FunctionRunnerWithRetry(MAX_RETRY).execute( + () -> { + value.increment(); + return true; + } + ); + assertThat(value.getValue()).isEqualTo(1); + } + + @Test + public void functionRunnerShouldWorkIfNotSucceededOnFirstTry() throws Exception { + final MutableInt value = new MutableInt(0); + new FunctionRunnerWithRetry(MAX_RETRY).execute( + () -> { + value.increment(); + return (Integer) value.getValue() == MAX_RETRY / 2; + } + ); + assertThat(value.getValue()).isEqualTo(MAX_RETRY / 2); + } + + @Test + public void functionRunnerShouldWorkIfNotSucceededOnMaxRetryReached() throws Exception { + final MutableInt value = new MutableInt(0); + new FunctionRunnerWithRetry(MAX_RETRY).execute( + () -> { + value.increment(); + return (Integer) value.getValue() == MAX_RETRY; + } + ); + assertThat(value.getValue()).isEqualTo(MAX_RETRY); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org