Skip to content

Commit 17e9cd5

Browse files
authored
Remove unused xContent serialization logic in transport objects. (#49346)
Previously, request and response objects related to index creation and mappings were used in both the transport layer and HLRC. Now that they are no longer shared, we can remove the extra xContent serialization + deserialization logic.
1 parent 1593da0 commit 17e9cd5

File tree

7 files changed

+91
-261
lines changed

7 files changed

+91
-261
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
import org.elasticsearch.common.io.stream.StreamInput;
2727
import org.elasticsearch.common.io.stream.StreamOutput;
2828
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
29-
import org.elasticsearch.common.xcontent.ObjectParser;
3029
import org.elasticsearch.common.xcontent.ToXContentFragment;
3130
import org.elasticsearch.common.xcontent.ToXContentObject;
3231
import org.elasticsearch.common.xcontent.XContentBuilder;
3332
import org.elasticsearch.common.xcontent.XContentHelper;
34-
import org.elasticsearch.common.xcontent.XContentParser;
3533
import org.elasticsearch.common.xcontent.XContentType;
3634
import org.elasticsearch.index.mapper.Mapper;
3735

@@ -42,8 +40,6 @@
4240
import java.util.Objects;
4341

4442
import static java.util.Collections.unmodifiableMap;
45-
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
46-
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
4743

4844
/**
4945
* Response object for {@link GetFieldMappingsRequest} API
@@ -55,32 +51,6 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
5551

5652
private static final ParseField MAPPINGS = new ParseField("mappings");
5753

58-
private static final ObjectParser<Map<String, Map<String, FieldMappingMetaData>>, String> PARSER =
59-
new ObjectParser<>(MAPPINGS.getPreferredName(), true, HashMap::new);
60-
61-
static {
62-
PARSER.declareField((p, typeMappings, index) -> {
63-
p.nextToken();
64-
while (p.currentToken() == XContentParser.Token.FIELD_NAME) {
65-
final String typeName = p.currentName();
66-
67-
if (p.nextToken() == XContentParser.Token.START_OBJECT) {
68-
final Map<String, FieldMappingMetaData> typeMapping = new HashMap<>();
69-
typeMappings.put(typeName, typeMapping);
70-
71-
while (p.nextToken() == XContentParser.Token.FIELD_NAME) {
72-
final String fieldName = p.currentName();
73-
final FieldMappingMetaData fieldMappingMetaData = FieldMappingMetaData.fromXContent(p);
74-
typeMapping.put(fieldName, fieldMappingMetaData);
75-
}
76-
} else {
77-
p.skipChildren();
78-
}
79-
p.nextToken();
80-
}
81-
}, MAPPINGS, ObjectParser.ValueType.OBJECT);
82-
}
83-
8454
// TODO remove the middle `type` level of this
8555
private final Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings;
8656

@@ -178,17 +148,6 @@ public static class FieldMappingMetaData implements ToXContentFragment {
178148
a -> new FieldMappingMetaData((String)a[0], (BytesReference)a[1])
179149
);
180150

181-
static {
182-
PARSER.declareField(optionalConstructorArg(),
183-
(p, c) -> p.text(), FULL_NAME, ObjectParser.ValueType.STRING);
184-
PARSER.declareField(optionalConstructorArg(),
185-
(p, c) -> {
186-
final XContentBuilder jsonBuilder = jsonBuilder().copyCurrentStructure(p);
187-
final BytesReference bytes = BytesReference.bytes(jsonBuilder);
188-
return bytes;
189-
}, MAPPING, ObjectParser.ValueType.OBJECT);
190-
}
191-
192151
private String fullName;
193152
private BytesReference source;
194153

@@ -215,10 +174,6 @@ BytesReference getSource() {
215174
return source;
216175
}
217176

218-
public static FieldMappingMetaData fromXContent(XContentParser parser) throws IOException {
219-
return PARSER.parse(parser, null);
220-
}
221-
222177
@Override
223178
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
224179
builder.field(FULL_NAME.getPreferredName(), fullName);

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequest.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.elasticsearch.common.bytes.BytesReference;
3333
import org.elasticsearch.common.io.stream.StreamInput;
3434
import org.elasticsearch.common.io.stream.StreamOutput;
35-
import org.elasticsearch.common.xcontent.ToXContentObject;
3635
import org.elasticsearch.common.xcontent.XContentBuilder;
3736
import org.elasticsearch.common.xcontent.XContentFactory;
3837
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -41,7 +40,6 @@
4140
import org.elasticsearch.index.mapper.MapperService;
4241

4342
import java.io.IOException;
44-
import java.io.InputStream;
4543
import java.io.UncheckedIOException;
4644
import java.util.Arrays;
4745
import java.util.Map;
@@ -60,7 +58,7 @@
6058
* @see org.elasticsearch.client.IndicesAdminClient#putMapping(PutMappingRequest)
6159
* @see AcknowledgedResponse
6260
*/
63-
public class PutMappingRequest extends AcknowledgedRequest<PutMappingRequest> implements IndicesRequest.Replaceable, ToXContentObject {
61+
public class PutMappingRequest extends AcknowledgedRequest<PutMappingRequest> implements IndicesRequest.Replaceable {
6462

6563
private static ObjectHashSet<String> RESERVED_FIELDS = ObjectHashSet.from(
6664
"_uid", "_id", "_type", "_source", "_all", "_analyzer", "_parent", "_routing", "_index",
@@ -306,16 +304,4 @@ public void writeTo(StreamOutput out) throws IOException {
306304
out.writeOptionalWriteable(concreteIndex);
307305
out.writeOptionalString(origin);
308306
}
309-
310-
@Override
311-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
312-
if (source != null) {
313-
try (InputStream stream = new BytesArray(source).streamInput()) {
314-
builder.rawValue(stream, XContentType.JSON);
315-
}
316-
} else {
317-
builder.startObject().endObject();
318-
}
319-
return builder;
320-
}
321307
}

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,14 @@
2323
import org.elasticsearch.common.ParseField;
2424
import org.elasticsearch.common.io.stream.StreamInput;
2525
import org.elasticsearch.common.io.stream.StreamOutput;
26-
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
27-
import org.elasticsearch.common.xcontent.ObjectParser;
2826
import org.elasticsearch.common.xcontent.ToXContentObject;
2927
import org.elasticsearch.common.xcontent.XContentBuilder;
30-
import org.elasticsearch.common.xcontent.XContentParser;
3128

3229
import java.io.IOException;
3330
import java.util.HashMap;
3431
import java.util.Map;
3532
import java.util.Objects;
3633

37-
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
38-
3934

4035
/**
4136
* Response object for {@link RolloverRequest} API
@@ -51,20 +46,6 @@ public final class RolloverResponse extends ShardsAcknowledgedResponse implement
5146
private static final ParseField ROLLED_OVER = new ParseField("rolled_over");
5247
private static final ParseField CONDITIONS = new ParseField("conditions");
5348

54-
@SuppressWarnings("unchecked")
55-
private static final ConstructingObjectParser<RolloverResponse, Void> PARSER = new ConstructingObjectParser<>("rollover",
56-
true, args -> new RolloverResponse((String) args[0], (String) args[1], (Map<String,Boolean>) args[2],
57-
(Boolean)args[3], (Boolean)args[4], (Boolean) args[5], (Boolean) args[6]));
58-
59-
static {
60-
PARSER.declareField(constructorArg(), (parser, context) -> parser.text(), OLD_INDEX, ObjectParser.ValueType.STRING);
61-
PARSER.declareField(constructorArg(), (parser, context) -> parser.text(), NEW_INDEX, ObjectParser.ValueType.STRING);
62-
PARSER.declareObject(constructorArg(), (parser, context) -> parser.map(), CONDITIONS);
63-
PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), DRY_RUN, ObjectParser.ValueType.BOOLEAN);
64-
PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ROLLED_OVER, ObjectParser.ValueType.BOOLEAN);
65-
declareAcknowledgedAndShardsAcknowledgedFields(PARSER);
66-
}
67-
6849
private final String oldIndex;
6950
private final String newIndex;
7051
private final Map<String, Boolean> conditionStatus;
@@ -168,10 +149,6 @@ protected void addCustomFields(XContentBuilder builder, Params params) throws IO
168149
builder.endObject();
169150
}
170151

171-
public static RolloverResponse fromXContent(XContentParser parser) {
172-
return PARSER.apply(parser, null);
173-
}
174-
175152
@Override
176153
public boolean equals(Object o) {
177154
if (super.equals(o)) {

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,13 @@
3535
import org.elasticsearch.common.io.stream.StreamOutput;
3636
import org.elasticsearch.common.logging.DeprecationLogger;
3737
import org.elasticsearch.common.settings.Settings;
38-
import org.elasticsearch.common.xcontent.DeprecationHandler;
3938
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
4039
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
41-
import org.elasticsearch.common.xcontent.ToXContentObject;
4240
import org.elasticsearch.common.xcontent.XContentBuilder;
4341
import org.elasticsearch.common.xcontent.XContentFactory;
4442
import org.elasticsearch.common.xcontent.XContentHelper;
4543
import org.elasticsearch.common.xcontent.XContentParser;
4644
import org.elasticsearch.common.xcontent.XContentType;
47-
import org.elasticsearch.common.xcontent.json.JsonXContent;
4845
import org.elasticsearch.common.xcontent.support.XContentMapValues;
4946

5047
import java.io.IOException;
@@ -58,14 +55,14 @@
5855
import java.util.stream.Collectors;
5956

6057
import static org.elasticsearch.action.ValidateActions.addValidationError;
58+
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6159
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
6260
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
63-
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6461

6562
/**
6663
* A request to create an index template.
6764
*/
68-
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContentObject {
65+
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest {
6966

7067
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(PutIndexTemplateRequest.class));
7168

@@ -339,7 +336,7 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
339336
List<String> elements = ((List<?>) entry.getValue()).stream().map(Object::toString).collect(Collectors.toList());
340337
patterns(elements);
341338
} else {
342-
throw new IllegalArgumentException("Malformed [template] value, should be a string or a list of strings");
339+
throw new IllegalArgumentException("Malformed [index_patterns] value, should be a string or a list of strings");
343340
}
344341
} else if (name.equals("order")) {
345342
order(XContentMapValues.nodeIntegerValue(entry.getValue(), order()));
@@ -490,38 +487,4 @@ public void writeTo(StreamOutput out) throws IOException {
490487
}
491488
out.writeOptionalVInt(version);
492489
}
493-
494-
@Override
495-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
496-
builder.startObject();
497-
{
498-
builder.field("index_patterns", indexPatterns);
499-
builder.field("order", order);
500-
if (version != null) {
501-
builder.field("version", version);
502-
}
503-
504-
builder.startObject("settings");
505-
settings.toXContent(builder, params);
506-
builder.endObject();
507-
508-
builder.startObject("mappings");
509-
for (Map.Entry<String, String> entry : mappings.entrySet()) {
510-
builder.field(entry.getKey());
511-
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
512-
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue())) {
513-
builder.copyCurrentStructure(parser);
514-
}
515-
}
516-
builder.endObject();
517-
518-
builder.startObject("aliases");
519-
for (Alias alias : aliases) {
520-
alias.toXContent(builder, params);
521-
}
522-
builder.endObject();
523-
}
524-
builder.endObject();
525-
return builder;
526-
}
527490
}

server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequestTests.java

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,10 @@
2020
package org.elasticsearch.action.admin.indices.mapping.put;
2121

2222
import org.elasticsearch.action.ActionRequestValidationException;
23-
import org.elasticsearch.common.Strings;
24-
import org.elasticsearch.common.bytes.BytesReference;
25-
import org.elasticsearch.common.xcontent.XContentBuilder;
26-
import org.elasticsearch.common.xcontent.XContentParser;
2723
import org.elasticsearch.common.xcontent.XContentType;
28-
import org.elasticsearch.common.xcontent.json.JsonXContent;
2924
import org.elasticsearch.index.Index;
30-
import org.elasticsearch.index.RandomCreateIndexGenerator;
3125
import org.elasticsearch.test.ESTestCase;
3226

33-
import java.io.IOException;
34-
35-
import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
36-
3727
public class PutMappingRequestTests extends ESTestCase {
3828

3929
public void testValidation() {
@@ -69,61 +59,4 @@ public void testBuildFromSimplifiedDef() {
6959
() -> PutMappingRequest.buildFromSimplifiedDef("type", "only_field"));
7060
assertEquals("mapping source must be pairs of fieldnames and properties definition.", e.getMessage());
7161
}
72-
73-
public void testToXContent() throws IOException {
74-
PutMappingRequest request = new PutMappingRequest("foo");
75-
76-
XContentBuilder mapping = JsonXContent.contentBuilder().startObject();
77-
mapping.startObject("properties");
78-
mapping.startObject("email");
79-
mapping.field("type", "text");
80-
mapping.endObject();
81-
mapping.endObject();
82-
mapping.endObject();
83-
request.source(mapping);
84-
85-
String actualRequestBody = Strings.toString(request);
86-
String expectedRequestBody = "{\"properties\":{\"email\":{\"type\":\"text\"}}}";
87-
assertEquals(expectedRequestBody, actualRequestBody);
88-
}
89-
90-
public void testToXContentWithEmptySource() throws IOException {
91-
PutMappingRequest request = new PutMappingRequest("foo");
92-
93-
String actualRequestBody = Strings.toString(request);
94-
String expectedRequestBody = "{}";
95-
assertEquals(expectedRequestBody, actualRequestBody);
96-
}
97-
98-
public void testToAndFromXContent() throws IOException {
99-
100-
final PutMappingRequest putMappingRequest = createTestItem();
101-
102-
boolean humanReadable = randomBoolean();
103-
final XContentType xContentType = randomFrom(XContentType.values());
104-
BytesReference originalBytes = toShuffledXContent(putMappingRequest, xContentType, EMPTY_PARAMS, humanReadable);
105-
106-
PutMappingRequest parsedPutMappingRequest = new PutMappingRequest();
107-
parsedPutMappingRequest.source(originalBytes, xContentType);
108-
109-
assertMappingsEqual(putMappingRequest.source(), parsedPutMappingRequest.source());
110-
}
111-
112-
private void assertMappingsEqual(String expected, String actual) throws IOException {
113-
114-
try (XContentParser expectedJson = createParser(XContentType.JSON.xContent(), expected);
115-
XContentParser actualJson = createParser(XContentType.JSON.xContent(), actual)) {
116-
assertEquals(expectedJson.mapOrdered(), actualJson.mapOrdered());
117-
}
118-
}
119-
120-
/**
121-
* Returns a random {@link PutMappingRequest}.
122-
*/
123-
private static PutMappingRequest createTestItem() throws IOException {
124-
String index = randomAlphaOfLength(5);
125-
PutMappingRequest request = new PutMappingRequest(index);
126-
request.source(RandomCreateIndexGenerator.randomMapping("_doc"));
127-
return request;
128-
}
12962
}

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponseTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919

2020
package org.elasticsearch.action.admin.indices.rollover;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.common.io.stream.Writeable;
2324
import org.elasticsearch.common.unit.ByteSizeValue;
2425
import org.elasticsearch.common.unit.TimeValue;
25-
import org.elasticsearch.common.xcontent.XContentParser;
26-
import org.elasticsearch.test.AbstractSerializingTestCase;
26+
import org.elasticsearch.test.AbstractWireTestCase;
2727

28+
import java.io.IOException;
2829
import java.util.ArrayList;
2930
import java.util.HashMap;
3031
import java.util.List;
3132
import java.util.Map;
32-
import java.util.function.Predicate;
3333
import java.util.function.Supplier;
3434

35-
public class RolloverResponseTests extends AbstractSerializingTestCase<RolloverResponse> {
35+
public class RolloverResponseTests extends AbstractWireTestCase<RolloverResponse> {
3636

3737
@Override
3838
protected RolloverResponse createTestInstance() {
@@ -66,13 +66,8 @@ protected Writeable.Reader<RolloverResponse> instanceReader() {
6666
}
6767

6868
@Override
69-
protected RolloverResponse doParseInstance(XContentParser parser) {
70-
return RolloverResponse.fromXContent(parser);
71-
}
72-
73-
@Override
74-
protected Predicate<String> getRandomFieldsExcludeFilter() {
75-
return field -> field.startsWith("conditions");
69+
protected RolloverResponse copyInstance(RolloverResponse instance, Version version) throws IOException {
70+
return copyWriteable(instance, writableRegistry(), RolloverResponse::new);
7671
}
7772

7873
@Override

0 commit comments

Comments
 (0)