Search in sources :

Example 6 with DataType

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

the class CachingCodecRegistry method codecFor.

@NonNull
@Override
public <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull JavaTypeT value) {
    Preconditions.checkNotNull(value);
    LOG.trace("[{}] Looking up codec for object {}", logPrefix, value);
    for (TypeCodec<?> primitiveCodec : primitiveCodecs) {
        if (primitiveCodec.accepts(value)) {
            LOG.trace("[{}] Found matching primitive codec {}", logPrefix, primitiveCodec);
            return uncheckedCast(primitiveCodec);
        }
    }
    for (TypeCodec<?> userCodec : userCodecs) {
        if (userCodec.accepts(value)) {
            LOG.trace("[{}] Found matching user codec {}", logPrefix, userCodec);
            return uncheckedCast(userCodec);
        }
    }
    DataType cqlType = inferCqlTypeFromValue(value);
    GenericType<?> javaType = inspectType(value, cqlType);
    LOG.trace("[{}] Continuing based on inferred CQL type {} and Java type {}", logPrefix, cqlType, javaType);
    return uncheckedCast(getCachedCodec(cqlType, javaType, true));
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 7 with DataType

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

the class CachingCodecRegistry method inferJavaTypeFromCqlType.

@NonNull
protected GenericType<?> inferJavaTypeFromCqlType(@NonNull DataType cqlType) {
    if (cqlType instanceof ListType) {
        DataType elementType = ((ListType) cqlType).getElementType();
        return GenericType.listOf(inferJavaTypeFromCqlType(elementType));
    } else if (cqlType instanceof SetType) {
        DataType elementType = ((SetType) cqlType).getElementType();
        return GenericType.setOf(inferJavaTypeFromCqlType(elementType));
    } else if (cqlType instanceof MapType) {
        DataType keyType = ((MapType) cqlType).getKeyType();
        DataType valueType = ((MapType) cqlType).getValueType();
        return GenericType.mapOf(inferJavaTypeFromCqlType(keyType), inferJavaTypeFromCqlType(valueType));
    }
    switch(cqlType.getProtocolCode()) {
        case ProtocolConstants.DataType.CUSTOM:
        case ProtocolConstants.DataType.BLOB:
            return GenericType.BYTE_BUFFER;
        case ProtocolConstants.DataType.ASCII:
        case ProtocolConstants.DataType.VARCHAR:
            return GenericType.STRING;
        case ProtocolConstants.DataType.BIGINT:
        case ProtocolConstants.DataType.COUNTER:
            return GenericType.LONG;
        case ProtocolConstants.DataType.BOOLEAN:
            return GenericType.BOOLEAN;
        case ProtocolConstants.DataType.DECIMAL:
            return GenericType.BIG_DECIMAL;
        case ProtocolConstants.DataType.DOUBLE:
            return GenericType.DOUBLE;
        case ProtocolConstants.DataType.FLOAT:
            return GenericType.FLOAT;
        case ProtocolConstants.DataType.INT:
            return GenericType.INTEGER;
        case ProtocolConstants.DataType.TIMESTAMP:
            return GenericType.INSTANT;
        case ProtocolConstants.DataType.UUID:
        case ProtocolConstants.DataType.TIMEUUID:
            return GenericType.UUID;
        case ProtocolConstants.DataType.VARINT:
            return GenericType.BIG_INTEGER;
        case ProtocolConstants.DataType.INET:
            return GenericType.INET_ADDRESS;
        case ProtocolConstants.DataType.DATE:
            return GenericType.LOCAL_DATE;
        case ProtocolConstants.DataType.TIME:
            return GenericType.LOCAL_TIME;
        case ProtocolConstants.DataType.SMALLINT:
            return GenericType.SHORT;
        case ProtocolConstants.DataType.TINYINT:
            return GenericType.BYTE;
        case ProtocolConstants.DataType.DURATION:
            return GenericType.CQL_DURATION;
        case ProtocolConstants.DataType.UDT:
            return GenericType.UDT_VALUE;
        case ProtocolConstants.DataType.TUPLE:
            return GenericType.TUPLE_VALUE;
        default:
            throw new CodecNotFoundException(cqlType, null);
    }
}
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) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) MapType(com.datastax.oss.driver.api.core.type.MapType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 8 with DataType

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

the class UdtCodec method format.

@NonNull
@Override
public String format(@Nullable UdtValue value) {
    if (value == null) {
        return "NULL";
    }
    CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
    StringBuilder sb = new StringBuilder("{");
    int size = cqlType.getFieldTypes().size();
    boolean first = true;
    for (int i = 0; i < size; i++) {
        if (first) {
            first = false;
        } else {
            sb.append(",");
        }
        CqlIdentifier elementName = cqlType.getFieldNames().get(i);
        sb.append(elementName.asCql(true));
        sb.append(":");
        DataType elementType = cqlType.getFieldTypes().get(i);
        TypeCodec<Object> codec = registry.codecFor(elementType);
        sb.append(codec.format(value.get(i, codec)));
    }
    sb.append("}");
    return sb.toString();
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 9 with DataType

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

the class CachingCodecRegistryTest method should_find_primitive_codecs_for_types.

@Test
@UseDataProvider(value = "primitiveCodecs", location = CachingCodecRegistryTestDataProviders.class)
public void should_find_primitive_codecs_for_types(TypeCodec<?> codec) {
    TestCachingCodecRegistry registry = new TestCachingCodecRegistry(mockCache);
    DataType cqlType = codec.getCqlType();
    GenericType<?> javaType = codec.getJavaType();
    assertThat(registry.codecFor(cqlType, javaType)).isSameAs(codec);
    assertThat(registry.codecFor(cqlType)).isSameAs(codec);
    assertThat(javaType.__getToken().getType()).isInstanceOf(Class.class);
    Class<?> javaClass = (Class<?>) javaType.__getToken().getType();
    assertThat(registry.codecFor(cqlType, javaClass)).isSameAs(codec);
    // Primitive mappings never hit the cache
    verifyZeroInteractions(mockCache);
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 10 with DataType

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

the class DefaultTupleValue method hashCode.

@Override
public int hashCode() {
    int result = type.hashCode();
    for (int i = 0; i < values.length; i++) {
        DataType innerThisType = type.getComponentTypes().get(i);
        Object thisValue = this.codecRegistry().codecFor(innerThisType).decode(this.values[i], this.protocolVersion());
        if (thisValue != null) {
            result = 31 * result + thisValue.hashCode();
        }
    }
    return result;
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType)

Aggregations

DataType (com.datastax.oss.driver.api.core.type.DataType)73 NonNull (edu.umd.cs.findbugs.annotations.NonNull)21 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)19 SetType (com.datastax.oss.driver.api.core.type.SetType)10 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)10 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)10 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)9 CheckReturnValue (edu.umd.cs.findbugs.annotations.CheckReturnValue)9 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)8 Map (java.util.Map)8 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)7 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)7 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)7 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)6 Nullable (edu.umd.cs.findbugs.annotations.Nullable)6 UUID (java.util.UUID)6 TupleType (com.datastax.oss.driver.api.core.type.TupleType)5 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)5 List (java.util.List)5