Create the QueryShardContext lazily in DocumentMapperParser. (#21287)

This would allow MapperService to still be usable in contexts when a
QueryShardContext cannot be obtained, for instance in the case that a
MapperService needs to be created only to merge mappings.
This commit is contained in:
Adrien Grand 2016-11-03 08:58:53 +01:00 committed by GitHub
parent 5d79eab982
commit cf667bcbd6
2 changed files with 12 additions and 6 deletions

View File

@ -69,7 +69,7 @@ public class DocumentMapperParser {
}
public Mapper.TypeParser.ParserContext parserContext(String type) {
return new Mapper.TypeParser.ParserContext(type, indexAnalyzers, similarityService::getSimilarity, mapperService, typeParsers::get, indexVersionCreated, parseFieldMatcher, queryShardContextSupplier.get());
return new Mapper.TypeParser.ParserContext(type, indexAnalyzers, similarityService::getSimilarity, mapperService, typeParsers::get, indexVersionCreated, parseFieldMatcher, queryShardContextSupplier);
}
public DocumentMapper parse(@Nullable String type, CompressedXContent source) throws MapperParsingException {

View File

@ -20,7 +20,6 @@
package org.elasticsearch.index.mapper;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
@ -31,6 +30,7 @@ import org.elasticsearch.index.similarity.SimilarityProvider;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
public abstract class Mapper implements ToXContent, Iterable<Mapper> {
@ -93,11 +93,13 @@ public abstract class Mapper implements ToXContent, Iterable<Mapper> {
private final ParseFieldMatcher parseFieldMatcher;
private final QueryShardContext queryShardContext;
private final Supplier<QueryShardContext> queryShardContextSupplier;
private QueryShardContext queryShardContext;
public ParserContext(String type, IndexAnalyzers indexAnalyzers, Function<String, SimilarityProvider> similarityLookupService,
MapperService mapperService, Function<String, TypeParser> typeParsers,
Version indexVersionCreated, ParseFieldMatcher parseFieldMatcher, QueryShardContext queryShardContext) {
Version indexVersionCreated, ParseFieldMatcher parseFieldMatcher,
Supplier<QueryShardContext> queryShardContextSupplier) {
this.type = type;
this.indexAnalyzers = indexAnalyzers;
this.similarityLookupService = similarityLookupService;
@ -105,7 +107,7 @@ public abstract class Mapper implements ToXContent, Iterable<Mapper> {
this.typeParsers = typeParsers;
this.indexVersionCreated = indexVersionCreated;
this.parseFieldMatcher = parseFieldMatcher;
this.queryShardContext = queryShardContext;
this.queryShardContextSupplier = queryShardContextSupplier;
}
public String type() {
@ -137,6 +139,10 @@ public abstract class Mapper implements ToXContent, Iterable<Mapper> {
}
public QueryShardContext queryShardContext() {
// No need for synchronization, this class must be used in a single thread
if (queryShardContext == null) {
queryShardContext = queryShardContextSupplier.get();
}
return queryShardContext;
}
@ -155,7 +161,7 @@ public abstract class Mapper implements ToXContent, Iterable<Mapper> {
static class MultiFieldParserContext extends ParserContext {
MultiFieldParserContext(ParserContext in) {
super(in.type(), in.indexAnalyzers, in.similarityLookupService(), in.mapperService(), in.typeParsers(), in.indexVersionCreated(), in.parseFieldMatcher(), in.queryShardContext());
super(in.type(), in.indexAnalyzers, in.similarityLookupService(), in.mapperService(), in.typeParsers(), in.indexVersionCreated(), in.parseFieldMatcher(), in::queryShardContext);
}
}