Remove `Settings,put(Map<String,String>)` (#26785)

`Map<String,String>` is basically erasing the type while other methods on
the `Settings.Builder` are type safe and have corresponding `get` methods.
This commit is contained in:
Simon Willnauer 2017-09-26 12:15:20 +02:00 committed by GitHub
parent 74473c1c3d
commit a506ba8602
4 changed files with 33 additions and 32 deletions

View File

@ -1073,15 +1073,6 @@ public final class Settings implements ToXContentFragment {
return this; return this;
} }
/**
* Sets all the provided settings.
*/
public Builder put(Map<String, String> settings) {
removeNonArraysFieldsIfNewSettingsContainsFieldAsArray(settings);
map.putAll(settings);
return this;
}
/** /**
* Removes non array values from the existing map, if settings contains an array value instead * Removes non array values from the existing map, if settings contains an array value instead
* *
@ -1176,7 +1167,7 @@ public final class Settings implements ToXContentFragment {
public Builder putProperties(final Map<String, String> esSettings, final Function<String, String> keyFunction) { public Builder putProperties(final Map<String, String> esSettings, final Function<String, String> keyFunction) {
for (final Map.Entry<String, String> esSetting : esSettings.entrySet()) { for (final Map.Entry<String, String> esSetting : esSettings.entrySet()) {
final String key = esSetting.getKey(); final String key = esSetting.getKey();
map.put(keyFunction.apply(key), esSetting.getValue()); put(keyFunction.apply(key), esSetting.getValue());
} }
return this; return this;
} }

View File

@ -82,6 +82,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.Collections.unmodifiableSet; import static java.util.Collections.unmodifiableSet;
@ -386,40 +387,45 @@ public class RestoreService extends AbstractComponent implements ClusterStateApp
} }
Settings normalizedChangeSettings = Settings.builder().put(changeSettings).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX).build(); Settings normalizedChangeSettings = Settings.builder().put(changeSettings).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX).build();
IndexMetaData.Builder builder = IndexMetaData.builder(indexMetaData); IndexMetaData.Builder builder = IndexMetaData.builder(indexMetaData);
Map<String, String> settingsMap = new HashMap<>(indexMetaData.getSettings().getAsMap()); Settings settings = indexMetaData.getSettings();
Set<String> keyFilters = new HashSet<>();
List<String> simpleMatchPatterns = new ArrayList<>(); List<String> simpleMatchPatterns = new ArrayList<>();
for (String ignoredSetting : ignoreSettings) { for (String ignoredSetting : ignoreSettings) {
if (!Regex.isSimpleMatchPattern(ignoredSetting)) { if (!Regex.isSimpleMatchPattern(ignoredSetting)) {
if (UNREMOVABLE_SETTINGS.contains(ignoredSetting)) { if (UNREMOVABLE_SETTINGS.contains(ignoredSetting)) {
throw new SnapshotRestoreException(snapshot, "cannot remove setting [" + ignoredSetting + "] on restore"); throw new SnapshotRestoreException(snapshot, "cannot remove setting [" + ignoredSetting + "] on restore");
} else { } else {
settingsMap.remove(ignoredSetting); keyFilters.add(ignoredSetting);
} }
} else { } else {
simpleMatchPatterns.add(ignoredSetting); simpleMatchPatterns.add(ignoredSetting);
} }
} }
if (!simpleMatchPatterns.isEmpty()) { Predicate<String> settingsFilter = k -> {
String[] removePatterns = simpleMatchPatterns.toArray(new String[simpleMatchPatterns.size()]); if (UNREMOVABLE_SETTINGS.contains(k) == false) {
Iterator<Map.Entry<String, String>> iterator = settingsMap.entrySet().iterator(); for (String filterKey : keyFilters) {
while (iterator.hasNext()) { if (k.equals(filterKey)) {
Map.Entry<String, String> entry = iterator.next(); return false;
if (UNREMOVABLE_SETTINGS.contains(entry.getKey()) == false) { }
if (Regex.simpleMatch(removePatterns, entry.getKey())) { }
iterator.remove(); for (String pattern : simpleMatchPatterns) {
if (Regex.simpleMatch(pattern, k)) {
return false;
} }
} }
} }
} return true;
for(Map.Entry<String, String> entry : normalizedChangeSettings.getAsMap().entrySet()) { };
if (UNMODIFIABLE_SETTINGS.contains(entry.getKey())) { Settings.Builder settingsBuilder = Settings.builder()
throw new SnapshotRestoreException(snapshot, "cannot modify setting [" + entry.getKey() + "] on restore"); .put(settings.filter(settingsFilter))
} else { .put(normalizedChangeSettings.filter(k -> {
settingsMap.put(entry.getKey(), entry.getValue()); if (UNMODIFIABLE_SETTINGS.contains(k)) {
} throw new SnapshotRestoreException(snapshot, "cannot modify setting [" + k + "] on restore");
} } else {
return true;
return builder.settings(Settings.builder().put(settingsMap)).build(); }
}));
return builder.settings(settingsBuilder).build();
} }
private void restoreGlobalStateIfRequested(MetaData.Builder mdBuilder) { private void restoreGlobalStateIfRequested(MetaData.Builder mdBuilder) {

View File

@ -69,7 +69,9 @@ public class ListPluginsCommandTests extends ESTestCase {
int status = new ListPluginsCommand() { int status = new ListPluginsCommand() {
@Override @Override
protected Environment createEnv(Terminal terminal, Map<String, String> settings) throws UserException { protected Environment createEnv(Terminal terminal, Map<String, String> settings) throws UserException {
final Settings realSettings = Settings.builder().put("path.home", home).put(settings).build(); Settings.Builder builder = Settings.builder().put("path.home", home);
settings.forEach((k,v) -> builder.put(k, v));
final Settings realSettings = builder.build();
return new Environment(realSettings, home.resolve("config")); return new Environment(realSettings, home.resolve("config"));
} }

View File

@ -52,7 +52,9 @@ abstract class ESElasticsearchCliTestCase extends ESTestCase {
final int status = Elasticsearch.main(args, new Elasticsearch() { final int status = Elasticsearch.main(args, new Elasticsearch() {
@Override @Override
protected Environment createEnv(final Terminal terminal, final Map<String, String> settings) throws UserException { protected Environment createEnv(final Terminal terminal, final Map<String, String> settings) throws UserException {
final Settings realSettings = Settings.builder().put("path.home", home).put(settings).build(); Settings.Builder builder = Settings.builder().put("path.home", home);
settings.forEach((k,v) -> builder.put(k, v));
final Settings realSettings = builder.build();
return new Environment(realSettings, home.resolve("config")); return new Environment(realSettings, home.resolve("config"));
} }
@Override @Override