Search in sources :

Example 1 with StringToPointCodec

use of com.datastax.oss.dsbulk.codecs.text.string.dse.StringToPointCodec in project dsbulk by datastax.

the class StringConvertingCodecProvider method createStringConvertingCodec.

@Nullable
private ConvertingCodec<String, ?> createStringConvertingCodec(@NonNull DataType cqlType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
    ConversionContext context = codecFactory.getContext();
    // DAT-297: 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 = codecFactory.getCodecRegistry().codecFor(cqlType);
            return new StringToStringCodec(typeCodec, nullStrings);
        case BOOLEAN:
            return new StringToBooleanCodec(context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_OUTPUT_WORDS), nullStrings);
        case TINYINT:
            return new StringToByteCodec(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 StringToShortCodec(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 StringToIntegerCodec(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 StringToLongCodec(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 StringToLongCodec(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 StringToFloatCodec(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 StringToDoubleCodec(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 StringToBigIntegerCodec(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 StringToBigDecimalCodec(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 StringToLocalDateCodec(context.getAttribute(LOCAL_DATE_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
        case TIME:
            return new StringToLocalTimeCodec(context.getAttribute(LOCAL_TIME_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
        case TIMESTAMP:
            return new StringToInstantCodec(context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH), nullStrings);
        case INET:
            return new StringToInetAddressCodec(nullStrings);
        case UUID:
            {
                ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
                return new StringToUUIDCodec(TypeCodecs.UUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
            }
        case TIMEUUID:
            {
                ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
                return new StringToUUIDCodec(TypeCodecs.TIMEUUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
            }
        case BLOB:
            return new StringToBlobCodec(nullStrings, context.getAttribute(BINARY_FORMAT));
        case DURATION:
            return new StringToDurationCodec(nullStrings);
        case LIST:
            {
                ConvertingCodec<JsonNode, List<Object>> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToListCodec<>(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case SET:
            {
                ConvertingCodec<JsonNode, Set<Object>> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToSetCodec<>(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case MAP:
            {
                ConvertingCodec<JsonNode, Map<Object, Object>> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToMapCodec<>(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case TUPLE:
            {
                ConvertingCodec<JsonNode, TupleValue> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToTupleCodec(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case UDT:
            {
                ConvertingCodec<JsonNode, UdtValue> jsonCodec = codecFactory.createConvertingCodec(cqlType, JSON_NODE_TYPE, false);
                return new StringToUDTCodec(jsonCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
            }
        case CUSTOM:
            {
                CustomType customType = (CustomType) cqlType;
                switch(customType.getClassName()) {
                    case POINT_CLASS_NAME:
                        return new StringToPointCodec(context.getAttribute(GEO_FORMAT), nullStrings);
                    case LINE_STRING_CLASS_NAME:
                        return new StringToLineStringCodec(context.getAttribute(GEO_FORMAT), nullStrings);
                    case POLYGON_CLASS_NAME:
                        return new StringToPolygonCodec(context.getAttribute(GEO_FORMAT), nullStrings);
                    case DATE_RANGE_CLASS_NAME:
                        return new StringToDateRangeCodec(nullStrings);
                }
            // fall through
            }
        default:
            try {
                TypeCodec<?> innerCodec = codecFactory.getCodecRegistry().codecFor(cqlType);
                LOGGER.warn(String.format("CQL type %s is not officially supported by this version of DSBulk; " + "string literals will be parsed and formatted using registered codec %s", cqlType, innerCodec.getClass().getSimpleName()));
                return new StringToUnknownTypeCodec<>(innerCodec, nullStrings);
            } catch (CodecNotFoundException ignored) {
            }
            return null;
    }
}
Also used : ConvertingCodec(com.datastax.oss.dsbulk.codecs.api.ConvertingCodec) StringToPolygonCodec(com.datastax.oss.dsbulk.codecs.text.string.dse.StringToPolygonCodec) StringToDateRangeCodec(com.datastax.oss.dsbulk.codecs.text.string.dse.StringToDateRangeCodec) CustomType(com.datastax.oss.driver.api.core.type.CustomType) StringToLineStringCodec(com.datastax.oss.dsbulk.codecs.text.string.dse.StringToLineStringCodec) ConversionContext(com.datastax.oss.dsbulk.codecs.api.ConversionContext) StringToPointCodec(com.datastax.oss.dsbulk.codecs.text.string.dse.StringToPointCodec) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Aggregations

CustomType (com.datastax.oss.driver.api.core.type.CustomType)1 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)1 ConversionContext (com.datastax.oss.dsbulk.codecs.api.ConversionContext)1 ConvertingCodec (com.datastax.oss.dsbulk.codecs.api.ConvertingCodec)1 StringToDateRangeCodec (com.datastax.oss.dsbulk.codecs.text.string.dse.StringToDateRangeCodec)1 StringToLineStringCodec (com.datastax.oss.dsbulk.codecs.text.string.dse.StringToLineStringCodec)1 StringToPointCodec (com.datastax.oss.dsbulk.codecs.text.string.dse.StringToPointCodec)1 StringToPolygonCodec (com.datastax.oss.dsbulk.codecs.text.string.dse.StringToPolygonCodec)1 Nullable (edu.umd.cs.findbugs.annotations.Nullable)1