Search in sources :

Example 1 with ConversionContext

use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.

the class TemporalConvertingCodecsProvider method maybeProvide.

@NonNull
@Override
public Optional<ConvertingCodec<?, ?>> maybeProvide(@NonNull DataType cqlType, @NonNull GenericType<?> externalJavaType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
    ConversionContext context = codecFactory.getContext();
    if (externalJavaType.isSubtypeOf(TEMPORAL_GENERIC_TYPE) && isTemporal(cqlType)) {
        @SuppressWarnings("unchecked") Class<Temporal> fromTemporalType = (Class<Temporal>) externalJavaType.getRawType();
        if (cqlType == DataTypes.DATE) {
            return Optional.of(new TemporalToTemporalCodec<>(fromTemporalType, TypeCodecs.DATE, context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH)));
        }
        if (cqlType == DataTypes.TIME) {
            return Optional.of(new TemporalToTemporalCodec<>(fromTemporalType, TypeCodecs.TIME, context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH)));
        }
        if (cqlType == DataTypes.TIMESTAMP) {
            return Optional.of(new TemporalToTemporalCodec<>(fromTemporalType, TypeCodecs.TIMESTAMP, context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH)));
        }
    }
    if (externalJavaType.isSubtypeOf(TEMPORAL_GENERIC_TYPE) && isUUID(cqlType)) {
        TypeCodec<UUID> uuidCodec = codecFactory.getCodecRegistry().codecFor(cqlType);
        @SuppressWarnings({ "unchecked", "rawtypes" }) TemporalToTemporalCodec<TemporalAccessor, Instant> instantCodec = (TemporalToTemporalCodec) codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, externalJavaType, false);
        return Optional.of(new TemporalToUUIDCodec<>(uuidCodec, instantCodec, context.getAttribute(TIME_UUID_GENERATOR)));
    }
    if (externalJavaType.isSubtypeOf(DATE_GENERIC_TYPE) && isTemporal(cqlType)) {
        if (cqlType == DataTypes.DATE) {
            return Optional.of(new DateToTemporalCodec<>(Date.class, TypeCodecs.DATE, context.getAttribute(TIME_ZONE)));
        }
        if (cqlType == DataTypes.TIME) {
            return Optional.of(new DateToTemporalCodec<>(Date.class, TypeCodecs.TIME, context.getAttribute(TIME_ZONE)));
        }
        if (cqlType == DataTypes.TIMESTAMP) {
            return Optional.of(new DateToTemporalCodec<>(Date.class, TypeCodecs.TIMESTAMP, context.getAttribute(TIME_ZONE)));
        }
    }
    if (externalJavaType.isSubtypeOf(DATE_GENERIC_TYPE) && isUUID(cqlType)) {
        TypeCodec<UUID> uuidCodec = codecFactory.getCodecRegistry().codecFor(cqlType);
        @SuppressWarnings({ "unchecked", "rawtypes" }) DateToTemporalCodec<Date, Instant> instantCodec = (DateToTemporalCodec) codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, externalJavaType, false);
        return Optional.of(new DateToUUIDCodec<>(uuidCodec, instantCodec, context.getAttribute(TIME_UUID_GENERATOR)));
    }
    return Optional.empty();
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) Instant(java.time.Instant) Date(java.util.Date) JdkCodecUtils.isTemporal(com.datastax.oss.dsbulk.codecs.jdk.JdkCodecUtils.isTemporal) Temporal(java.time.temporal.Temporal) UUID(java.util.UUID) JdkCodecUtils.isUUID(com.datastax.oss.dsbulk.codecs.jdk.JdkCodecUtils.isUUID) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with ConversionContext

use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.

the class JsonNodeConvertingCodecProvider method createJsonNodeConvertingCodec.

@Nullable
private ConvertingCodec<JsonNode, ?> createJsonNodeConvertingCodec(@NonNull DataType cqlType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
    CodecRegistry codecRegistry = codecFactory.getCodecRegistry();
    ConversionContext context = codecFactory.getContext();
    // Don't apply null strings for non-root codecs
    List<String> nullStrings = rootCodec ? context.getAttribute(NULL_STRINGS) : ImmutableList.of();
    int cqlTypeCode = cqlType.getProtocolCode();
    switch(cqlTypeCode) {
        case ASCII:
        case VARCHAR:
            TypeCodec<String> typeCodec = codecRegistry.codecFor(cqlType);
            return new JsonNodeToStringCodec(typeCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
        case BOOLEAN:
            return new JsonNodeToBooleanCodec(context.getAttribute(BOOLEAN_INPUT_WORDS), nullStrings);
        case TINYINT:
            return new JsonNodeToByteCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case SMALLINT:
            return new JsonNodeToShortCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case INT:
            return new JsonNodeToIntegerCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case BIGINT:
            return new JsonNodeToLongCodec(TypeCodecs.BIGINT, context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case COUNTER:
            return new JsonNodeToLongCodec(TypeCodecs.COUNTER, context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case FLOAT:
            return new JsonNodeToFloatCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case DOUBLE:
            return new JsonNodeToDoubleCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case VARINT:
            return new JsonNodeToBigIntegerCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case DECIMAL:
            return new JsonNodeToBigDecimalCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
        case DATE:
            return new JsonNodeToLocalDateCodec(context.getAttribute(LOCAL_DATE_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
        case TIME:
            return new JsonNodeToLocalTimeCodec(context.getAttribute(LOCAL_TIME_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
        case TIMESTAMP:
            return new JsonNodeToInstantCodec(context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH), nullStrings);
        case INET:
            return new JsonNodeToInetAddressCodec(nullStrings);
        case UUID:
            {
                ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
                return new JsonNodeToUUIDCodec(TypeCodecs.UUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
            }
        case TIMEUUID:
            {
                ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
                return new JsonNodeToUUIDCodec(TypeCodecs.TIMEUUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
            }
        case BLOB:
            return new JsonNodeToBlobCodec(context.getAttribute(BINARY_FORMAT), nullStrings);
        case DURATION:
            return new JsonNodeToDurationCodec(nullStrings);
        case LIST:
            {
                DataType elementType = ((ListType) cqlType).getElementType();
                TypeCodec<List<Object>> collectionCodec = codecRegistry.codecFor(cqlType);
                @SuppressWarnings("unchecked") ConvertingCodec<JsonNode, Object> eltCodec = (ConvertingCodec<JsonNode, Object>) createJsonNodeConvertingCodec(elementType, codecFactory, false);
                return new JsonNodeToListCodec<>(collectionCodec, eltCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case SET:
            {
                DataType elementType = ((SetType) cqlType).getElementType();
                TypeCodec<Set<Object>> collectionCodec = codecRegistry.codecFor(cqlType);
                @SuppressWarnings("unchecked") ConvertingCodec<JsonNode, Object> eltCodec = (ConvertingCodec<JsonNode, Object>) createJsonNodeConvertingCodec(elementType, codecFactory, false);
                return new JsonNodeToSetCodec<>(collectionCodec, eltCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case MAP:
            {
                DataType keyType = ((MapType) cqlType).getKeyType();
                DataType valueType = ((MapType) cqlType).getValueType();
                TypeCodec<Map<Object, Object>> mapCodec = codecRegistry.codecFor(cqlType);
                ConvertingCodec<String, Object> keyCodec = codecFactory.createConvertingCodec(keyType, GenericType.STRING, false);
                ConvertingCodec<JsonNode, Object> valueCodec = codecFactory.createConvertingCodec(valueType, JSON_NODE_TYPE, false);
                return new JsonNodeToMapCodec<>(mapCodec, keyCodec, valueCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case TUPLE:
            {
                TypeCodec<TupleValue> tupleCodec = codecRegistry.codecFor(cqlType);
                ImmutableList.Builder<ConvertingCodec<JsonNode, Object>> eltCodecs = new ImmutableList.Builder<>();
                for (DataType eltType : ((TupleType) cqlType).getComponentTypes()) {
                    ConvertingCodec<JsonNode, Object> eltCodec = codecFactory.createConvertingCodec(eltType, JSON_NODE_TYPE, false);
                    eltCodecs.add(Objects.requireNonNull(eltCodec));
                }
                return new JsonNodeToTupleCodec(tupleCodec, eltCodecs.build(), context.getAttribute(OBJECT_MAPPER), nullStrings, context.getAttribute(ALLOW_EXTRA_FIELDS), context.getAttribute(ALLOW_MISSING_FIELDS));
            }
        case UDT:
            {
                TypeCodec<UdtValue> udtCodec = codecRegistry.codecFor(cqlType);
                ImmutableMap.Builder<CqlIdentifier, ConvertingCodec<JsonNode, Object>> fieldCodecs = new ImmutableMap.Builder<>();
                List<CqlIdentifier> fieldNames = ((UserDefinedType) cqlType).getFieldNames();
                List<DataType> fieldTypes = ((UserDefinedType) cqlType).getFieldTypes();
                assert (fieldNames.size() == fieldTypes.size());
                for (int idx = 0; idx < fieldNames.size(); idx++) {
                    CqlIdentifier fieldName = fieldNames.get(idx);
                    DataType fieldType = fieldTypes.get(idx);
                    ConvertingCodec<JsonNode, Object> fieldCodec = codecFactory.createConvertingCodec(fieldType, JSON_NODE_TYPE, false);
                    fieldCodecs.put(fieldName, Objects.requireNonNull(fieldCodec));
                }
                return new JsonNodeToUDTCodec(udtCodec, fieldCodecs.build(), context.getAttribute(OBJECT_MAPPER), nullStrings, context.getAttribute(ALLOW_EXTRA_FIELDS), context.getAttribute(ALLOW_MISSING_FIELDS));
            }
        case CUSTOM:
            {
                CustomType customType = (CustomType) cqlType;
                switch(customType.getClassName()) {
                    case POINT_CLASS_NAME:
                        return new JsonNodeToPointCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
                    case LINE_STRING_CLASS_NAME:
                        return new JsonNodeToLineStringCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
                    case POLYGON_CLASS_NAME:
                        return new JsonNodeToPolygonCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
                    case DATE_RANGE_CLASS_NAME:
                        return new JsonNodeToDateRangeCodec(nullStrings);
                }
            // fall through
            }
        default:
            try {
                TypeCodec<?> innerCodec = codecRegistry.codecFor(cqlType);
                LOGGER.warn(String.format("CQL type %s is not officially supported by this version of DSBulk; " + "JSON literals will be parsed and formatted using registered codec %s", cqlType, innerCodec.getClass().getSimpleName()));
                return new JsonNodeToUnknownTypeCodec<>(innerCodec, nullStrings);
            } catch (CodecNotFoundException ignored) {
            }
            return null;
    }
}
Also used : ConvertingCodec(com.datastax.oss.dsbulk.codecs.api.ConvertingCodec) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) JsonNode(com.fasterxml.jackson.databind.JsonNode) DataType(com.datastax.oss.driver.api.core.type.DataType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) List(java.util.List) DefaultCodecRegistry(com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) CustomType(com.datastax.oss.driver.api.core.type.CustomType) JsonNodeToPointCodec(com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToPointCodec) JsonNodeToPolygonCodec(com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToPolygonCodec) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) JsonNodeToDateRangeCodec(com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToDateRangeCodec) JsonNodeToLineStringCodec(com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToLineStringCodec) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 3 with ConversionContext

use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.

the class JsonNodeToMapCodecTest method setUp.

@BeforeEach
void setUp() {
    ConversionContext context = new TextConversionContext().setNullStrings("NULL").setFormatNumbers(true).setRoundingMode(HALF_EVEN);
    ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
    codec = (JsonNodeToMapCodec<Double, List<String>>) codecFactory.<JsonNode, Map<Double, List<String>>>createConvertingCodec(DataTypes.mapOf(DataTypes.DOUBLE, DataTypes.listOf(DataTypes.TEXT)), JSON_NODE_TYPE, true);
}
Also used : ConvertingCodecFactory(com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) List(java.util.List) JsonNode(com.fasterxml.jackson.databind.JsonNode) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ConversionContext

use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.

the class JsonNodeToSetCodecTest method setUp.

@BeforeEach
void setUp() {
    ConversionContext context = new TextConversionContext().setNullStrings("NULL");
    ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
    codec1 = (JsonNodeToSetCodec<Double>) codecFactory.<JsonNode, Set<Double>>createConvertingCodec(DataTypes.setOf(DataTypes.DOUBLE), JSON_NODE_TYPE, true);
    codec2 = (JsonNodeToSetCodec<String>) codecFactory.<JsonNode, Set<String>>createConvertingCodec(DataTypes.setOf(DataTypes.TEXT), JSON_NODE_TYPE, true);
}
Also used : ConvertingCodecFactory(com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) Set(java.util.Set) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with ConversionContext

use of com.datastax.oss.dsbulk.codecs.api.ConversionContext in project dsbulk by datastax.

the class JsonNodeToShortCodecTest method setUp.

@BeforeEach
void setUp() {
    ConversionContext context = new TextConversionContext().setNullStrings("NULL").setFormatNumbers(true);
    ConvertingCodecFactory codecFactory = new ConvertingCodecFactory(context);
    codec = (JsonNodeToShortCodec) codecFactory.<JsonNode, Short>createConvertingCodec(DataTypes.SMALLINT, JSON_NODE_TYPE, true);
}
Also used : ConvertingCodecFactory(com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) TextConversionContext(com.datastax.oss.dsbulk.codecs.text.TextConversionContext) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ConversionContext (com.datastax.oss.dsbulk.codecs.api.ConversionContext)32 ConvertingCodecFactory (com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory)28 TextConversionContext (com.datastax.oss.dsbulk.codecs.text.TextConversionContext)28 BeforeEach (org.junit.jupiter.api.BeforeEach)28 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 List (java.util.List)5 Instant (java.time.Instant)4 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)3 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)2 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)2 CustomType (com.datastax.oss.driver.api.core.type.CustomType)2 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)2 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)2 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)2 ConvertingCodec (com.datastax.oss.dsbulk.codecs.api.ConvertingCodec)2 JdkCodecUtils.isUUID (com.datastax.oss.dsbulk.codecs.jdk.JdkCodecUtils.isUUID)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 Nullable (edu.umd.cs.findbugs.annotations.Nullable)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2