/*
 * Licensed 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 io.trino.plugin.deltalake.statistics;

import com.google.inject.Inject;
import io.airlift.json.JsonCodec;
import io.trino.plugin.hive.HdfsEnvironment;
import io.trino.spi.TrinoException;
import io.trino.spi.connector.ConnectorSession;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;

import static io.trino.plugin.deltalake.transactionlog.TransactionLogUtil.TRANSACTION_LOG_DIRECTORY;
import static io.trino.plugin.hive.util.HiveWriteUtils.createDirectory;
import static io.trino.plugin.hive.util.HiveWriteUtils.pathExists;
import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class MetaDirStatisticsAccess
        implements ExtendedStatisticsAccess
{
    private static final String STATISTICS_META_DIR = TRANSACTION_LOG_DIRECTORY + "/_trino_meta"; // store inside TL directory so it is not deleted by VACUUM
    private static final String STATISTICS_FILE = "extended_stats.json";

    private final HdfsEnvironment hdfsEnvironment;
    private final JsonCodec<ExtendedStatistics> statisticsCodec;

    @Inject
    public MetaDirStatisticsAccess(
            HdfsEnvironment hdfsEnvironment,
            JsonCodec<ExtendedStatistics> statisticsCodec)
    {
        this.hdfsEnvironment = requireNonNull(hdfsEnvironment, "hdfsEnvironment is null");
        this.statisticsCodec = requireNonNull(statisticsCodec, "statisticsCodec is null");
    }

    @Override
    public Optional<ExtendedStatistics> readExtendedStatistics(
            ConnectorSession session,
            String tableLocation)
    {
        try {
            Path metadataDir = new Path(tableLocation, STATISTICS_META_DIR);
            Path statisticsPath = new Path(metadataDir, STATISTICS_FILE);
            FileSystem fileSystem = hdfsEnvironment.getFileSystem(new HdfsEnvironment.HdfsContext(session), statisticsPath);
            if (!fileSystem.exists(statisticsPath)) {
                return Optional.empty();
            }

            try (InputStream inputStream = fileSystem.open(statisticsPath)) {
                return Optional.of(statisticsCodec.fromJson(inputStream.readAllBytes()));
            }
        }
        catch (IOException e) {
            throw new TrinoException(GENERIC_INTERNAL_ERROR, format("failed to read statistics with table location %s", tableLocation), e);
        }
    }

    @Override
    public void updateExtendedStatistics(
            ConnectorSession session,
            String tableLocation,
            ExtendedStatistics statistics)
    {
        Path metaPath = new Path(tableLocation, STATISTICS_META_DIR);
        ensureDirectoryExists(session, metaPath);
        try {
            Path statisticsPath = new Path(metaPath, STATISTICS_FILE);

            FileSystem fileSystem = hdfsEnvironment.getFileSystem(new HdfsEnvironment.HdfsContext(session), metaPath);
            try (OutputStream outputStream = fileSystem.create(statisticsPath, true)) {
                outputStream.write(statisticsCodec.toJsonBytes(statistics));
            }
        }
        catch (IOException e) {
            throw new TrinoException(GENERIC_INTERNAL_ERROR, format("failed to store statistics with table location %s", tableLocation), e);
        }
    }

    @Override
    public void deleteExtendedStatistics(ConnectorSession session, String tableLocation)
    {
        Path statisticsPath = new Path(new Path(tableLocation, STATISTICS_META_DIR), STATISTICS_FILE);
        try {
            FileSystem hdfs = hdfsEnvironment.getFileSystem(new HdfsEnvironment.HdfsContext(session), statisticsPath);
            if (!hdfs.delete(statisticsPath, false) && hdfs.exists(statisticsPath)) {
                throw new TrinoException(GENERIC_INTERNAL_ERROR, format("Failed to delete statistics file %s", statisticsPath));
            }
        }
        catch (IOException e) {
            throw new TrinoException(GENERIC_INTERNAL_ERROR, format("Error deleting statistics file %s", statisticsPath), e);
        }
    }

    private void ensureDirectoryExists(ConnectorSession session, Path directoryPath)
    {
        HdfsEnvironment.HdfsContext hdfsContext = new HdfsEnvironment.HdfsContext(session);
        if (!pathExists(hdfsContext, hdfsEnvironment, directoryPath)) {
            createDirectory(hdfsContext, hdfsEnvironment, directoryPath);
        }
    }
}

@@ -1698,16 +1698,203 @@
 json%22;%0A%0A
+    private static final String STARBURST_META_DIR = TRANSACTION_LOG_DIRECTORY + %22/_starburst_meta%22;%0A    private static final String STARBURST_STATISTICS_FILE = %22extendeded_stats.json%22;%0A%0A
     priv
@@ -2517,54 +2517,158 @@
     
-try %7B%0A            Path metadataDir = new Path(
+return readExtendedStatistics(session, tableLocation, STATISTICS_META_DIR, STATISTICS_FILE)%0A                .or(() -%3E readExtendedStatistics(session, 
 tabl
@@ -2673,39 +2673,38 @@
 bleLocation, STA
-TISTICS
+RBURST
 _META_DIR);%0A    
@@ -2700,82 +2700,332 @@
 _DIR
-);%0A            Path statisticsPath = new Path(metadataDir, STATISTICS_FILE
+, STARBURST_STATISTICS_FILE));%0A    %7D%0A%0A    private Optional%3CExtendedStatistics%3E readExtendedStatistics(ConnectorSession session, String tableLocation, String statisticsDirectory, String statisticsFile)%0A    %7B%0A        try %7B%0A            Path statisticsPath = new Path(new Path(tableLocation, statisticsDirectory), statisticsFile
 );%0A 
@@ -4311,32 +4311,220 @@
 );%0A            %7D
+%0A%0A            // Remove outdated Starburst stats file, if it exists.%0A            fileSystem.delete(new Path(new Path(tableLocation, STARBURST_META_DIR), STARBURST_STATISTICS_FILE), false);
 %0A        %7D%0A     

Support Starburst-created Delta Lake statistics