Index Templates settings provided in a config file fails to load properly, closes #1960.

This commit is contained in:
Shay Banon 2012-05-17 01:00:59 +03:00
parent d031662da3
commit be01e8fe19
5 changed files with 75 additions and 94 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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<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.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);

View File

@ -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<String, String> loadNestedFromMap(@Nullable Map map) {
Map<String, String> settings = newHashMap();
if (map == null) {
return settings;
}
StringBuilder sb = new StringBuilder();
List<String> path = newArrayList();
serializeMap(settings, sb, path, map);
return settings;
}
private static void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> map) {
for (Map.Entry<Object, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
path.add((String) entry.getKey());
serializeMap(settings, sb, path, (Map<Object, Object>) 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<String, String> settings, StringBuilder sb, List<String> path, List list) {
int counter = 0;
for (Object listEle : list) {
if (listEle instanceof Map) {
path.add(Integer.toString(counter));
serializeMap(settings, sb, path, (Map<Object, Object>) 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<String, String> settings, StringBuilder sb, List<String> 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.
*/

View File

@ -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<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(source);
StringBuilder sb = new StringBuilder();
Map<String, String> settings = newHashMap();
if (yamlMap == null) {
return settings;
}
List<String> path = newArrayList();
serializeMap(settings, sb, path, yamlMap);
return settings;
return Helper.loadNestedFromMap(yamlMap);
}
@Override
public Map<String, String> load(byte[] source) throws IOException {
Yaml yaml = new Yaml();
Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(new FastByteArrayInputStream(source));
StringBuilder sb = new StringBuilder();
Map<String, String> settings = newHashMap();
if (yamlMap == null) {
return settings;
}
List<String> path = newArrayList();
serializeMap(settings, sb, path, yamlMap);
return settings;
}
private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
if (entry.getValue() instanceof Map) {
path.add((String) entry.getKey());
serializeMap(settings, sb, path, (Map<Object, Object>) 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<String, String> settings, StringBuilder sb, List<String> path, List yamlList) {
int counter = 0;
for (Object listEle : yamlList) {
if (listEle instanceof Map) {
path.add(Integer.toString(counter));
serializeMap(settings, sb, path, (Map<Object, Object>) 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<String, String> settings, StringBuilder sb, List<String> 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);
}
}