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;
}
/**
* 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
*
@ -1176,7 +1167,7 @@ public final class Settings implements ToXContentFragment {
public Builder putProperties(final Map<String, String> esSettings, final Function<String, String> keyFunction) {
for (final Map.Entry<String, String> esSetting : esSettings.entrySet()) {
final String key = esSetting.getKey();
map.put(keyFunction.apply(key), esSetting.getValue());
put(keyFunction.apply(key), esSetting.getValue());
}
return this;
}

View File

@ -82,6 +82,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
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();
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<>();
for (String ignoredSetting : ignoreSettings) {
if (!Regex.isSimpleMatchPattern(ignoredSetting)) {
if (UNREMOVABLE_SETTINGS.contains(ignoredSetting)) {
throw new SnapshotRestoreException(snapshot, "cannot remove setting [" + ignoredSetting + "] on restore");
} else {
settingsMap.remove(ignoredSetting);
keyFilters.add(ignoredSetting);
}
} else {
simpleMatchPatterns.add(ignoredSetting);
}
}
if (!simpleMatchPatterns.isEmpty()) {
String[] removePatterns = simpleMatchPatterns.toArray(new String[simpleMatchPatterns.size()]);
Iterator<Map.Entry<String, String>> iterator = settingsMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if (UNREMOVABLE_SETTINGS.contains(entry.getKey()) == false) {
if (Regex.simpleMatch(removePatterns, entry.getKey())) {
iterator.remove();
Predicate<String> settingsFilter = k -> {
if (UNREMOVABLE_SETTINGS.contains(k) == false) {
for (String filterKey : keyFilters) {
if (k.equals(filterKey)) {
return false;
}
}
for (String pattern : simpleMatchPatterns) {
if (Regex.simpleMatch(pattern, k)) {
return false;
}
}
}
}
for(Map.Entry<String, String> entry : normalizedChangeSettings.getAsMap().entrySet()) {
if (UNMODIFIABLE_SETTINGS.contains(entry.getKey())) {
throw new SnapshotRestoreException(snapshot, "cannot modify setting [" + entry.getKey() + "] on restore");
} else {
settingsMap.put(entry.getKey(), entry.getValue());
}
}
return builder.settings(Settings.builder().put(settingsMap)).build();
return true;
};
Settings.Builder settingsBuilder = Settings.builder()
.put(settings.filter(settingsFilter))
.put(normalizedChangeSettings.filter(k -> {
if (UNMODIFIABLE_SETTINGS.contains(k)) {
throw new SnapshotRestoreException(snapshot, "cannot modify setting [" + k + "] on restore");
} else {
return true;
}
}));
return builder.settings(settingsBuilder).build();
}
private void restoreGlobalStateIfRequested(MetaData.Builder mdBuilder) {

View File

@ -69,7 +69,9 @@ public class ListPluginsCommandTests extends ESTestCase {
int status = new ListPluginsCommand() {
@Override
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"));
}

View File

@ -52,7 +52,9 @@ abstract class ESElasticsearchCliTestCase extends ESTestCase {
final int status = Elasticsearch.main(args, new Elasticsearch() {
@Override
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"));
}
@Override