Search in sources :

Example 1 with DefaultTupleType

use of com.datastax.oss.driver.internal.core.type.DefaultTupleType in project java-driver by datastax.

the class TupleCodecTest method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    when(attachmentPoint.getCodecRegistry()).thenReturn(codecRegistry);
    when(attachmentPoint.getProtocolVersion()).thenReturn(ProtocolVersion.DEFAULT);
    intCodec = spy(TypeCodecs.INT);
    doubleCodec = spy(TypeCodecs.DOUBLE);
    textCodec = spy(TypeCodecs.TEXT);
    // Called by the getters/setters
    when(codecRegistry.codecFor(DataTypes.INT, Integer.class)).thenAnswer(i -> intCodec);
    when(codecRegistry.codecFor(DataTypes.DOUBLE, Double.class)).thenAnswer(i -> doubleCodec);
    when(codecRegistry.codecFor(DataTypes.TEXT, String.class)).thenAnswer(i -> textCodec);
    // Called by format/parse
    when(codecRegistry.codecFor(DataTypes.INT)).thenAnswer(i -> intCodec);
    when(codecRegistry.codecFor(DataTypes.DOUBLE)).thenAnswer(i -> doubleCodec);
    when(codecRegistry.codecFor(DataTypes.TEXT)).thenAnswer(i -> textCodec);
    tupleType = new DefaultTupleType(ImmutableList.of(DataTypes.INT, DataTypes.DOUBLE, DataTypes.TEXT), attachmentPoint);
    codec = TypeCodecs.tupleOf(tupleType);
}
Also used : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) Before(org.junit.Before)

Example 2 with DefaultTupleType

use of com.datastax.oss.driver.internal.core.type.DefaultTupleType in project java-driver by datastax.

the class DefaultTupleValueTest method should_serialize_and_deserialize.

@Test
public void should_serialize_and_deserialize() {
    DefaultTupleType type = new DefaultTupleType(ImmutableList.of(DataTypes.INT, DataTypes.TEXT), attachmentPoint);
    TupleValue in = type.newValue();
    in = in.setBytesUnsafe(0, Bytes.fromHexString("0x00000001"));
    in = in.setBytesUnsafe(1, Bytes.fromHexString("0x61"));
    TupleValue 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 : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 3 with DefaultTupleType

use of com.datastax.oss.driver.internal.core.type.DefaultTupleType in project java-driver by datastax.

the class DataTypeCqlNameParser method parse.

private DataType parse(String toParse, CqlIdentifier keyspaceId, boolean frozen, Map<CqlIdentifier, UserDefinedType> userTypes, InternalDriverContext context) {
    if (toParse.startsWith("'")) {
        return DataTypes.custom(toParse.substring(1, toParse.length() - 1));
    }
    Parser parser = new Parser(toParse, 0);
    String type = parser.parseTypeName();
    if (type.equalsIgnoreCase(RawColumn.THRIFT_EMPTY_TYPE)) {
        return DataTypes.custom(type);
    }
    DataType nativeType = NATIVE_TYPES_BY_NAME.get(type.toLowerCase(Locale.ROOT));
    if (nativeType != null) {
        return nativeType;
    }
    if (parser.isEOS()) {
        // No parameters => it's a UDT
        CqlIdentifier name = CqlIdentifier.fromCql(type);
        if (userTypes != null) {
            UserDefinedType userType = userTypes.get(name);
            if (userType == null) {
                throw new IllegalStateException(String.format("Can't find referenced user type %s", type));
            }
            return userType.copy(frozen);
        } else {
            return new ShallowUserDefinedType(keyspaceId, name, frozen);
        }
    }
    List<String> parameters = parser.parseTypeParameters();
    if (type.equalsIgnoreCase("list")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for list, got %s", parameters));
        }
        DataType elementType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        return DataTypes.listOf(elementType, frozen);
    }
    if (type.equalsIgnoreCase("set")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for set, got %s", parameters));
        }
        DataType elementType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        return DataTypes.setOf(elementType, frozen);
    }
    if (type.equalsIgnoreCase("map")) {
        if (parameters.size() != 2) {
            throw new IllegalArgumentException(String.format("Expecting two parameters for map, got %s", parameters));
        }
        DataType keyType = parse(parameters.get(0), keyspaceId, false, userTypes, context);
        DataType valueType = parse(parameters.get(1), keyspaceId, false, userTypes, context);
        return DataTypes.mapOf(keyType, valueType, frozen);
    }
    if (type.equalsIgnoreCase("frozen")) {
        if (parameters.size() != 1) {
            throw new IllegalArgumentException(String.format("Expecting single parameter for frozen keyword, got %s", parameters));
        }
        return parse(parameters.get(0), keyspaceId, true, userTypes, context);
    }
    if (type.equalsIgnoreCase("tuple")) {
        if (parameters.isEmpty()) {
            throw new IllegalArgumentException("Expecting at list one parameter for tuple, got none");
        }
        ImmutableList.Builder<DataType> componentTypesBuilder = ImmutableList.builder();
        for (String rawType : parameters) {
            componentTypesBuilder.add(parse(rawType, keyspaceId, false, userTypes, context));
        }
        return new DefaultTupleType(componentTypesBuilder.build(), context);
    }
    throw new IllegalArgumentException("Could not parse type name " + toParse);
}
Also used : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) ShallowUserDefinedType(com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) DataType(com.datastax.oss.driver.api.core.type.DataType) 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)

Example 4 with DefaultTupleType

use of com.datastax.oss.driver.internal.core.type.DefaultTupleType 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 5 with DefaultTupleType

use of com.datastax.oss.driver.internal.core.type.DefaultTupleType in project java-driver by datastax.

the class DefaultTupleValueTest method should_support_null_items_when_setting_in_bulk.

@Test
public void should_support_null_items_when_setting_in_bulk() {
    DefaultTupleType type = new DefaultTupleType(ImmutableList.of(DataTypes.INT, DataTypes.TEXT), attachmentPoint);
    when(codecRegistry.<Integer>codecFor(DataTypes.INT)).thenReturn(TypeCodecs.INT);
    when(codecRegistry.codecFor(DataTypes.TEXT, "foo")).thenReturn(TypeCodecs.TEXT);
    TupleValue value = type.newValue(null, "foo");
    assertThat(value.isNull(0)).isTrue();
    assertThat(value.getString(1)).isEqualTo("foo");
}
Also used : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Aggregations

DefaultTupleType (com.datastax.oss.driver.internal.core.type.DefaultTupleType)5 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)2 DataType (com.datastax.oss.driver.api.core.type.DataType)2 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)2 Test (org.junit.Test)2 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)1 ShallowUserDefinedType (com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType)1 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1