Improve Settings#get lookup for camel case support

Today, if we miss on a get on setting, we try its camel case version. The assumption is that in our code, we never use camel case to lookup a setting, but we want to support camel case if the user provided one.
This can be expensive (#toCamelCase) when the get on the setting is done in a tight call, which is evident when running the allocation deciders as part of the reroute logic.
Instead of doing the camel case check on get, prepare an additional map that includes all the settings that are provided as came case, and try and lookup from it if needed.
closes #6765
This commit is contained in:
Shay Banon 2014-07-07 11:06:32 +02:00
parent 9abb7c45b4
commit ecde4e0c8e
2 changed files with 23 additions and 7 deletions

View File

@ -57,10 +57,22 @@ public class ImmutableSettings implements Settings {
public static final Settings EMPTY = new Builder().build();
private ImmutableMap<String, String> settings;
private final ImmutableMap<String, String> forcedUnderscoreSettings;
private transient ClassLoader classLoader;
ImmutableSettings(Map<String, String> settings, ClassLoader classLoader) {
this.settings = ImmutableMap.copyOf(settings);
Map<String, String> forcedUnderscoreSettings = null;
for (Map.Entry<String, String> entry : settings.entrySet()) {
String toUnderscoreCase = Strings.toUnderscoreCase(entry.getKey());
if (!toUnderscoreCase.equals(entry.getKey())) {
if (forcedUnderscoreSettings == null) {
forcedUnderscoreSettings = new HashMap<>();
}
forcedUnderscoreSettings.put(toUnderscoreCase, entry.getValue());
}
}
this.forcedUnderscoreSettings = forcedUnderscoreSettings == null ? ImmutableMap.<String, String>of() : ImmutableMap.copyOf(forcedUnderscoreSettings);
this.classLoader = classLoader;
}
@ -212,18 +224,13 @@ public class ImmutableSettings implements Settings {
if (retVal != null) {
return retVal;
}
// try camel case version
return settings.get(toCamelCase(setting));
return forcedUnderscoreSettings.get(setting);
}
@Override
public String get(String[] settings) {
for (String setting : settings) {
String retVal = this.settings.get(setting);
if (retVal != null) {
return retVal;
}
retVal = this.settings.get(toCamelCase(setting));
String retVal = get(setting);
if (retVal != null) {
return retVal;
}

View File

@ -35,6 +35,15 @@ import static org.hamcrest.Matchers.*;
*/
public class ImmutableSettingsTests extends ElasticsearchTestCase {
@Test
public void testCamelCaseSupport() {
Settings settings = settingsBuilder()
.put("test.camelCase", "bar")
.build();
assertThat(settings.get("test.camelCase"), equalTo("bar"));
assertThat(settings.get("test.camel_case"), equalTo("bar"));
}
@Test
public void testGetAsClass() {
Settings settings = settingsBuilder()