Skip to content

Commit 6de5818

Browse files
committed
Decouple XContentBuilder from BytesReference (#28972)
* Decouple XContentBuilder from BytesReference This commit removes all mentions of `BytesReference` from `XContentBuilder`. This is needed so that we can completely decouple the XContent code and move it into its own dependency. While this change appears large, it is due to two main changes, moving `.bytes()` and `.string()` out of XContentBuilder itself into static methods `BytesReference.bytes` and `Strings.toString` respectively. The rest of the change is code reacting to these changes (the majority of it in tests). Relates to #28504
1 parent f501139 commit 6de5818

File tree

306 files changed

+3577
-3375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

306 files changed

+3577
-3375
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
330330
}
331331
metadata.endObject();
332332

333-
BytesRef metadataSource = metadata.bytes().toBytesRef();
333+
BytesRef metadataSource = BytesReference.bytes(metadata).toBytesRef();
334334
content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length);
335335
content.write(separator);
336336
}
@@ -345,7 +345,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
345345
LoggingDeprecationHandler.INSTANCE, indexSource, indexXContentType)) {
346346
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
347347
builder.copyCurrentStructure(parser);
348-
source = builder.bytes().toBytesRef();
348+
source = BytesReference.bytes(builder).toBytesRef();
349349
}
350350
}
351351
} else if (opType == DocWriteRequest.OpType.UPDATE) {

client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ public void testBulk() throws IOException {
617617
bulkRequest.add(deleteRequest);
618618

619619
} else {
620-
BytesReference source = XContentBuilder.builder(xContentType.xContent()).startObject().field("id", i).endObject().bytes();
620+
BytesReference source = BytesReference.bytes(XContentBuilder.builder(xContentType.xContent())
621+
.startObject().field("id", i).endObject());
621622
if (opType == DocWriteRequest.OpType.INDEX) {
622623
IndexRequest indexRequest = new IndexRequest("index", "test", id).source(source, xContentType);
623624
if (erroneous) {

client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.elasticsearch.action.search.ShardSearchFailure;
5656
import org.elasticsearch.cluster.ClusterName;
5757
import org.elasticsearch.common.CheckedFunction;
58+
import org.elasticsearch.common.bytes.BytesReference;
5859
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
5960
import org.elasticsearch.common.xcontent.ToXContent;
6061
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -272,7 +273,7 @@ private static HttpEntity createBinaryEntity(XContentBuilder xContentBuilder, Co
272273
builder.startObject();
273274
builder.field("field", "value");
274275
builder.endObject();
275-
return new ByteArrayEntity(builder.bytes().toBytesRef().bytes, contentType);
276+
return new ByteArrayEntity(BytesReference.bytes(builder).toBytesRef().bytes, contentType);
276277
}
277278
}
278279

client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.action.search.SearchRequest;
3535
import org.elasticsearch.action.search.SearchResponse;
3636
import org.elasticsearch.action.search.SearchScrollRequest;
37+
import org.elasticsearch.common.Strings;
3738
import org.elasticsearch.common.unit.TimeValue;
3839
import org.elasticsearch.common.xcontent.XContentBuilder;
3940
import org.elasticsearch.index.query.MatchQueryBuilder;
@@ -478,7 +479,7 @@ public void testSearchScroll() throws Exception {
478479

479480
for (int i = 0; i < 100; i++) {
480481
XContentBuilder builder = jsonBuilder().startObject().field("field", i).endObject();
481-
HttpEntity entity = new NStringEntity(builder.string(), ContentType.APPLICATION_JSON);
482+
HttpEntity entity = new NStringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
482483
client().performRequest(HttpPut.METHOD_NAME, "test/type1/" + Integer.toString(i), Collections.emptyMap(), entity);
483484
}
484485
client().performRequest(HttpPost.METHOD_NAME, "/test/_refresh");

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,13 @@ public void testUpdate() throws Exception {
266266
assertSame(indexResponse.status(), RestStatus.CREATED);
267267

268268
XContentType xContentType = XContentType.JSON;
269-
String script = XContentBuilder.builder(xContentType.xContent())
269+
String script = Strings.toString(XContentBuilder.builder(xContentType.xContent())
270270
.startObject()
271271
.startObject("script")
272272
.field("lang", "painless")
273273
.field("code", "ctx._source.field += params.count")
274274
.endObject()
275-
.endObject().string();
275+
.endObject());
276276
HttpEntity body = new NStringEntity(script, ContentType.create(xContentType.mediaType()));
277277
Response response = client().performRequest(HttpPost.METHOD_NAME, "/_scripts/increment-field", emptyMap(), body);
278278
assertEquals(response.getStatusLine().getStatusCode(), RestStatus.OK.getStatus());

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationDocumentationIT.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.http.entity.ContentType;
3434
import org.apache.http.nio.entity.NStringEntity;
3535
import org.elasticsearch.cluster.health.ClusterHealthStatus;
36+
import org.elasticsearch.common.Strings;
3637
import org.elasticsearch.common.settings.Settings;
3738
import org.elasticsearch.common.xcontent.XContentFactory;
3839
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -75,7 +76,7 @@ public void testCreateIndex() throws IOException {
7576
.put(SETTING_NUMBER_OF_REPLICAS, 0)
7677
.build();
7778

78-
String payload = XContentFactory.jsonBuilder() // <2>
79+
String payload = Strings.toString(XContentFactory.jsonBuilder() // <2>
7980
.startObject()
8081
.startObject("settings") // <3>
8182
.value(indexSettings)
@@ -89,7 +90,7 @@ public void testCreateIndex() throws IOException {
8990
.endObject()
9091
.endObject()
9192
.endObject()
92-
.endObject().string();
93+
.endObject());
9394

9495
HttpEntity entity = new NStringEntity(payload, ContentType.APPLICATION_JSON); // <5>
9596

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorGetActionTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.ingest.common;
2121

22+
import org.elasticsearch.common.bytes.BytesReference;
2223
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2324
import org.elasticsearch.common.io.stream.StreamInput;
2425
import org.elasticsearch.common.xcontent.ToXContent;
@@ -63,7 +64,7 @@ public void testResponseToXContent() throws Exception {
6364
GrokProcessorGetAction.Response response = new GrokProcessorGetAction.Response(TEST_PATTERNS);
6465
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
6566
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
66-
Map<String, Object> converted = XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2();
67+
Map<String, Object> converted = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
6768
Map<String, String> patterns = (Map<String, String>) converted.get("patterns");
6869
assertThat(patterns.size(), equalTo(1));
6970
assertThat(patterns.get("PATTERN"), equalTo("foo"));

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.ingest.common;
2121

22+
import org.elasticsearch.common.bytes.BytesReference;
2223
import org.elasticsearch.common.xcontent.XContentBuilder;
2324
import org.elasticsearch.common.xcontent.XContentHelper;
2425
import org.elasticsearch.common.xcontent.XContentType;
@@ -48,7 +49,7 @@ public void testExecute() throws Exception {
4849

4950
Map<String, Object> randomJsonMap = RandomDocumentPicks.randomSource(random());
5051
XContentBuilder builder = JsonXContent.contentBuilder().map(randomJsonMap);
51-
String randomJson = XContentHelper.convertToJson(builder.bytes(), false, XContentType.JSON);
52+
String randomJson = XContentHelper.convertToJson(BytesReference.bytes(builder), false, XContentType.JSON);
5253
document.put(randomField, randomJson);
5354

5455
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.github.mustachejava.codes.DefaultMustache;
3131
import com.github.mustachejava.codes.IterableCode;
3232
import com.github.mustachejava.codes.WriteCode;
33+
import org.elasticsearch.common.Strings;
3334
import org.elasticsearch.common.xcontent.XContentBuilder;
3435
import org.elasticsearch.common.xcontent.XContentType;
3536

@@ -215,7 +216,7 @@ protected Function<String, String> createFunction(Object resolved) {
215216
// Do not handle as JSON
216217
return oh.stringify(resolved);
217218
}
218-
return builder.string();
219+
return Strings.toString(builder);
219220
} catch (IOException e) {
220221
throw new MustacheException("Failed to convert object to JSON", e);
221222
}

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.client.node.NodeClient;
2424
import org.elasticsearch.common.ParseField;
2525
import org.elasticsearch.common.ParsingException;
26+
import org.elasticsearch.common.Strings;
2627
import org.elasticsearch.common.settings.Settings;
2728
import org.elasticsearch.common.xcontent.ObjectParser;
2829
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -63,7 +64,7 @@ public class RestSearchTemplateAction extends BaseRestHandler {
6364
if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
6465
//convert the template to json which is the only supported XContentType (see CustomMustacheFactory#createEncoder)
6566
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
66-
request.setScript(builder.copyCurrentStructure(parser).string());
67+
request.setScript(Strings.toString(builder.copyCurrentStructure(parser)));
6768
} catch (IOException e) {
6869
throw new ParsingException(parser.getTokenLocation(), "Could not parse inline template", e);
6970
}

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.rest.RestStatus;
3131

3232
import java.io.IOException;
33+
import java.io.InputStream;
3334

3435
public class SearchTemplateResponse extends ActionResponse implements StatusToXContentObject {
3536

@@ -83,7 +84,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
8384
} else {
8485
builder.startObject();
8586
//we can assume the template is always json as we convert it before compiling it
86-
builder.rawField("template_output", source, XContentType.JSON);
87+
try (InputStream stream = source.streamInput()) {
88+
builder.rawField("template_output", stream, XContentType.JSON);
89+
}
8790
builder.endObject();
8891
}
8992
return builder;

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.action.index.IndexRequestBuilder;
2323
import org.elasticsearch.action.search.SearchRequest;
24+
import org.elasticsearch.common.Strings;
2425
import org.elasticsearch.index.IndexNotFoundException;
2526
import org.elasticsearch.plugins.Plugin;
2627
import org.elasticsearch.script.ScriptType;
@@ -61,13 +62,13 @@ public void testBasic() throws Exception {
6162
}
6263
indexRandom(true, indexRequestBuilders);
6364

64-
final String template = jsonBuilder().startObject()
65+
final String template = Strings.toString(jsonBuilder().startObject()
6566
.startObject("query")
6667
.startObject("{{query_type}}")
6768
.field("{{field_name}}", "{{field_value}}")
6869
.endObject()
6970
.endObject()
70-
.endObject().string();
71+
.endObject());
7172

7273
MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
7374

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Set;
3131

3232
import com.github.mustachejava.MustacheException;
33+
import org.elasticsearch.common.bytes.BytesReference;
3334
import org.elasticsearch.common.xcontent.XContentBuilder;
3435
import org.elasticsearch.common.xcontent.XContentHelper;
3536
import org.elasticsearch.script.ScriptEngine;
@@ -248,7 +249,7 @@ public void testEmbeddedToJSON() throws Exception {
248249
.endObject();
249250

250251
Map<String, Object> ctx =
251-
singletonMap("ctx", XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2());
252+
singletonMap("ctx", XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2());
252253

253254
assertScript("{{#ctx.bulks}}{{#toJson}}.{{/toJson}}{{/ctx.bulks}}", ctx,
254255
equalTo("{\"index\":\"index-1\",\"id\":1,\"type\":\"type-1\"}{\"index\":\"index-2\",\"id\":2,\"type\":\"type-2\"}"));
@@ -290,7 +291,7 @@ public void testEmbeddedArrayJoin() throws Exception {
290291
.endObject();
291292

292293
Map<String, Object> ctx =
293-
singletonMap("ctx", XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2());
294+
singletonMap("ctx", XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2());
294295

295296
assertScript("{{#join}}ctx.people.0.emails{{/join}}", ctx,
296297

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.action.bulk.BulkRequestBuilder;
2424
import org.elasticsearch.action.search.SearchRequest;
2525
import org.elasticsearch.common.bytes.BytesArray;
26+
import org.elasticsearch.common.bytes.BytesReference;
2627
import org.elasticsearch.common.xcontent.XContentType;
2728
import org.elasticsearch.common.xcontent.json.JsonXContent;
2829
import org.elasticsearch.plugins.Plugin;
@@ -317,7 +318,8 @@ public void testIndexedTemplateWithArray() throws Exception {
317318
assertAcked(
318319
client().admin().cluster().preparePutStoredScript()
319320
.setId("4")
320-
.setContent(jsonBuilder().startObject().field("template", multiQuery).endObject().bytes(), XContentType.JSON)
321+
.setContent(BytesReference.bytes(jsonBuilder().startObject().field("template", multiQuery).endObject()),
322+
XContentType.JSON)
321323
);
322324
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
323325
bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RangeFieldQueryStringQueryBuilderTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.lucene.search.Query;
3232
import org.apache.lucene.util.BytesRef;
3333
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
34+
import org.elasticsearch.common.Strings;
3435
import org.elasticsearch.common.compress.CompressedXContent;
3536
import org.elasticsearch.common.joda.DateMathParser;
3637
import org.elasticsearch.common.joda.Joda;
@@ -65,14 +66,14 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
6566

6667
@Override
6768
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
68-
mapperService.merge("_doc", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("_doc",
69+
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef("_doc",
6970
INTEGER_RANGE_FIELD_NAME, "type=integer_range",
7071
LONG_RANGE_FIELD_NAME, "type=long_range",
7172
FLOAT_RANGE_FIELD_NAME, "type=float_range",
7273
DOUBLE_RANGE_FIELD_NAME, "type=double_range",
7374
DATE_RANGE_FIELD_NAME, "type=date_range",
7475
IP_RANGE_FIELD_NAME, "type=ip_range"
75-
).string()), MapperService.MergeReason.MAPPING_UPDATE, false);
76+
))), MapperService.MergeReason.MAPPING_UPDATE, false);
7677

7778
}
7879

0 commit comments

Comments
 (0)