use of com.datastax.oss.driver.api.core.type.DataType in project jnosql-diana-driver by eclipse.
the class CassandraConverter method getUDT.
private static UDT getUDT(ColumnDefinition definition, UdtValue udtValue) {
String name = definition.getName().asInternal();
final UserDefinedType type = udtValue.getType();
List<Column> columns = new ArrayList<>();
List<String> names = type.getFieldNames().stream().map(CqlIdentifier::asInternal).collect(toList());
for (CqlIdentifier fieldName : type.getFieldNames()) {
final int index = names.indexOf(fieldName.asInternal());
DataType fieldType = type.getFieldTypes().get(index);
Object elementValue = udtValue.get(fieldName, CodecRegistry.DEFAULT.codecFor(fieldType));
if (elementValue != null) {
columns.add(Column.of(fieldName.asInternal(), elementValue));
}
}
return UDT.builder(type.getName().asInternal()).withName(name).addUDT(columns).build();
}
use of com.datastax.oss.driver.api.core.type.DataType in project jnosql-diana-driver by eclipse.
the class QueryUtils method insertUDT.
private static void insertUDT(UDT udt, String keyspace, String columnFamily, CqlSession session, Map<String, Term> values) {
final Optional<KeyspaceMetadata> keyspaceMetadata = session.getMetadata().getKeyspace(keyspace);
UserDefinedType userType = keyspaceMetadata.flatMap(ks -> ks.getUserDefinedType(udt.getUserType())).orElseThrow(() -> new IllegalArgumentException("Missing UDT definition"));
final TableMetadata tableMetadata = keyspaceMetadata.flatMap(k -> k.getTable(columnFamily)).orElseThrow(() -> new IllegalArgumentException("Missing Table definition"));
final ColumnMetadata columnMetadata = tableMetadata.getColumn(getName(udt)).orElseThrow(() -> new IllegalArgumentException("Missing the column definition"));
final DataType type = columnMetadata.getType();
Iterable elements = Iterable.class.cast(udt.get());
Object udtValue = getUdtValue(userType, elements, type);
values.put(getName(udt), QueryBuilder.literal(udtValue));
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class DataTypeCqlNameParser method parse.
private DataType parse(String toParse, CqlIdentifier keyspaceId, boolean frozen, Map<CqlIdentifier, UserDefinedType> userTypes, InternalDriverContext context) {
if (toParse.startsWith("'")) {
return DataTypes.custom(toParse.substring(1, toParse.length() - 1));
}
Parser parser = new Parser(toParse, 0);
String type = parser.parseTypeName();
if (type.equalsIgnoreCase(RawColumn.THRIFT_EMPTY_TYPE)) {
return DataTypes.custom(type);
}
DataType nativeType = NATIVE_TYPES_BY_NAME.get(type.toLowerCase(Locale.ROOT));
if (nativeType != null) {
return nativeType;
}
if (parser.isEOS()) {
// No parameters => it's a UDT
CqlIdentifier name = CqlIdentifier.fromCql(type);
if (userTypes != null) {
UserDefinedType userType = userTypes.get(name);
if (userType == null) {
throw new IllegalStateException(String.format("Can't find referenced user type %s", type));
}
return userType.copy(frozen);
} else {
return new ShallowUserDefinedType(keyspaceId, name, frozen);
}
}
List<String> parameters = parser.parseTypeParameters();
if (type.equalsIgnoreCase("list")) {
if (parameters.size() != 1) {
throw new IllegalArgumentException(String.format("Expecting single parameter for list, got %s", parameters));
}
DataType elementType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
return DataTypes.listOf(elementType, frozen);
}
if (type.equalsIgnoreCase("set")) {
if (parameters.size() != 1) {
throw new IllegalArgumentException(String.format("Expecting single parameter for set, got %s", parameters));
}
DataType elementType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
return DataTypes.setOf(elementType, frozen);
}
if (type.equalsIgnoreCase("map")) {
if (parameters.size() != 2) {
throw new IllegalArgumentException(String.format("Expecting two parameters for map, got %s", parameters));
}
DataType keyType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
DataType valueType = parse(parameters.get(1), keyspaceId, false, userTypes, context);
return DataTypes.mapOf(keyType, valueType, frozen);
}
if (type.equalsIgnoreCase("frozen")) {
if (parameters.size() != 1) {
throw new IllegalArgumentException(String.format("Expecting single parameter for frozen keyword, got %s", parameters));
}
return parse(parameters.get(0), keyspaceId, true, userTypes, context);
}
if (type.equalsIgnoreCase("tuple")) {
if (parameters.isEmpty()) {
throw new IllegalArgumentException("Expecting at list one parameter for tuple, got none");
}
ImmutableList.Builder<DataType> componentTypesBuilder = ImmutableList.builder();
for (String rawType : parameters) {
componentTypesBuilder.add(parse(rawType, keyspaceId, false, userTypes, context));
}
return new DefaultTupleType(componentTypesBuilder.build(), context);
}
throw new IllegalArgumentException("Could not parse type name " + toParse);
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class TableParser method parseTable.
public TableMetadata parseTable(AdminRow tableRow, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userTypes) {
// 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);
ColumnMetadata column = new DefaultColumnMetadata(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);
IndexMetadata 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) {
IndexMetadata index = buildModernIndex(keyspaceId, tableId, indexRow);
indexesBuilder.put(index.getName(), index);
}
return new DefaultTableMetadata(keyspaceId, tableId, uuid, isCompactStorage, false, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), options, indexesBuilder.build());
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class TableParser method buildLegacyIndexTarget.
private static String buildLegacyIndexTarget(ColumnMetadata column, Map<String, String> options) {
String columnName = column.getName().asCql(true);
DataType columnType = column.getType();
if (options.containsKey("index_keys")) {
return String.format("keys(%s)", columnName);
}
if (options.containsKey("index_keys_and_values")) {
return String.format("entries(%s)", columnName);
}
if ((columnType instanceof ListType && ((ListType) columnType).isFrozen()) || (columnType instanceof SetType && ((SetType) columnType).isFrozen()) || (columnType instanceof MapType && ((MapType) columnType).isFrozen())) {
return String.format("full(%s)", columnName);
}
// Note: the keyword 'values' is not accepted as a valid index target function until 3.0
return columnName;
}
Aggregations