package com.lanyuanxiaoyao.service.executor.task;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.hadoop.fs.Path;
import org.apache.hudi.common.fs.FSUtils;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;

/**
 * @author lanyuanxiaoyao
 * @date 2024-01-17
 */
public class ParseLatestPath {
    public static void main(String[] args) throws IOException {
        List<Path> paths = Files.readAllLines(Paths.get("/Users/lanyuanxiaoyao/SynologyDrive/document/office/(2024-01-17-459) list.txt"))
                .stream()
                .map(Path::new)
                .collect(Collectors.toList());
        ImmutableList<String> targetPaths = Lists.immutable.ofAll(paths)
                .select(FSUtils::isBaseFile)
                .groupBy(FSUtils::getFileIdFromFilePath)
                .multiValuesView()
                .collect(pathList -> pathList
                        .toSortedListBy(path -> {
                            String commitTime = FSUtils.getCommitTime(path.getName());
                            System.out.println(commitTime);
                            return Long.valueOf(commitTime);
                        })
                        .getLastOptional())
                .select(Optional::isPresent)
                .collect(Optional::get)
                .collect(Path::toString)
                .toList()
                .toImmutable();
        targetPaths.forEach(System.out::println);
    }
}
