Skip to content

Commit 5e55991

Browse files
authored
Simplify DocumentParser handling (#63800)
DocumentMapperParser holds a reference to the xcontent registry, only with the purpose of being retrieved in DocumentParser#parsedDocument . This can be greatly simplified by making DocumentParser take the registry as a constructor argument, and removing the same reference from DocumentMapperParser. This may also allow for further refactorings around how DocumentMapperParser is used. With this change it is MapperService that creates DocumentParser and exposes it so that DocumentMapper can use it.
1 parent f42d179 commit 5e55991

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ public static class Builder {
5959
private final IndexSettings indexSettings;
6060
private final IndexAnalyzers indexAnalyzers;
6161
private final DocumentMapperParser documentMapperParser;
62+
private final DocumentParser documentParser;
6263

6364
private Map<String, Object> meta;
6465

6566
public Builder(RootObjectMapper.Builder builder, MapperService mapperService) {
6667
this.indexSettings = mapperService.getIndexSettings();
6768
this.indexAnalyzers = mapperService.getIndexAnalyzers();
6869
this.documentMapperParser = mapperService.documentMapperParser();
70+
this.documentParser = mapperService.documentParser();
6971
this.builderContext = new Mapper.BuilderContext(indexSettings.getSettings(), new ContentPath(1));
7072
this.rootObjectMapper = builder.build(builderContext);
7173

@@ -106,7 +108,7 @@ public DocumentMapper build() {
106108
rootObjectMapper,
107109
metadataMappers.values().toArray(new MetadataFieldMapper[0]),
108110
meta);
109-
return new DocumentMapper(indexSettings, documentMapperParser, indexAnalyzers, mapping);
111+
return new DocumentMapper(indexSettings, documentMapperParser, indexAnalyzers, documentParser, mapping);
110112
}
111113
}
112114

@@ -125,14 +127,15 @@ public DocumentMapper build() {
125127
private DocumentMapper(IndexSettings indexSettings,
126128
DocumentMapperParser documentMapperParser,
127129
IndexAnalyzers indexAnalyzers,
130+
DocumentParser documentParser,
128131
Mapping mapping) {
129132
this.type = mapping.root().name();
130133
this.typeText = new Text(this.type);
131134
this.mapping = mapping;
132135
this.documentMapperParser = documentMapperParser;
136+
this.documentParser = documentParser;
133137
this.indexSettings = indexSettings;
134138
this.indexAnalyzers = indexAnalyzers;
135-
this.documentParser = new DocumentParser();
136139
this.fieldMappers = MappingLookup.fromMapping(this.mapping, indexAnalyzers.getDefaultIndexAnalyzer());
137140

138141
try {
@@ -279,7 +282,7 @@ public ObjectMapper findNestedObjectMapper(int nestedDocId, SearchContext sc, Le
279282

280283
public DocumentMapper merge(Mapping mapping, MergeReason reason) {
281284
Mapping merged = this.mapping.merge(mapping, reason);
282-
return new DocumentMapper(this.indexSettings, this.documentMapperParser, this.indexAnalyzers, merged);
285+
return new DocumentMapper(this.indexSettings, this.documentMapperParser, this.indexAnalyzers, this.documentParser, merged);
283286
}
284287

285288
public void validate(IndexSettings settings, boolean checkLimits) {

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.common.Nullable;
2424
import org.elasticsearch.common.compress.CompressedXContent;
2525
import org.elasticsearch.common.time.DateFormatter;
26-
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2726
import org.elasticsearch.common.xcontent.XContentHelper;
2827
import org.elasticsearch.common.xcontent.XContentType;
2928
import org.elasticsearch.index.IndexSettings;
@@ -41,23 +40,21 @@
4140
public class DocumentMapperParser {
4241

4342
final MapperService mapperService;
44-
private final NamedXContentRegistry xContentRegistry;
4543
private final SimilarityService similarityService;
4644
private final Supplier<QueryShardContext> queryShardContextSupplier;
47-
4845
private final RootObjectMapper.TypeParser rootObjectTypeParser = new RootObjectMapper.TypeParser();
49-
5046
private final Version indexVersionCreated;
51-
5247
private final Map<String, Mapper.TypeParser> typeParsers;
5348
private final Map<String, MetadataFieldMapper.TypeParser> rootTypeParsers;
5449
private final ScriptService scriptService;
5550

56-
public DocumentMapperParser(IndexSettings indexSettings, MapperService mapperService, NamedXContentRegistry xContentRegistry,
57-
SimilarityService similarityService, MapperRegistry mapperRegistry,
58-
Supplier<QueryShardContext> queryShardContextSupplier, ScriptService scriptService) {
51+
public DocumentMapperParser(IndexSettings indexSettings,
52+
MapperService mapperService,
53+
SimilarityService similarityService,
54+
MapperRegistry mapperRegistry,
55+
Supplier<QueryShardContext> queryShardContextSupplier,
56+
ScriptService scriptService) {
5957
this.mapperService = mapperService;
60-
this.xContentRegistry = xContentRegistry;
6158
this.similarityService = similarityService;
6259
this.queryShardContextSupplier = queryShardContextSupplier;
6360
this.scriptService = scriptService;
@@ -173,8 +170,4 @@ private static String getRemainingFields(Map<?, ?> map) {
173170
}
174171
return remainingFields.toString();
175172
}
176-
177-
NamedXContentRegistry getXContentRegistry() {
178-
return xContentRegistry;
179-
}
180173
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.common.settings.Settings;
2929
import org.elasticsearch.common.time.DateFormatter;
3030
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
31+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3132
import org.elasticsearch.common.xcontent.XContentHelper;
3233
import org.elasticsearch.common.xcontent.XContentParser;
3334
import org.elasticsearch.common.xcontent.XContentType;
@@ -45,6 +46,12 @@
4546
/** A parser for documents, given mappings from a DocumentMapper */
4647
final class DocumentParser {
4748

49+
private final NamedXContentRegistry xContentRegistry;
50+
51+
DocumentParser(NamedXContentRegistry xContentRegistry) {
52+
this.xContentRegistry = xContentRegistry;
53+
}
54+
4855
ParsedDocument parseDocument(SourceToParse source,
4956
MetadataFieldMapper[] metadataFieldsMappers,
5057
DocumentMapper docMapper) throws MapperParsingException {
@@ -53,7 +60,7 @@ ParsedDocument parseDocument(SourceToParse source,
5360
final ParseContext.InternalParseContext context;
5461
final XContentType xContentType = source.getXContentType();
5562

56-
try (XContentParser parser = XContentHelper.createParser(docMapper.documentMapperParser().getXContentRegistry(),
63+
try (XContentParser parser = XContentHelper.createParser(xContentRegistry,
5764
LoggingDeprecationHandler.INSTANCE, source.source(), xContentType)) {
5865
context = new ParseContext.InternalParseContext(docMapper, source, parser);
5966
validateStart(parser);

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,24 @@ public enum MergeReason {
103103
Setting.longSetting("index.mapping.field_name_length.limit", Long.MAX_VALUE, 1L, Property.Dynamic, Property.IndexScope);
104104

105105
private final IndexAnalyzers indexAnalyzers;
106-
107-
private volatile DocumentMapper mapper;
108-
109-
private final DocumentMapperParser documentParser;
106+
private final DocumentMapperParser documentMapperParser;
107+
private final DocumentParser documentParser;
110108
private final Version indexVersionCreated;
111-
112109
private final MapperAnalyzerWrapper indexAnalyzer;
113-
114110
final MapperRegistry mapperRegistry;
115-
116111
private final BooleanSupplier idFieldDataEnabled;
117112

113+
private volatile DocumentMapper mapper;
114+
118115
public MapperService(IndexSettings indexSettings, IndexAnalyzers indexAnalyzers, NamedXContentRegistry xContentRegistry,
119116
SimilarityService similarityService, MapperRegistry mapperRegistry,
120117
Supplier<QueryShardContext> queryShardContextSupplier, BooleanSupplier idFieldDataEnabled,
121118
ScriptService scriptService) {
122119
super(indexSettings);
123120
this.indexVersionCreated = indexSettings.getIndexVersionCreated();
124121
this.indexAnalyzers = indexAnalyzers;
125-
this.documentParser = new DocumentMapperParser(indexSettings, this, xContentRegistry, similarityService, mapperRegistry,
122+
this.documentParser = new DocumentParser(xContentRegistry);
123+
this.documentMapperParser = new DocumentMapperParser(indexSettings, this, similarityService, mapperRegistry,
126124
queryShardContextSupplier, scriptService);
127125
this.indexAnalyzer = new MapperAnalyzerWrapper(indexAnalyzers.getDefaultIndexAnalyzer(), MappedFieldType::indexAnalyzer);
128126
this.mapperRegistry = mapperRegistry;
@@ -142,6 +140,10 @@ public NamedAnalyzer getNamedAnalyzer(String analyzerName) {
142140
}
143141

144142
public DocumentMapperParser documentMapperParser() {
143+
return this.documentMapperParser;
144+
}
145+
146+
DocumentParser documentParser() {
145147
return this.documentParser;
146148
}
147149

@@ -278,7 +280,7 @@ private synchronized DocumentMapper internalMerge(String type, CompressedXConten
278280
DocumentMapper documentMapper;
279281

280282
try {
281-
documentMapper = documentParser.parse(type, mappings);
283+
documentMapper = documentMapperParser.parse(type, mappings);
282284
} catch (Exception e) {
283285
throw new MapperParsingException("Failed to parse mapping: {}", e, e.getMessage());
284286
}
@@ -326,7 +328,7 @@ private boolean assertSerialization(DocumentMapper mapper) {
326328
}
327329

328330
public DocumentMapper parse(String mappingType, CompressedXContent mappingSource) throws MapperParsingException {
329-
return documentParser.parse(mappingType, mappingSource);
331+
return documentMapperParser.parse(mappingType, mappingSource);
330332
}
331333

332334
/**

0 commit comments

Comments
 (0)