Skip to content

Commit

Permalink
Remove parsing with types
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Büscher committed Jan 7, 2019
1 parent e3988b7 commit d169030
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,23 +317,7 @@ private static ImmutableOpenMap<String, MappingMetaData> parseMappings(XContentP
return indexMappings.build();
}

private static ImmutableOpenMap<String, MappingMetaData> parseMappingsWithTypes(XContentParser parser) throws IOException {
ImmutableOpenMap.Builder<String, MappingMetaData> indexMappings = ImmutableOpenMap.builder();
// We start at START_OBJECT since parseIndexEntry ensures that
while (parser.nextToken() != Token.END_OBJECT) {
ensureExpectedToken(Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation);
parser.nextToken();
if (parser.currentToken() == Token.START_OBJECT) {
String mappingType = parser.currentName();
indexMappings.put(mappingType, new MappingMetaData(mappingType, parser.map()));
} else if (parser.currentToken() == Token.START_ARRAY) {
parser.skipChildren();
}
}
return indexMappings.build();
}

private static IndexEntry parseIndexEntry(XContentParser parser, boolean includeTypeName) throws IOException {
private static IndexEntry parseIndexEntry(XContentParser parser) throws IOException {
List<AliasMetaData> indexAliases = null;
ImmutableOpenMap<String, MappingMetaData> indexMappings = null;
Settings indexSettings = null;
Expand All @@ -348,11 +332,7 @@ private static IndexEntry parseIndexEntry(XContentParser parser, boolean include
indexAliases = parseAliases(parser);
break;
case "mappings":
if (includeTypeName) {
indexMappings = parseMappingsWithTypes(parser);
} else {
indexMappings = parseMappings(parser);
}
indexMappings = parseMappings(parser);
break;
case "settings":
indexSettings = Settings.fromXContent(parser);
Expand Down Expand Up @@ -386,10 +366,6 @@ private static class IndexEntry {
}

public static GetIndexResponse fromXContent(XContentParser parser) throws IOException {
return fromXContent(parser, false);
}

public static GetIndexResponse fromXContent(XContentParser parser, boolean legacyWithTypes) throws IOException {
ImmutableOpenMap.Builder<String, List<AliasMetaData>> aliases = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> mappings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
Expand All @@ -407,7 +383,7 @@ public static GetIndexResponse fromXContent(XContentParser parser, boolean legac
// we assume this is an index entry
String indexName = parser.currentName();
indices.add(indexName);
IndexEntry indexEntry = parseIndexEntry(parser, legacyWithTypes);
IndexEntry indexEntry = parseIndexEntry(parser);
// make the order deterministic
CollectionUtil.timSort(indexEntry.indexAliases, Comparator.comparing(AliasMetaData::alias));
aliases.put(indexName, Collections.unmodifiableList(indexEntry.indexAliases));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.RandomCreateIndexGenerator;
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import org.junit.Assert;
Expand All @@ -49,11 +45,9 @@
import java.util.List;
import java.util.function.Predicate;

import static org.elasticsearch.action.admin.indices.get.GetIndexResponse.fromXContent;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
import static org.elasticsearch.index.IndexSettings.INDEX_REFRESH_INTERVAL_SETTING;
import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER;

public class GetIndexResponseTests extends AbstractStreamableXContentTestCase<GetIndexResponse> {

Expand Down Expand Up @@ -202,24 +196,4 @@ public void testCanOutput622Response() throws IOException {

Assert.assertEquals(TEST_6_3_0_RESPONSE_BYTES, base64OfResponse);
}

/**
* test that the old response format with types can still be parsed with the special parser method
* when output is rendered with types
*/
public void testFromXContentWithTypes() throws IOException {
GetIndexResponse testInstance = this.createTestInstance(true);
try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
// this renders the response in the "old" format with types
testInstance.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap(INCLUDE_TYPE_NAME_PARAMETER, "true")));
BytesReference bytes = BytesReference.bytes(builder);
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
// this parses the output expecting the "old" format with types
GetIndexResponse parsedInstance = fromXContent(parser, true);
assertNotSame(parsedInstance, testInstance);
assertEquals(testInstance, parsedInstance);
assertEquals(testInstance.hashCode(), parsedInstance.hashCode());
}
}

}

0 comments on commit d169030

Please sign in to comment.