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