Search in sources :

Example 1 with JsonNodeToPolygonCodec

use of com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToPolygonCodec 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)

Aggregations

CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 CustomType (com.datastax.oss.driver.api.core.type.CustomType)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)1 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)1 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)1 DefaultCodecRegistry (com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1 ConversionContext (com.datastax.oss.dsbulk.codecs.api.ConversionContext)1 ConvertingCodec (com.datastax.oss.dsbulk.codecs.api.ConvertingCodec)1 JsonNodeToDateRangeCodec (com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToDateRangeCodec)1 JsonNodeToLineStringCodec (com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToLineStringCodec)1 JsonNodeToPointCodec (com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToPointCodec)1 JsonNodeToPolygonCodec (com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToPolygonCodec)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Nullable (edu.umd.cs.findbugs.annotations.Nullable)1 List (java.util.List)1