use of com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseTableMetadata in project java-driver by datastax.
the class DseTableParser method parseTable.
public DseTableMetadata parseTable(AdminRow tableRow, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userTypes, Multimap<CqlIdentifier, AdminRow> vertices, Multimap<CqlIdentifier, AdminRow> edges) {
// Cassandra <= 2.2:
// CREATE TABLE system.schema_columnfamilies (
// keyspace_name text,
// columnfamily_name text,
// bloom_filter_fp_chance double,
// caching text,
// cf_id uuid,
// column_aliases text, (2.1 only)
// comment text,
// compaction_strategy_class text,
// compaction_strategy_options text,
// comparator text,
// compression_parameters text,
// default_time_to_live int,
// default_validator text,
// dropped_columns map<text, bigint>,
// gc_grace_seconds int,
// index_interval int,
// is_dense boolean, (2.1 only)
// key_aliases text, (2.1 only)
// key_validator text,
// local_read_repair_chance double,
// max_compaction_threshold int,
// max_index_interval int,
// memtable_flush_period_in_ms int,
// min_compaction_threshold int,
// min_index_interval int,
// read_repair_chance double,
// speculative_retry text,
// subcomparator text,
// type text,
// value_alias text, (2.1 only)
// PRIMARY KEY (keyspace_name, columnfamily_name)
// ) WITH CLUSTERING ORDER BY (columnfamily_name ASC)
//
// Cassandra 3.0:
// CREATE TABLE system_schema.tables (
// keyspace_name text,
// 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>>,
// flags frozen<set<text>>,
// gc_grace_seconds int,
// id uuid,
// max_index_interval int,
// memtable_flush_period_in_ms int,
// min_index_interval int,
// read_repair_chance double,
// speculative_retry text,
// PRIMARY KEY (keyspace_name, table_name)
// ) WITH CLUSTERING ORDER BY (table_name ASC)
CqlIdentifier tableId = CqlIdentifier.fromInternal(tableRow.getString(tableRow.contains("table_name") ? "table_name" : "columnfamily_name"));
UUID uuid = tableRow.contains("id") ? tableRow.getUuid("id") : tableRow.getUuid("cf_id");
List<RawColumn> rawColumns = RawColumn.toRawColumns(rows.columns().getOrDefault(keyspaceId, ImmutableMultimap.of()).get(tableId));
if (rawColumns.isEmpty()) {
LOG.warn("[{}] Processing TABLE refresh for {}.{} but found no matching rows, skipping", logPrefix, keyspaceId, tableId);
return null;
}
boolean isCompactStorage;
if (tableRow.contains("flags")) {
Set<String> flags = tableRow.getSetOfString("flags");
boolean isDense = flags.contains("dense");
boolean isSuper = flags.contains("super");
boolean isCompound = flags.contains("compound");
isCompactStorage = isSuper || isDense || !isCompound;
boolean isStaticCompact = !isSuper && !isDense && !isCompound;
if (isStaticCompact) {
RawColumn.pruneStaticCompactTableColumns(rawColumns);
} else if (isDense) {
RawColumn.pruneDenseTableColumnsV3(rawColumns);
}
} else {
boolean isDense = tableRow.getBoolean("is_dense");
if (isDense) {
RawColumn.pruneDenseTableColumnsV2(rawColumns);
}
DataTypeClassNameCompositeParser.ParseResult comparator = new DataTypeClassNameCompositeParser().parseWithComposite(tableRow.getString("comparator"), keyspaceId, userTypes, context);
isCompactStorage = isDense || !comparator.isComposite;
}
Collections.sort(rawColumns);
ImmutableMap.Builder<CqlIdentifier, ColumnMetadata> allColumnsBuilder = ImmutableMap.builder();
ImmutableList.Builder<ColumnMetadata> partitionKeyBuilder = ImmutableList.builder();
ImmutableMap.Builder<ColumnMetadata, ClusteringOrder> clusteringColumnsBuilder = ImmutableMap.builder();
ImmutableMap.Builder<CqlIdentifier, IndexMetadata> indexesBuilder = ImmutableMap.builder();
for (RawColumn raw : rawColumns) {
DataType dataType = rows.dataTypeParser().parse(keyspaceId, raw.dataType, userTypes, context);
DseColumnMetadata column = new DefaultDseColumnMetadata(keyspaceId, tableId, 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);
DseIndexMetadata index = buildLegacyIndex(raw, column);
if (index != null) {
indexesBuilder.put(index.getName(), index);
}
}
Map<CqlIdentifier, Object> options;
try {
options = parseOptions(tableRow);
} 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, tableId, e);
options = Collections.emptyMap();
}
Collection<AdminRow> indexRows = rows.indexes().getOrDefault(keyspaceId, ImmutableMultimap.of()).get(tableId);
for (AdminRow indexRow : indexRows) {
DseIndexMetadata index = buildModernIndex(keyspaceId, tableId, indexRow);
indexesBuilder.put(index.getName(), index);
}
return new DefaultDseTableMetadata(keyspaceId, tableId, uuid, isCompactStorage, false, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), options, indexesBuilder.build(), buildVertex(tableId, vertices), buildEdge(tableId, edges, vertices));
}
use of com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseTableMetadata in project java-driver by datastax.
the class DseTableParser method parseVirtualTable.
DseTableMetadata parseVirtualTable(AdminRow tableRow, CqlIdentifier keyspaceId) {
CqlIdentifier tableId = CqlIdentifier.fromInternal(tableRow.getString("table_name"));
List<RawColumn> rawColumns = RawColumn.toRawColumns(rows.virtualColumns().getOrDefault(keyspaceId, ImmutableMultimap.of()).get(tableId));
if (rawColumns.isEmpty()) {
LOG.warn("[{}] Processing TABLE refresh for {}.{} but found no matching rows, skipping", logPrefix, keyspaceId, tableId);
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, Collections.emptyMap(), context);
DseColumnMetadata column = new DefaultDseColumnMetadata(keyspaceId, tableId, 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);
}
return new DefaultDseTableMetadata(keyspaceId, tableId, null, false, true, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), Collections.emptyMap(), Collections.emptyMap(), null, null);
}
use of com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseTableMetadata in project java-driver by datastax.
the class GraphSchemaRefreshTest method should_detect_adding_and_renaming_and_removing_vertex_label.
@Test
public void should_detect_adding_and_renaming_and_removing_vertex_label() {
DefaultDseTableMetadata newTable = newTable(KS_WITH_ENGINE.getName(), CqlIdentifier.fromInternal("tbl"), new DefaultDseVertexMetadata(CqlIdentifier.fromInternal("someLabel")), null);
DefaultDseKeyspaceMetadata ks = newKeyspace(KS_WITH_ENGINE.getName(), "Core", ImmutableMap.of(CqlIdentifier.fromInternal("tbl"), newTable));
SchemaRefresh refresh = new SchemaRefresh(ImmutableMap.of(KS_WITH_ENGINE.getName(), ks, OLD_KS1.getName(), OLD_KS1));
MetadataRefresh.Result result = refresh.compute(oldMetadata, false, context);
assertThat(result.newMetadata.getKeyspaces()).hasSize(2);
assertThat(result.events).containsExactly(TableChangeEvent.updated(OLD_TABLE, newTable));
assertThat(result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName())).isNotNull();
assertThat(((DseGraphTableMetadata) result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName()).getTable("tbl").get()).getVertex()).isNotNull();
assertThat(((DseGraphTableMetadata) result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName()).getTable("tbl").get()).getVertex().get().getLabelName().asInternal()).isEqualTo("someLabel");
// now rename the vertex label
newTable = newTable(KS_WITH_ENGINE.getName(), CqlIdentifier.fromInternal("tbl"), new DefaultDseVertexMetadata(CqlIdentifier.fromInternal("someNewLabel")), null);
ks = newKeyspace(KS_WITH_ENGINE.getName(), "Core", ImmutableMap.of(CqlIdentifier.fromInternal("tbl"), newTable));
refresh = new SchemaRefresh(ImmutableMap.of(KS_WITH_ENGINE.getName(), ks, OLD_KS1.getName(), OLD_KS1));
result = refresh.compute(oldMetadata, false, context);
assertThat(result.newMetadata.getKeyspaces()).hasSize(2);
assertThat(result.events).containsExactly(TableChangeEvent.updated(OLD_TABLE, newTable));
assertThat(((DseGraphTableMetadata) result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName()).getTable("tbl").get()).getVertex().get().getLabelName().asInternal()).isEqualTo("someNewLabel");
// now remove the vertex label from the table
DefaultMetadata metadataWithVertexLabel = result.newMetadata;
DefaultDseTableMetadata tableWithRemovedLabel = newTable(KS_WITH_ENGINE.getName(), CqlIdentifier.fromInternal("tbl"), null, null);
ks = newKeyspace(KS_WITH_ENGINE.getName(), "Core", ImmutableMap.of(CqlIdentifier.fromInternal("tbl"), tableWithRemovedLabel));
refresh = new SchemaRefresh(ImmutableMap.of(KS_WITH_ENGINE.getName(), ks, OLD_KS1.getName(), OLD_KS1));
result = refresh.compute(metadataWithVertexLabel, false, context);
assertThat(result.newMetadata.getKeyspaces()).hasSize(2);
assertThat(result.events).containsExactly(TableChangeEvent.updated(newTable, tableWithRemovedLabel));
assertThat(((DseGraphTableMetadata) result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName()).getTable("tbl").get()).getVertex().isPresent()).isFalse();
}
use of com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseTableMetadata in project java-driver by datastax.
the class GraphSchemaRefreshTest method should_detect_adding_and_renaming_and_removing_edge_label.
@Test
public void should_detect_adding_and_renaming_and_removing_edge_label() {
DefaultDseTableMetadata newTable = newTable(KS_WITH_ENGINE.getName(), CqlIdentifier.fromInternal("tbl"), null, newEdgeMetadata(CqlIdentifier.fromInternal("created"), CqlIdentifier.fromInternal("person"), CqlIdentifier.fromInternal("software")));
DefaultDseKeyspaceMetadata ks = newKeyspace(KS_WITH_ENGINE.getName(), "Core", ImmutableMap.of(CqlIdentifier.fromInternal("tbl"), newTable));
SchemaRefresh refresh = new SchemaRefresh(ImmutableMap.of(KS_WITH_ENGINE.getName(), ks, OLD_KS1.getName(), OLD_KS1));
MetadataRefresh.Result result = refresh.compute(oldMetadata, false, context);
assertThat(result.newMetadata.getKeyspaces()).hasSize(2);
assertThat(result.events).containsExactly(TableChangeEvent.updated(OLD_TABLE, newTable));
assertThat(result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName())).isNotNull();
assertThat(((DseGraphTableMetadata) result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName()).getTable("tbl").get()).getVertex()).isNotNull();
assertThat(((DseGraphTableMetadata) result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName()).getTable("tbl").get()).getEdge().get().getLabelName().asInternal()).isEqualTo("created");
// now rename the edge label
newTable = newTable(KS_WITH_ENGINE.getName(), CqlIdentifier.fromInternal("tbl"), null, newEdgeMetadata(CqlIdentifier.fromInternal("CHANGED"), CqlIdentifier.fromInternal("person"), CqlIdentifier.fromInternal("software")));
ks = newKeyspace(KS_WITH_ENGINE.getName(), "Core", ImmutableMap.of(CqlIdentifier.fromInternal("tbl"), newTable));
refresh = new SchemaRefresh(ImmutableMap.of(KS_WITH_ENGINE.getName(), ks, OLD_KS1.getName(), OLD_KS1));
result = refresh.compute(oldMetadata, false, context);
assertThat(result.newMetadata.getKeyspaces()).hasSize(2);
assertThat(result.events).containsExactly(TableChangeEvent.updated(OLD_TABLE, newTable));
assertThat(((DseGraphTableMetadata) result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName()).getTable("tbl").get()).getEdge().get().getLabelName().asInternal()).isEqualTo("CHANGED");
// now remove the edge label from the table
DefaultMetadata metadataWithEdgeLabel = result.newMetadata;
DefaultDseTableMetadata tableWithRemovedLabel = newTable(KS_WITH_ENGINE.getName(), CqlIdentifier.fromInternal("tbl"), null, null);
ks = newKeyspace(KS_WITH_ENGINE.getName(), "Core", ImmutableMap.of(CqlIdentifier.fromInternal("tbl"), tableWithRemovedLabel));
refresh = new SchemaRefresh(ImmutableMap.of(KS_WITH_ENGINE.getName(), ks, OLD_KS1.getName(), OLD_KS1));
result = refresh.compute(metadataWithEdgeLabel, false, context);
assertThat(result.newMetadata.getKeyspaces()).hasSize(2);
assertThat(result.events).containsExactly(TableChangeEvent.updated(newTable, tableWithRemovedLabel));
assertThat(((DseGraphTableMetadata) result.newMetadata.getKeyspaces().get(KS_WITH_ENGINE.getName()).getTable("tbl").get()).getEdge().isPresent()).isFalse();
}
Aggregations