This commit is contained in:
john-mcpeek
2013-12-30 09:19:13 -05:00
9 changed files with 262 additions and 26 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.cassandra.core.keyspace.AlterTableSpecification;
import org.springframework.cassandra.core.keyspace.ColumnChangeSpecification;
import org.springframework.cassandra.core.keyspace.DropColumnSpecification;
import org.springframework.cassandra.core.keyspace.Option;
import org.springframework.cassandra.core.keyspace.TableOption;
/**
* CQL generator for generating <code>ALTER TABLE</code> statements.
@@ -94,6 +95,16 @@ public class AlterTableCqlGenerator extends TableOptionsCqlGenerator<AlterTableS
cql.append(" WITH ");
boolean first = true;
for (String key : options.keySet()) {
/*
* Compact storage is illegal on alter table.
*
* TODO - Is there a way to handle this in the specification?
*/
if (key.equals(TableOption.COMPACT_STORAGE.getName())) {
throw new IllegalArgumentException("Alter table cannot contain the COMPACT STORAGE option");
}
if (first) {
first = false;
} else {

View File

@@ -31,7 +31,8 @@ public class DropTableCqlGenerator extends TableNameCqlGenerator<DropTableSpecif
}
public StringBuilder toCql(StringBuilder cql) {
return noNull(cql).append("DROP TABLE ").append(spec().getIfExists() ? "IF EXISTS " : "")
return noNull(cql).append("DROP TABLE ")
// .append(spec().getIfExists() ? "IF EXISTS " : "")
.append(spec().getNameAsIdentifier()).append(";");
}
}

View File

@@ -33,13 +33,15 @@ public class AlterTableSpecification extends TableOptionsSpecification<AlterTabl
*/
private List<ColumnChangeSpecification> changes = new ArrayList<ColumnChangeSpecification>();
/**
/*
* Adds a <code>DROP</code> to the list of column changes.
*
* DW Removed as this only works in C* 2.0
*/
public AlterTableSpecification drop(String column) {
changes.add(new DropColumnSpecification(column));
return this;
}
// public AlterTableSpecification drop(String column) {
// changes.add(new DropColumnSpecification(column));
// return this;
// }
/**
* Adds an <code>ADD</code> to the list of column changes.

View File

@@ -22,20 +22,22 @@ package org.springframework.cassandra.core.keyspace;
*/
public class DropTableSpecification extends TableNameSpecification<DropTableSpecification> {
private boolean ifExists;
// private boolean ifExists;
public DropTableSpecification ifExists() {
return ifExists(true);
}
// Added in Cassandra 2.0.
public DropTableSpecification ifExists(boolean ifExists) {
this.ifExists = ifExists;
return this;
}
public boolean getIfExists() {
return ifExists;
}
// public DropTableSpecification ifExists() {
// return ifExists(true);
// }
//
// public DropTableSpecification ifExists(boolean ifExists) {
// this.ifExists = ifExists;
// return this;
// }
//
// public boolean getIfExists() {
// return ifExists;
// }
/**
* Entry point into the {@link DropTableSpecification}'s fluent API to drop a table. Convenient if imported

View File

@@ -16,6 +16,7 @@
package org.springframework.cassandra.test.integration.core.cql.generator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.List;
import java.util.Map;
@@ -24,6 +25,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cassandra.core.cql.CqlStringUtils;
import org.springframework.cassandra.core.keyspace.ColumnSpecification;
import org.springframework.cassandra.core.keyspace.DropTableSpecification;
import org.springframework.cassandra.core.keyspace.TableDescriptor;
import org.springframework.cassandra.core.keyspace.TableOption;
@@ -49,6 +51,13 @@ public class CqlTableSpecificationAssertions {
assertOptions(expected.getOptions(), tmd.getOptions());
}
public static void assertNoTable(DropTableSpecification expected, String keyspace, Session session) {
TableMetadata tmd = session.getCluster().getMetadata().getKeyspace(keyspace.toLowerCase())
.getTable(expected.getName());
assertNull(tmd);
}
public static void assertPartitionKeyColumns(TableDescriptor expected, TableMetadata actual) {
assertColumns(expected.getPartitionKeyColumns(), actual.getPartitionKey());
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2011-2013 the original author or authors.
*
* Licensed 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.springframework.cassandra.test.integration.core.cql.generator;
import static org.springframework.cassandra.test.integration.core.cql.generator.CqlTableSpecificationAssertions.assertNoTable;
import static org.springframework.cassandra.test.integration.core.cql.generator.CqlTableSpecificationAssertions.assertTable;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cassandra.core.cql.generator.DropTableCqlGenerator;
import org.springframework.cassandra.core.keyspace.DropTableSpecification;
import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest;
import org.springframework.cassandra.test.unit.core.cql.generator.AlterTableCqlGeneratorTests;
import org.springframework.cassandra.test.unit.core.cql.generator.CreateTableCqlGeneratorTests;
import org.springframework.cassandra.test.unit.core.cql.generator.DropTableCqlGeneratorTests;
/**
* Test CREATE TABLE / ALTER TABLE / DROP TABLE
*
* @author David Webb
*/
public class TableLifecycleIntegrationTest extends AbstractEmbeddedCassandraIntegrationTest {
private final static Logger log = LoggerFactory.getLogger(TableLifecycleIntegrationTest.class);
CreateTableCqlGeneratorTests.MultipleOptionsTest createTableTest = new CreateTableCqlGeneratorTests.MultipleOptionsTest();
@Test
public void testDrop() {
createTableTest.prepare();
log.info(createTableTest.cql);
session.execute(createTableTest.cql);
assertTable(createTableTest.specification, keyspace, session);
DropTableTest dropTest = new DropTableTest();
dropTest.prepare();
log.info(dropTest.cql);
session.execute(dropTest.cql);
assertNoTable(dropTest.specification, keyspace, session);
}
@Test
public void testAlter() {
createTableTest.prepare();
log.info(createTableTest.cql);
session.execute(createTableTest.cql);
assertTable(createTableTest.specification, keyspace, session);
AlterTableCqlGeneratorTests.MultipleOptionsTest alterTest = new AlterTableCqlGeneratorTests.MultipleOptionsTest();
alterTest.prepare();
log.info(alterTest.cql);
session.execute(alterTest.cql);
// assertTable(alterTest.specification, keyspace, session);
}
public class DropTableTest extends DropTableCqlGeneratorTests.DropTableTest {
/* (non-Javadoc)
* @see org.springframework.cassandra.test.unit.core.cql.generator.TableOperationCqlGeneratorTest#specification()
*/
@Override
public DropTableSpecification specification() {
return DropTableSpecification.dropTable().name(createTableTest.specification.getName());
}
/* (non-Javadoc)
* @see org.springframework.cassandra.test.unit.core.cql.generator.TableOperationCqlGeneratorTest#generator()
*/
@Override
public DropTableCqlGenerator generator() {
return new DropTableCqlGenerator(specification);
}
}
}

View File

@@ -1,15 +1,27 @@
package org.springframework.cassandra.test.unit.core.cql.generator;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cassandra.core.cql.generator.AlterTableCqlGenerator;
import org.springframework.cassandra.core.keyspace.AlterTableSpecification;
import org.springframework.cassandra.core.keyspace.Option;
import org.springframework.cassandra.core.keyspace.TableOption;
import org.springframework.cassandra.core.keyspace.TableOption.CachingOption;
import org.springframework.cassandra.core.keyspace.TableOption.CompactionOption;
import org.springframework.cassandra.core.keyspace.TableOption.CompressionOption;
import com.datastax.driver.core.DataType;
public class AlterTableCqlGeneratorTests {
private final static Logger log = LoggerFactory.getLogger(AlterTableCqlGeneratorTests.class);
/**
* Asserts that the preamble is first & correctly formatted in the given CQL string.
*/
@@ -45,7 +57,7 @@ public class AlterTableCqlGeneratorTests {
public String dropped = "dropped";
public AlterTableSpecification specification() {
return AlterTableSpecification.alterTable().name(name).alter(altered, alteredType).add(added, addedType).drop(dropped);
return AlterTableSpecification.alterTable().name(name).alter(altered, alteredType).add(added, addedType);
}
public AlterTableCqlGenerator generator() {
@@ -61,4 +73,80 @@ public class AlterTableCqlGeneratorTests {
String.format("ALTER %s TYPE %s, ADD %s %s, DROP %s", altered, alteredType, added, addedType, dropped), cql);
}
}
/**
* Fully test all available create table options
*
* @author David Webb
*
*/
public static class MultipleOptionsTest extends AlterTableTest {
public String name = "timeseries_table";
public DataType partitionKeyType0 = DataType.timeuuid();
public String partitionKey0 = "tid";
public DataType partitionKeyType1 = DataType.timestamp();
public String partitionKey1 = "create_timestamp";
public DataType columnType1 = DataType.text();
public String column1 = "data_point";
public Double readRepairChance = 0.6;
public Double dcLocalReadRepairChance = 0.8;
public Double bloomFilterFpChance = 0.002;
public Boolean replcateOnWrite = Boolean.FALSE;
public Long gcGraceSeconds = 1200l;
public String comment = "This is My Table";
public Map<Option, Object> compactionMap = new LinkedHashMap<Option, Object>();
public Map<Option, Object> compressionMap = new LinkedHashMap<Option, Object>();
public AlterTableSpecification specification() {
// Compaction
compactionMap.put(CompactionOption.CLASS, "SizeTieredCompactionStrategy");
compactionMap.put(CompactionOption.MIN_THRESHOLD, "4");
// Compression
compressionMap.put(CompressionOption.SSTABLE_COMPRESSION, "SnappyCompressor");
compressionMap.put(CompressionOption.CHUNK_LENGTH_KB, 128);
compressionMap.put(CompressionOption.CRC_CHECK_CHANCE, 0.75);
return (AlterTableSpecification) AlterTableSpecification
.alterTable()
.name(name)
// .with(TableOption.COMPACT_STORAGE)
.with(TableOption.READ_REPAIR_CHANCE, readRepairChance).with(TableOption.COMPACTION, compactionMap)
.with(TableOption.COMPRESSION, compressionMap).with(TableOption.BLOOM_FILTER_FP_CHANCE, bloomFilterFpChance)
.with(TableOption.CACHING, CachingOption.KEYS_ONLY).with(TableOption.REPLICATE_ON_WRITE, replcateOnWrite)
.with(TableOption.COMMENT, comment).with(TableOption.DCLOCAL_READ_REPAIR_CHANCE, dcLocalReadRepairChance)
.with(TableOption.GC_GRACE_SECONDS, gcGraceSeconds);
}
@Test
public void test() {
prepare();
log.info(cql);
assertPreamble(name, cql);
// assertColumns(String.format("%s %s, %s %s, %s %s", partitionKey0, partitionKeyType0, partitionKey1,
// partitionKeyType1, column1, columnType1), cql);
// assertPrimaryKey(String.format("(%s, %s)", partitionKey0, partitionKey1), cql);
// assertNullOption(TableOption.COMPACT_STORAGE.getName(), cql);
// assertDoubleOption(TableOption.READ_REPAIR_CHANCE.getName(), readRepairChance, cql);
// assertDoubleOption(TableOption.DCLOCAL_READ_REPAIR_CHANCE.getName(), dcLocalReadRepairChance, cql);
// assertDoubleOption(TableOption.BLOOM_FILTER_FP_CHANCE.getName(), bloomFilterFpChance, cql);
// assertStringOption(TableOption.CACHING.getName(), CachingOption.KEYS_ONLY.getValue(), cql);
// assertStringOption(TableOption.REPLICATE_ON_WRITE.getName(), replcateOnWrite.toString(), cql);
// assertStringOption(TableOption.COMMENT.getName(), comment, cql);
// assertLongOption(TableOption.GC_GRACE_SECONDS.getName(), gcGraceSeconds, cql);
}
/* (non-Javadoc)
* @see org.springframework.cassandra.test.unit.core.cql.generator.TableOperationCqlGeneratorTest#generator()
*/
@Override
public AlterTableCqlGenerator generator() {
return new AlterTableCqlGenerator(specification);
}
}
}

View File

@@ -177,8 +177,6 @@ public class CreateTableCqlGeneratorTests {
/**
* Fully test all available create table options
*
* TODO - Determine how to assert the options with map values
*
* @author David Webb
*
*/

View File

@@ -1,6 +1,6 @@
package org.springframework.cassandra.test.unit.core.cql.generator;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.cassandra.core.cql.generator.DropTableCqlGenerator;
@@ -11,8 +11,8 @@ public class DropTableCqlGeneratorTests {
/**
* Asserts that the preamble is first & correctly formatted in the given CQL string.
*/
public static void assertStatement(String tableName, String cql) {
assertTrue(cql.equals("DROP TABLE " + tableName + ";"));
public static void assertStatement(String tableName, boolean ifExists, String cql) {
assertTrue(cql.equals("DROP TABLE " + (ifExists ? "IF EXISTS " : "") + tableName + ";"));
}
/**
@@ -47,7 +47,27 @@ public class DropTableCqlGeneratorTests {
public void test() {
prepare();
assertStatement(name, cql);
assertStatement(name, false, cql);
}
}
// public static class IfExistsTest extends DropTableTest {
//
// public String name = "mytable";
//
// public DropTableSpecification specification() {
// return DropTableSpecification.dropTable().ifExists().name(name);
// }
//
// public DropTableCqlGenerator generator() {
// return new DropTableCqlGenerator(specification);
// }
//
// @Test
// public void test() {
// prepare();
//
// assertStatement(name, true, cql);
// }
// }
}