All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.tarantool.driver.mappers.factories.ResultMapperFactoryFactoryImpl Maven / Gradle / Ivy

Go to download

Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework

There is a newer version: 0.14.0
Show newest version
package io.tarantool.driver.mappers.factories;

import io.tarantool.driver.api.MultiValueCallResult;
import io.tarantool.driver.api.SingleValueCallResult;
import io.tarantool.driver.api.TarantoolResult;
import io.tarantool.driver.api.metadata.TarantoolSpaceMetadata;
import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.mappers.CallResultMapper;
import io.tarantool.driver.mappers.DefaultMultiValueResultMapper;
import io.tarantool.driver.mappers.DefaultSingleValueResultMapper;
import io.tarantool.driver.mappers.MessagePackMapper;
import io.tarantool.driver.mappers.MessagePackValueMapper;
import io.tarantool.driver.mappers.TarantoolTupleResultMapperFactory;
import io.tarantool.driver.mappers.TarantoolTupleResultMapperFactoryImpl;
import io.tarantool.driver.mappers.converters.ValueConverter;
import io.tarantool.driver.mappers.converters.ValueConverterWithInputTypeWrapper;
import io.tarantool.driver.mappers.converters.value.ArrayValueToMultiValueListConverter;
import org.msgpack.value.Value;
import org.msgpack.value.ValueType;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * Manages instantiation of the operation result factories
 *
 * @author Alexey Kuzin
 * @author Artyom Dubinin
 */
public final class ResultMapperFactoryFactoryImpl implements ResultMapperFactoryFactory {

    /**
     * Basic constructor.
     */
    public ResultMapperFactoryFactoryImpl() {
    }

    @Override
    public ArrayValueToTarantoolTupleResultMapperFactory arrayTupleResultMapperFactory() {
        return new ArrayValueToTarantoolTupleResultMapperFactory();
    }

    @Override
    public TarantoolTupleResultMapperFactory getTarantoolTupleResultMapperFactory() {
        return TarantoolTupleResultMapperFactoryImpl.getInstance();
    }

    @Override
    public RowsMetadataToTarantoolTupleResultMapperFactory rowsMetadataTupleResultMapperFactory() {
        return new RowsMetadataToTarantoolTupleResultMapperFactory();
    }

    @Override
    public SingleValueWithTarantoolTupleResultMapperFactory singleValueTupleResultMapperFactory() {
        return new SingleValueWithTarantoolTupleResultMapperFactory();
    }

    @Override
    public MultiValueWithTarantoolTupleResultMapperFactory multiValueTupleResultMapperFactory() {
        return new MultiValueWithTarantoolTupleResultMapperFactory();
    }

    @Override
    public  ArrayValueToTarantoolResultMapperFactory rowsMetadataStructureResultMapperFactory() {
        return new ArrayValueToTarantoolResultMapperFactory<>();
    }

    @Override
    public  SingleValueResultMapperFactory singleValueResultMapperFactory() {
        return new SingleValueResultMapperFactory<>();
    }

    @Override
    public  SingleValueWithTarantoolResultMapperFactory singleValueTarantoolResultMapperFactory() {
        return new SingleValueWithTarantoolResultMapperFactory<>();
    }

    @Override
    public > MultiValueResultMapperFactory multiValueResultMapperFactory() {
        return new MultiValueResultMapperFactory<>();
    }

    @Override
    public  MultiValueWithTarantoolResultMapperFactory multiValueTarantoolResultMapperFactory() {
        return new MultiValueWithTarantoolResultMapperFactory<>();
    }

    public  CallResultMapper>
    getSingleValueResultMapper(ValueConverter valueConverter) {
        return this.singleValueResultMapperFactory().withSingleValueResultConverter(
            valueConverter, (Class>) (Class) SingleValueCallResult.class);
    }

    public > CallResultMapper>
    getMultiValueResultMapper(Supplier containerSupplier, ValueConverter valueConverter) {
        return this.multiValueResultMapperFactory().withMultiValueResultConverter(
            new ArrayValueToMultiValueListConverter<>(valueConverter, containerSupplier),
            (Class>) (Class) MultiValueCallResult.class);
    }

    public  CallResultMapper, SingleValueCallResult>>
    getTarantoolResultMapper(MessagePackMapper mapper, Class tupleClass) {
        return this.singleValueTarantoolResultMapperFactory()
            .withSingleValueArrayTarantoolResultConverter(getConverter(mapper, ValueType.ARRAY, tupleClass));
    }

    public > CallResultMapper>
    getDefaultMultiValueMapper(MessagePackMapper mapper, Class tupleClass) {
        return new DefaultMultiValueResultMapper<>(mapper, tupleClass);
    }

    public  CallResultMapper>
    getDefaultSingleValueMapper(MessagePackValueMapper mapper, Class tupleClass) {
        return new DefaultSingleValueResultMapper<>(mapper, tupleClass);
    }

    private  ValueConverter getConverter(
        MessagePackMapper mapper, ValueType valueType, Class tupleClass) {
        Optional> converter = mapper.getValueConverter(valueType, tupleClass);
        if (!converter.isPresent()) {
            throw new TarantoolClientException(
                "No converter for value type %s and type %s is present", valueType, tupleClass);
        }
        return converter.get();
    }

    @Override
    public Builder createMapper(MessagePackMapper messagePackMapper) {
        return createMapper(messagePackMapper, null);
    }

    @Override
    public Builder createMapper(MessagePackMapper messagePackMapper, TarantoolSpaceMetadata spaceMetadata) {
        return new Builder(messagePackMapper, spaceMetadata);
    }

    private class Builder implements ResultMapperFactoryFactory.Builder {

        private final List> mappers;
        private final MessagePackMapper clientMapper;
        private final TarantoolSpaceMetadata spaceMetadata;

        Builder(MessagePackMapper messagePackMapper, TarantoolSpaceMetadata spaceMetadata) {
            this.mappers = new ArrayList<>();
            this.clientMapper = messagePackMapper;
            this.spaceMetadata = spaceMetadata;
        }

        @Override
        public ResultMapperFactoryFactory.Builder withSingleValueConverter() {
            this.mappers.add(
                singleValueResultMapperFactory()
                    .getSingleValueResultConverter(
                        clientMapper
                    )
            );
            return this;
        }

        public Builder withSingleValueConverter(
            MessagePackValueMapper messagePackMapper) {
            this.mappers.add(
                singleValueResultMapperFactory()
                    .getSingleValueResultConverter(
                        messagePackMapper
                    )
            );
            return this;
        }

        @Override
        public Builder withArrayValueToTarantoolTupleResultConverter(
            MessagePackMapper messagePackMapper) {
            this.mappers.add(
                arrayTupleResultMapperFactory()
                    .getArrayValueToTarantoolTupleResultConverter(messagePackMapper, spaceMetadata)
            );
            return this;
        }

        @Override
        public ResultMapperFactoryFactory.Builder withArrayValueToTarantoolTupleResultConverter() {
            this.mappers.add(
                arrayTupleResultMapperFactory()
                    .getArrayValueToTarantoolTupleResultConverter(clientMapper, spaceMetadata)
            );
            return this;
        }

        @Override
        public Builder withRowsMetadataToTarantoolTupleResultConverter(
            MessagePackMapper messagePackMapper) {
            this.mappers.add(
                rowsMetadataTupleResultMapperFactory()
                    .getRowsMetadataToTarantoolTupleResultConverter(messagePackMapper, spaceMetadata)
            );
            return this;
        }

        @Override
        public ResultMapperFactoryFactory.Builder withRowsMetadataToTarantoolTupleResultConverter() {
            this.mappers.add(
                rowsMetadataTupleResultMapperFactory()
                    .getRowsMetadataToTarantoolTupleResultConverter(clientMapper, spaceMetadata)
            );
            return this;
        }

        @Override
        public CallResultMapper buildCallResultMapper() {
            return new CallResultMapper(clientMapper.copy(), mappers);
        }

        @Override
        public CallResultMapper buildCallResultMapper(MessagePackMapper valueMapper) {
            return new CallResultMapper(valueMapper, mappers);
        }

        @Override
        public  CallResultMapper>
            buildSingleValueResultMapper(MessagePackValueMapper valueMapper, Class classResult) {
            return getDefaultSingleValueMapper(valueMapper, classResult);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy