DATACASS-355 - Polishing.
Extend JavaDoc. Add ticket reference to test method. Reorder methods. Add leading line to multi-statement methods.
This commit is contained in:
@@ -24,8 +24,6 @@ import org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaDr
|
||||
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.datastax.driver.core.Session;
|
||||
|
||||
/**
|
||||
* Factory to create and configure a Cassandra {@link com.datastax.driver.core.Session} with support for executing CQL
|
||||
* and initializing the database schema (a.k.a. keyspace).
|
||||
@@ -44,28 +42,72 @@ public class CassandraSessionFactoryBean extends CassandraCqlSessionFactoryBean
|
||||
protected static final boolean DEFAULT_DROP_UNUSED_TABLES = false;
|
||||
|
||||
private CassandraAdminOperations admin;
|
||||
|
||||
private CassandraConverter converter;
|
||||
|
||||
private SchemaAction schemaAction = SchemaAction.NONE;
|
||||
|
||||
/**
|
||||
* Set the {@link CassandraConverter} to use. Schema actions will derive table and user type information from the
|
||||
* {@link CassandraMappingContext} inside {@code converter}.
|
||||
*
|
||||
* @param converter must not be {@literal null}.
|
||||
*/
|
||||
public void setConverter(CassandraConverter converter) {
|
||||
|
||||
Assert.notNull(converter, "CassandraConverter must not be null");
|
||||
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link CassandraConverter}.
|
||||
*/
|
||||
public CassandraConverter getConverter() {
|
||||
return this.converter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link CassandraMappingContext}.
|
||||
*/
|
||||
protected CassandraMappingContext getMappingContext() {
|
||||
return getConverter().getMappingContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link SchemaAction}.
|
||||
*
|
||||
* @param schemaAction must not be {@literal null}.
|
||||
*/
|
||||
public void setSchemaAction(SchemaAction schemaAction) {
|
||||
|
||||
Assert.notNull(schemaAction, "SchemaAction must not be null");
|
||||
this.schemaAction = schemaAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link SchemaAction}.
|
||||
*/
|
||||
public SchemaAction getSchemaAction() {
|
||||
return schemaAction;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.cassandra.config.CassandraCqlSessionFactoryBean#afterPropertiesSet()
|
||||
*/
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
super.afterPropertiesSet();
|
||||
|
||||
Assert.state(converter != null, "Converter was not properly initialized");
|
||||
|
||||
admin = newCassandraAdminOperations(getObject(), converter);
|
||||
super.afterPropertiesSet();
|
||||
|
||||
admin = new CassandraAdminTemplate(getObject(), converter);
|
||||
|
||||
performSchemaAction();
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
CassandraAdminOperations newCassandraAdminOperations(Session session, CassandraConverter converter) {
|
||||
return new CassandraAdminTemplate(session, converter);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
/**
|
||||
* Perform the configure {@link SchemaAction} using {@link CassandraMappingContext} metadata.
|
||||
*/
|
||||
protected void performSchemaAction() {
|
||||
|
||||
boolean create = false;
|
||||
@@ -92,7 +134,20 @@ public class CassandraSessionFactoryBean extends CassandraCqlSessionFactoryBean
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform schema actions.
|
||||
*
|
||||
* @param drop {@literal true} to drop types/tables.
|
||||
* @param dropUnused {@literal true} to drop unused types/tables (i.e. types/tables not know to be used by
|
||||
* {@link CassandraMappingContext}).
|
||||
* @param ifNotExists {@literal true} to perform creations fail-safe by adding {@code IF NOT EXISTS} to each creation
|
||||
* statement.
|
||||
*/
|
||||
protected void createTables(boolean drop, boolean dropUnused, boolean ifNotExists) {
|
||||
performSchemaActions(drop, dropUnused, ifNotExists);
|
||||
}
|
||||
|
||||
private void performSchemaActions(boolean drop, boolean dropUnused, boolean ifNotExists) {
|
||||
|
||||
CassandraPersistentEntitySchemaCreator schemaCreator = new CassandraPersistentEntitySchemaCreator(
|
||||
getMappingContext(), getCassandraAdminOperations());
|
||||
@@ -110,35 +165,10 @@ public class CassandraSessionFactoryBean extends CassandraCqlSessionFactoryBean
|
||||
schemaCreator.createTables(ifNotExists);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
/**
|
||||
* @return the {@link CassandraAdminOperations}.
|
||||
*/
|
||||
protected CassandraAdminOperations getCassandraAdminOperations() {
|
||||
return this.admin;
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public void setConverter(CassandraConverter converter) {
|
||||
Assert.notNull(converter, "CassandraConverter must not be null");
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public CassandraConverter getConverter() {
|
||||
return this.converter;
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
protected CassandraMappingContext getMappingContext() {
|
||||
return getConverter().getMappingContext();
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public void setSchemaAction(SchemaAction schemaAction) {
|
||||
Assert.notNull(schemaAction, "SchemaAction must not be null");
|
||||
this.schemaAction = schemaAction;
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public SchemaAction getSchemaAction() {
|
||||
return schemaAction;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,12 @@ public class CassandraPersistentEntitySchemaCreator {
|
||||
.execute(CreateTableCqlGenerator.toCql(specification)));
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
/**
|
||||
* Create {@link List} of {@link CreateTableSpecification}.
|
||||
*
|
||||
* @param ifNotExists {@literal true} to create tables using {@code IF NOT EXISTS}.
|
||||
* @return {@link List} of {@link CreateTableSpecification}.
|
||||
*/
|
||||
protected List<CreateTableSpecification> createTableSpecifications(boolean ifNotExists) {
|
||||
|
||||
return mappingContext.getTableEntities().stream()
|
||||
@@ -99,7 +104,12 @@ public class CassandraPersistentEntitySchemaCreator {
|
||||
.execute(CreateUserTypeCqlGenerator.toCql(specification)));
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
/**
|
||||
* Create {@link List} of {@link CreateUserTypeSpecification}.
|
||||
*
|
||||
* @param ifNotExists {@literal true} to create types using {@code IF NOT EXISTS}.
|
||||
* @return {@link List} of {@link CreateUserTypeSpecification}.
|
||||
*/
|
||||
protected List<CreateUserTypeSpecification> createUserTypeSpecifications(boolean ifNotExists) {
|
||||
|
||||
Collection<? extends CassandraPersistentEntity<?>> entities = new ArrayList<>(
|
||||
@@ -132,7 +142,6 @@ public class CassandraPersistentEntitySchemaCreator {
|
||||
return specifications;
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
private void visitUserTypes(CassandraPersistentEntity<?> entity, final Set<CqlIdentifier> seen) {
|
||||
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
|
||||
@@ -66,8 +66,11 @@ public class CassandraPersistentEntitySchemaCreatorUnitTests {
|
||||
when(adminOperations.getCqlOperations()).thenReturn(operations);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACASS-172
|
||||
*/
|
||||
@Test
|
||||
public void shouldCreateTypesInOrder() throws Exception {
|
||||
public void shouldCreateTypesInOrder() {
|
||||
|
||||
context.getPersistentEntity(MoonType.class);
|
||||
context.getPersistentEntity(PlanetType.class);
|
||||
@@ -92,27 +95,18 @@ public class CassandraPersistentEntitySchemaCreatorUnitTests {
|
||||
inOrder.verify(operations).execute(Mockito.contains("CREATE TYPE planettype"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@UserDefinedType
|
||||
@Data
|
||||
static class UniverseType {
|
||||
String name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@UserDefinedType
|
||||
static class MoonType {
|
||||
|
||||
UniverseType universeType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@UserDefinedType
|
||||
static class PlanetType {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user