Skip to content

Commit 4b8fa3e

Browse files
committed
Pass InputStream when creating XContent parser (#28754)
* Pass InputStream when creating XContent parser Rather than passing the raw `BytesReference` in when creating the xcontent parser, this passes the StreamInput (which is an InputStream), this allows us to decouple XContent from BytesReference. This also removes the use of `commons.Booleans` so it doesn't require more external commons classes. Related to #28504 * Undo boolean removal * Enhance deprecation javadoc
1 parent 08cd1b7 commit 4b8fa3e

File tree

21 files changed

+42
-34
lines changed

21 files changed

+42
-34
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void execute(IngestDocument document) throws Exception {
7878
Object fieldValue = document.getFieldValue(field, Object.class);
7979
BytesReference bytesRef = (fieldValue == null) ? new BytesArray("null") : new BytesArray(fieldValue.toString());
8080
try (XContentParser parser = JsonXContent.jsonXContent
81-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytesRef)) {
81+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytesRef.streamInput())) {
8282
XContentParser.Token token = parser.nextToken();
8383
Object value = null;
8484
if (token == XContentParser.Token.VALUE_NULL) {

modules/reindex/src/main/java/org/elasticsearch/index/reindex/AbstractBulkByQueryRestHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private XContentParser extractRequestSpecificFields(RestRequest restRequest,
9191
}
9292
}
9393
return parser.contentType().xContent().createParser(parser.getXContentRegistry(),
94-
parser.getDeprecationHandler(), builder.map(body).bytes());
94+
parser.getDeprecationHandler(), builder.map(body).bytes().streamInput());
9595
}
9696
}
9797
}

modules/reindex/src/main/java/org/elasticsearch/index/reindex/RestReindexAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexReq
7575
XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType());
7676
builder.map(source);
7777
try (XContentParser innerParser = parser.contentType().xContent()
78-
.createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), builder.bytes())) {
78+
.createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), builder.bytes().streamInput())) {
7979
request.getSearchRequest().source().parseXContent(innerParser);
8080
}
8181
};

modules/reindex/src/main/java/org/elasticsearch/index/reindex/TransportReindexAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ protected RequestWrapper<IndexRequest> buildRequest(ScrollableHitSource.Hit doc)
339339
if (mainRequestXContentType != null && doc.getXContentType() != mainRequestXContentType) {
340340
// we need to convert
341341
try (XContentParser parser = sourceXContentType.xContent()
342-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, doc.getSource());
342+
.createParser(NamedXContentRegistry.EMPTY,
343+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, doc.getSource().streamInput());
343344
XContentBuilder builder = XContentBuilder.builder(mainRequestXContentType.xContent())) {
344345
parser.nextToken();
345346
builder.copyCurrentStructure(parser);

server/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
307307
// EMPTY is safe here because we never call namedObject
308308
try (XContentParser parser = xContent
309309
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE,
310-
data.slice(from, nextMarker - from))) {
310+
data.slice(from, nextMarker - from).streamInput())) {
311311
// move pointers
312312
from = nextMarker + 1;
313313

@@ -432,7 +432,7 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
432432
.parent(parent);
433433
// EMPTY is safe here because we never call namedObject
434434
try (XContentParser sliceParser = xContent.createParser(NamedXContentRegistry.EMPTY,
435-
LoggingDeprecationHandler.INSTANCE, sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType))) {
435+
LoggingDeprecationHandler.INSTANCE, sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType).streamInput())) {
436436
updateRequest.fromXContent(sliceParser);
437437
}
438438
if (fetchSourceContext != null) {

server/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ public static void readMultiLineFormat(BytesReference data,
209209
// now parse the action
210210
if (nextMarker - from > 0) {
211211
try (XContentParser parser = xContent
212-
.createParser(registry, LoggingDeprecationHandler.INSTANCE, data.slice(from, nextMarker - from))) {
212+
.createParser(registry, LoggingDeprecationHandler.INSTANCE,
213+
data.slice(from, nextMarker - from).streamInput())) {
213214
Map<String, Object> source = parser.map();
214215
for (Map.Entry<String, Object> entry : source.entrySet()) {
215216
Object value = entry.getValue();
@@ -245,7 +246,7 @@ public static void readMultiLineFormat(BytesReference data,
245246
break;
246247
}
247248
BytesReference bytes = data.slice(from, nextMarker - from);
248-
try (XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, bytes)) {
249+
try (XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, bytes.streamInput())) {
249250
consumer.accept(searchRequest, parser);
250251
}
251252
// move pointers

server/src/main/java/org/elasticsearch/common/xcontent/XContent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ XContentParser createParser(NamedXContentRegistry xContentRegistry,
106106

107107
/**
108108
* Creates a parser over the provided bytes.
109+
* @deprecated use {@link #createParser(NamedXContentRegistry, DeprecationHandler, InputStream)} instead,
110+
* the BytesReference coupling in this class will be removed in a future commit
109111
*/
112+
@Deprecated
110113
XContentParser createParser(NamedXContentRegistry xContentRegistry,
111114
DeprecationHandler deprecationHandler, BytesReference bytes) throws IOException;
112115

server/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ protected ScoreFunction doToFunction(QueryShardContext context) throws IOExcepti
183183
AbstractDistanceScoreFunction scoreFunction;
184184
// EMPTY is safe because parseVariable doesn't use namedObject
185185
try (XContentParser parser = XContentFactory.xContent(functionBytes)
186-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, functionBytes)) {
186+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, functionBytes.streamInput())) {
187187
scoreFunction = parseVariable(fieldName, parser, context, multiValueMode);
188188
}
189189
return scoreFunction;

server/src/main/java/org/elasticsearch/rest/RestRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public NamedXContentRegistry getXContentRegistry() {
343343
*/
344344
public final XContentParser contentParser() throws IOException {
345345
BytesReference content = requiredContent(); // will throw exception if body or content type missing
346-
return xContentType.get().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content);
346+
return xContentType.get().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content.streamInput());
347347
}
348348

349349
/**
@@ -372,7 +372,7 @@ public final boolean hasContentOrSourceParam() {
372372
*/
373373
public final XContentParser contentOrSourceParamParser() throws IOException {
374374
Tuple<XContentType, BytesReference> tuple = contentOrSourceParam();
375-
return tuple.v1().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, tuple.v2());
375+
return tuple.v1().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, tuple.v2().streamInput());
376376
}
377377

378378
/**
@@ -386,7 +386,7 @@ public final void withContentOrSourceParamParserOrNull(CheckedConsumer<XContentP
386386
BytesReference content = tuple.v2();
387387
XContentType xContentType = tuple.v1();
388388
try (XContentParser parser = xContentType.xContent()
389-
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content)) {
389+
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content.streamInput())) {
390390
withParser.accept(parser);
391391
}
392392
} else {

server/src/main/java/org/elasticsearch/script/Script.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public static Script parse(Settings settings) {
283283
settings.toXContent(builder, ToXContent.EMPTY_PARAMS);
284284
builder.endObject();
285285
return parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
286-
LoggingDeprecationHandler.INSTANCE, builder.bytes()));
286+
LoggingDeprecationHandler.INSTANCE, builder.bytes().streamInput()));
287287
} catch (IOException e) {
288288
// it should not happen since we are not actually reading from a stream but an in-memory byte[]
289289
throw new IllegalStateException(e);

0 commit comments

Comments
 (0)