use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class DseSchemaParser method parseKeyspace.
private DseKeyspaceMetadata parseKeyspace(AdminRow keyspaceRow) {
// Cassandra <= 2.2
// CREATE TABLE system.schema_keyspaces (
// keyspace_name text PRIMARY KEY,
// durable_writes boolean,
// strategy_class text,
// strategy_options text
// )
//
// Cassandra >= 3.0:
// CREATE TABLE system_schema.keyspaces (
// keyspace_name text PRIMARY KEY,
// durable_writes boolean,
// replication frozen<map<text, text>>
// )
//
// DSE >= 6.8: same as Cassandra 3 + graph_engine text
CqlIdentifier keyspaceId = CqlIdentifier.fromInternal(keyspaceRow.getString("keyspace_name"));
boolean durableWrites = MoreObjects.firstNonNull(keyspaceRow.getBoolean("durable_writes"), false);
String graphEngine = keyspaceRow.getString("graph_engine");
Map<String, String> replicationOptions;
if (keyspaceRow.contains("strategy_class")) {
String strategyClass = keyspaceRow.getString("strategy_class");
Map<String, String> strategyOptions = SimpleJsonParser.parseStringMap(keyspaceRow.getString("strategy_options"));
replicationOptions = ImmutableMap.<String, String>builder().putAll(strategyOptions).put("class", strategyClass).build();
} else {
replicationOptions = keyspaceRow.getMapOfStringToString("replication");
}
Map<CqlIdentifier, UserDefinedType> types = parseTypes(keyspaceId);
return new DefaultDseKeyspaceMetadata(keyspaceId, durableWrites, false, graphEngine, replicationOptions, types, parseTables(keyspaceId, types), parseViews(keyspaceId, types), parseFunctions(keyspaceId, types), parseAggregates(keyspaceId, types));
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class ComplexTypeSerializerUtil method toProtocolSpec.
private static RawType toProtocolSpec(DataType dataType) {
int id = dataType.getProtocolCode();
RawType type = RawType.PRIMITIVES.get(id);
if (type != null) {
return type;
}
switch(id) {
case ProtocolConstants.DataType.CUSTOM:
CustomType customType = ((CustomType) dataType);
type = new RawType.RawCustom(customType.getClassName());
break;
case ProtocolConstants.DataType.LIST:
ListType listType = ((ListType) dataType);
type = new RawType.RawList(toProtocolSpec(listType.getElementType()));
break;
case ProtocolConstants.DataType.SET:
SetType setType = ((SetType) dataType);
type = new RawType.RawSet(toProtocolSpec(setType.getElementType()));
break;
case ProtocolConstants.DataType.MAP:
MapType mapType = ((MapType) dataType);
type = new RawType.RawMap(toProtocolSpec(mapType.getKeyType()), toProtocolSpec(mapType.getValueType()));
break;
case ProtocolConstants.DataType.TUPLE:
TupleType tupleType = ((TupleType) dataType);
ImmutableList.Builder<RawType> subTypesList = ImmutableList.builderWithExpectedSize(tupleType.getComponentTypes().size());
for (int i = 0; i < tupleType.getComponentTypes().size(); i++) {
subTypesList.add(toProtocolSpec(tupleType.getComponentTypes().get(i)));
}
type = new RawType.RawTuple(subTypesList.build());
break;
case ProtocolConstants.DataType.UDT:
UserDefinedType userDefinedType = ((UserDefinedType) dataType);
ImmutableMap.Builder<String, RawType> subTypesMap = ImmutableMap.builderWithExpectedSize(userDefinedType.getFieldNames().size());
for (int i = 0; i < userDefinedType.getFieldTypes().size(); i++) {
subTypesMap.put(userDefinedType.getFieldNames().get(i).asInternal(), toProtocolSpec(userDefinedType.getFieldTypes().get(i)));
}
type = new RawType.RawUdt(Objects.requireNonNull(userDefinedType.getKeyspace()).asInternal(), userDefinedType.getName().asInternal(), subTypesMap.build());
break;
default:
throw new IllegalArgumentException("Unsupported type: " + dataType.asCql(true, true));
}
return type;
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class UdtValueSerializer method readDynamicCustomValue.
@Override
public UdtValue readDynamicCustomValue(Buffer buffer, GraphBinaryReader context) throws IOException {
// read type definition first
DataType driverType = ComplexTypeSerializerUtil.decodeTypeDefinition(buffer, driverContext);
assert driverType instanceof UserDefinedType : "GraphBinary UdtValue deserializer was called on a value that is not encoded as a UdtValue.";
UserDefinedType userDefinedType = (UserDefinedType) driverType;
UdtValue value = userDefinedType.newValue();
// then read values
return ComplexTypeSerializerUtil.decodeValue(buffer, value, userDefinedType.getFieldTypes().size());
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class SchemaRefreshTest method should_detect_top_level_change_and_children_changes.
@Test
public void should_detect_top_level_change_and_children_changes() {
// Drop one type, modify the other and add a third one
UserDefinedType newT2 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks1"), CqlIdentifier.fromInternal("t2")).withField(CqlIdentifier.fromInternal("i"), DataTypes.TEXT).build();
UserDefinedType t3 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks1"), CqlIdentifier.fromInternal("t3")).withField(CqlIdentifier.fromInternal("i"), DataTypes.INT).build();
// Also disable durable writes
DefaultKeyspaceMetadata newKs1 = newKeyspace("ks1", false, newT2, t3);
SchemaRefresh refresh = new SchemaRefresh(ImmutableMap.of(OLD_KS1.getName(), newKs1));
MetadataRefresh.Result result = refresh.compute(oldMetadata, false, context);
assertThat(result.newMetadata.getKeyspaces().get(OLD_KS1.getName())).isEqualTo(newKs1);
assertThat(result.events).containsExactly(KeyspaceChangeEvent.updated(OLD_KS1, newKs1), TypeChangeEvent.dropped(OLD_T1), TypeChangeEvent.updated(OLD_T2, newT2), TypeChangeEvent.created(t3));
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class DataTypeSerializationTest method should_serialize_and_deserialize.
@Test
public void should_serialize_and_deserialize() {
TupleType tuple = DataTypes.tupleOf(DataTypes.INT, DataTypes.TEXT);
UserDefinedType udt = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
// Because primitive and custom types never use the codec registry, we consider them always
// attached
should_serialize_and_deserialize(DataTypes.INT, false);
should_serialize_and_deserialize(DataTypes.custom("some.class.name"), false);
should_serialize_and_deserialize(tuple, true);
should_serialize_and_deserialize(udt, true);
should_serialize_and_deserialize(DataTypes.listOf(DataTypes.INT), false);
should_serialize_and_deserialize(DataTypes.listOf(tuple), true);
should_serialize_and_deserialize(DataTypes.setOf(udt), true);
should_serialize_and_deserialize(DataTypes.mapOf(tuple, udt), true);
}
Aggregations