Search in sources :

Example 6 with UserDefinedTypeBuilder

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));
}
Also used : DefaultKeyspaceMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultKeyspaceMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) MetadataRefresh(com.datastax.oss.driver.internal.core.metadata.MetadataRefresh) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Example 7 with UserDefinedTypeBuilder

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;
}
Also used : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with UserDefinedTypeBuilder

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);
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Example 9 with UserDefinedTypeBuilder

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");
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Example 10 with UserDefinedTypeBuilder

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");
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Aggregations

UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)16 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)15 Test (org.junit.Test)10 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)9 TupleType (com.datastax.oss.driver.api.core.type.TupleType)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)3 DefaultUdtValue (com.datastax.oss.driver.internal.core.data.DefaultUdtValue)3 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)3 AttachmentPoint (com.datastax.oss.driver.api.core.detach.AttachmentPoint)2 DefaultTupleValue (com.datastax.oss.driver.internal.core.data.DefaultTupleValue)2 MetadataRefresh (com.datastax.oss.driver.internal.core.metadata.MetadataRefresh)2 DefaultKeyspaceMetadata (com.datastax.oss.driver.internal.core.metadata.schema.DefaultKeyspaceMetadata)2 BigInteger (java.math.BigInteger)2 InetAddress (java.net.InetAddress)2 ByteBuffer (java.nio.ByteBuffer)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 CharsetCodec (com.datastax.oss.driver.api.querybuilder.CharsetCodec)1 DefaultTupleType (com.datastax.oss.driver.internal.core.type.DefaultTupleType)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1