Skip to content

Commit

Permalink
Mappings: Remove dead code after previous refactorings
Browse files Browse the repository at this point in the history
This is mostly removing code that handled deletion of types, which was
removed in elastic#8877.

closes elastic#10666
  • Loading branch information
rjernst committed Apr 20, 2015
1 parent dc17427 commit ba6a0c1
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData;
import org.elasticsearch.action.support.ActionFilters;
Expand Down Expand Up @@ -187,7 +188,7 @@ private ImmutableMap<String, FieldMappingMetaData> findFieldMappingsByType(Docum
} else if (Regex.isSimpleMatchPattern(field)) {
// go through the field mappers 3 times, to make sure we give preference to the resolve order: full name, index name, name.
// also make sure we only store each mapper once.
Collection<FieldMapper<?>> remainingFieldMappers = new LinkedList<>(allFieldMappers);
Collection<FieldMapper<?>> remainingFieldMappers = Lists.newLinkedList(allFieldMappers);
for (Iterator<FieldMapper<?>> it = remainingFieldMappers.iterator(); it.hasNext(); ) {
final FieldMapper<?> fieldMapper = it.next();
if (Regex.simpleMatch(field, fieldMapper.names().fullName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ public void onResponse(GetResponse getResponse) {
final Set<String> fields = newHashSet();
if (request.fields() != null) {
for (String field : request.fields()) {
FieldMappers fieldMappers = docMapper.mappers().smartName(field);
if (fieldMappers != null) {
fields.add(fieldMappers.mapper().names().indexName());
FieldMapper fieldMapper = docMapper.mappers().smartNameFieldMapper(field);
if (fieldMapper != null) {
fields.add(fieldMapper.names().indexName());
} else {
fields.add(field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,13 @@ private GetResult innerGetLoadFromStoredFields(String type, String id, String[]
SearchLookup searchLookup = null;
for (String field : gFields) {
Object value = null;
FieldMappers fieldMapper = docMapper.mappers().smartName(field);
FieldMapper fieldMapper = docMapper.mappers().smartNameFieldMapper(field);
if (fieldMapper == null) {
if (docMapper.objectMappers().get(field) != null) {
// Only fail if we know it is a object field, missing paths / fields shouldn't fail.
throw new ElasticsearchIllegalArgumentException("field [" + field + "] isn't a leaf field");
}
} else if (!fieldMapper.mapper().fieldType().stored() && !fieldMapper.mapper().isGenerated()) {
} else if (!fieldMapper.fieldType().stored() && !fieldMapper.isGenerated()) {
if (searchLookup == null) {
searchLookup = new SearchLookup(mapperService, fieldDataService, new String[]{type});
LeafSearchLookup leafSearchLookup = searchLookup.getLeafSearchLookup(docIdAndVersion.context);
Expand All @@ -380,7 +380,7 @@ private GetResult innerGetLoadFromStoredFields(String type, String id, String[]
List<Object> values = searchLookup.source().extractRawValues(field);
if (!values.isEmpty()) {
for (int i = 0; i < values.size(); i++) {
values.set(i, fieldMapper.mapper().valueForSearch(values.get(i)));
values.set(i, fieldMapper.valueForSearch(values.get(i)));
}
value = values;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.Maps;
import org.apache.lucene.analysis.Analyzer;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.analysis.FieldNameAnalyzer;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
*
*/
public final class DocumentFieldMappers extends ForwardingSet<FieldMapper<?>> {
public final class DocumentFieldMappers implements Iterable<FieldMapper<?>> {

private final FieldMappersLookup fieldMappers;

Expand Down Expand Up @@ -104,7 +103,7 @@ public List<String> simpleMatchToFullName(String pattern) {
* Tries to find first based on {@link #fullName(String)}, then by {@link #indexName(String)}, and last
* by {@link #name(String)}.
*/
public FieldMappers smartName(String name) {
FieldMappers smartName(String name) {
return fieldMappers.smartName(name);
}

Expand Down Expand Up @@ -140,8 +139,7 @@ public Analyzer searchQuoteAnalyzer() {
return this.searchQuoteAnalyzer;
}

@Override
protected Set<FieldMapper<?>> delegate() {
return fieldMappers;
public Iterator<FieldMapper<?>> iterator() {
return fieldMappers.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,14 @@ public ParsedDocument parse(SourceToParse source, @Nullable ParseListener listen
// lock to avoid concurrency issues with mapping updates coming from the API
synchronized(this) {
// simulate on the first time to check if the mapping update is applicable
MergeContext mergeContext = newMmergeContext(new MergeFlags().simulate(true));
MergeContext mergeContext = newMergeContext(new MergeFlags().simulate(true));
rootObjectMapper.merge(update, mergeContext);
if (mergeContext.hasConflicts()) {
throw new MapperParsingException("Could not apply generated dynamic mappings: " + Arrays.toString(mergeContext.buildConflicts()));
} else {
// then apply it for real
mappingsModified = true;
mergeContext = newMmergeContext(new MergeFlags().simulate(false));
mergeContext = newMergeContext(new MergeFlags().simulate(false));
rootObjectMapper.merge(update, mergeContext);
}
}
Expand Down Expand Up @@ -665,7 +665,7 @@ public void traverse(ObjectMapperListener listener) {
rootObjectMapper.traverse(listener);
}

private MergeContext newMmergeContext(MergeFlags mergeFlags) {
private MergeContext newMergeContext(MergeFlags mergeFlags) {
return new MergeContext(mergeFlags) {

List<String> conflicts = new ArrayList<>();
Expand Down Expand Up @@ -699,7 +699,7 @@ public String[] buildConflicts() {
}

public synchronized MergeResult merge(DocumentMapper mergeWith, MergeFlags mergeFlags) {
final MergeContext mergeContext = newMmergeContext(mergeFlags);
final MergeContext mergeContext = newMergeContext(mergeFlags);
assert rootMappers.size() == mergeWith.rootMappers.size();

rootObjectMapper.merge(mergeWith.rootObjectMapper, mergeContext);
Expand Down
101 changes: 36 additions & 65 deletions src/main/java/org/elasticsearch/index/mapper/FieldMappersLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@

package org.elasticsearch.index.mapper;

import com.google.common.collect.ForwardingSet;
import com.google.common.collect.Lists;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.collect.CopyOnWriteHashMap;
import org.elasticsearch.common.collect.CopyOnWriteHashSet;
import org.elasticsearch.common.regex.Regex;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* A class that holds a map of field mappers from name, index name, and full name.
*/
public class FieldMappersLookup extends ForwardingSet<FieldMapper<?>> {
class FieldMappersLookup implements Iterable<FieldMapper<?>> {

private static CopyOnWriteHashMap<String, FieldMappers> add(CopyOnWriteHashMap<String, FieldMappers> map, String key, FieldMapper<?> mapper) {
FieldMappers mappers = map.get(key);
Expand All @@ -45,92 +44,44 @@ private static CopyOnWriteHashMap<String, FieldMappers> add(CopyOnWriteHashMap<S
return map.copyAndPut(key, mappers);
}

private static CopyOnWriteHashMap<String, FieldMappers> remove(CopyOnWriteHashMap<String, FieldMappers> map, String key, FieldMapper<?> mapper) {
FieldMappers mappers = map.get(key);
if (mappers == null) {
return map;
}
mappers = mappers.remove(mapper);
if (mappers.isEmpty()) {
return map.copyAndRemove(key);
} else {
return map.copyAndPut(key, mappers);
}
}

private static class MappersLookup {

final CopyOnWriteHashMap<String, FieldMappers> name, indexName, fullName;
final CopyOnWriteHashMap<String, FieldMappers> indexName, fullName;

MappersLookup(CopyOnWriteHashMap<String, FieldMappers> name, CopyOnWriteHashMap<String,
FieldMappers> indexName, CopyOnWriteHashMap<String, FieldMappers> fullName) {
this.name = name;
MappersLookup(CopyOnWriteHashMap<String, FieldMappers> indexName, CopyOnWriteHashMap<String, FieldMappers> fullName) {
this.indexName = indexName;
this.fullName = fullName;
}

MappersLookup addNewMappers(Iterable<? extends FieldMapper<?>> mappers) {
CopyOnWriteHashMap<String, FieldMappers> name = this.name;
CopyOnWriteHashMap<String, FieldMappers> indexName = this.indexName;
CopyOnWriteHashMap<String, FieldMappers> fullName = this.fullName;
for (FieldMapper<?> mapper : mappers) {
name = add(name, mapper.names().name(), mapper);
indexName = add(indexName, mapper.names().indexName(), mapper);
fullName = add(fullName, mapper.names().fullName(), mapper);
}
return new MappersLookup(name, indexName, fullName);
}

MappersLookup removeMappers(Iterable<?> mappers) {
CopyOnWriteHashMap<String, FieldMappers> name = this.name;
CopyOnWriteHashMap<String, FieldMappers> indexName = this.indexName;
CopyOnWriteHashMap<String, FieldMappers> fullName = this.fullName;
for (Object o : mappers) {
if (!(o instanceof FieldMapper)) {
continue;
}
FieldMapper<?> mapper = (FieldMapper<?>) o;
name = remove(name, mapper.names().name(), mapper);
indexName = remove(indexName, mapper.names().indexName(), mapper);
fullName = remove(fullName, mapper.names().fullName(), mapper);
}
return new MappersLookup(name, indexName, fullName);
return new MappersLookup(indexName, fullName);
}

}

private final CopyOnWriteHashSet<FieldMapper<?>> mappers;
private final MappersLookup lookup;

/** Create a new empty instance. */
public FieldMappersLookup() {
this(new CopyOnWriteHashSet<FieldMapper<?>>(),
new MappersLookup(new CopyOnWriteHashMap<String, FieldMappers>(),
new CopyOnWriteHashMap<String, FieldMappers>(),
this(new MappersLookup(new CopyOnWriteHashMap<String, FieldMappers>(),
new CopyOnWriteHashMap<String, FieldMappers>()));
}

private FieldMappersLookup(CopyOnWriteHashSet<FieldMapper<?>> mappers, MappersLookup lookup) {
this.mappers = mappers;
private FieldMappersLookup(MappersLookup lookup) {
this.lookup = lookup;
}

/**
* Return a new instance that contains the union of this instance and the provided mappers.
*/
public FieldMappersLookup copyAndAddAll(Collection<? extends FieldMapper<?>> newMappers) {
return new FieldMappersLookup(mappers.copyAndAddAll(newMappers), lookup.addNewMappers(newMappers));
}

/**
* Return a new instance that contains this instance minus the provided mappers.
*/
public FieldMappersLookup copyAndRemoveAll(Collection<?> mappersToRemove) {
final CopyOnWriteHashSet<FieldMapper<?>> newMappers = mappers.copyAndRemoveAll(mappersToRemove);
if (newMappers != mappers) {
return new FieldMappersLookup(newMappers, lookup.removeMappers(mappersToRemove));
} else {
return this;
}
return new FieldMappersLookup(lookup.addNewMappers(newMappers));
}

/**
Expand All @@ -152,7 +103,7 @@ public FieldMappers fullName(String fullName) {
*/
public List<String> simpleMatchToIndexNames(String pattern) {
List<String> fields = Lists.newArrayList();
for (FieldMapper<?> fieldMapper : mappers) {
for (FieldMapper<?> fieldMapper : this) {
if (Regex.simpleMatch(pattern, fieldMapper.names().fullName())) {
fields.add(fieldMapper.names().indexName());
} else if (Regex.simpleMatch(pattern, fieldMapper.names().indexName())) {
Expand All @@ -167,7 +118,7 @@ public List<String> simpleMatchToIndexNames(String pattern) {
*/
public List<String> simpleMatchToFullName(String pattern) {
List<String> fields = Lists.newArrayList();
for (FieldMapper<?> fieldMapper : mappers) {
for (FieldMapper<?> fieldMapper : this) {
if (Regex.simpleMatch(pattern, fieldMapper.names().fullName())) {
fields.add(fieldMapper.names().fullName());
} else if (Regex.simpleMatch(pattern, fieldMapper.names().indexName())) {
Expand All @@ -181,7 +132,7 @@ public List<String> simpleMatchToFullName(String pattern) {
* Tries to find first based on {@link #fullName(String)}, then by {@link #indexName(String)}.
*/
@Nullable
public FieldMappers smartName(String name) {
FieldMappers smartName(String name) {
FieldMappers fieldMappers = fullName(name);
if (fieldMappers != null) {
return fieldMappers;
Expand All @@ -202,8 +153,28 @@ public FieldMapper<?> smartNameFieldMapper(String name) {
return fieldMappers.mapper();
}

@Override
protected Set<FieldMapper<?>> delegate() {
return mappers;
public Iterator<FieldMapper<?>> iterator() {
final Iterator<FieldMappers> fieldsItr = lookup.fullName.values().iterator();
if (fieldsItr.hasNext() == false) {
return Collections.emptyIterator();
}
return new Iterator<FieldMapper<?>>() {
Iterator<FieldMapper> fieldValuesItr = fieldsItr.next().iterator();
@Override
public boolean hasNext() {
return fieldsItr.hasNext() || fieldValuesItr.hasNext();
}
@Override
public FieldMapper next() {
if (fieldValuesItr.hasNext() == false && fieldsItr.hasNext()) {
fieldValuesItr = fieldsItr.next().iterator();
}
return fieldValuesItr.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException("cannot remove field mapper from lookup");
}
};
}
}
39 changes: 1 addition & 38 deletions src/main/java/org/elasticsearch/index/mapper/MapperService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class MapperService extends AbstractIndexComponent {

public static final String DEFAULT_MAPPING = "_default_";
private static ObjectOpenHashSet<String> META_FIELDS = ObjectOpenHashSet.from(
"_uid", "_id", "_type", "_all", "_analyzer", "_parent", "_routing", "_index",
"_uid", "_id", "_type", "_all", "_parent", "_routing", "_index",
"_size", "_timestamp", "_ttl"
);
private final AnalysisService analysisService;
Expand All @@ -104,7 +104,6 @@ public class MapperService extends AbstractIndexComponent {
private volatile String defaultMappingSource;
private volatile String defaultPercolatorMappingSource;


private volatile Map<String, DocumentMapper> mappers = ImmutableMap.of();

private final Object typeMutex = new Object();
Expand Down Expand Up @@ -394,42 +393,6 @@ private void addFieldMappers(List<FieldMapper<?>> fieldMappers) {
}
}

public void remove(String type) {
synchronized (typeMutex) {
DocumentMapper docMapper = mappers.get(type);
if (docMapper == null) {
return;
}
docMapper.close();
mappers = newMapBuilder(mappers).remove(type).map();
removeObjectAndFieldMappers(docMapper);
for (DocumentTypeListener typeListener : typeListeners) {
typeListener.afterRemove(docMapper);
}
}
}

private void removeObjectAndFieldMappers(DocumentMapper docMapper) {
synchronized (mappersMutex) {
fieldMappers = fieldMappers.copyAndRemoveAll(docMapper.mappers());

ImmutableOpenMap.Builder<String, ObjectMappers> fullPathObjectMappers = ImmutableOpenMap.builder(this.fullPathObjectMappers);
for (ObjectMapper mapper : docMapper.objectMappers().values()) {
ObjectMappers mappers = fullPathObjectMappers.get(mapper.fullPath());
if (mappers != null) {
mappers = mappers.remove(mapper);
if (mappers.isEmpty()) {
fullPathObjectMappers.remove(mapper.fullPath());
} else {
fullPathObjectMappers.put(mapper.fullPath(), mappers);
}
}
}

this.fullPathObjectMappers = fullPathObjectMappers.build();
}
}

public DocumentMapper parse(String mappingType, CompressedString mappingSource, boolean applyDefault) throws MapperParsingException {
String defaultMappingSource;
if (PercolatorService.TYPE_NAME.equals(mappingType)) {
Expand Down
Loading

0 comments on commit ba6a0c1

Please sign in to comment.