/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.connector.jdbc.core.datastream.source;

import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.connector.source.Boundedness;
import org.apache.flink.api.connector.source.Source;
import org.apache.flink.api.connector.source.SourceReader;
import org.apache.flink.api.connector.source.SourceReaderContext;
import org.apache.flink.api.connector.source.SplitEnumerator;
import org.apache.flink.api.connector.source.SplitEnumeratorContext;
import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.base.DeliveryGuarantee;
import org.apache.flink.connector.jdbc.core.datastream.source.config.ContinuousUnBoundingSettings;
import org.apache.flink.connector.jdbc.core.datastream.source.enumerator.JdbcSourceEnumStateSerializer;
import org.apache.flink.connector.jdbc.core.datastream.source.enumerator.JdbcSourceEnumerator;
import org.apache.flink.connector.jdbc.core.datastream.source.enumerator.JdbcSourceEnumeratorState;
import org.apache.flink.connector.jdbc.core.datastream.source.enumerator.JdbcSqlSplitEnumeratorBase;
import org.apache.flink.connector.jdbc.core.datastream.source.enumerator.splitter.JdbcSqlSplitterEnumerator;
import org.apache.flink.connector.jdbc.core.datastream.source.enumerator.splitter.SplitterEnumerator;
import org.apache.flink.connector.jdbc.core.datastream.source.reader.JdbcSourceReader;
import org.apache.flink.connector.jdbc.core.datastream.source.reader.JdbcSourceSplitReader;
import org.apache.flink.connector.jdbc.core.datastream.source.reader.extractor.ResultExtractor;
import org.apache.flink.connector.jdbc.core.datastream.source.split.JdbcSourceSplit;
import org.apache.flink.connector.jdbc.core.datastream.source.split.JdbcSourceSplitSerializer;
import org.apache.flink.connector.jdbc.datasource.connections.JdbcConnectionProvider;
import org.apache.flink.connector.jdbc.lineage.DefaultTypeDatasetFacet;
import org.apache.flink.connector.jdbc.lineage.LineageUtils;
import org.apache.flink.core.io.SimpleVersionedSerializer;
import org.apache.flink.streaming.api.lineage.LineageDataset;
import org.apache.flink.streaming.api.lineage.LineageVertex;
import org.apache.flink.streaming.api.lineage.LineageVertexProvider;
import org.apache.flink.util.Preconditions;

import javax.annotation.Nullable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;

/** JDBC source. */
@PublicEvolving
public class JdbcSource<OUT>
        implements LineageVertexProvider,
                Source<OUT, JdbcSourceSplit, JdbcSourceEnumeratorState>,
                ResultTypeQueryable<OUT> {

    private final TypeInformation<OUT> typeInformation;

    private final Configuration configuration;
    private final SplitterEnumerator splitterEnumerator;

    protected JdbcConnectionProvider connectionProvider;
    private final ResultExtractor<OUT> resultExtractor;
    private final DeliveryGuarantee deliveryGuarantee;

    @Deprecated
    JdbcSource(
            Configuration configuration,
            JdbcConnectionProvider connectionProvider,
            JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
            ResultExtractor<OUT> resultExtractor,
            TypeInformation<OUT> typeInformation,
            @Nullable DeliveryGuarantee deliveryGuarantee,
            @Nullable ContinuousUnBoundingSettings continuousUnBoundingSettings) {
        this(
                configuration,
                connectionProvider,
                new JdbcSqlSplitterEnumerator(
                        sqlSplitEnumeratorProvider, continuousUnBoundingSettings),
                resultExtractor,
                typeInformation,
                deliveryGuarantee);
    }

    JdbcSource(
            Configuration configuration,
            JdbcConnectionProvider connectionProvider,
            SplitterEnumerator splitterEnumerator,
            ResultExtractor<OUT> resultExtractor,
            TypeInformation<OUT> typeInformation,
            @Nullable DeliveryGuarantee deliveryGuarantee) {
        this.configuration = Preconditions.checkNotNull(configuration);
        this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
        this.splitterEnumerator = Preconditions.checkNotNull(splitterEnumerator);
        this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
        this.deliveryGuarantee =
                Objects.isNull(deliveryGuarantee) ? DeliveryGuarantee.NONE : deliveryGuarantee;
        this.typeInformation = Preconditions.checkNotNull(typeInformation);
    }

    @Override
    public Boundedness getBoundedness() {
        return splitterEnumerator.getBoundedness();
    }

    @Override
    public SourceReader<OUT, JdbcSourceSplit> createReader(SourceReaderContext readerContext)
            throws Exception {
        return new JdbcSourceReader<>(
                () ->
                        new JdbcSourceSplitReader<>(
                                readerContext,
                                configuration,
                                typeInformation,
                                connectionProvider,
                                deliveryGuarantee,
                                resultExtractor),
                configuration,
                readerContext);
    }

    @Override
    public SplitEnumerator<JdbcSourceSplit, JdbcSourceEnumeratorState> createEnumerator(
            SplitEnumeratorContext<JdbcSourceSplit> enumContext) throws Exception {
        return new JdbcSourceEnumerator(
                enumContext, splitterEnumerator, connectionProvider, new ArrayList<>());
    }

    @Override
    public SplitEnumerator<JdbcSourceSplit, JdbcSourceEnumeratorState> restoreEnumerator(
            SplitEnumeratorContext<JdbcSourceSplit> enumContext,
            JdbcSourceEnumeratorState checkpoint)
            throws Exception {
        Serializable optionalUserDefinedSplitEnumeratorState =
                checkpoint.getOptionalUserDefinedSplitEnumeratorState();
        return new JdbcSourceEnumerator(
                enumContext,
                splitterEnumerator.restoreState(optionalUserDefinedSplitEnumeratorState),
                connectionProvider,
                checkpoint.getRemainingSplits());
    }

    @Override
    public SimpleVersionedSerializer<JdbcSourceSplit> getSplitSerializer() {
        return new JdbcSourceSplitSerializer();
    }

    @Override
    public SimpleVersionedSerializer<JdbcSourceEnumeratorState>
            getEnumeratorCheckpointSerializer() {
        return new JdbcSourceEnumStateSerializer((JdbcSourceSplitSerializer) getSplitSerializer());
    }

    @Override
    public TypeInformation<OUT> getProducedType() {
        return typeInformation;
    }

    public static <OUT> JdbcSourceBuilder<OUT> builder() {
        return new JdbcSourceBuilder<>();
    }

    // ---- Visible for testing methods. ---

    @VisibleForTesting
    public SplitterEnumerator getSplitterEnumerator() {
        return splitterEnumerator;
    }

    @VisibleForTesting
    TypeInformation<OUT> getTypeInformation() {
        return typeInformation;
    }

    @VisibleForTesting
    Configuration getConfiguration() {
        return configuration;
    }

    @VisibleForTesting
    ResultExtractor<OUT> getResultExtractor() {
        return resultExtractor;
    }

    @VisibleForTesting
    JdbcConnectionProvider getConnectionProvider() {
        return connectionProvider;
    }

    @VisibleForTesting
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        JdbcSource<?> that = (JdbcSource<?>) o;
        return Objects.equals(typeInformation, that.typeInformation)
                && Objects.equals(configuration, that.configuration)
                && Objects.equals(splitterEnumerator, that.splitterEnumerator)
                && Objects.equals(connectionProvider, that.connectionProvider)
                && Objects.equals(resultExtractor, that.resultExtractor)
                && deliveryGuarantee == that.deliveryGuarantee;
    }

    @Override
    public LineageVertex getLineageVertex() {
        DefaultTypeDatasetFacet defaultTypeDatasetFacet =
                new DefaultTypeDatasetFacet(getTypeInformation());
        Optional<String> nameOpt =
                LineageUtils.tableNameOf(splitterEnumerator.lineageQueries(), true);
        String namespace = LineageUtils.namespaceOf(connectionProvider);
        LineageDataset dataset =
                LineageUtils.datasetOf(
                        nameOpt.orElse(""), namespace, Arrays.asList(defaultTypeDatasetFacet));
        return LineageUtils.sourceLineageVertexOf(getBoundedness(), Collections.singleton(dataset));
    }
}
