Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify + Cleanup Dead Code in Settings #37341

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 9 additions & 92 deletions server/src/main/java/org/elasticsearch/common/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.MemorySizeValue;
import org.elasticsearch.common.unit.RatioValue;
import org.elasticsearch.common.unit.SizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
Expand Down Expand Up @@ -64,6 +62,7 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.ListIterator;
Expand All @@ -75,7 +74,6 @@
import java.util.stream.Stream;

import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue;
import static org.elasticsearch.common.unit.SizeValue.parseSizeValue;
import static org.elasticsearch.common.unit.TimeValue.parseTimeValue;

/**
Expand All @@ -100,7 +98,7 @@ public final class Settings implements ToXContentFragment {
*/
private final SetOnce<Set<String>> keys = new SetOnce<>();

Settings(Map<String, Object> settings, SecureSettings secureSettings) {
private Settings(Map<String, Object> settings, SecureSettings secureSettings) {
// we use a sorted map for consistent serialization when using getAsMap()
this.settings = Collections.unmodifiableSortedMap(new TreeMap<>(settings));
this.secureSettings = secureSettings;
Expand Down Expand Up @@ -245,30 +243,6 @@ public String get(String setting, String defaultValue) {
return retVal == null ? defaultValue : retVal;
}

/**
* Returns the setting value associated with the setting key. If it does not exists,
* returns the default value provided.
*/
String get(String setting, String defaultValue, boolean isList) {
Object value = settings.get(setting);
if (value != null) {
if (value instanceof List) {
if (isList == false) {
throw new IllegalArgumentException(
"Found list type value for setting [" + setting + "] but but did not expect a list for it."
);
}
} else if (isList) {
throw new IllegalArgumentException(
"Expected list type value for setting [" + setting + "] but found [" + value.getClass() + ']'
);
}
return toString(value);
} else {
return defaultValue;
}
}

/**
* Returns the setting value (as float) associated with the setting key. If it does not exists,
* returns the default value provided.
Expand Down Expand Up @@ -382,23 +356,6 @@ public ByteSizeValue getAsMemory(String setting, String defaultValue) throws Set
return MemorySizeValue.parseBytesSizeValueOrHeapRatio(get(setting, defaultValue), setting);
}

/**
* Returns the setting value (as a RatioValue) associated with the setting key. Provided values can
* either be a percentage value (eg. 23%), or expressed as a floating point number (eg. 0.23). If
* it does not exist, parses the default value provided.
*/
public RatioValue getAsRatio(String setting, String defaultValue) throws SettingsException {
return RatioValue.parseRatioValue(get(setting, defaultValue));
}

/**
* Returns the setting value (as size) associated with the setting key. If it does not exists,
* returns the default value provided.
*/
public SizeValue getAsSize(String setting, SizeValue defaultValue) throws SettingsException {
return parseSizeValue(get(setting), defaultValue);
}

/**
* The values associated with a setting key as an immutable list.
* <p>
Expand Down Expand Up @@ -503,11 +460,7 @@ private Map<String, Settings> getGroupsInternal(String settingPrefix, boolean ig
* Returns group settings for the given setting prefix.
*/
public Map<String, Settings> getAsGroups() throws SettingsException {
return getAsGroups(false);
}

public Map<String, Settings> getAsGroups(boolean ignoreNonGrouped) throws SettingsException {
return getGroupsInternal("", ignoreNonGrouped);
return getGroupsInternal("", false);
}

/**
Expand Down Expand Up @@ -566,14 +519,12 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;

Settings that = (Settings) o;
if (settings != null ? !settings.equals(that.settings) : that.settings != null) return false;
return true;
return Objects.equals(settings, that.settings);
}

@Override
public int hashCode() {
int result = settings != null ? settings.hashCode() : 0;
return result;
return settings != null ? settings.hashCode() : 0;
}

public static Settings readSettingsFromStream(StreamInput in) throws IOException {
Expand Down Expand Up @@ -791,7 +742,7 @@ public static class Builder {
// we use a sorted map for consistent serialization when using getAsMap()
private final Map<String, Object> map = new TreeMap<>();

private SetOnce<SecureSettings> secureSettings = new SetOnce<>();
private final SetOnce<SecureSettings> secureSettings = new SetOnce<>();

private Builder() {

Expand Down Expand Up @@ -935,18 +886,6 @@ public Builder putNull(String key) {
return put(key, (String) null);
}

/**
* Sets a setting with the provided setting key and class as value.
*
* @param key The setting key
* @param clazz The setting class value
* @return The builder
*/
public Builder put(String key, Class clazz) {
map.put(key, clazz.getName());
return this;
}

/**
* Sets the setting with the provided setting key and the boolean value.
*
Expand Down Expand Up @@ -1061,22 +1000,6 @@ public Builder putList(String setting, List<String> values) {
return this;
}

/**
* Sets the setting group.
*/
public Builder put(String settingPrefix, String groupName, String[] settings, String[] values) throws SettingsException {
if (settings.length != values.length) {
throw new SettingsException("The settings length must match the value length");
}
for (int i = 0; i < settings.length; i++) {
if (values[i] == null) {
continue;
}
put(settingPrefix + "." + groupName + "." + settings[i], values[i]);
}
return this;
}

/**
* Sets all the provided settings including secure settings
*/
Expand Down Expand Up @@ -1210,18 +1133,12 @@ public String resolvePlaceholder(String placeholderName) {

@Override
public boolean shouldIgnoreMissing(String placeholderName) {
if (placeholderName.startsWith("prompt.")) {
return true;
}
return false;
return placeholderName.startsWith("prompt.");
}

@Override
public boolean shouldRemoveMissingPlaceholder(String placeholderName) {
if (placeholderName.startsWith("prompt.")) {
return false;
}
return true;
return !placeholderName.startsWith("prompt.");
}
};

Expand Down Expand Up @@ -1395,7 +1312,7 @@ public boolean containsKey(Object key) {
@Override
public int size() {
if (size == -1) {
size = Math.toIntExact(delegate.keySet().stream().filter((e) -> filter.test(e)).count());
size = Math.toIntExact(delegate.keySet().stream().filter(filter).count());
}
return size;
}
Expand Down