primary key contains { partitioned keys, clustered keys } refactoring

This commit is contained in:
Alex Shvid
2013-12-03 11:15:57 -08:00
parent d2313980c1
commit 5c11d4aac2
6 changed files with 44 additions and 26 deletions

View File

@@ -26,7 +26,7 @@ public enum PrimaryKeyType {
/**
* Used for a column that is part of the partition key.
*/
PARTITION,
PARTITIONED,
/**
* Used for a column that is clustered key.

View File

@@ -16,7 +16,7 @@
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
import static org.springframework.cassandra.core.PrimaryKeyType.PARTITION;
import static org.springframework.cassandra.core.PrimaryKeyType.PARTITIONED;
import static org.springframework.cassandra.core.PrimaryKeyType.CLUSTERED;
import java.util.ArrayList;
@@ -69,7 +69,7 @@ public class CreateTableCqlGenerator extends TableCqlGenerator<CreateTableSpecif
for (ColumnSpecification col : spec().getColumns()) {
col.toCql(cql).append(", ");
if (col.getKeyType() == PARTITION) {
if (col.getKeyType() == PARTITIONED) {
partitionKeys.add(col);
} else if (col.getKeyType() == CLUSTERED) {
clusteredKeys.add(col);

View File

@@ -18,7 +18,7 @@ package org.springframework.cassandra.core.keyspace;
import static org.springframework.cassandra.core.cql.CqlStringUtils.checkIdentifier;
import static org.springframework.cassandra.core.cql.CqlStringUtils.identifize;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
import static org.springframework.cassandra.core.PrimaryKeyType.PARTITION;
import static org.springframework.cassandra.core.PrimaryKeyType.PARTITIONED;
import static org.springframework.cassandra.core.PrimaryKeyType.CLUSTERED;
import static org.springframework.cassandra.core.Ordering.ASCENDING;
@@ -36,6 +36,7 @@ import com.datastax.driver.core.DataType;
* {@link #primary()} or {@link #primary(Ordering)}.
*
* @author Matthew T. Adams
* @author Alex Shvid
*/
public class ColumnSpecification {
@@ -72,7 +73,7 @@ public class ColumnSpecification {
/**
* Identifies this column as a primary key column that is also part of a partition key. Sets the column's
* {@link #keyType} to {@link PrimaryKeyType#PARTITION} and its {@link #ordering} to <code>null</code>.
* {@link #keyType} to {@link PrimaryKeyType#PARTITIONED} and its {@link #ordering} to <code>null</code>.
*
* @return this
*/
@@ -83,12 +84,12 @@ public class ColumnSpecification {
/**
* Toggles the identification of this column as a primary key column that also is or is part of a partition key. Sets
* {@link #ordering} to <code>null</code> and, if the given boolean is <code>true</code>, then sets the column's
* {@link #keyType} to {@link PrimaryKeyType#PARTITION}, else sets it to <code>null</code>.
* {@link #keyType} to {@link PrimaryKeyType#PARTITIONED}, else sets it to <code>null</code>.
*
* @return this
*/
public ColumnSpecification partition(boolean partition) {
this.keyType = partition ? PARTITION : null;
this.keyType = partition ? PARTITIONED : null;
this.ordering = null;
return this;
}
@@ -115,7 +116,7 @@ public class ColumnSpecification {
/**
* Toggles the identification of this column as a primary key column. If the given boolean is <code>true</code>, then
* sets the column's {@link #keyType} to {@link PrimaryKeyType#PARTITION} and {@link #ordering} to the given
* sets the column's {@link #keyType} to {@link PrimaryKeyType#PARTITIONED} and {@link #ordering} to the given
* {@link Ordering} , else sets both {@link #keyType} and {@link #ordering} to <code>null</code>.
*
* @return this

View File

@@ -22,6 +22,7 @@ import java.util.Map;
* Describes a table.
*
* @author Matthew T. Adams
* @author Alex Shvid
*/
public interface TableDescriptor {
@@ -48,12 +49,12 @@ public interface TableDescriptor {
/**
* Returns an unmodifiable list of all primary key columns that are not also partition key columns.
*/
public List<ColumnSpecification> getPrimaryKeyColumns();
public List<ColumnSpecification> getClusteredKeyColumns();
/**
* Returns an unmodifiable list of all partition and primary key columns.
*/
public List<ColumnSpecification> getKeyColumns();
public List<ColumnSpecification> getPrimaryKeyColumns();
/**
* Returns an unmodifiable list of all non-key columns.

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.cassandra.core.keyspace;
import static org.springframework.cassandra.core.PrimaryKeyType.PARTITION;
import static org.springframework.cassandra.core.PrimaryKeyType.PARTITIONED;
import static org.springframework.cassandra.core.PrimaryKeyType.CLUSTERED;
import static org.springframework.cassandra.core.Ordering.ASCENDING;
@@ -33,6 +33,7 @@ import com.datastax.driver.core.DataType;
* standalone {@link TableDescriptor}, independent of {@link CreateTableSpecification}.
*
* @author Matthew T. Adams
* @author Alex Shvid
*/
public class TableSpecification<T> extends TableOptionsSpecification<TableSpecification<T>> implements TableDescriptor {
@@ -49,7 +50,7 @@ public class TableSpecification<T> extends TableOptionsSpecification<TableSpecif
/**
* List of only those columns that comprise the primary key that are not also part of the partition key.
*/
private List<ColumnSpecification> primaryKeyColumns = new ArrayList<ColumnSpecification>();
private List<ColumnSpecification> clusteredKeyColumns = new ArrayList<ColumnSpecification>();
/**
* List of only those columns that are not partition or primary key columns.
@@ -74,7 +75,7 @@ public class TableSpecification<T> extends TableOptionsSpecification<TableSpecif
* @return this
*/
public T partitionKeyColumn(String name, DataType type) {
return column(name, type, PARTITION, null);
return column(name, type, PARTITIONED, null);
}
/**
@@ -85,8 +86,8 @@ public class TableSpecification<T> extends TableOptionsSpecification<TableSpecif
* @param type The data type of the column.
* @return this
*/
public T primaryKeyColumn(String name, DataType type) {
return primaryKeyColumn(name, type, ASCENDING);
public T clusteredKeyColumn(String name, DataType type) {
return clusteredKeyColumn(name, type, ASCENDING);
}
/**
@@ -97,7 +98,7 @@ public class TableSpecification<T> extends TableOptionsSpecification<TableSpecif
* @param type The data type of the column.
* @return this
*/
public T primaryKeyColumn(String name, DataType type, Ordering ordering) {
public T clusteredKeyColumn(String name, DataType type, Ordering ordering) {
return column(name, type, CLUSTERED, ordering);
}
@@ -120,12 +121,12 @@ public class TableSpecification<T> extends TableOptionsSpecification<TableSpecif
columns.add(column);
if (keyType == PrimaryKeyType.PARTITION) {
if (keyType == PrimaryKeyType.PARTITIONED) {
partitionKeyColumns.add(column);
}
if (keyType == PrimaryKeyType.CLUSTERED) {
primaryKeyColumns.add(column);
clusteredKeyColumns.add(column);
}
if (keyType == null) {
@@ -152,20 +153,20 @@ public class TableSpecification<T> extends TableOptionsSpecification<TableSpecif
/**
* Returns an unmodifiable list of all primary key columns that are not also partition key columns.
*/
public List<ColumnSpecification> getPrimaryKeyColumns() {
return Collections.unmodifiableList(primaryKeyColumns);
public List<ColumnSpecification> getClusteredKeyColumns() {
return Collections.unmodifiableList(clusteredKeyColumns);
}
/**
* Returns an unmodifiable list of all primary key columns that are not also partition key columns.
*/
public List<ColumnSpecification> getKeyColumns() {
public List<ColumnSpecification> getPrimaryKeyColumns() {
ArrayList<ColumnSpecification> keyColumns = new ArrayList<ColumnSpecification>();
keyColumns.addAll(partitionKeyColumns);
keyColumns.addAll(primaryKeyColumns);
ArrayList<ColumnSpecification> primaryKeyColumns = new ArrayList<ColumnSpecification>();
primaryKeyColumns.addAll(partitionKeyColumns);
primaryKeyColumns.addAll(clusteredKeyColumns);
return Collections.unmodifiableList(keyColumns);
return Collections.unmodifiableList(primaryKeyColumns);
}
/**

View File

@@ -1,3 +1,18 @@
/*
* 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 junit.framework.Assert.assertEquals;
@@ -35,7 +50,7 @@ public class CqlTableSpecificationAssertions {
}
public static void assertPrimaryKeyColumns(TableDescriptor expected, TableMetadata actual) {
assertColumns(expected.getKeyColumns(), actual.getPrimaryKey());
assertColumns(expected.getPrimaryKeyColumns(), actual.getPrimaryKey());
}
public static void assertOptions(Map<String, Object> expected, Options actual) {