Search in sources :

Example 1 with Direction

use of com.datastax.driver.core.schemabuilder.SchemaBuilder.Direction in project scalardb by scalar-labs.

the class CassandraAdmin method createTableInternal.

@VisibleForTesting
void createTableInternal(String keyspace, String table, TableMetadata metadata, Map<String, String> options) throws ExecutionException {
    Create createTable = SchemaBuilder.createTable(quoteIfNecessary(keyspace), quoteIfNecessary(table));
    // Add columns
    for (String pk : metadata.getPartitionKeyNames()) {
        createTable = createTable.addPartitionKey(quoteIfNecessary(pk), toCassandraDataType(metadata.getColumnDataType(pk)));
    }
    for (String ck : metadata.getClusteringKeyNames()) {
        createTable = createTable.addClusteringColumn(quoteIfNecessary(ck), toCassandraDataType(metadata.getColumnDataType(ck)));
    }
    for (String column : metadata.getColumnNames()) {
        if (metadata.getPartitionKeyNames().contains(column) || metadata.getClusteringKeyNames().contains(column)) {
            continue;
        }
        createTable = createTable.addColumn(quoteIfNecessary(column), toCassandraDataType(metadata.getColumnDataType(column)));
    }
    // Add clustering order
    Create.Options createTableWithOptions = createTable.withOptions();
    for (String ck : metadata.getClusteringKeyNames()) {
        Direction direction = metadata.getClusteringOrder(ck) == Order.ASC ? Direction.ASC : Direction.DESC;
        createTableWithOptions = createTableWithOptions.clusteringOrder(quoteIfNecessary(ck), direction);
    }
    // Add compaction strategy
    CompactionStrategy compactionStrategy = CompactionStrategy.valueOf(options.getOrDefault(COMPACTION_STRATEGY, CompactionStrategy.STCS.toString()));
    CompactionOptions<?> strategy;
    switch(compactionStrategy) {
        case LCS:
            strategy = SchemaBuilder.leveledStrategy();
            break;
        case TWCS:
            strategy = SchemaBuilder.timeWindowCompactionStrategy();
            break;
        default:
            strategy = SchemaBuilder.sizedTieredStategy();
    }
    createTableWithOptions = createTableWithOptions.compactionOptions(strategy);
    try {
        clusterManager.getSession().execute(createTableWithOptions.getQueryString());
    } catch (RuntimeException e) {
        throw new ExecutionException(String.format("creating the table %s.%s failed", keyspace, table), e);
    }
}
Also used : Create(com.datastax.driver.core.schemabuilder.Create) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Direction(com.datastax.driver.core.schemabuilder.SchemaBuilder.Direction) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Create (com.datastax.driver.core.schemabuilder.Create)1 Direction (com.datastax.driver.core.schemabuilder.SchemaBuilder.Direction)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ExecutionException (com.scalar.db.exception.storage.ExecutionException)1