Search in sources :

Example 6 with ListType

use of com.datastax.oss.driver.api.core.type.ListType 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 7 with ListType

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

the class DataTypeDetachableTest method attaching_list_should_attach_its_element.

@Test
public void attaching_list_should_attach_its_element() {
    TupleType tuple = DataTypes.tupleOf(DataTypes.INT);
    ListType list = DataTypes.listOf(tuple);
    assertThat(tuple.isDetached()).isTrue();
    assertThat(list.isDetached()).isTrue();
    list.attach(attachmentPoint);
    assertThat(tuple.isDetached()).isFalse();
}
Also used : ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Test(org.junit.Test)

Example 8 with ListType

use of com.datastax.oss.driver.api.core.type.ListType 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;
}
Also used : SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) DataType(com.datastax.oss.driver.api.core.type.DataType) MapType(com.datastax.oss.driver.api.core.type.MapType)

Example 9 with ListType

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

the class CachingCodecRegistry method createCodec.

// Try to create a codec when we haven't found it in the cache
@NonNull
protected TypeCodec<?> createCodec(@Nullable DataType cqlType, @Nullable GenericType<?> javaType, boolean isJavaCovariant) {
    LOG.trace("[{}] Cache miss, creating codec", logPrefix);
    // Either type can be null, but not both.
    if (javaType == null) {
        assert cqlType != null;
        return createCodec(cqlType);
    } else if (cqlType == null) {
        return createCodec(javaType, isJavaCovariant);
    } else {
        // Both non-null
        TypeToken<?> token = javaType.__getToken();
        if (cqlType instanceof ListType && List.class.isAssignableFrom(token.getRawType())) {
            DataType elementCqlType = ((ListType) cqlType).getElementType();
            TypeCodec<Object> elementCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
                elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
            } else {
                elementCodec = codecFor(elementCqlType);
            }
            return TypeCodecs.listOf(elementCodec);
        } else if (cqlType instanceof SetType && Set.class.isAssignableFrom(token.getRawType())) {
            DataType elementCqlType = ((SetType) cqlType).getElementType();
            TypeCodec<Object> elementCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
                elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
            } else {
                elementCodec = codecFor(elementCqlType);
            }
            return TypeCodecs.setOf(elementCodec);
        } else if (cqlType instanceof MapType && Map.class.isAssignableFrom(token.getRawType())) {
            DataType keyCqlType = ((MapType) cqlType).getKeyType();
            DataType valueCqlType = ((MapType) cqlType).getValueType();
            TypeCodec<Object> keyCodec;
            TypeCodec<Object> valueCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> keyJavaType = GenericType.of(typeArguments[0]);
                GenericType<?> valueJavaType = GenericType.of(typeArguments[1]);
                keyCodec = uncheckedCast(codecFor(keyCqlType, keyJavaType, isJavaCovariant));
                valueCodec = uncheckedCast(codecFor(valueCqlType, valueJavaType, isJavaCovariant));
            } else {
                keyCodec = codecFor(keyCqlType);
                valueCodec = codecFor(valueCqlType);
            }
            return TypeCodecs.mapOf(keyCodec, valueCodec);
        } else if (cqlType instanceof TupleType && TupleValue.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.tupleOf((TupleType) cqlType);
        } else if (cqlType instanceof UserDefinedType && UdtValue.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.udtOf((UserDefinedType) cqlType);
        } else if (cqlType instanceof CustomType && ByteBuffer.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.custom(cqlType);
        }
        throw new CodecNotFoundException(cqlType, javaType);
    }
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) Set(java.util.Set) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ByteBuffer(java.nio.ByteBuffer) MapType(com.datastax.oss.driver.api.core.type.MapType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) ParameterizedType(java.lang.reflect.ParameterizedType) ListType(com.datastax.oss.driver.api.core.type.ListType) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) MapType(com.datastax.oss.driver.api.core.type.MapType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) SetType(com.datastax.oss.driver.api.core.type.SetType) CustomType(com.datastax.oss.driver.api.core.type.CustomType) SetType(com.datastax.oss.driver.api.core.type.SetType) TypeToken(com.datastax.oss.driver.shaded.guava.common.reflect.TypeToken) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 10 with ListType

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

the class DataTypeCqlNameParserTest method should_reuse_existing_user_type_when_not_top_level.

@Test
public void should_reuse_existing_user_type_when_not_top_level() {
    UserDefinedType addressType = mock(UserDefinedType.class);
    UserDefinedType frozenAddressType = mock(UserDefinedType.class);
    when(addressType.copy(false)).thenReturn(addressType);
    when(addressType.copy(true)).thenReturn(frozenAddressType);
    ImmutableMap<CqlIdentifier, UserDefinedType> existingTypes = ImmutableMap.of(CqlIdentifier.fromInternal("address"), addressType);
    ListType listOfAddress = (ListType) parse("list<address>", existingTypes);
    assertThat(listOfAddress.getElementType()).isEqualTo(addressType);
    ListType listOfFrozenAddress = (ListType) parse("list<frozen<address>>", existingTypes);
    assertThat(listOfFrozenAddress.getElementType()).isEqualTo(frozenAddressType);
}
Also used : ListType(com.datastax.oss.driver.api.core.type.ListType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ShallowUserDefinedType(com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Aggregations

ListType (com.datastax.oss.driver.api.core.type.ListType)13 MapType (com.datastax.oss.driver.api.core.type.MapType)7 SetType (com.datastax.oss.driver.api.core.type.SetType)7 TupleType (com.datastax.oss.driver.api.core.type.TupleType)7 DataType (com.datastax.oss.driver.api.core.type.DataType)6 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)6 Test (org.junit.Test)6 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)4 CustomType (com.datastax.oss.driver.api.core.type.CustomType)3 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)2 List (java.util.List)2 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)1 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)1 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)1 ViewMetadata (com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)1 GenericType (com.datastax.oss.driver.api.core.type.reflect.GenericType)1 ShallowUserDefinedType (com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1