org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaCreator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-cassandra Show documentation
Show all versions of spring-data-cassandra Show documentation
Cassandra support for Spring Data
/*
* Copyright 2016-2018 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.data.cassandra.core;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.data.cassandra.core.cql.CqlIdentifier;
import org.springframework.data.cassandra.core.cql.generator.CreateIndexCqlGenerator;
import org.springframework.data.cassandra.core.cql.generator.CreateTableCqlGenerator;
import org.springframework.data.cassandra.core.cql.generator.CreateUserTypeCqlGenerator;
import org.springframework.data.cassandra.core.cql.keyspace.CreateIndexSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.CreateTableSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification;
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty;
import org.springframework.util.Assert;
/**
* Schema creation support for Cassandra based on {@link CassandraMappingContext} and {@link CassandraPersistentEntity}.
* This class generates CQL to create user types (UDT) and tables.
*
* @author Mark Paluch
* @author Jens Schauder
* @since 1.5
* @see org.springframework.data.cassandra.core.mapping.Table
* @see org.springframework.data.cassandra.core.mapping.UserDefinedType
* @see org.springframework.data.cassandra.core.mapping.CassandraType
*/
public class CassandraPersistentEntitySchemaCreator {
private final CassandraAdminOperations cassandraAdminOperations;
private final CassandraMappingContext mappingContext;
/**
* Create a new {@link CassandraPersistentEntitySchemaCreator} for the given {@link CassandraMappingContext} and
* {@link CassandraAdminOperations}.
*
* @param mappingContext must not be {@literal null}.
* @param cassandraAdminOperations must not be {@literal null}.
*/
public CassandraPersistentEntitySchemaCreator(CassandraMappingContext mappingContext,
CassandraAdminOperations cassandraAdminOperations) {
Assert.notNull(cassandraAdminOperations, "CassandraAdminOperations must not be null");
Assert.notNull(mappingContext, "CassandraMappingContext must not be null");
this.cassandraAdminOperations = cassandraAdminOperations;
this.mappingContext = mappingContext;
}
/**
* Create tables from types known to {@link CassandraMappingContext}.
*
* @param ifNotExists {@literal true} to create tables using {@code IF NOT EXISTS}.
*/
public void createTables(boolean ifNotExists) {
createTableSpecifications(ifNotExists).stream() //
.map(CreateTableCqlGenerator::toCql) //
.forEach(cql -> this.cassandraAdminOperations.getCqlOperations().execute(cql));
}
/**
* 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 createTableSpecifications(boolean ifNotExists) {
return this.mappingContext.getTableEntities() //
.stream() //
.map(entity -> this.mappingContext.getCreateTableSpecificationFor(entity).ifNotExists(ifNotExists)) //
.collect(Collectors.toList());
}
/**
* Create indexes from types known to {@link CassandraMappingContext}.
*
* @param ifNotExists {@literal true} to create tables using {@code IF NOT EXISTS}.
*/
public void createIndexes(boolean ifNotExists) {
createIndexSpecifications(ifNotExists).stream() //
.map(CreateIndexCqlGenerator::toCql) //
.forEach(cql -> this.cassandraAdminOperations.getCqlOperations().execute(cql));
}
/**
* Create {@link List} of {@link CreateIndexSpecification}.
*
* @param ifNotExists {@literal true} to create indexes using {@code IF NOT EXISTS}.
* @return {@link List} of {@link CreateIndexSpecification}.
*/
protected List createIndexSpecifications(boolean ifNotExists) {
return this.mappingContext.getTableEntities() //
.stream() //
.flatMap(entity -> this.mappingContext.getCreateIndexSpecificationsFor(entity).stream()) //
.peek(it -> it.ifNotExists(ifNotExists)) //
.collect(Collectors.toList());
}
/**
* Create user types from types known to {@link CassandraMappingContext}.
*
* @param ifNotExists {@literal true} to create types using {@code IF NOT EXISTS}.
*/
public void createUserTypes(boolean ifNotExists) {
createUserTypeSpecifications(ifNotExists).stream() //
.map(CreateUserTypeCqlGenerator::toCql) //
.forEach(cql -> this.cassandraAdminOperations.getCqlOperations().execute(cql));
}
/**
* 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 createUserTypeSpecifications(boolean ifNotExists) {
Collection> entities =
new ArrayList<>(this.mappingContext.getUserDefinedTypeEntities());
Map> byTableName = entities.stream()
.collect(Collectors.toMap(CassandraPersistentEntity::getTableName, entity -> entity));
List specifications = new ArrayList<>();
Set created = new HashSet<>();
entities.forEach(entity -> {
Set seen = new LinkedHashSet<>();
seen.add(entity.getTableName());
visitUserTypes(entity, seen);
List ordered = new ArrayList<>(seen);
Collections.reverse(ordered);
specifications.addAll(ordered.stream().filter(created::add).map(identifier ->
this.mappingContext.getCreateUserTypeSpecificationFor(byTableName.get(identifier)).ifNotExists(ifNotExists))
.collect(Collectors.toList()));
});
return specifications;
}
private void visitUserTypes(CassandraPersistentEntity entity, final Set seen) {
for (CassandraPersistentProperty property : entity) {
BasicCassandraPersistentEntity persistentEntity = this.mappingContext.getPersistentEntity(property);
if (persistentEntity == null) {
continue;
}
if (persistentEntity.isUserDefinedType() && seen.add(persistentEntity.getTableName())) {
visitUserTypes(persistentEntity, seen);
}
}
}
}