Search in sources :

Example 6 with TupleType

use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.

the class DataTypeCqlNameParserTest method should_parse_tuple.

@Test
public void should_parse_tuple() {
    TupleType tupleType = (TupleType) parse("tuple<int,text,float>");
    assertThat(tupleType.getComponentTypes().size()).isEqualTo(3);
    assertThat(tupleType.getComponentTypes().get(0)).isEqualTo(DataTypes.INT);
    assertThat(tupleType.getComponentTypes().get(1)).isEqualTo(DataTypes.TEXT);
    assertThat(tupleType.getComponentTypes().get(2)).isEqualTo(DataTypes.FLOAT);
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) Test(org.junit.Test)

Example 7 with TupleType

use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.

the class DataTypeClassNameParserTest method should_parse_tuple.

@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_parse_tuple(Locale locale) {
    Locale def = Locale.getDefault();
    try {
        Locale.setDefault(locale);
        TupleType tupleType = (TupleType) parse("org.apache.cassandra.db.marshal.TupleType(" + "org.apache.cassandra.db.marshal.Int32Type," + "org.apache.cassandra.db.marshal.UTF8Type," + "org.apache.cassandra.db.marshal.FloatType)");
        assertThat(tupleType.getComponentTypes().size()).isEqualTo(3);
        assertThat(tupleType.getComponentTypes().get(0)).isEqualTo(DataTypes.INT);
        assertThat(tupleType.getComponentTypes().get(1)).isEqualTo(DataTypes.TEXT);
        assertThat(tupleType.getComponentTypes().get(2)).isEqualTo(DataTypes.FLOAT);
    } finally {
        Locale.setDefault(def);
    }
}
Also used : Locale(java.util.Locale) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 8 with TupleType

use of com.datastax.oss.driver.api.core.type.TupleType 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;
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) MapType(com.datastax.oss.driver.api.core.type.MapType) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) RawType(com.datastax.oss.protocol.internal.response.result.RawType)

Example 9 with TupleType

use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.

the class TupleValueSerializer method readDynamicCustomValue.

@Override
public TupleValue readDynamicCustomValue(Buffer buffer, GraphBinaryReader context) throws IOException {
    // read the type first
    DataType type = ComplexTypeSerializerUtil.decodeTypeDefinition(buffer, driverContext);
    assert type instanceof TupleType : "GraphBinary TupleValue deserializer was called on a value that is not encoded as a TupleValue.";
    TupleType tupleType = (TupleType) type;
    TupleValue value = tupleType.newValue();
    // then decode the values from the buffer
    return ComplexTypeSerializerUtil.decodeValue(buffer, value, tupleType.getComponentTypes().size());
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue)

Example 10 with TupleType

use of com.datastax.oss.driver.api.core.type.TupleType 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);
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) Test(org.junit.Test)

Aggregations

TupleType (com.datastax.oss.driver.api.core.type.TupleType)32 Test (org.junit.Test)22 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)13 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)12 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)5 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)5 ByteBuffer (java.nio.ByteBuffer)5 DataType (com.datastax.oss.driver.api.core.type.DataType)4 BigInteger (java.math.BigInteger)4 CustomType (com.datastax.oss.driver.api.core.type.CustomType)3 DefaultTupleValue (com.datastax.oss.driver.internal.core.data.DefaultTupleValue)3 LineString (com.datastax.dse.driver.api.core.data.geometry.LineString)2 Point (com.datastax.dse.driver.api.core.data.geometry.Point)2 Polygon (com.datastax.dse.driver.api.core.data.geometry.Polygon)2 Geo (com.datastax.dse.driver.api.core.graph.predicates.Geo)2 DseDataTypes (com.datastax.dse.driver.api.core.type.DseDataTypes)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2