Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
add create keyspace options
Browse files Browse the repository at this point in the history
  • Loading branch information
otaviojava committed Mar 31, 2014
1 parent b806d6a commit 1599c33
Show file tree
Hide file tree
Showing 18 changed files with 502 additions and 60 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/easycassandra/ReplicaStrategy.java
Expand Up @@ -34,7 +34,11 @@ public enum ReplicaStrategy {
* strategy allows you to specify how many replicas you want in each data
* center.
*/
NETWORK_TOPOLOGY_STRATEGY("'NetworkTopologyStrategy'");
NETWORK_TOPOLOGY_STRATEGY("'NetworkTopologyStrategy'"),
/**
* use a custom query.
*/
CUSTOM_STRATEGY("");

private String value;

Expand Down
Expand Up @@ -75,7 +75,7 @@ protected void verifyKeySpace(String keySpace, Session session) {
*/
private void initConection() {
cluter = clusterInformation.build();
new FixKeySpace().verifyKeySpace(clusterInformation.getKeySpace(), getSession());
new FixKeySpace().createKeySpace(clusterInformation.toInformationKeySpace(), getSession());
}

}
Expand Up @@ -37,8 +37,9 @@ public interface CassandraFactory {
/**
* create a new keySpace on the cluster.
* @param keySpace - the keySpace name
* @param replicaStrategy - {@link ReplicaStrategy}
* @param factor - the number of replica factor
* @param replicaStrategy the replicaStrategy
*/
void createKeySpace(String keySpace, ReplicaStrategy replicaStrategy, int factor);
void createKeySpace(String keySpace, ReplicaStrategy replicaStrategy,
int factor);
}
@@ -1,7 +1,12 @@
package org.easycassandra.persistence.cassandra;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.easycassandra.ReplicaStrategy;
import org.easycassandra.persistence.cassandra.FixKeySpaceUtil.KeySpaceQueryInformation;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Cluster.Builder;
Expand All @@ -16,17 +21,25 @@ public class ClusterInformation {

private String keySpace;

private int port = PORT_DEFAULT;

private int replicaFactor = REPLICA_FACTOR_DEFAULT;

private String user = "";

private String password = "";

private Map<String, Integer> dataCenter = new HashMap<>();

private ReplicaStrategy replicaStrategy = REPLICASTRATEGY_DEFAULT;

private String customQuery;

private int replicaFactor = REPLICA_FACTOR_DEFAULT;

private int port = PORT_DEFAULT;

private static final int PORT_DEFAULT = 9042;

private static final int REPLICA_FACTOR_DEFAULT = 3;
private static final ReplicaStrategy REPLICASTRATEGY_DEFAULT = ReplicaStrategy.SIMPLES_TRATEGY;


public List<String> getHosts() {
return hosts;
Expand Down Expand Up @@ -75,6 +88,31 @@ public int getReplicaFactor() {
public void setReplicaFactor(int replicaFactor) {
this.replicaFactor = replicaFactor;
}

public Map<String, Integer> getDataCenter() {
return dataCenter;
}

public void setDataCenter(Map<String, Integer> dataCenter) {
this.dataCenter = dataCenter;
}

public ReplicaStrategy getReplicaStrategy() {
return replicaStrategy;
}

public void setReplicaStrategy(ReplicaStrategy replicaStrategy) {
this.replicaStrategy = replicaStrategy;
}

public String getCustomQuery() {
return customQuery;
}

public void setCustomQuery(String customQuery) {
this.customQuery = customQuery;
}

/**
* new instance of ClusterInformation.
* @return the new instance
Expand Down Expand Up @@ -144,7 +182,40 @@ public ClusterInformation addHost(String parameterHost, String... parameterHosts
}
return this;
}

/**
* define the replica placement strategy on Cassandra and
* how a keyspace will create if not exists.
* @param replicaStrategy {@link ReplicaStrategy}
* @return this
*/
public ClusterInformation withReplicaStrategy(
ReplicaStrategy replicaStrategy) {
this.replicaStrategy = replicaStrategy;
return this;
}
/**
* a custom query to create the keyspace if not exist, it is mandatory when is
* {@link ReplicaStrategy#CUSTOM_STRATEGY}
* if you define and must begin with.
* @param customQuery the create to be executed
* @return this
*/
public ClusterInformation withCustomQuery(String customQuery) {
this.customQuery = customQuery;
return this;
}
/**
* Inform a replica factor to a specific data center, you should use when define
* {@link ReplicaStrategy#NETWORK_TOPOLOGY_STRATEGY}
* as Replica Strategy.
* @param dataCenterName the data center name
* @param factor the replica factor to data center
* @return this.
*/
public ClusterInformation addDataCenter(String dataCenterName, int factor) {
this.dataCenter.put(dataCenterName, factor);
return this;
}
Cluster build() {

Builder buildCluster = Cluster.builder().withPort(port)
Expand All @@ -155,4 +226,14 @@ Cluster build() {
}
return buildCluster.build();
}

KeySpaceQueryInformation toInformationKeySpace() {
KeySpaceQueryInformation information = new KeySpaceQueryInformation();
information.setCustomQuery(customQuery);
information.setDataCenter(dataCenter);
information.setFactor(replicaFactor);
information.setKeySpace(keySpace);
information.setReplicaStrategy(replicaStrategy);
return information;
}
}
Expand Up @@ -17,6 +17,7 @@ public interface EasyCassandraFactory {
* - the keyspace's name
* @return the client bridge for the Cassandra data base
*/
@Deprecated
Persistence getPersistence(String keySpace);
/**
* Method for create the Cassandra's Client, if the keyspace there is not,if
Expand All @@ -32,6 +33,7 @@ public interface EasyCassandraFactory {
* - number of the factor
* @return the client bridge for the Cassandra data base
*/
@Deprecated
Persistence getPersistence(String host, String keySpace,
ReplicaStrategy replicaStrategy, int factor);

Expand All @@ -43,10 +45,10 @@ Persistence getPersistence(String host, String keySpace,

/**
* interface to create an easy way to create query.
* @see BuilderPersistence
* @return the {@link BuilderPersistenceImpl}
* @see PersistenceBuilder
* @return the {@link PersistenceBuilderImpl}
*/
BuilderPersistence getBuilderPersistence();
PersistenceBuilder getBuilderPersistence();

/**
* returns a persistence async.
Expand Down
Expand Up @@ -13,8 +13,11 @@
*/
package org.easycassandra.persistence.cassandra;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.easycassandra.ClassInformation;
import org.easycassandra.ClassInformations;
Expand All @@ -33,30 +36,47 @@
public class EasyCassandraManager extends AbstractCassandraFactory implements
EasyCassandraFactory {


private Map<String, Persistence> persistenceMap = new HashMap<String, Persistence>();
private List<String> keySpaces = new ArrayList<>();
/**
* the constructor using default port.
* @param clusterInformation {@link ClusterInformation}
*/
public EasyCassandraManager(ClusterInformation clusterInformation) {
super(clusterInformation);
keySpaces.add(getKeySpace());
}

@Override
@Override
public Persistence getPersistence(String keySpace) {
Cluster cluter = getCluster();
Session session = cluter.connect();
verifyKeySpace(keySpace, cluter.connect());
return new PersistenceSimpleImpl(session, keySpace);

if (!persistenceMap.containsKey(keySpace)) {
Session session = getSession();
if (!keySpace.contains(keySpace)) {
verifyKeySpace(keySpace, getSession());
keySpaces.add(keySpace);
}
persistenceMap.put(keySpace, new PersistenceSimpleImpl(session,
keySpace));
}
return persistenceMap.get(keySpace);
}

@Override
@Override
public Persistence getPersistence(String host, String keySpace,
ReplicaStrategy replicaStrategy, int factor) {
Cluster cluter = Cluster.builder().addContactPoints(host).build();
Session session = cluter.connect();
verifyKeySpace(keySpace, cluter.connect(), replicaStrategy, factor);
return new PersistenceSimpleImpl(session, keySpace);

if (!persistenceMap.containsKey(keySpace)) {
Cluster cluter = Cluster.builder().addContactPoints(host).build();
Session session = cluter.connect();
if (!keySpace.contains(keySpace)) {
verifyKeySpace(keySpace, cluter.connect(), replicaStrategy, factor);
keySpaces.add(keySpace);
}
persistenceMap.put(keySpace, new PersistenceSimpleImpl(session,
keySpace));
}
return persistenceMap.get(keySpace);
}

@Override
Expand All @@ -65,8 +85,8 @@ public Persistence getPersistence() {
}

@Override
public BuilderPersistence getBuilderPersistence() {
return new BuilderPersistenceImpl(getSession(), getKeySpace());
public PersistenceBuilder getBuilderPersistence() {
return new PersistenceBuilderImpl(getSession(), getKeySpace());
}

@Override
Expand Down
Expand Up @@ -14,9 +14,10 @@
*/
package org.easycassandra.persistence.cassandra;

import java.util.logging.Logger;

import org.easycassandra.ReplicaStrategy;
import org.easycassandra.persistence.cassandra.FixKeySpaceUtil.CreateKeySpace;
import org.easycassandra.persistence.cassandra.FixKeySpaceUtil.CreateKeySpaceException;
import org.easycassandra.persistence.cassandra.FixKeySpaceUtil.KeySpaceQueryInformation;

import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.InvalidQueryException;
Expand All @@ -27,44 +28,66 @@
*/
class FixKeySpace {

private static final String CREATE_KEY_SPACE_CQL = "CREATE KEYSPACE "
+ ":keySpace WITH replication = {'class': :replication , 'replication_factor': "
+ ":factor};";

private static final int DEFAULT_REPLICATION_FACTOR = 3;

private static final ReplicaStrategy DEFAULT_REPLICA_STRATEGY = ReplicaStrategy.SIMPLES_TRATEGY;

/**
* Verify if keySpace exist.
* @param keySpace
* - nome of keyspace
* @param session
* - session of Cassandra
* @param keySpace - nome of keyspace
* @param session - session of Cassandra
*/
public final void verifyKeySpace(String keySpace, Session session) {
verifyKeySpace(keySpace, session, DEFAULT_REPLICA_STRATEGY, DEFAULT_REPLICATION_FACTOR);
}

public void verifyKeySpace(String keySpace, Session session,
ReplicaStrategy replicaStrategy, int factor) {
createKeySpace(session, keySpace, replicaStrategy, factor);
}

public void createKeySpace(Session session, String keySpace,
ReplicaStrategy replicaStrategy, int factor) {

KeySpaceQueryInformation information = getInformation(replicaStrategy,
factor);
createKeySpace(information, session);

}

public void createKeySpace(KeySpaceQueryInformation information, Session session) {

CreateKeySpace createKeySpace = FixKeySpaceUtil.INSTANCE
.getCreate(information.getReplicaStrategy());
try {
session.execute("use " + keySpace);
session.execute(createKeySpace.createQuery(information));
} catch (InvalidQueryException exception) {
Logger.getLogger(FixKeySpace.class.getName()).info(
"KeySpace does not exist, create a keySpace: " + keySpace);
createKeySpace(session, keySpace, replicaStrategy, factor);
verifyKeySpace(keySpace, session);
error(information, createKeySpace, exception);
}
}

private void error(KeySpaceQueryInformation information,
CreateKeySpace createKeySpace, InvalidQueryException exception) {
StringBuilder error = new StringBuilder();
error.append(" An error happened when execute create keyspace: ")
.append(information.getKeySpace());
error.append(" with type: ").append(information.getReplicaStrategy());
error.append(" With query: ").append(
createKeySpace.createQuery(information));
error.append(" Error cause: ").append(exception.getCause()).append(" ")
.append(exception.getMessage());

throw new CreateKeySpaceException(error.toString());

}

public void createKeySpace(Session session, String keySpace,
private KeySpaceQueryInformation getInformation(
ReplicaStrategy replicaStrategy, int factor) {
String query = CREATE_KEY_SPACE_CQL.replace(":keySpace", keySpace)
.replace(":replication", replicaStrategy.getValue())
.replace(":factor", String.valueOf(factor));
session.execute(query);

KeySpaceQueryInformation information = new KeySpaceQueryInformation();
information.setReplicaStrategy(replicaStrategy);
information.setFactor(factor);
return information;
}

}

0 comments on commit 1599c33

Please sign in to comment.