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

org.apache.flink.cdc.common.configuration.Configuration Maven / Gradle / Ivy

The 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.cdc.common.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;

import static org.apache.flink.cdc.common.configuration.ConfigurationUtils.canBePrefixMap;
import static org.apache.flink.cdc.common.configuration.ConfigurationUtils.containsPrefixMap;
import static org.apache.flink.cdc.common.configuration.ConfigurationUtils.convertToPropertiesPrefixed;
import static org.apache.flink.cdc.common.configuration.ConfigurationUtils.removePrefixMap;

/** Lightweight configuration object which stores key/value pairs. */
public class Configuration implements java.io.Serializable, Cloneable {

    private static final long serialVersionUID = 1L;

    /** The log object used for debugging. */
    private static final Logger LOG = LoggerFactory.getLogger(Configuration.class);

    /** Stores the concrete key/value pairs of this configuration object. */
    protected final HashMap confData;

    // --------------------------------------------------------------------------------------------

    /** Creates a new empty configuration. */
    public Configuration() {
        this.confData = new HashMap<>();
    }

    /**
     * Creates a new configuration with the copy of the given configuration.
     *
     * @param other The configuration to copy the entries from.
     */
    public Configuration(Configuration other) {
        this.confData = new HashMap<>(other.confData);
    }

    /** Creates a new configuration that is initialized with the options of the given map. */
    public static Configuration fromMap(Map map) {
        final Configuration configuration = new Configuration();
        configuration.confData.putAll(map);
        return configuration;
    }

    public void addAll(Configuration other) {
        synchronized (this.confData) {
            synchronized (other.confData) {
                this.confData.putAll(other.confData);
            }
        }
    }

    @Override
    public Configuration clone() {
        Configuration config = new Configuration();
        config.addAll(this);

        return config;
    }

    /**
     * Checks whether there is an entry for the given config option.
     *
     * @param configOption The configuration option
     * @return true if a valid (current or deprecated) key of the config option is stored,
     *     false otherwise
     */
    public boolean contains(ConfigOption configOption) {
        synchronized (this.confData) {
            final BiFunction> applier =
                    (key, canBePrefixMap) -> {
                        if (canBePrefixMap && containsPrefixMap(this.confData, key)
                                || this.confData.containsKey(key)) {
                            return Optional.of(true);
                        }
                        return Optional.empty();
                    };
            return applyWithOption(configOption, applier).orElse(false);
        }
    }

    public  T get(ConfigOption option) {
        return getOptional(option).orElseGet(option::defaultValue);
    }

    public  Optional getOptional(ConfigOption option) {
        Optional rawValue = getRawValueFromOption(option);
        Class clazz = option.getClazz();

        try {
            if (option.isList()) {
                return rawValue.map(v -> ConfigurationUtils.convertToList(v, clazz));
            } else {
                return rawValue.map(v -> ConfigurationUtils.convertValue(v, clazz));
            }
        } catch (Exception e) {
            throw new IllegalArgumentException(
                    String.format(
                            "Could not parse value '%s' for key '%s'.",
                            rawValue.map(Object::toString).orElse(""), option.key()),
                    e);
        }
    }

    public  Configuration set(ConfigOption option, T value) {
        final boolean canBePrefixMap = canBePrefixMap(option);
        setValueInternal(option.key(), value, canBePrefixMap);
        return this;
    }

    /**
     * Returns the keys of all key/value pairs stored inside this configuration object.
     *
     * @return the keys of all key/value pairs stored inside this configuration object
     */
    public Set keySet() {
        synchronized (this.confData) {
            return new HashSet<>(this.confData.keySet());
        }
    }

    public Map toMap() {
        synchronized (this.confData) {
            Map ret = new HashMap<>(this.confData.size());
            for (Map.Entry entry : confData.entrySet()) {
                ret.put(entry.getKey(), ConfigurationUtils.convertToString(entry.getValue()));
            }
            return ret;
        }
    }

    /**
     * Removes given config option from the configuration.
     *
     * @param configOption config option to remove
     * @param  Type of the config option
     * @return true is config has been removed, false otherwise
     */
    public  boolean remove(ConfigOption configOption) {
        synchronized (this.confData) {
            final BiFunction> applier =
                    (key, canBePrefixMap) -> {
                        if (canBePrefixMap && removePrefixMap(this.confData, key)
                                || this.confData.remove(key) != null) {
                            return Optional.of(true);
                        }
                        return Optional.empty();
                    };
            return applyWithOption(configOption, applier).orElse(false);
        }
    }

    // --------------------------------------------------------------------------------------------

    private  void setValueInternal(String key, T value, boolean canBePrefixMap) {
        if (key == null) {
            throw new NullPointerException("Key must not be null.");
        }
        if (value == null) {
            throw new NullPointerException("Value must not be null.");
        }

        synchronized (this.confData) {
            if (canBePrefixMap) {
                removePrefixMap(this.confData, key);
            }
            this.confData.put(key, value);
        }
    }

    private Optional getRawValue(String key, boolean canBePrefixMap) {
        if (key == null) {
            throw new NullPointerException("Key must not be null.");
        }

        synchronized (this.confData) {
            final Object valueFromExactKey = this.confData.get(key);
            if (!canBePrefixMap || valueFromExactKey != null) {
                return Optional.ofNullable(valueFromExactKey);
            }
            final Map valueFromPrefixMap =
                    convertToPropertiesPrefixed(confData, key);
            if (valueFromPrefixMap.isEmpty()) {
                return Optional.empty();
            }
            return Optional.of(valueFromPrefixMap);
        }
    }

    /**
     * This method will do the following steps to get the value of a config option:
     *
     * 

1. get the value from {@link Configuration}.
* 2. if key is not found, try to get the value with fallback keys from {@link Configuration} *
* 3. if no fallback keys are found, return {@link Optional#empty()}.
* * @return the value of the configuration or {@link Optional#empty()}. */ private Optional getRawValueFromOption(ConfigOption configOption) { return applyWithOption(configOption, this::getRawValue); } private void loggingFallback(FallbackKey fallbackKey, ConfigOption configOption) { if (fallbackKey.isDeprecated()) { LOG.warn( "Config uses deprecated configuration key '{}' instead of proper key '{}'", fallbackKey.getKey(), configOption.key()); } else { LOG.info( "Config uses fallback configuration key '{}' instead of key '{}'", fallbackKey.getKey(), configOption.key()); } } private Optional applyWithOption( ConfigOption option, BiFunction> applier) { final boolean canBePrefixMap = canBePrefixMap(option); final Optional valueFromExactKey = applier.apply(option.key(), canBePrefixMap); if (valueFromExactKey.isPresent()) { return valueFromExactKey; } else if (option.hasFallbackKeys()) { // try the fallback keys for (FallbackKey fallbackKey : option.fallbackKeys()) { final Optional valueFromFallbackKey = applier.apply(fallbackKey.getKey(), canBePrefixMap); if (valueFromFallbackKey.isPresent()) { loggingFallback(fallbackKey, option); return valueFromFallbackKey; } } } return Optional.empty(); } public Set getKeys() { return confData.keySet(); } @Override public int hashCode() { int hash = 0; for (String s : this.confData.keySet()) { hash ^= s.hashCode(); } return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } else if (obj instanceof Configuration) { Map otherConf = ((Configuration) obj).confData; for (Map.Entry e : this.confData.entrySet()) { Object thisVal = e.getValue(); Object otherVal = otherConf.get(e.getKey()); if (!thisVal.getClass().equals(byte[].class)) { if (!thisVal.equals(otherVal)) { return false; } } else if (otherVal.getClass().equals(byte[].class)) { if (!Arrays.equals((byte[]) thisVal, (byte[]) otherVal)) { return false; } } else { return false; } } return true; } else { return false; } } @Override public String toString() { return this.confData.toString(); } }