if(builderObj == null || !(builderObj instanceof Builder))
throw new IllegalArgumentException("The property Bootstrap.CASSANDRA_BUILDER was not in the Map or was in Map but was not of type Builder and must be supplied when using Cassandra plugin");
callback = (CreateCfCallback) properties.get(SpiConstants.CASSANDRA_CF_CREATE_CALLBACK);
Builder builder = (Builder) builderObj;
clusterContext = builder.buildCluster(ThriftFamilyFactory.getInstance());
keyspaceName = clusterContext.getKeyspaceName();
if(keyspaceName == null)
throw new IllegalArgumentException("You did not call Builder.forKeyspace on the astyanax Builder api. We need to know the keyspace to continue");
clusterContext.start();
cluster = clusterContext.getClient();
List<KeyspaceDefinition> keyspaces = cluster.describeKeyspaces();
List<String> keyspaceNames = new ArrayList<String>();
boolean exists = false;
for(KeyspaceDefinition kDef : keyspaces) {
keyspaceNames.add(kDef.getName());
if(keyspaceName.equalsIgnoreCase(kDef.getName())) {
exists = true;
break;
}
}
log.info("your keyspace="+keyspaceName+" keyspaces="+keyspaceNames+" exists="+exists);
if(!exists) {
KeyspaceDefinition def = cluster.makeKeyspaceDefinition();
def.setName(keyspaceName);
def.setStrategyClass("org.apache.cassandra.locator.SimpleStrategy");
Map<String, String> map = new HashMap<String, String>();
map.put("replication_factor", "3");
def.setStrategyOptions(map);
if(callback != null)
def = (KeyspaceDefinition) callback.configureKeySpace(keyspaceName, def);
cluster.addKeyspace(def);
log.info("added keyspace="+keyspaceName+" so it now exists");
}
context = builder.buildKeyspace(ThriftFamilyFactory.getInstance());
context.start();
keyspace = context.getClient();
//for some reason, the keyspace is not always created fast enough so sleep 3 seconds and try again