diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java index 98fb7203a..6537e2103 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/CassandraTemplate.java @@ -428,6 +428,7 @@ public class CassandraTemplate extends CassandraAccessor implements CassandraOpe * @see org.springframework.cassandra.core.CassandraOperations#processList(com.datastax.driver.core.ResultSet, java.lang.Class) */ @Override + @SuppressWarnings( "unchecked" ) public List processList(ResultSet resultSet, Class elementType) throws DataAccessException { List rows = resultSet.all(); List list = new ArrayList(rows.size()); diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/AlterKeyspaceCqlGenerator.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/AlterKeyspaceCqlGenerator.java index beb0c08da..ffb404909 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/AlterKeyspaceCqlGenerator.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/AlterKeyspaceCqlGenerator.java @@ -19,11 +19,7 @@ import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull; import java.util.Map; -import org.springframework.cassandra.core.keyspace.AddColumnSpecification; -import org.springframework.cassandra.core.keyspace.AlterColumnSpecification; import org.springframework.cassandra.core.keyspace.AlterKeyspaceSpecification; -import org.springframework.cassandra.core.keyspace.ColumnChangeSpecification; -import org.springframework.cassandra.core.keyspace.DropColumnSpecification; import org.springframework.cassandra.core.keyspace.Option; /** @@ -41,8 +37,7 @@ public class AlterKeyspaceCqlGenerator extends KeyspaceOptionsCqlGenerator getCqlGeneratorFor(ColumnChangeSpecification change) { -// if (change instanceof AddColumnSpecification) { -// return new AddColumnCqlGenerator((AddColumnSpecification) change); -// } -// if (change instanceof DropColumnSpecification) { -// return new DropColumnCqlGenerator((DropColumnSpecification) change); -// } -// if (change instanceof AlterColumnSpecification) { -// return new AlterColumnCqlGenerator((AlterColumnSpecification) change); -// } -// throw new IllegalArgumentException("unknown ColumnChangeSpecification type: " + change.getClass().getName()); -// } -// -// @SuppressWarnings("unchecked") -// protected StringBuilder optionsCql(StringBuilder cql) { -// cql = noNull(cql); -// -// Map options = spec().getOptions(); -// if (options == null || options.isEmpty()) { -// return cql; -// } -// -// cql.append(" WITH "); -// boolean first = true; -// for (String key : options.keySet()) { -// if (first) { -// first = false; -// } else { -// cql.append(" AND "); -// } -// -// cql.append(key); -// -// Object value = options.get(key); -// if (value == null) { -// continue; -// } -// cql.append(" = "); -// -// if (value instanceof Map) { -// optionValueMap((Map) value, cql); -// continue; -// } -// -// // else just use value as string -// cql.append(value.toString()); -// } -// return cql; -// } + @SuppressWarnings( "unchecked" ) + protected StringBuilder optionsCql(StringBuilder cql) { + cql = noNull(cql); + + // begin options clause + Map options = spec().getOptions(); + + if (!options.isEmpty()) { + + // option preamble + boolean first = true; + cql.append(" WITH "); + // end option preamble + + if (!options.isEmpty()) { + for (String name : options.keySet()) { + // append AND if we're not on first option + if (first) { + first = false; + } else { + cql.append(" AND "); + } + + // append = + cql.append(name); + + Object value = options.get(name); + if (value == null) { // then assume string-only, valueless option like "COMPACT STORAGE" + continue; + } + + cql.append(" = "); + + if (value instanceof Map) { + optionValueMap((Map) value, cql); + continue; // end non-empty value map + } + + // else just use value as string + cql.append(value.toString()); + } + } + } + // end options + + return cql; + } } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/CreateKeyspaceCqlGenerator.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/CreateKeyspaceCqlGenerator.java index 66a999f62..2f4a75ccc 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/CreateKeyspaceCqlGenerator.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/CreateKeyspaceCqlGenerator.java @@ -15,15 +15,10 @@ */ package org.springframework.cassandra.core.cql.generator; -import static org.springframework.cassandra.core.PrimaryKeyType.CLUSTERED; -import static org.springframework.cassandra.core.PrimaryKeyType.PARTITIONED; import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull; -import java.util.ArrayList; -import java.util.List; import java.util.Map; -import org.springframework.cassandra.core.keyspace.ColumnSpecification; import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification; import org.springframework.cassandra.core.keyspace.Option; @@ -56,146 +51,53 @@ public class CreateKeyspaceCqlGenerator extends KeyspaceCqlGenerator options = spec().getOptions(); + + if (!options.isEmpty()) { + + // option preamble + boolean first = true; + cql.append(" WITH "); + // end option preamble + + if (!options.isEmpty()) { + for (String name : options.keySet()) { + // append AND if we're not on first option + if (first) { + first = false; + } else { + cql.append(" AND "); + } + + // append = + cql.append(name); + + Object value = options.get(name); + if (value == null) { // then assume string-only, valueless option like "COMPACT STORAGE" + continue; + } + + cql.append(" = "); + + if (value instanceof Map) { + optionValueMap((Map) value, cql); + continue; // end non-empty value map + } + + // else just use value as string + cql.append(value.toString()); + } + } + } + // end options + + return cql; } - - -// @SuppressWarnings("unchecked") -// protected StringBuilder columnsAndOptionsCql(StringBuilder cql) { -// -// cql = noNull(cql); -// -// // begin columns -// cql.append(" ("); -// -// List partitionKeys = new ArrayList(); -// List clusterKeys = new ArrayList(); -// for (ColumnSpecification col : spec().getColumns()) { -// col.toCql(cql).append(", "); -// -// if (col.getKeyType() == PARTITIONED) { -// partitionKeys.add(col); -// } else if (col.getKeyType() == CLUSTERED) { -// clusterKeys.add(col); -// } -// } -// -// // begin primary key clause -// cql.append("PRIMARY KEY ("); -// -// if (partitionKeys.size() > 1) { -// // begin partition key clause -// cql.append("("); -// } -// -// appendColumnNames(cql, partitionKeys); -// -// if (partitionKeys.size() > 1) { -// cql.append(")"); -// // end partition key clause -// } -// -// if (!clusterKeys.isEmpty()) { -// cql.append(", "); -// } -// -// appendColumnNames(cql, clusterKeys); -// -// cql.append(")"); -// // end primary key clause -// -// cql.append(")"); -// // end columns -// -// StringBuilder ordering = createOrderingClause(clusterKeys); -// // begin options -// // begin option clause -// Map options = spec().getOptions(); -// -// if (ordering != null || !options.isEmpty()) { -// -// // option preamble -// boolean first = true; -// cql.append(" WITH "); -// // end option preamble -// -// if (ordering != null) { -// cql.append(ordering); -// first = false; -// } -// if (!options.isEmpty()) { -// for (String name : options.keySet()) { -// // append AND if we're not on first option -// if (first) { -// first = false; -// } else { -// cql.append(" AND "); -// } -// -// // append = -// cql.append(name); -// -// Object value = options.get(name); -// if (value == null) { // then assume string-only, valueless option like "COMPACT STORAGE" -// continue; -// } -// -// cql.append(" = "); -// -// if (value instanceof Map) { -// optionValueMap((Map) value, cql); -// continue; // end non-empty value map -// } -// -// // else just use value as string -// cql.append(value.toString()); -// } -// } -// } -// // end options -// -// return cql; -// } - -// private static StringBuilder createOrderingClause(List columns) { -// StringBuilder ordering = null; -// boolean first = true; -// for (ColumnSpecification col : columns) { -// -// if (col.getOrdering() != null) { // then ordering specified -// if (ordering == null) { // then initialize ordering clause -// ordering = new StringBuilder().append("CLUSTERING ORDER BY ("); -// } -// if (first) { -// first = false; -// } else { -// ordering.append(", "); -// } -// ordering.append(col.getName()).append(" ").append(col.getOrdering().cql()); -// } -// } -// if (ordering != null) { // then end ordering option -// ordering.append(")"); -// } -// return ordering; -// } -// -// private static void appendColumnNames(StringBuilder str, List columns) { -// -// boolean first = true; -// for (ColumnSpecification col : columns) { -// if (first) { -// first = false; -// } else { -// str.append(", "); -// } -// str.append(col.getName()); -// -// } -// -// } - } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/KeyspaceCqlGenerator.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/KeyspaceCqlGenerator.java index 32f504fb3..9c3c87eb2 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/KeyspaceCqlGenerator.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/KeyspaceCqlGenerator.java @@ -15,14 +15,7 @@ */ package org.springframework.cassandra.core.cql.generator; -import static org.springframework.cassandra.core.cql.CqlStringUtils.escapeSingle; -import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull; -import static org.springframework.cassandra.core.cql.CqlStringUtils.singleQuote; - -import java.util.Map; - import org.springframework.cassandra.core.keyspace.KeyspaceSpecification; -import org.springframework.cassandra.core.keyspace.Option; /** * Base class that contains behavior common to CQL generation for table operations. @@ -41,40 +34,4 @@ public abstract class KeyspaceCqlGenerator> e protected T spec() { return (T) getSpecification(); } - - protected StringBuilder optionValueMap(Map valueMap, StringBuilder cql) { - cql = noNull(cql); - - if (valueMap == null || valueMap.isEmpty()) { - return cql; - } - // else option value is a non-empty map - - // append { 'name' : 'value', ... } - cql.append("{ "); - boolean mapFirst = true; - for (Map.Entry entry : valueMap.entrySet()) { - if (mapFirst) { - mapFirst = false; - } else { - cql.append(", "); - } - - Option option = entry.getKey(); - cql.append(singleQuote(option.getName())); // entries in map keys are always quoted - cql.append(" : "); - Object entryValue = entry.getValue(); - entryValue = entryValue == null ? "" : entryValue.toString(); - if (option.escapesValue()) { - entryValue = escapeSingle(entryValue); - } - if (option.quotesValue()) { - entryValue = singleQuote(entryValue); - } - cql.append(entryValue); - } - cql.append(" }"); - - return cql; - } } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/TableCqlGenerator.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/TableCqlGenerator.java index 10e883349..857bb9503 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/TableCqlGenerator.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/cql/generator/TableCqlGenerator.java @@ -15,13 +15,6 @@ */ package org.springframework.cassandra.core.cql.generator; -import static org.springframework.cassandra.core.cql.CqlStringUtils.escapeSingle; -import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull; -import static org.springframework.cassandra.core.cql.CqlStringUtils.singleQuote; - -import java.util.Map; - -import org.springframework.cassandra.core.keyspace.Option; import org.springframework.cassandra.core.keyspace.TableSpecification; /** @@ -41,40 +34,4 @@ public abstract class TableCqlGenerator> extends protected T spec() { return (T) getSpecification(); } - - protected StringBuilder optionValueMap(Map valueMap, StringBuilder cql) { - cql = noNull(cql); - - if (valueMap == null || valueMap.isEmpty()) { - return cql; - } - // else option value is a non-empty map - - // append { 'name' : 'value', ... } - cql.append("{ "); - boolean mapFirst = true; - for (Map.Entry entry : valueMap.entrySet()) { - if (mapFirst) { - mapFirst = false; - } else { - cql.append(", "); - } - - Option option = entry.getKey(); - cql.append(singleQuote(option.getName())); // entries in map keys are always quoted - cql.append(" : "); - Object entryValue = entry.getValue(); - entryValue = entryValue == null ? "" : entryValue.toString(); - if (option.escapesValue()) { - entryValue = escapeSingle(entryValue); - } - if (option.quotesValue()) { - entryValue = singleQuote(entryValue); - } - cql.append(entryValue); - } - cql.append(" }"); - - return cql; - } } diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/CreateIndexSpecification.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/CreateIndexSpecification.java index 5b8d8f656..7c8a86426 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/CreateIndexSpecification.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/CreateIndexSpecification.java @@ -88,7 +88,6 @@ public class CreateIndexSpecification extends IndexNameSpecification getDataCenters(); + Boolean getDurableWrites(); /** * Returns an unmodifiable {@link Map} of keyspace options. diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/KeyspaceOption.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/KeyspaceOption.java index c14ddf04f..3e5905f64 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/KeyspaceOption.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/KeyspaceOption.java @@ -1,9 +1,11 @@ package org.springframework.cassandra.core.keyspace; +import java.util.Map; + public enum KeyspaceOption implements Option { - REPLICATION_STRATEGY("class", String.class, true, true, true), + REPLICATION("replication", Map.class, true, false, false), - REPLICATION_FACTOR("replication_factor", Long.class, false, false, false); + DURABLE_WRITES("durable_writes", Boolean.class, false, false, false); private Option delegate; diff --git a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/KeyspaceSpecification.java b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/KeyspaceSpecification.java index c1b28dfa8..b91e1b48d 100644 --- a/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/KeyspaceSpecification.java +++ b/spring-cassandra/src/main/java/org/springframework/cassandra/core/keyspace/KeyspaceSpecification.java @@ -15,9 +15,6 @@ */ package org.springframework.cassandra.core.keyspace; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; /** @@ -28,48 +25,27 @@ import java.util.Map; */ public class KeyspaceSpecification extends KeyspaceOptionsSpecification> implements KeyspaceDescriptor { - private String replicationStrategy; - - private Long replicationFactor; - - private Map dataCenters = new HashMap(); - - @Override - public String getReplicationStrategy() { - return replicationStrategy; - } + private Boolean durableWrites; /** - * @param replicationStrategy the replicationStrategy to set + * @param durableWrites the durableWrites to set */ - public void setReplicationStrategy(String replicationStrategy) { - this.replicationStrategy = replicationStrategy; - } - - /** - * @return the replicationFactor - */ - @Override - public Long getReplicationFactor() { - return replicationFactor; - } - - /** - * @param replicationFactor the replicationFactor to set - */ - public void setReplicationFactor(Long replicationFactor) { - this.replicationFactor = replicationFactor; + @SuppressWarnings( "unchecked" ) + public T durableWrites(Boolean durableWrites) { + this.durableWrites = durableWrites; + + return (T) this; } @Override - public Map getDataCenters() { - return Collections.unmodifiableMap( dataCenters ); + public Boolean getDurableWrites() { + return durableWrites; } /** - * @param dataCenters the dataCenters to set + * @param durableWrites the durableWrites to set */ - public void setDataCenters(Map dataCenters) { - this.dataCenters = dataCenters; + public void setDurableWrites(Boolean durableWrites) { + this.durableWrites = durableWrites; } } diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java index 7b763431d..82340180f 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java @@ -140,6 +140,7 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio } @Test + @SuppressWarnings( "unchecked" ) public void ingestionTestListOfList() { String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)"; @@ -507,6 +508,7 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio // Insert our 3 test books. ingestionTestObjectArray(); + @SuppressWarnings( "unused" ) Book book = cassandraTemplate.queryForObject("select * from book where isbn in ('1234','2345','3456')", new RowMapper() { @Override @@ -561,6 +563,7 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio @Test(expected = ClassCastException.class) public void queryForObjectTestCqlStringRequiredTypeInvalid() { + @SuppressWarnings( "unused" ) Float title = cassandraTemplate.queryForObject("select title from book where isbn in ('" + ISBN_NINES + "')", Float.class);