Skip to content

Commit

Permalink
Mappings: Remove mapper listeners
Browse files Browse the repository at this point in the history
The mapper listener concept is only now used as a callback to the
MapperService when new fields are added. This change removes the
listeners, instead storing a link to the mapper service in
each doc mapper.
  • Loading branch information
rjernst committed May 7, 2015
1 parent f1e0fb6 commit 6dd8434
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 172 deletions.
49 changes: 25 additions & 24 deletions src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ public Builder transform(ScriptService scriptService, String script, ScriptType
return this;
}

public DocumentMapper build(DocumentMapperParser docMapperParser) {
public DocumentMapper build(MapperService mapperService, DocumentMapperParser docMapperParser) {
Preconditions.checkNotNull(rootObjectMapper, "Mapper builder must have the root object mapper set");
return new DocumentMapper(index, indexSettings, docMapperParser, rootObjectMapper, meta, rootMappers, sourceTransforms);
return new DocumentMapper(mapperService, index, indexSettings, docMapperParser, rootObjectMapper, meta, rootMappers, sourceTransforms);
}
}

private final MapperService mapperService;

private final String type;
private final StringAndBytesText typeText;

Expand All @@ -177,20 +179,17 @@ public DocumentMapper build(DocumentMapperParser docMapperParser) {

private volatile ImmutableMap<String, ObjectMapper> objectMappers = ImmutableMap.of();

private final List<FieldMapperListener> fieldMapperListeners = new CopyOnWriteArrayList<>();

private final List<ObjectMapperListener> objectMapperListeners = new CopyOnWriteArrayList<>();

private boolean hasNestedObjects = false;

private final Filter typeFilter;

private final Object mappersMutex = new Object();

public DocumentMapper(String index, @Nullable Settings indexSettings, DocumentMapperParser docMapperParser,
public DocumentMapper(MapperService mapperService, String index, @Nullable Settings indexSettings, DocumentMapperParser docMapperParser,
RootObjectMapper rootObjectMapper,
ImmutableMap<String, Object> meta,
Map<Class<? extends RootMapper>, RootMapper> rootMappers, List<SourceTransform> sourceTransforms) {
this.mapperService = mapperService;
this.type = rootObjectMapper.name();
this.typeText = new StringAndBytesText(this.type);
this.mapping = new Mapping(
Expand Down Expand Up @@ -414,13 +413,7 @@ public void addFieldMappers(Collection<FieldMapper<?>> fieldMappers) {
synchronized (mappersMutex) {
this.fieldMappers = this.fieldMappers.copyAndAllAll(fieldMappers);
}
for (FieldMapperListener listener : fieldMapperListeners) {
listener.fieldMappers(fieldMappers);
}
}

public void addFieldMapperListener(FieldMapperListener fieldMapperListener) {
fieldMapperListeners.add(fieldMapperListener);
mapperService.addFieldMappers(fieldMappers);
}

private void addObjectMappers(Collection<ObjectMapper> objectMappers) {
Expand All @@ -434,30 +427,36 @@ private void addObjectMappers(Collection<ObjectMapper> objectMappers) {
}
this.objectMappers = builder.immutableMap();
}
for (ObjectMapperListener objectMapperListener : objectMapperListeners) {
objectMapperListener.objectMappers(objectMappers);
}
}

public void addObjectMapperListener(ObjectMapperListener objectMapperListener) {
objectMapperListeners.add(objectMapperListener);
mapperService.addObjectMappers(objectMappers);
}

private MergeResult newMergeContext(boolean simulate) {
return new MergeResult(simulate) {

List<String> conflicts = new ArrayList<>();
final List<String> conflicts = new ArrayList<>();
final List<FieldMapper<?>> newFieldMappers = new ArrayList<>();
final List<ObjectMapper> newObjectMappers = new ArrayList<>();

@Override
public void addFieldMappers(Collection<FieldMapper<?>> fieldMappers) {
assert simulate() == false;
DocumentMapper.this.addFieldMappers(fieldMappers);
newFieldMappers.addAll(fieldMappers);
}

@Override
public void addObjectMappers(Collection<ObjectMapper> objectMappers) {
assert simulate() == false;
DocumentMapper.this.addObjectMappers(objectMappers);
newObjectMappers.addAll(objectMappers);
}

@Override
public Collection<FieldMapper<?>> getNewFieldMappers() {
return newFieldMappers;
}

@Override
public Collection<ObjectMapper> getNewObjectMappers() {
return newObjectMappers;
}

@Override
Expand All @@ -482,6 +481,8 @@ public synchronized MergeResult merge(Mapping mapping, boolean simulate) {
final MergeResult mergeResult = newMergeContext(simulate);
this.mapping.merge(mapping, mergeResult);
if (simulate == false) {
addFieldMappers(mergeResult.getNewFieldMappers());
addObjectMappers(mergeResult.getNewObjectMappers());
refreshSource();
}
return mergeResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
*/
public class DocumentMapperParser extends AbstractIndexComponent {

final MapperService mapperService;
final AnalysisService analysisService;
private static final ESLogger logger = Loggers.getLogger(DocumentMapperParser.class);
private final SimilarityLookupService similarityLookupService;
Expand All @@ -100,9 +101,10 @@ public class DocumentMapperParser extends AbstractIndexComponent {
private volatile ImmutableMap<String, Mapper.TypeParser> typeParsers;
private volatile ImmutableMap<String, Mapper.TypeParser> rootTypeParsers;

public DocumentMapperParser(Index index, @IndexSettings Settings indexSettings, AnalysisService analysisService,
public DocumentMapperParser(Index index, @IndexSettings Settings indexSettings, MapperService mapperService, AnalysisService analysisService,
SimilarityLookupService similarityLookupService, ScriptService scriptService) {
super(index, indexSettings);
this.mapperService = mapperService;
this.analysisService = analysisService;
this.similarityLookupService = similarityLookupService;
this.scriptService = scriptService;
Expand Down Expand Up @@ -269,7 +271,7 @@ private DocumentMapper parse(String type, Map<String, Object> mapping, String de

checkNoRemainingFields(mapping, parserContext.indexVersionCreated(), "Root mapping definition has unsupported parameters: ");

DocumentMapper documentMapper = docBuilder.build(this);
DocumentMapper documentMapper = docBuilder.build(mapperService, this);
// update the source with the generated one
documentMapper.refreshSource();
return documentMapper;
Expand Down

This file was deleted.

35 changes: 3 additions & 32 deletions src/main/java/org/elasticsearch/index/mapper/MapperService.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ public class MapperService extends AbstractIndexComponent {

private final DocumentMapperParser documentParser;

private final InternalFieldMapperListener fieldMapperListener = new InternalFieldMapperListener();
private final InternalObjectMapperListener objectMapperListener = new InternalObjectMapperListener();

private final SmartIndexNameSearchAnalyzer searchAnalyzer;
private final SmartIndexNameSearchQuoteAnalyzer searchQuoteAnalyzer;

Expand All @@ -122,7 +119,7 @@ public MapperService(Index index, @IndexSettings Settings indexSettings, Analysi
this.analysisService = analysisService;
this.fieldDataService = fieldDataService;
this.fieldMappers = new FieldMappersLookup();
this.documentParser = new DocumentMapperParser(index, indexSettings, analysisService, similarityLookupService, scriptService);
this.documentParser = new DocumentMapperParser(index, indexSettings, this, analysisService, similarityLookupService, scriptService);
this.searchAnalyzer = new SmartIndexNameSearchAnalyzer(analysisService.defaultSearchAnalyzer());
this.searchQuoteAnalyzer = new SmartIndexNameSearchQuoteAnalyzer(analysisService.defaultSearchQuoteAnalyzer());

Expand Down Expand Up @@ -275,9 +272,7 @@ private DocumentMapper merge(DocumentMapper mapper) {
}
MapperUtils.collect(mapper.mapping().root, newObjectMappers, newFieldMappers);
addFieldMappers(newFieldMappers);
mapper.addFieldMapperListener(fieldMapperListener);
addObjectMappers(newObjectMappers);
mapper.addObjectMapperListener(objectMapperListener);

for (DocumentTypeListener typeListener : typeListeners) {
typeListener.beforeCreate(mapper);
Expand All @@ -288,7 +283,7 @@ private DocumentMapper merge(DocumentMapper mapper) {
}
}

private void addObjectMappers(Collection<ObjectMapper> objectMappers) {
protected void addObjectMappers(Collection<ObjectMapper> objectMappers) {
synchronized (mappersMutex) {
ImmutableOpenMap.Builder<String, ObjectMappers> fullPathObjectMappers = ImmutableOpenMap.builder(this.fullPathObjectMappers);
for (ObjectMapper objectMapper : objectMappers) {
Expand All @@ -308,7 +303,7 @@ private void addObjectMappers(Collection<ObjectMapper> objectMappers) {
}
}

private void addFieldMappers(Collection<FieldMapper<?>> fieldMappers) {
protected void addFieldMappers(Collection<FieldMapper<?>> fieldMappers) {
synchronized (mappersMutex) {
this.fieldMappers = this.fieldMappers.copyAndAddAll(fieldMappers);
}
Expand Down Expand Up @@ -848,28 +843,4 @@ protected Analyzer getWrappedAnalyzer(String fieldName) {
return defaultAnalyzer;
}
}

class InternalFieldMapperListener extends FieldMapperListener {
@Override
public void fieldMapper(FieldMapper<?> fieldMapper) {
addFieldMappers(Collections.<FieldMapper<?>>singletonList(fieldMapper));
}

@Override
public void fieldMappers(Collection<FieldMapper<?>> fieldMappers) {
addFieldMappers(fieldMappers);
}
}

class InternalObjectMapperListener extends ObjectMapperListener {
@Override
public void objectMapper(ObjectMapper objectMapper) {
addObjectMappers(Collections.singletonList(objectMapper));
}

@Override
public void objectMappers(Collection<ObjectMapper> objectMappers) {
addObjectMappers(objectMappers);
}
}
}
20 changes: 15 additions & 5 deletions src/main/java/org/elasticsearch/index/mapper/MapperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public enum MapperUtils {
;

private static MergeResult newStrictMergeContext() {
private static MergeResult newStrictMergeResult() {
return new MergeResult(false) {

@Override
Expand All @@ -43,15 +43,25 @@ public String[] buildConflicts() {
}

@Override
public void addObjectMappers(Collection<ObjectMapper> objectMappers) {
public void addFieldMappers(Collection<FieldMapper<?>> fieldMappers) {
// no-op
}

@Override
public void addFieldMappers(Collection<FieldMapper<?>> fieldMappers) {
public void addObjectMappers(Collection<ObjectMapper> objectMappers) {
// no-op
}

@Override
public Collection<FieldMapper<?>> getNewFieldMappers() {
throw new UnsupportedOperationException("Strict merge result does not support new field mappers");
}

@Override
public Collection<ObjectMapper> getNewObjectMappers() {
throw new UnsupportedOperationException("Strict merge result does not support new object mappers");
}

@Override
public void addConflict(String mergeFailure) {
throw new MapperParsingException("Merging dynamic updates triggered a conflict: " + mergeFailure);
Expand All @@ -64,15 +74,15 @@ public void addConflict(String mergeFailure) {
* merges mappings, not lookup structures. Conflicts are returned as exceptions.
*/
public static void merge(Mapper mergeInto, Mapper mergeWith) {
mergeInto.merge(mergeWith, newStrictMergeContext());
mergeInto.merge(mergeWith, newStrictMergeResult());
}

/**
* Merge {@code mergeWith} into {@code mergeTo}. Note: this method only
* merges mappings, not lookup structures. Conflicts are returned as exceptions.
*/
public static void merge(Mapping mergeInto, Mapping mergeWith) {
mergeInto.merge(mergeWith, newStrictMergeContext());
mergeInto.merge(mergeWith, newStrictMergeResult());
}

/** Split mapper and its descendants into object and field mappers. */
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/elasticsearch/index/mapper/MergeResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public MergeResult(boolean simulate) {

public abstract void addObjectMappers(Collection<ObjectMapper> objectMappers);

public abstract Collection<FieldMapper<?>> getNewFieldMappers();

public abstract Collection<ObjectMapper> getNewObjectMappers();

public boolean simulate() {
return simulate;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.FieldMapperListener;
import org.elasticsearch.index.mapper.InternalMapper;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.MapperUtils;
import org.elasticsearch.index.mapper.MergeMappingException;
import org.elasticsearch.index.mapper.MergeResult;
import org.elasticsearch.index.mapper.ObjectMapperListener;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
import org.elasticsearch.index.settings.IndexSettings;
Expand Down
Loading

0 comments on commit 6dd8434

Please sign in to comment.