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

org.apache.hudi.common.config.HoodieConfig Maven / Gradle / Ivy

There is a newer version: 1.0.0-beta2
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.hudi.common.config;

import org.apache.hudi.common.util.Option;
import org.apache.hudi.common.util.ReflectionUtils;
import org.apache.hudi.common.util.StringUtils;
import org.apache.hudi.exception.HoodieException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.Serializable;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * This class deals with {@link ConfigProperty} and provides get/set functionalities.
 */
public class HoodieConfig implements Serializable {

  private static final Logger LOG = LogManager.getLogger(HoodieConfig.class);

  protected static final String CONFIG_VALUES_DELIMITER = ",";

  protected TypedProperties props;

  public HoodieConfig() {
    this.props = new TypedProperties();
  }

  public HoodieConfig(Properties props) {
    this.props = new TypedProperties(props);
  }

  public  void setValue(ConfigProperty cfg, String val) {
    cfg.checkValues(val);
    props.setProperty(cfg.key(), val);
  }

  public  void setValue(String key, String val) {
    props.setProperty(key, val);
  }

  public void setAll(Properties properties) {
    props.putAll(properties);
  }

  /**
   * Sets the default value of a config if user does not set it already.
   * The default value can only be set if the config property has a built-in
   * default value or an infer function.  When the infer function is present,
   * the infer function is used first to derive the config value based on other
   * configs.  If the config value cannot be inferred, the built-in default value
   * is used if present.
   *
   * @param configProperty Config to set a default value.
   * @param             Data type of the config.
   */
  public  void setDefaultValue(ConfigProperty configProperty) {
    if (!contains(configProperty)) {
      Option inferValue = Option.empty();
      if (configProperty.hasInferFunction()) {
        inferValue = configProperty.getInferFunction().get().apply(this);
      }
      if (inferValue.isPresent() || configProperty.hasDefaultValue()) {
        props.setProperty(
            configProperty.key(),
            inferValue.isPresent()
                ? inferValue.get().toString()
                : configProperty.defaultValue().toString());
      }
    }
  }

  public  void setDefaultValue(ConfigProperty configProperty, T defaultVal) {
    if (!contains(configProperty)) {
      props.setProperty(configProperty.key(), defaultVal.toString());
    }
  }

  public Boolean contains(String key) {
    return props.containsKey(key);
  }

  public  boolean contains(ConfigProperty configProperty) {
    if (props.containsKey(configProperty.key())) {
      return true;
    }
    return configProperty.getAlternatives().stream().anyMatch(props::containsKey);
  }

  private  Option getRawValue(ConfigProperty configProperty) {
    if (props.containsKey(configProperty.key())) {
      return Option.ofNullable(props.get(configProperty.key()));
    }
    for (String alternative : configProperty.getAlternatives()) {
      if (props.containsKey(alternative)) {
        LOG.warn(String.format("The configuration key '%s' has been deprecated "
                + "and may be removed in the future. Please use the new key '%s' instead.",
            alternative, configProperty.key()));
        return Option.ofNullable(props.get(alternative));
      }
    }
    return Option.empty();
  }

  protected void setDefaults(String configClassName) {
    Class configClass = ReflectionUtils.getClass(configClassName);
    Arrays.stream(configClass.getDeclaredFields())
        .filter(f -> Modifier.isStatic(f.getModifiers()))
        .filter(f -> f.getType().isAssignableFrom(ConfigProperty.class))
        .forEach(f -> {
          try {
            ConfigProperty cfgProp = (ConfigProperty) f.get("null");
            if (cfgProp.hasDefaultValue() || cfgProp.hasInferFunction()) {
              setDefaultValue(cfgProp);
            }
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          }
        });
  }

  public  String getString(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(Object::toString).orElse(null);
  }

  public  List getSplitStrings(ConfigProperty configProperty) {
    return getSplitStrings(configProperty, ",");
  }

  public  List getSplitStrings(ConfigProperty configProperty, String delimiter) {
    return StringUtils.split(getString(configProperty), delimiter);
  }

  public String getString(String key) {
    return props.getProperty(key);
  }

  public  Integer getInt(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Integer.parseInt(v.toString())).orElse(null);
  }

  public  Integer getIntOrDefault(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Integer.parseInt(v.toString()))
        .orElse(Integer.parseInt(configProperty.defaultValue().toString()));
  }

  public  Boolean getBoolean(ConfigProperty configProperty) {
    if (configProperty.hasDefaultValue()) {
      return getBooleanOrDefault(configProperty);
    }
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Boolean.parseBoolean(v.toString())).orElse(null);
  }

  public boolean getBooleanOrDefault(String key, boolean defaultVal) {
    return Option.ofNullable(props.getProperty(key)).map(Boolean::parseBoolean).orElse(defaultVal);
  }

  public  boolean getBooleanOrDefault(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Boolean.parseBoolean(v.toString()))
            .orElseGet(() -> Boolean.parseBoolean(configProperty.defaultValue().toString()));
  }

  public  boolean getBooleanOrDefault(ConfigProperty configProperty, boolean defaultVal) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Boolean.parseBoolean(v.toString())).orElse(defaultVal);
  }

  public  Long getLong(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Long.parseLong(v.toString())).orElse(null);
  }

  public  Long getLongOrDefault(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Long.parseLong(v.toString()))
            .orElseGet(() -> Long.parseLong(configProperty.defaultValue().toString()));
  }

  public  Float getFloat(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Float.parseFloat(v.toString())).orElse(null);
  }

  public  Float getFloatOrDefault(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Float.parseFloat(v.toString()))
            .orElseGet(() -> Float.parseFloat(configProperty.defaultValue().toString()));
  }

  public  Double getDouble(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Double.parseDouble(v.toString())).orElse(null);
  }

  public  Double getDoubleOrDefault(ConfigProperty configProperty) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(v -> Double.parseDouble(v.toString()))
            .orElseGet(() -> Double.parseDouble(configProperty.defaultValue().toString()));
  }

  public  String getStringOrDefault(ConfigProperty configProperty) {
    return getStringOrDefault(configProperty, configProperty.defaultValue().toString());
  }

  public  String getStringOrDefault(ConfigProperty configProperty, String defaultVal) {
    Option rawValue = getRawValue(configProperty);
    return rawValue.map(Object::toString).orElse(defaultVal);
  }

  public TypedProperties getProps() {
    return getProps(false);
  }

  public TypedProperties getProps(boolean includeGlobalProps) {
    if (includeGlobalProps) {
      TypedProperties mergedProps = DFSPropertiesConfiguration.getGlobalProps();
      mergedProps.putAll(props);
      return mergedProps;
    } else {
      return props;
    }
  }

  public void setDefaultOnCondition(boolean condition, HoodieConfig config) {
    if (condition) {
      setDefault(config);
    }
  }

  public void setDefault(HoodieConfig config) {
    props.putAll(config.getProps());
  }

  public  String getStringOrThrow(ConfigProperty configProperty, String errorMessage) throws HoodieException {
    Option rawValue = getRawValue(configProperty);
    if (rawValue.isPresent()) {
      return rawValue.get().toString();
    } else {
      throw new HoodieException(errorMessage);
    }
  }
}