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

org.apache.flink.runtime.io.compression.BlockCompressionFactory Maven / Gradle / Ivy

There is a newer version: 1.19.0
Show newest version
/*
 * 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.runtime.io.compression;

import org.apache.flink.configuration.IllegalConfigurationException;

import io.airlift.compress.lzo.LzoCompressor;
import io.airlift.compress.lzo.LzoDecompressor;
import io.airlift.compress.zstd.ZstdCompressor;
import io.airlift.compress.zstd.ZstdDecompressor;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
 * Each compression codec has a implementation of {@link BlockCompressionFactory} to create
 * compressors and decompressors.
 */
public interface BlockCompressionFactory {

    BlockCompressor getCompressor();

    BlockDecompressor getDecompressor();

    /** Name of {@link BlockCompressionFactory}. */
    enum CompressionFactoryName {
        LZ4,
        LZO,
        ZSTD
    }

    /**
     * Creates {@link BlockCompressionFactory} according to the configuration.
     *
     * @param compressionFactoryName supported compression codecs or user-defined class name
     *     inherited from {@link BlockCompressionFactory}.
     */
    static BlockCompressionFactory createBlockCompressionFactory(String compressionFactoryName) {

        checkNotNull(compressionFactoryName);

        CompressionFactoryName compressionName;
        try {
            compressionName = CompressionFactoryName.valueOf(compressionFactoryName.toUpperCase());
        } catch (IllegalArgumentException e) {
            compressionName = null;
        }

        BlockCompressionFactory blockCompressionFactory;
        if (compressionName != null) {
            switch (compressionName) {
                case LZ4:
                    blockCompressionFactory = new Lz4BlockCompressionFactory();
                    break;
                case LZO:
                    blockCompressionFactory =
                            new AirCompressorFactory(new LzoCompressor(), new LzoDecompressor());
                    break;
                case ZSTD:
                    blockCompressionFactory =
                            new AirCompressorFactory(new ZstdCompressor(), new ZstdDecompressor());
                    break;
                default:
                    throw new IllegalStateException("Unknown CompressionMethod " + compressionName);
            }
        } else {
            Object factoryObj;
            try {
                factoryObj = Class.forName(compressionFactoryName).newInstance();
            } catch (ClassNotFoundException e) {
                throw new IllegalConfigurationException(
                        "Cannot load class " + compressionFactoryName, e);
            } catch (Exception e) {
                throw new IllegalConfigurationException(
                        "Cannot create object for class " + compressionFactoryName, e);
            }
            if (factoryObj instanceof BlockCompressionFactory) {
                blockCompressionFactory = (BlockCompressionFactory) factoryObj;
            } else {
                throw new IllegalArgumentException(
                        "CompressionFactoryName should inherit from"
                                + " interface BlockCompressionFactory, or use the default compression codec.");
            }
        }

        checkNotNull(blockCompressionFactory);
        return blockCompressionFactory;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy