From be01e8fe19c822fe30ba1cff4d9e96c3f567e4c7 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Thu, 17 May 2012 01:00:59 +0300 Subject: [PATCH] Index Templates settings provided in a config file fails to load properly, closes #1960. --- .../cluster/metadata/IndexMetaData.java | 10 +-- .../metadata/IndexTemplateMetaData.java | 10 +-- .../cluster/metadata/MetaData.java | 10 +-- .../settings/loader/SettingsLoader.java | 69 +++++++++++++++++- .../settings/loader/YamlSettingsLoader.java | 70 +------------------ 5 files changed, 75 insertions(+), 94 deletions(-) diff --git a/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java index 929aebf3602..37806228cb4 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java @@ -35,6 +35,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.settings.loader.SettingsLoader; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; @@ -562,14 +563,7 @@ public class IndexMetaData { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { if ("settings".equals(currentFieldName)) { - ImmutableSettings.Builder settingsBuilder = settingsBuilder(); - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - String key = parser.currentName(); - token = parser.nextToken(); - String value = parser.text(); - settingsBuilder.put(key, value); - } - builder.settings(settingsBuilder.build()); + builder.settings(ImmutableSettings.settingsBuilder().put(SettingsLoader.Helper.loadNestedFromMap(parser.mapOrdered()))); } else if ("mappings".equals(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { diff --git a/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaData.java index 729152e85ff..a0058238717 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaData.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.settings.loader.SettingsLoader; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; @@ -282,14 +283,7 @@ public class IndexTemplateMetaData { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { if ("settings".equals(currentFieldName)) { - ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder(); - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - String key = parser.currentName(); - token = parser.nextToken(); - String value = parser.text(); - settingsBuilder.put(key, value); - } - builder.settings(settingsBuilder.build()); + builder.settings(ImmutableSettings.settingsBuilder().put(SettingsLoader.Helper.loadNestedFromMap(parser.mapOrdered()))); } else if ("mappings".equals(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 76b6fc797a0..0a0a80d1450 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -32,6 +32,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.settings.loader.SettingsLoader; import org.elasticsearch.common.xcontent.*; import org.elasticsearch.index.Index; import org.elasticsearch.indices.IndexMissingException; @@ -904,14 +905,7 @@ public class MetaData implements Iterable { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { if ("settings".equals(currentFieldName)) { - ImmutableSettings.Builder settingsBuilder = settingsBuilder(); - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - String key = parser.currentName(); - token = parser.nextToken(); - String value = parser.text(); - settingsBuilder.put(key, value); - } - builder.persistentSettings(settingsBuilder.build()); + builder.persistentSettings(ImmutableSettings.settingsBuilder().put(SettingsLoader.Helper.loadNestedFromMap(parser.mapOrdered())).build()); } else if ("indices".equals(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { builder.put(IndexMetaData.Builder.fromXContent(parser), false); diff --git a/src/main/java/org/elasticsearch/common/settings/loader/SettingsLoader.java b/src/main/java/org/elasticsearch/common/settings/loader/SettingsLoader.java index 7e764062473..ddac5a2b95e 100644 --- a/src/main/java/org/elasticsearch/common/settings/loader/SettingsLoader.java +++ b/src/main/java/org/elasticsearch/common/settings/loader/SettingsLoader.java @@ -19,17 +19,82 @@ package org.elasticsearch.common.settings.loader; +import org.elasticsearch.common.Nullable; + import java.io.IOException; +import java.util.List; import java.util.Map; +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Maps.newHashMap; + /** * Provides the ability to load settings (in the form of a simple Map) from * the actual source content that represents them. - * - * */ public interface SettingsLoader { + static class Helper { + + public static Map loadNestedFromMap(@Nullable Map map) { + Map settings = newHashMap(); + if (map == null) { + return settings; + } + StringBuilder sb = new StringBuilder(); + List path = newArrayList(); + serializeMap(settings, sb, path, map); + return settings; + } + + private static void serializeMap(Map settings, StringBuilder sb, List path, Map map) { + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() instanceof Map) { + path.add((String) entry.getKey()); + serializeMap(settings, sb, path, (Map) entry.getValue()); + path.remove(path.size() - 1); + } else if (entry.getValue() instanceof List) { + path.add((String) entry.getKey()); + serializeList(settings, sb, path, (List) entry.getValue()); + path.remove(path.size() - 1); + } else { + serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue()); + } + } + } + + private static void serializeList(Map settings, StringBuilder sb, List path, List list) { + int counter = 0; + for (Object listEle : list) { + if (listEle instanceof Map) { + path.add(Integer.toString(counter)); + serializeMap(settings, sb, path, (Map) listEle); + path.remove(path.size() - 1); + } else if (listEle instanceof List) { + path.add(Integer.toString(counter)); + serializeList(settings, sb, path, (List) listEle); + path.remove(path.size() - 1); + } else { + serializeValue(settings, sb, path, Integer.toString(counter), listEle); + } + counter++; + } + } + + private static void serializeValue(Map settings, StringBuilder sb, List path, String name, Object value) { + if (value == null) { + return; + } + sb.setLength(0); + for (String pathEle : path) { + sb.append(pathEle).append('.'); + } + sb.append(name); + settings.put(sb.toString(), value.toString()); + } + } + + /** * Loads (parses) the settings from a source string. */ diff --git a/src/main/java/org/elasticsearch/common/settings/loader/YamlSettingsLoader.java b/src/main/java/org/elasticsearch/common/settings/loader/YamlSettingsLoader.java index 113063208fd..3b2ead065e3 100644 --- a/src/main/java/org/elasticsearch/common/settings/loader/YamlSettingsLoader.java +++ b/src/main/java/org/elasticsearch/common/settings/loader/YamlSettingsLoader.java @@ -23,17 +23,11 @@ import org.elasticsearch.common.io.FastByteArrayInputStream; import org.yaml.snakeyaml.Yaml; import java.io.IOException; -import java.util.List; import java.util.Map; -import static com.google.common.collect.Lists.newArrayList; -import static com.google.common.collect.Maps.newHashMap; - /** * Settings loader that loads (parses) the settings in a yaml format by flattening them * into a map. - * - * */ public class YamlSettingsLoader implements SettingsLoader { @@ -43,73 +37,13 @@ public class YamlSettingsLoader implements SettingsLoader { source = source.replace("\t", " "); Yaml yaml = new Yaml(); Map yamlMap = (Map) yaml.load(source); - StringBuilder sb = new StringBuilder(); - Map settings = newHashMap(); - if (yamlMap == null) { - return settings; - } - List path = newArrayList(); - serializeMap(settings, sb, path, yamlMap); - return settings; + return Helper.loadNestedFromMap(yamlMap); } @Override public Map load(byte[] source) throws IOException { Yaml yaml = new Yaml(); Map yamlMap = (Map) yaml.load(new FastByteArrayInputStream(source)); - StringBuilder sb = new StringBuilder(); - Map settings = newHashMap(); - if (yamlMap == null) { - return settings; - } - List path = newArrayList(); - serializeMap(settings, sb, path, yamlMap); - return settings; - } - - private void serializeMap(Map settings, StringBuilder sb, List path, Map yamlMap) { - for (Map.Entry entry : yamlMap.entrySet()) { - if (entry.getValue() instanceof Map) { - path.add((String) entry.getKey()); - serializeMap(settings, sb, path, (Map) entry.getValue()); - path.remove(path.size() - 1); - } else if (entry.getValue() instanceof List) { - path.add((String) entry.getKey()); - serializeList(settings, sb, path, (List) entry.getValue()); - path.remove(path.size() - 1); - } else { - serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue()); - } - } - } - - private void serializeList(Map settings, StringBuilder sb, List path, List yamlList) { - int counter = 0; - for (Object listEle : yamlList) { - if (listEle instanceof Map) { - path.add(Integer.toString(counter)); - serializeMap(settings, sb, path, (Map) listEle); - path.remove(path.size() - 1); - } else if (listEle instanceof List) { - path.add(Integer.toString(counter)); - serializeList(settings, sb, path, (List) listEle); - path.remove(path.size() - 1); - } else { - serializeValue(settings, sb, path, Integer.toString(counter), listEle); - } - counter++; - } - } - - private void serializeValue(Map settings, StringBuilder sb, List path, String name, Object value) { - if (value == null) { - return; - } - sb.setLength(0); - for (String pathEle : path) { - sb.append(pathEle).append('.'); - } - sb.append(name); - settings.put(sb.toString(), value.toString()); + return Helper.loadNestedFromMap(yamlMap); } }