use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder 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.internal.core.type.UserDefinedTypeBuilder in project java-driver by datastax.
the class DataTypeClassNameParser method parse.
private DataType parse(String toParse, Map<CqlIdentifier, UserDefinedType> userTypes, AttachmentPoint attachmentPoint, String logPrefix) {
boolean frozen = false;
if (isReversed(toParse)) {
// Just skip the ReversedType part, we don't care
toParse = getNestedClassName(toParse);
} else if (toParse.startsWith("org.apache.cassandra.db.marshal.FrozenType")) {
frozen = true;
toParse = getNestedClassName(toParse);
}
Parser parser = new Parser(toParse, 0);
String next = parser.parseNextName();
if (next.startsWith("org.apache.cassandra.db.marshal.ListType")) {
DataType elementType = parse(parser.getTypeParameters().get(0), userTypes, attachmentPoint, logPrefix);
return DataTypes.listOf(elementType, frozen);
}
if (next.startsWith("org.apache.cassandra.db.marshal.SetType")) {
DataType elementType = parse(parser.getTypeParameters().get(0), userTypes, attachmentPoint, logPrefix);
return DataTypes.setOf(elementType, frozen);
}
if (next.startsWith("org.apache.cassandra.db.marshal.MapType")) {
List<String> parameters = parser.getTypeParameters();
DataType keyType = parse(parameters.get(0), userTypes, attachmentPoint, logPrefix);
DataType valueType = parse(parameters.get(1), userTypes, attachmentPoint, logPrefix);
return DataTypes.mapOf(keyType, valueType, frozen);
}
if (frozen)
LOG.warn("[{}] Got o.a.c.db.marshal.FrozenType for something else than a collection, " + "this driver version might be too old for your version of Cassandra", logPrefix);
if (next.startsWith("org.apache.cassandra.db.marshal.UserType")) {
// skipping '('
++parser.idx;
CqlIdentifier keyspace = CqlIdentifier.fromInternal(parser.readOne());
parser.skipBlankAndComma();
String typeName = TypeCodecs.TEXT.decode(Bytes.fromHexString("0x" + parser.readOne()), attachmentPoint.getProtocolVersion());
if (typeName == null) {
throw new AssertionError("Type name cannot be null, this is a server bug");
}
CqlIdentifier typeId = CqlIdentifier.fromInternal(typeName);
Map<String, String> nameAndTypeParameters = parser.getNameAndTypeParameters();
// Avoid re-parsing if we already have the definition
if (userTypes != null && userTypes.containsKey(typeId)) {
// copy as frozen since C* 2.x UDTs are always frozen.
return userTypes.get(typeId).copy(true);
} else {
UserDefinedTypeBuilder builder = new UserDefinedTypeBuilder(keyspace, typeId);
parser.skipBlankAndComma();
for (Map.Entry<String, String> entry : nameAndTypeParameters.entrySet()) {
CqlIdentifier fieldName = CqlIdentifier.fromInternal(entry.getKey());
DataType fieldType = parse(entry.getValue(), userTypes, attachmentPoint, logPrefix);
builder.withField(fieldName, fieldType);
}
// Create a frozen UserType since C* 2.x UDTs are always frozen.
return builder.frozen().withAttachmentPoint(attachmentPoint).build();
}
}
if (next.startsWith("org.apache.cassandra.db.marshal.TupleType")) {
List<String> rawTypes = parser.getTypeParameters();
ImmutableList.Builder<DataType> componentTypesBuilder = ImmutableList.builder();
for (String rawType : rawTypes) {
componentTypesBuilder.add(parse(rawType, userTypes, attachmentPoint, logPrefix));
}
return new DefaultTupleType(componentTypesBuilder.build(), attachmentPoint);
}
DataType type = NATIVE_TYPES_BY_CLASS_NAME.get(next);
return type == null ? DataTypes.custom(toParse) : type;
}
use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder in project java-driver by datastax.
the class DefaultUdtValueTest method should_equate_instances_with_same_values_but_different_binary_representations.
@Test
public void should_equate_instances_with_same_values_but_different_binary_representations() {
UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("f"), DataTypes.VARINT).build();
UdtValue udt1 = type.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x01"));
UdtValue udt2 = type.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x0001"));
assertThat(udt1).isEqualTo(udt2);
}
use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder in project java-driver by datastax.
the class DefaultUdtValueTest method should_support_null_items_when_setting_in_bulk.
@Test
public void should_support_null_items_when_setting_in_bulk() throws UnsupportedEncodingException {
UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
when(codecRegistry.<Integer>codecFor(DataTypes.INT)).thenReturn(TypeCodecs.INT);
when(codecRegistry.codecFor(DataTypes.TEXT, "foo")).thenReturn(TypeCodecs.TEXT);
UdtValue value = type.newValue(null, "foo");
assertThat(value.isNull(0)).isTrue();
assertThat(value.getString(1)).isEqualTo("foo");
}
use of com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder in project java-driver by datastax.
the class DefaultUdtValueTest method should_serialize_and_deserialize.
@Test
public void should_serialize_and_deserialize() {
UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
UdtValue in = type.newValue();
in = in.setBytesUnsafe(0, Bytes.fromHexString("0x00000001"));
in = in.setBytesUnsafe(1, Bytes.fromHexString("0x61"));
UdtValue out = SerializationHelper.serializeAndDeserialize(in);
assertThat(out.getType()).isEqualTo(in.getType());
assertThat(out.getType().isDetached()).isTrue();
assertThat(Bytes.toHexString(out.getBytesUnsafe(0))).isEqualTo("0x00000001");
assertThat(Bytes.toHexString(out.getBytesUnsafe(1))).isEqualTo("0x61");
}
Aggregations