use of com.datastax.oss.driver.shaded.guava.common.base.Function in project sdc by onap.
the class SdcSchemaBuilder method createTables.
/**
* the method creats all the tables and indexes thet do not already exist
*
* @param iTableDescriptions: a list of table description we want to create
* @param keyspaceMetadata: the current tables that exist in the cassandra under this keyspace
* @param session: the session object used for the execution of the query.
* @param existingTablesMetadata the current tables columns that exist in the cassandra under this keyspace
*/
private static void createTables(List<ITableDescription> iTableDescriptions, Map<String, List<String>> keyspaceMetadata, Session session, Map<String, List<String>> existingTablesMetadata) {
for (ITableDescription tableDescription : iTableDescriptions) {
String tableName = tableDescription.getTableName().toLowerCase();
Map<String, ImmutablePair<DataType, Boolean>> columnDescription = tableDescription.getColumnDescription();
log.info("creating tables:{}.", tableName);
if (keyspaceMetadata == null || !keyspaceMetadata.containsKey(tableName)) {
Create create = SchemaBuilder.createTable(tableDescription.getKeyspace(), tableDescription.getTableName());
for (ImmutablePair<String, DataType> key : tableDescription.primaryKeys()) {
create.addPartitionKey(key.getLeft(), key.getRight());
}
if (tableDescription.clusteringKeys() != null) {
for (ImmutablePair<String, DataType> key : tableDescription.clusteringKeys()) {
create.addClusteringColumn(key.getLeft(), key.getRight());
}
}
final Function<Entry<String, ?>, Boolean> notPrimaryKeyFilter = (Entry<String, ?> entry) -> {
if (entry == null) {
return true;
}
return tableDescription.primaryKeys().stream().noneMatch(primaryKeyPair -> primaryKeyPair.getLeft().equals(entry.getKey()));
};
columnDescription.entrySet().stream().filter(notPrimaryKeyFilter::apply).forEach(entry -> create.addColumn(entry.getKey(), entry.getValue().getLeft()));
log.trace("exacuting :{}", create);
session.execute(create);
log.info("table:{} created successfully.", tableName);
} else {
log.info("table:{} already exists, skipping.", tableName);
alterTable(session, existingTablesMetadata, tableDescription, tableName, columnDescription);
}
log.info("keyspacemetadata:{}", keyspaceMetadata);
List<String> indexNames = (keyspaceMetadata != null && keyspaceMetadata.get(tableName) != null ? keyspaceMetadata.get(tableName) : new ArrayList<>());
log.info("table:{} creating indexes.", tableName);
for (Map.Entry<String, ImmutablePair<DataType, Boolean>> description : columnDescription.entrySet()) {
String indexName = createIndexName(tableName, description.getKey()).toLowerCase();
if (description.getValue().getRight()) {
if (!indexNames.contains(indexName)) {
SchemaStatement creatIndex = SchemaBuilder.createIndex(indexName).onTable(tableDescription.getKeyspace(), tableName).andColumn(description.getKey());
log.info("executing :{}", creatIndex);
session.execute(creatIndex);
log.info("index:{} created successfully.", indexName);
} else {
log.info("index:{} already exists, skipping.", indexName);
}
}
}
}
}
Aggregations