Skip to content

Commit

Permalink
JAMES-1772 Make something like a builder in ClusterWithKeyspaceCreate…
Browse files Browse the repository at this point in the history
…dFactory
  • Loading branch information
Raphael Ouazana committed Jun 21, 2016
1 parent 1590e60 commit 68ddd58
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
Expand Up @@ -25,24 +25,40 @@
public class ClusterWithKeyspaceCreatedFactory { public class ClusterWithKeyspaceCreatedFactory {


private final static int DEFAULT_REPLICATION_FACTOR = 1; private final static int DEFAULT_REPLICATION_FACTOR = 1;


public static Cluster clusterWithInitializedKeyspaceWithoutDurableWrites(Cluster cluster, String keyspace, int replicationFactor) { public static Configuration config(Cluster cluster, String keyspace) {
return clusterWithInitializedKeyspace(cluster, keyspace, replicationFactor, false); return new Configuration(cluster, keyspace);
}

public static Cluster clusterWithInitializedKeyspace(Cluster cluster, String keyspace, int replicationFactor) {
return clusterWithInitializedKeyspace(cluster, keyspace, replicationFactor, true);
} }


private static Cluster clusterWithInitializedKeyspace(Cluster cluster, String keyspace, int replicationFactor, boolean durableWrites) { public static class Configuration {
if (isKeyspacePresent(cluster, keyspace)) { private Cluster cluster;
createKeyspace(cluster, keyspace, replicationFactor, durableWrites); private String keyspace;
private boolean durableWrites;
private int replicationFactor;

private Configuration(Cluster cluster, String keyspace) {
this.cluster = cluster;
this.keyspace = keyspace;
this.durableWrites = true;
this.replicationFactor = DEFAULT_REPLICATION_FACTOR;
}

public Configuration disableDurableWrites() {
this.durableWrites = false;
return this;
}

public Configuration replicationFactor(int replicationFactor) {
this.replicationFactor = replicationFactor;
return this;
}

public Cluster clusterWithInitializedKeyspace() {
if (isKeyspacePresent(cluster, keyspace)) {
createKeyspace(cluster, keyspace, replicationFactor, durableWrites);
}
return cluster;
} }
return cluster;
}

public static Cluster clusterWithInitializedKeyspace(Cluster cluster, String keyspace) {
return clusterWithInitializedKeyspace(cluster, keyspace, DEFAULT_REPLICATION_FACTOR);
} }


private static boolean isKeyspacePresent(Cluster cluster, String keyspace) { private static boolean isKeyspacePresent(Cluster cluster, String keyspace) {
Expand Down
Expand Up @@ -80,7 +80,10 @@ public void clearAllTables() {
private Optional<Session> tryInitializeSession() { private Optional<Session> tryInitializeSession() {
try { try {
Cluster clusterWithInitializedKeyspace = ClusterWithKeyspaceCreatedFactory Cluster clusterWithInitializedKeyspace = ClusterWithKeyspaceCreatedFactory
.clusterWithInitializedKeyspaceWithoutDurableWrites(getCluster(), KEYSPACE_NAME, REPLICATION_FACTOR); .config(getCluster(), KEYSPACE_NAME)
.replicationFactor(REPLICATION_FACTOR)
.disableDurableWrites()
.clusterWithInitializedKeyspace();
return Optional.of(new SessionWithInitializedTablesFactory(module).createSession(clusterWithInitializedKeyspace, KEYSPACE_NAME)); return Optional.of(new SessionWithInitializedTablesFactory(module).createSession(clusterWithInitializedKeyspace, KEYSPACE_NAME));
} catch (NoHostAvailableException exception) { } catch (NoHostAvailableException exception) {
sleep(SLEEP_BEFORE_RETRY); sleep(SLEEP_BEFORE_RETRY);
Expand Down
Expand Up @@ -76,12 +76,14 @@ Cluster provideCluster(FileSystem fileSystem, AsyncRetryExecutor executor) throw
PropertiesConfiguration configuration = getConfiguration(fileSystem); PropertiesConfiguration configuration = getConfiguration(fileSystem);


return getRetryer(executor, configuration) return getRetryer(executor, configuration)
.getWithRetry(ctx -> ClusterWithKeyspaceCreatedFactory.clusterWithInitializedKeyspace( .getWithRetry(ctx -> ClusterWithKeyspaceCreatedFactory
ClusterFactory.createClusterForSingleServerWithoutPassWord( .config(
configuration.getString("cassandra.ip"), ClusterFactory.createClusterForSingleServerWithoutPassWord(
configuration.getInt("cassandra.port")), configuration.getString("cassandra.ip"),
configuration.getString("cassandra.keyspace"), configuration.getInt("cassandra.port")),
configuration.getInt("cassandra.replication.factor"))) configuration.getString("cassandra.keyspace"))
.replicationFactor(configuration.getInt("cassandra.replication.factor"))
.clusterWithInitializedKeyspace())
.get(); .get();
} }


Expand Down

0 comments on commit 68ddd58

Please sign in to comment.