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

org.dinky.shaded.paimon.mergetree.MergeTreeReaders Maven / Gradle / Ivy

/*
 * 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.dinky.shaded.paimon.mergetree;

import org.dinky.shaded.paimon.KeyValue;
import org.dinky.shaded.paimon.data.InternalRow;
import org.dinky.shaded.paimon.io.DataFileMeta;
import org.dinky.shaded.paimon.io.KeyValueFileReaderFactory;
import org.dinky.shaded.paimon.mergetree.compact.ConcatRecordReader;
import org.dinky.shaded.paimon.mergetree.compact.ConcatRecordReader.ReaderSupplier;
import org.dinky.shaded.paimon.mergetree.compact.MergeFunction;
import org.dinky.shaded.paimon.mergetree.compact.MergeFunctionWrapper;
import org.dinky.shaded.paimon.mergetree.compact.ReducerMergeFunctionWrapper;
import org.dinky.shaded.paimon.reader.RecordReader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/** Utility class to create commonly used {@link RecordReader}s for merge trees. */
public class MergeTreeReaders {

    private MergeTreeReaders() {}

    public static RecordReader readerForMergeTree(
            List> sections,
            boolean dropDelete,
            KeyValueFileReaderFactory readerFactory,
            Comparator userKeyComparator,
            MergeFunction mergeFunction,
            MergeSorter mergeSorter)
            throws IOException {
        List> readers = new ArrayList<>();
        for (List section : sections) {
            readers.add(
                    () ->
                            readerForSection(
                                    section,
                                    readerFactory,
                                    userKeyComparator,
                                    new ReducerMergeFunctionWrapper(mergeFunction),
                                    mergeSorter));
        }
        RecordReader reader = ConcatRecordReader.create(readers);
        if (dropDelete) {
            reader = new DropDeleteReader(reader);
        }
        return reader;
    }

    public static  RecordReader readerForSection(
            List section,
            KeyValueFileReaderFactory readerFactory,
            Comparator userKeyComparator,
            MergeFunctionWrapper mergeFunctionWrapper,
            MergeSorter mergeSorter)
            throws IOException {
        List> readers = new ArrayList<>();
        for (SortedRun run : section) {
            readers.add(() -> readerForRun(run, readerFactory));
        }
        return mergeSorter.mergeSort(readers, userKeyComparator, mergeFunctionWrapper);
    }

    public static RecordReader readerForRun(
            SortedRun run, KeyValueFileReaderFactory readerFactory) throws IOException {
        List> readers = new ArrayList<>();
        for (DataFileMeta file : run.files()) {
            readers.add(
                    () ->
                            readerFactory.createRecordReader(
                                    file.schemaId(),
                                    file.fileName(),
                                    file.fileSize(),
                                    file.level()));
        }
        return ConcatRecordReader.create(readers);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy