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

org.apache.hudi.common.config.ConfigProperty 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.apache.hudi.common.config;

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

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

/**
 * ConfigProperty describes a configuration property. It contains the configuration
 * key, deprecated older versions of the key, and an optional default value for the configuration,
 * configuration descriptions and also an inferring mechanism to infer the configuration value
 * based on other configurations.
 *
 * @param  The type of the default value.
 */
public class ConfigProperty implements Serializable {

  private final String key;

  private final T defaultValue;

  private final String docOnDefaultValue;

  private final String doc;

  private final Option sinceVersion;

  private final Option deprecatedVersion;

  private final Set validValues;

  private final boolean advanced;

  private final String[] alternatives;

  // provide the ability to infer config value based on other configs
  private final Option>> inferFunction;

  ConfigProperty(String key, T defaultValue, String docOnDefaultValue, String doc,
                 Option sinceVersion, Option deprecatedVersion,
                 Option>> inferFunc, Set validValues,
                 boolean advanced, String... alternatives) {
    this.key = Objects.requireNonNull(key);
    this.defaultValue = defaultValue;
    this.docOnDefaultValue = docOnDefaultValue;
    this.doc = doc;
    this.sinceVersion = sinceVersion;
    this.deprecatedVersion = deprecatedVersion;
    this.inferFunction = inferFunc;
    this.validValues = validValues;
    this.advanced = advanced;
    this.alternatives = alternatives;
  }

  public String key() {
    return key;
  }

  public T defaultValue() {
    if (defaultValue == null) {
      throw new HoodieException(String.format("There's no default value for this config: %s", key));
    }
    return defaultValue;
  }

  public boolean hasDefaultValue() {
    return defaultValue != null;
  }

  public String getDocOnDefaultValue() {
    return StringUtils.isNullOrEmpty(docOnDefaultValue)
        ? StringUtils.EMPTY_STRING : docOnDefaultValue;
  }

  public String doc() {
    return StringUtils.isNullOrEmpty(doc) ? StringUtils.EMPTY_STRING : doc;
  }

  public Option getSinceVersion() {
    return sinceVersion;
  }

  public Option getDeprecatedVersion() {
    return deprecatedVersion;
  }

  public boolean hasInferFunction() {
    return getInferFunction().isPresent();
  }

  Option>> getInferFunction() {
    return inferFunction;
  }

  public void checkValues(String value) {
    if (!isValid(value)) {
      throw new IllegalArgumentException(
          "The value of " + key + " should be one of "
              + String.join(",", validValues) + ", but was " + value);
    }
  }

  private boolean isValid(String value) {
    return validValues == null || validValues.isEmpty() || validValues.contains(value.toUpperCase());
  }

  public List getAlternatives() {
    return Arrays.asList(alternatives);
  }

  public boolean isAdvanced() {
    return advanced;
  }

  public ConfigProperty withDocumentation(String doc) {
    Objects.requireNonNull(doc);
    return new ConfigProperty<>(key, defaultValue, docOnDefaultValue, doc, sinceVersion, deprecatedVersion, inferFunction, validValues, advanced, alternatives);
  }

  public > ConfigProperty withDocumentation(Class e) {
    return withDocumentation(e, "");
  }

  private > boolean isDefaultField(Class e, Field f) {
    if (!hasDefaultValue()) {
      return false;
    }
    if (defaultValue() instanceof String) {
      return f.getName().equals(((String) defaultValue()).toUpperCase());
    }
    return Enum.valueOf(e, f.getName()).equals(defaultValue());
  }

  public > ConfigProperty withDocumentation(Class e, String doc) {
    Objects.requireNonNull(e);
    StringBuilder sb = new StringBuilder();
    if (StringUtils.nonEmpty(doc)) {
      sb.append(doc);
      sb.append('\n');
    }
    sb.append(e.getName());
    sb.append(": ");
    EnumDescription description =  e.getAnnotation(EnumDescription.class);
    Objects.requireNonNull(description);
    sb.append(description.value());
    for (Field f: e.getFields()) {
      if (f.isEnumConstant() && isValid(f.getName())) {
        EnumFieldDescription fieldDescription = f.getAnnotation(EnumFieldDescription.class);
        Objects.requireNonNull(fieldDescription);
        sb.append("\n    ");
        sb.append(f.getName());
        if (isDefaultField(e, f)) {
          sb.append("(default)");
        }
        sb.append(": ");
        sb.append(fieldDescription.value());
      }
    }

    return new ConfigProperty<>(key, defaultValue, docOnDefaultValue, sb.toString(), sinceVersion, deprecatedVersion, inferFunction, validValues, advanced, alternatives);
  }

  public ConfigProperty withValidValues(String... validValues) {
    Objects.requireNonNull(validValues);
    return new ConfigProperty<>(key, defaultValue, docOnDefaultValue, doc, sinceVersion, deprecatedVersion, inferFunction, new HashSet<>(Arrays.asList(validValues)), advanced, alternatives);
  }

  public ConfigProperty withAlternatives(String... alternatives) {
    Objects.requireNonNull(alternatives);
    return new ConfigProperty<>(key, defaultValue, docOnDefaultValue, doc, sinceVersion, deprecatedVersion, inferFunction, validValues, advanced, alternatives);
  }

  public ConfigProperty sinceVersion(String sinceVersion) {
    Objects.requireNonNull(sinceVersion);
    return new ConfigProperty<>(key, defaultValue, docOnDefaultValue, doc, Option.of(sinceVersion), deprecatedVersion, inferFunction, validValues, advanced, alternatives);
  }

  public ConfigProperty deprecatedAfter(String deprecatedVersion) {
    Objects.requireNonNull(deprecatedVersion);
    return new ConfigProperty<>(key, defaultValue, docOnDefaultValue, doc, sinceVersion, Option.of(deprecatedVersion), inferFunction, validValues, advanced, alternatives);
  }

  public ConfigProperty withInferFunction(Function> inferFunction) {
    Objects.requireNonNull(inferFunction);
    return new ConfigProperty<>(key, defaultValue, docOnDefaultValue, doc, sinceVersion, deprecatedVersion, Option.of(inferFunction), validValues, advanced, alternatives);
  }

  /**
   * Marks the config as an advanced config.
   */
  public ConfigProperty markAdvanced() {
    return new ConfigProperty<>(key, defaultValue, docOnDefaultValue, doc, sinceVersion, deprecatedVersion, inferFunction, validValues, true, alternatives);
  }

  /**
   * Create a OptionBuilder with key.
   *
   * @param key The key of the option
   * @return Return a OptionBuilder.
   */
  public static PropertyBuilder key(String key) {
    Objects.requireNonNull(key);
    return new PropertyBuilder(key);
  }

  @Override
  public String toString() {
    return String.format(
        "Key: '%s' , default: %s , isAdvanced: %s , description: %s since version: %s deprecated after: %s",
        key, defaultValue, advanced, doc, sinceVersion.isPresent() ? sinceVersion.get() : "version is not defined",
        deprecatedVersion.isPresent() ? deprecatedVersion.get() : "version is not defined");
  }

  /**
   * The PropertyBuilder is used to build the ConfigProperty.
   */
  public static final class PropertyBuilder {

    private final String key;

    PropertyBuilder(String key) {
      this.key = key;
    }

    public  ConfigProperty defaultValue(T value) {
      return defaultValue(value, "");
    }

    public  ConfigProperty defaultValue(T value, String docOnDefaultValue) {
      Objects.requireNonNull(value);
      Objects.requireNonNull(docOnDefaultValue);
      ConfigProperty configProperty = new ConfigProperty<>(key, value, docOnDefaultValue, "", Option.empty(), Option.empty(), Option.empty(), Collections.emptySet(), false);
      return configProperty;
    }

    public ConfigProperty noDefaultValue() {
      return noDefaultValue("");
    }

    public ConfigProperty noDefaultValue(String docOnDefaultValue) {
      ConfigProperty configProperty = new ConfigProperty<>(key, null, docOnDefaultValue, "", Option.empty(),
          Option.empty(), Option.empty(), Collections.emptySet(), false);
      return configProperty;
    }
  }
}