Skip to content

Commit c3a3f24

Browse files
authored
Wrap metering parser decorator around dot expanding parser if necessary (elastic#138660)
1 parent 515d639 commit c3a3f24

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper;
2929
import org.elasticsearch.index.query.SearchExecutionContext;
3030
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
31-
import org.elasticsearch.plugins.internal.XContentMeteringParserDecorator;
31+
import org.elasticsearch.plugins.internal.XContentParserDecorator;
3232
import org.elasticsearch.search.lookup.LeafFieldLookupProvider;
3333
import org.elasticsearch.search.lookup.SearchLookup;
3434
import org.elasticsearch.search.lookup.Source;
@@ -37,7 +37,6 @@
3737
import org.elasticsearch.xcontent.XContentParseException;
3838
import org.elasticsearch.xcontent.XContentParser;
3939
import org.elasticsearch.xcontent.XContentParserConfiguration;
40-
import org.elasticsearch.xcontent.XContentType;
4140

4241
import java.io.IOException;
4342
import java.util.ArrayList;
@@ -85,19 +84,22 @@ public ParsedDocument parseDocument(SourceToParse source, MappingLookup mappingL
8584
throw new DocumentParsingException(new XContentLocation(0, 0), "failed to parse, document is empty");
8685
}
8786
final RootDocumentParserContext context;
88-
final XContentType xContentType = source.getXContentType();
8987

90-
XContentMeteringParserDecorator meteringParserDecorator = source.getMeteringParserDecorator();
9188
try (
92-
XContentParser parser = meteringParserDecorator.decorate(
89+
// the context needs to be closed after parsing to make sure the actual parser is properly closed;
90+
// closing won't impact other state of the context
91+
RootDocumentParserContext ctx = new RootDocumentParserContext(
92+
mappingLookup,
93+
mappingParserContext,
94+
source,
9395
XContentHelper.createParser(
9496
parserConfiguration.withIncludeSourceOnError(source.getIncludeSourceOnError()),
9597
source.source(),
96-
xContentType
98+
source.getXContentType()
9799
)
98100
)
99101
) {
100-
context = new RootDocumentParserContext(mappingLookup, mappingParserContext, source, parser);
102+
context = ctx;
101103
validateStart(context.parser());
102104
MetadataFieldMapper[] metadataFieldsMappers = mappingLookup.getMapping().getSortedMetadataMappers();
103105
internalParseDocument(metadataFieldsMappers, context);
@@ -121,7 +123,7 @@ public ParsedDocument parseDocument(SourceToParse source, MappingLookup mappingL
121123
context.sourceToParse().source(),
122124
context.sourceToParse().getXContentType(),
123125
dynamicUpdate,
124-
meteringParserDecorator.meteredDocumentSize()
126+
source.getMeteringParserDecorator().meteredDocumentSize()
125127
) {
126128
@Override
127129
public String documentDescription() {
@@ -1085,7 +1087,7 @@ public ObjectMapper merge(Mapper mergeWith, MapperMergeContext mapperMergeContex
10851087
* Internal version of {@link DocumentParserContext} that is aware of implementation details like nested documents
10861088
* and how they are stored in the lucene index.
10871089
*/
1088-
private static class RootDocumentParserContext extends DocumentParserContext {
1090+
private static class RootDocumentParserContext extends DocumentParserContext implements AutoCloseable {
10891091
private final ContentPath path = new ContentPath();
10901092
private final XContentParser parser;
10911093
private final LuceneDocument document;
@@ -1120,17 +1122,23 @@ private static class RootDocumentParserContext extends DocumentParserContext {
11201122
this.tsid = tsid;
11211123
assert this.tsid == null || indexSettings.getMode() == IndexMode.TIME_SERIES
11221124
: "tsid should only be set for time series indices";
1125+
XContentParserDecorator parserDecorator = source.getMeteringParserDecorator();
11231126
if (mappingLookup.getMapping().getRoot().subobjects() == ObjectMapper.Subobjects.ENABLED) {
1124-
this.parser = DotExpandingXContentParser.expandDots(parser, this.path);
1127+
this.parser = parserDecorator.decorate(DotExpandingXContentParser.expandDots(parser, this.path));
11251128
} else {
1126-
this.parser = parser;
1129+
this.parser = parserDecorator.decorate(parser);
11271130
}
11281131
this.document = new LuceneDocument();
11291132
this.documents.add(document);
11301133
this.maxAllowedNumNestedDocs = indexSettings().getMappingNestedDocsLimit();
11311134
this.numNestedDocs = 0L;
11321135
}
11331136

1137+
@Override
1138+
public void close() throws IOException {
1139+
parser.close();
1140+
}
1141+
11341142
@Override
11351143
public Mapper getMapper(String name) {
11361144
Mapper mapper = getMetadataMapper(name);

server/src/main/java/org/elasticsearch/index/mapper/DotExpandingXContentParser.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,14 @@ public XContentString optimizedTextOrNull() throws IOException {
389389
return super.optimizedTextOrNull();
390390
}
391391

392+
@Override
393+
public XContentString optimizedText() throws IOException {
394+
if (state == State.EXPANDING_START_OBJECT) {
395+
throw new IllegalStateException("Can't get text on a " + currentToken() + " at " + getTokenLocation());
396+
}
397+
return super.optimizedText();
398+
}
399+
392400
@Override
393401
public String textOrNull() throws IOException {
394402
if (state == State.EXPANDING_START_OBJECT) {
@@ -397,6 +405,14 @@ public String textOrNull() throws IOException {
397405
return super.textOrNull();
398406
}
399407

408+
@Override
409+
public String text() throws IOException {
410+
if (state == State.EXPANDING_START_OBJECT) {
411+
throw new IllegalStateException("Can't get text on a " + currentToken() + " at " + getTokenLocation());
412+
}
413+
return super.text();
414+
}
415+
400416
@Override
401417
public Number numberValue() throws IOException {
402418
if (state == State.EXPANDING_START_OBJECT) {

0 commit comments

Comments
 (0)