Search in sources :

Example 1 with DefaultViewMetadata

use of com.datastax.oss.driver.internal.core.metadata.schema.DefaultViewMetadata in project java-driver by datastax.

the class ViewParser method parseView.

public ViewMetadata parseView(AdminRow viewRow, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userTypes) {
    // Cassandra 3.0 (no views in earlier versions):
    // CREATE TABLE system_schema.views (
    // keyspace_name text,
    // view_name text,
    // base_table_id uuid,
    // base_table_name text,
    // bloom_filter_fp_chance double,
    // caching frozen<map<text, text>>,
    // cdc boolean,
    // comment text,
    // compaction frozen<map<text, text>>,
    // compression frozen<map<text, text>>,
    // crc_check_chance double,
    // dclocal_read_repair_chance double,
    // default_time_to_live int,
    // extensions frozen<map<text, blob>>,
    // gc_grace_seconds int,
    // id uuid,
    // include_all_columns boolean,
    // max_index_interval int,
    // memtable_flush_period_in_ms int,
    // min_index_interval int,
    // read_repair_chance double,
    // speculative_retry text,
    // where_clause text,
    // PRIMARY KEY (keyspace_name, view_name)
    // ) WITH CLUSTERING ORDER BY (view_name ASC)
    CqlIdentifier viewId = CqlIdentifier.fromInternal(viewRow.getString("view_name"));
    UUID uuid = viewRow.getUuid("id");
    CqlIdentifier baseTableId = CqlIdentifier.fromInternal(viewRow.getString("base_table_name"));
    boolean includesAllColumns = MoreObjects.firstNonNull(viewRow.getBoolean("include_all_columns"), false);
    String whereClause = viewRow.getString("where_clause");
    List<RawColumn> rawColumns = RawColumn.toRawColumns(rows.columns().getOrDefault(keyspaceId, ImmutableMultimap.of()).get(viewId));
    if (rawColumns.isEmpty()) {
        LOG.warn("[{}] Processing VIEW refresh for {}.{} but found no matching rows, skipping", logPrefix, keyspaceId, viewId);
        return null;
    }
    Collections.sort(rawColumns);
    ImmutableMap.Builder<CqlIdentifier, ColumnMetadata> allColumnsBuilder = ImmutableMap.builder();
    ImmutableList.Builder<ColumnMetadata> partitionKeyBuilder = ImmutableList.builder();
    ImmutableMap.Builder<ColumnMetadata, ClusteringOrder> clusteringColumnsBuilder = ImmutableMap.builder();
    for (RawColumn raw : rawColumns) {
        DataType dataType = rows.dataTypeParser().parse(keyspaceId, raw.dataType, userTypes, context);
        ColumnMetadata column = new DefaultColumnMetadata(keyspaceId, viewId, raw.name, dataType, raw.kind.equals(RawColumn.KIND_STATIC));
        switch(raw.kind) {
            case RawColumn.KIND_PARTITION_KEY:
                partitionKeyBuilder.add(column);
                break;
            case RawColumn.KIND_CLUSTERING_COLUMN:
                clusteringColumnsBuilder.put(column, raw.reversed ? ClusteringOrder.DESC : ClusteringOrder.ASC);
                break;
            default:
        }
        allColumnsBuilder.put(column.getName(), column);
    }
    Map<CqlIdentifier, Object> options;
    try {
        options = parseOptions(viewRow);
    } catch (Exception e) {
        // Options change the most often, so be especially lenient if anything goes wrong.
        Loggers.warnWithException(LOG, "[{}] Error while parsing options for {}.{}, getOptions() will be empty", logPrefix, keyspaceId, viewId, e);
        options = Collections.emptyMap();
    }
    return new DefaultViewMetadata(keyspaceId, viewId, baseTableId, includesAllColumns, whereClause, uuid, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), options);
}
Also used : DefaultColumnMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultColumnMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) DefaultColumnMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultColumnMetadata) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) DefaultViewMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultViewMetadata) DataType(com.datastax.oss.driver.api.core.type.DataType) UUID(java.util.UUID) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)

Aggregations

CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)1 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 DefaultColumnMetadata (com.datastax.oss.driver.internal.core.metadata.schema.DefaultColumnMetadata)1 DefaultViewMetadata (com.datastax.oss.driver.internal.core.metadata.schema.DefaultViewMetadata)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1 UUID (java.util.UUID)1