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

commit a82f3313a0e16a2eeb8c065fb4e01e3fa4b781b7
Author: Benoit Tellier <[email protected]>
AuthorDate: Wed Oct 30 10:53:29 2019 +0700

    JAMES-2904 Introduce a KeyspaceFactory
    
    Handles the responsibility of creating the keyspace, if it does not
    already exists.
---
 .../backends/cassandra/init/ClusterFactory.java    | 16 ----------
 .../backends/cassandra/init/KeyspaceFactory.java   | 36 ++++++++++++++++++++++
 .../cassandra/init/ResilientClusterProvider.java   |  2 +-
 .../james/backends/cassandra/CassandraCluster.java |  4 ++-
 .../SessionWithInitializedTablesFactoryTest.java   |  4 +--
 5 files changed, 42 insertions(+), 20 deletions(-)

diff --git 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterFactory.java
 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterFactory.java
index eafd622..ff04fb4 100644
--- 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterFactory.java
+++ 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterFactory.java
@@ -24,7 +24,6 @@ import 
org.apache.james.backends.cassandra.init.configuration.ClusterConfigurati
 import com.datastax.driver.core.Cluster;
 import com.datastax.driver.core.ConsistencyLevel;
 import com.datastax.driver.core.QueryOptions;
-import com.datastax.driver.core.Session;
 import com.datastax.driver.core.SocketOptions;
 import com.google.common.base.Preconditions;
 
@@ -65,21 +64,6 @@ public class ClusterFactory {
         }
     }
 
-    public static Cluster createWithKeyspace(ClusterConfiguration 
clusterConfiguration) {
-        Cluster cluster = create(clusterConfiguration);
-        createKeyspace(clusterConfiguration, cluster);
-        return cluster;
-    }
-
-    public static void createKeyspace(ClusterConfiguration 
clusterConfiguration, Cluster cluster) {
-        try (Session session = cluster.connect()) {
-            session.execute("CREATE KEYSPACE IF NOT EXISTS " + 
clusterConfiguration.getKeyspace()
-                + " WITH replication = {'class':'SimpleStrategy', 
'replication_factor':" + clusterConfiguration.getReplicationFactor() + "}"
-                + " AND durable_writes = " + 
clusterConfiguration.isDurableWrites()
-                + ";");
-        }
-    }
-
     private static QueryOptions queryOptions() {
         return new QueryOptions()
                 .setConsistencyLevel(ConsistencyLevel.QUORUM);
diff --git 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/KeyspaceFactory.java
 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/KeyspaceFactory.java
new file mode 100644
index 0000000..3f71803
--- /dev/null
+++ 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/KeyspaceFactory.java
@@ -0,0 +1,36 @@
+/****************************************************************
+ * 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.init;
+
+import 
org.apache.james.backends.cassandra.init.configuration.ClusterConfiguration;
+
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.Session;
+
+public class KeyspaceFactory {
+    public static void createKeyspace(ClusterConfiguration 
clusterConfiguration, Cluster cluster) {
+        try (Session session = cluster.connect()) {
+            session.execute("CREATE KEYSPACE IF NOT EXISTS " + 
clusterConfiguration.getKeyspace()
+                + " WITH replication = {'class':'SimpleStrategy', 
'replication_factor':" + clusterConfiguration.getReplicationFactor() + "}"
+                + " AND durable_writes = " + 
clusterConfiguration.isDurableWrites()
+                + ";");
+        }
+    }
+}
diff --git 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ResilientClusterProvider.java
 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ResilientClusterProvider.java
index 743b887..657f118 100644
--- 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ResilientClusterProvider.java
+++ 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ResilientClusterProvider.java
@@ -62,7 +62,7 @@ public class ResilientClusterProvider implements 
Provider<Cluster> {
         return () -> {
             Cluster cluster = ClusterFactory.create(configuration);
             try {
-                ClusterFactory.createKeyspace(configuration, cluster);
+                KeyspaceFactory.createKeyspace(configuration, cluster);
                 return cluster;
             } catch (Exception e) {
                 cluster.close();
diff --git 
a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
 
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
index 50c6099..d83fc26 100644
--- 
a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
+++ 
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
@@ -24,6 +24,7 @@ import 
org.apache.james.backends.cassandra.components.CassandraModule;
 import org.apache.james.backends.cassandra.init.CassandraTableManager;
 import org.apache.james.backends.cassandra.init.CassandraTypesProvider;
 import org.apache.james.backends.cassandra.init.ClusterFactory;
+import org.apache.james.backends.cassandra.init.KeyspaceFactory;
 import 
org.apache.james.backends.cassandra.init.SessionWithInitializedTablesFactory;
 import 
org.apache.james.backends.cassandra.init.configuration.ClusterConfiguration;
 import org.apache.james.util.Host;
@@ -61,7 +62,8 @@ public final class CassandraCluster implements AutoCloseable {
                 .keyspace(KEYSPACE)
                 .disableDurableWrites()
                 .build();
-            cluster = ClusterFactory.createWithKeyspace(clusterConfiguration);
+            cluster = ClusterFactory.create(clusterConfiguration);
+            KeyspaceFactory.createKeyspace(clusterConfiguration, cluster);
             session = new 
SessionWithInitializedTablesFactory(clusterConfiguration, cluster, 
module).get();
             typesProvider = new CassandraTypesProvider(module, session);
         } catch (Exception exception) {
diff --git 
a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/init/SessionWithInitializedTablesFactoryTest.java
 
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/init/SessionWithInitializedTablesFactoryTest.java
index f3e6dd9..3235b40 100644
--- 
a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/init/SessionWithInitializedTablesFactoryTest.java
+++ 
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/init/SessionWithInitializedTablesFactoryTest.java
@@ -33,7 +33,6 @@ import 
org.apache.james.backends.cassandra.init.configuration.ClusterConfigurati
 import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionDAO;
 import 
org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager;
 import 
org.apache.james.backends.cassandra.versions.CassandraSchemaVersionModule;
-import org.apache.james.util.Host;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeAll;
@@ -127,7 +126,8 @@ class SessionWithInitializedTablesFactoryTest {
             .replicationFactor(1)
             .disableDurableWrites()
             .build();
-        Cluster cluster = 
ClusterFactory.createWithKeyspace(clusterConfiguration);
+        Cluster cluster = ClusterFactory.create(clusterConfiguration);
+        KeyspaceFactory.createKeyspace(clusterConfiguration, cluster);
         return () -> new SessionWithInitializedTablesFactory(
                 clusterConfiguration,
                 cluster,


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to