org.elasticsearch.common.collect.CopyOnWriteHashMap Java Examples

The following examples show how to use org.elasticsearch.common.collect.CopyOnWriteHashMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FieldTypeLookup.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private static CopyOnWriteHashMap<String, Set<String>> addType(CopyOnWriteHashMap<String, Set<String>> map, String key, String type) {
    Set<String> types = map.get(key);
    if (types == null) {
        return map.copyAndPut(key, Collections.singleton(type));
    } else if (types.contains(type)) {
        // noting to do
        return map;
    } else {
        Set<String> newTypes = new HashSet<>(types.size() + 1);
        newTypes.addAll(types);
        newTypes.add(type);
        assert newTypes.size() == types.size() + 1;
        newTypes = Collections.unmodifiableSet(newTypes);
        return map.copyAndPut(key, newTypes);
    }
}
 
Example #2
Source File: ObjectMapper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
ObjectMapper(String name, String fullPath, boolean enabled, Nested nested, Dynamic dynamic, ContentPath.Type pathType, Map<String, Mapper> mappers) {
    super(name);
    this.fullPath = fullPath;
    this.enabled = enabled;
    this.nested = nested;
    this.dynamic = dynamic;
    this.pathType = pathType;
    if (mappers == null) {
        this.mappers = new CopyOnWriteHashMap<>();
    } else {
        this.mappers = CopyOnWriteHashMap.copyOf(mappers);
    }
    this.nestedTypePathAsString = "__" + fullPath;
    this.nestedTypePathAsBytes = new BytesRef(nestedTypePathAsString);
    this.nestedTypeFilter = new TermQuery(new Term(TypeFieldMapper.NAME, nestedTypePathAsBytes));
}
 
Example #3
Source File: FieldTypeLookup.java    From crate with Apache License 2.0 6 votes vote down vote up
private static CopyOnWriteHashMap<String, Set<String>> addType(CopyOnWriteHashMap<String, Set<String>> map,
                                                               String key,
                                                               String type) {
    Set<String> types = map.get(key);
    if (types == null) {
        return map.copyAndPut(key, Collections.singleton(type));
    } else if (types.contains(type)) {
        // noting to do
        return map;
    } else {
        Set<String> newTypes = new HashSet<>(types.size() + 1);
        newTypes.addAll(types);
        newTypes.add(type);
        assert newTypes.size() == types.size() + 1;
        newTypes = Collections.unmodifiableSet(newTypes);
        return map.copyAndPut(key, newTypes);
    }
}
 
Example #4
Source File: FieldTypeLookup.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the given field type is compatible with an existing field type.
 * An IllegalArgumentException is thrown in case of incompatibility.
 * If updateAllTypes is true, only basic compatibility is checked.
 */
private void validateField(String type,
                           MappedFieldType existingFieldType,
                           MappedFieldType newFieldType,
                           CopyOnWriteHashMap<String, String> aliasToConcreteName,
                           boolean updateAllTypes) {
    String fieldName = newFieldType.name();
    if (aliasToConcreteName.containsKey(fieldName)) {
        throw new IllegalArgumentException("The name for field [" + fieldName + "] has already" +
            " been used to define a field alias.");
    }

    if (existingFieldType != null) {
        List<String> conflicts = new ArrayList<>();
        final Set<String> types = fullNameToTypes.get(newFieldType.name());
        boolean strict = beStrict(type, types, updateAllTypes);
        existingFieldType.checkCompatibility(newFieldType, conflicts, strict);
        if (conflicts.isEmpty() == false) {
            throw new IllegalArgumentException("Mapper for [" + fieldName +
                "] conflicts with existing mapping in other types:\n" + conflicts.toString());
        }
    }
}
 
Example #5
Source File: FieldTypeLookup.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Checks that the new field alias is valid.
 *
 * Note that this method assumes that new concrete fields have already been processed, so that it
 * can verify that an alias refers to an existing concrete field.
 */
private void validateAlias(String aliasName,
                           String path,
                           CopyOnWriteHashMap<String, String> aliasToConcreteName,
                           CopyOnWriteHashMap<String, MappedFieldType> fullNameToFieldType) {
    if (fullNameToFieldType.containsKey(aliasName)) {
        throw new IllegalArgumentException("The name for field alias [" + aliasName + "] has already" +
            " been used to define a concrete field.");
    }

    if (path.equals(aliasName)) {
        throw new IllegalArgumentException("Invalid [path] value [" + path + "] for field alias [" +
            aliasName + "]: an alias cannot refer to itself.");
    }

    if (aliasToConcreteName.containsKey(path)) {
        throw new IllegalArgumentException("Invalid [path] value [" + path + "] for field alias [" +
            aliasName + "]: an alias cannot refer to another alias.");
    }

    if (!fullNameToFieldType.containsKey(path)) {
        throw new IllegalArgumentException("Invalid [path] value [" + path + "] for field alias [" +
            aliasName + "]: an alias must refer to an existing field in the mappings.");
    }
}
 
Example #6
Source File: FieldTypeLookup.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/** Create a new empty instance. */
public FieldTypeLookup() {
    fullNameToFieldType = new CopyOnWriteHashMap<>();
    fullNameToTypes = new CopyOnWriteHashMap<>();
    indexNameToFieldType = new CopyOnWriteHashMap<>();
    indexNameToTypes = new CopyOnWriteHashMap<>();
    needReindex = false;
}
 
Example #7
Source File: FieldTypeLookup.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private FieldTypeLookup(
        CopyOnWriteHashMap<String, MappedFieldType> fullName,
        CopyOnWriteHashMap<String, Set<String>> fullNameToTypes,
        CopyOnWriteHashMap<String, MappedFieldType> indexName,
        CopyOnWriteHashMap<String, Set<String>> indexNameToTypes,
        boolean needReindex) {
    this.fullNameToFieldType = fullName;
    this.fullNameToTypes = fullNameToTypes;
    this.indexNameToFieldType = indexName;
    this.indexNameToTypes = indexNameToTypes;
    this.needReindex = needReindex;
}
 
Example #8
Source File: FieldTypeLookup.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Return a new instance that contains the union of this instance and the field types
 * from the provided fields. If a field already exists, the field type will be updated
 * to use the new mappers field type.
 */
public FieldTypeLookup copyAndAddAll(String type, Collection<FieldMapper> fieldMappers, boolean updateAllTypes, boolean reindex) {
    boolean needReindex = false;
    Objects.requireNonNull(type, "type must not be null");
    if (MapperService.DEFAULT_MAPPING.equals(type)) {
        throw new IllegalArgumentException("Default mappings should not be added to the lookup");
    }

    CopyOnWriteHashMap<String, MappedFieldType> fullName = this.fullNameToFieldType;
    CopyOnWriteHashMap<String, Set<String>> fullNameToTypes = this.fullNameToTypes;
    CopyOnWriteHashMap<String, MappedFieldType> indexName = this.indexNameToFieldType;
    CopyOnWriteHashMap<String, Set<String>> indexNameToTypes = this.indexNameToTypes;

    for (FieldMapper fieldMapper : fieldMappers) {
        MappedFieldType fieldType = fieldMapper.fieldType();
        MappedFieldType fullNameFieldType = fullName.get(fieldType.names().fullName());
        MappedFieldType indexNameFieldType = indexName.get(fieldType.names().indexName());

        if (fullNameFieldType != null && indexNameFieldType != null && fullNameFieldType != indexNameFieldType) {
            // this new field bridges between two existing field names (a full and index name), which we cannot support
            throw new IllegalStateException("insane mappings found. field " + fieldType.names().fullName() + " maps across types to field " + fieldType.names().indexName());
        }

        // is the update even legal?
        needReindex |= checkCompatibility(type, fieldMapper, updateAllTypes, reindex);
        if (fieldType != fullNameFieldType || fieldType != indexNameFieldType) {
            fullName = fullName.copyAndPut(fieldType.names().fullName(), fieldMapper.fieldType());
            indexName = indexName.copyAndPut(fieldType.names().indexName(), fieldMapper.fieldType());
        }

        fullNameToTypes = addType(fullNameToTypes, fieldType.names().fullName(), type);
        indexNameToTypes = addType(indexNameToTypes, fieldType.names().indexName(), type);
    }
    return new FieldTypeLookup(fullName, fullNameToTypes, indexName, indexNameToTypes, needReindex);
}
 
Example #9
Source File: OntologyMapper.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
public OntologyMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType,
		Settings indexSettings, MultiFields multiFields, OntologySettings oSettings,
		Map<String, StringFieldMapper> fieldMappers) {
	super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, null);
	this.ontologySettings = oSettings;
	// Dynamic mappers are added to mappers map as they are used/created
	this.mappers = CopyOnWriteHashMap.copyOf(fieldMappers);
}
 
Example #10
Source File: OntologyMapper.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
public OntologyMapper(FieldMapper.Names names, FieldType fieldType, Boolean docValues,
		NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer,
		PostingsFormatProvider postingsFormat, DocValuesFormatProvider docValuesFormat,
		SimilarityProvider similarity, @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, OntologySettings oSettings,
		Map<String, FieldMapper<String>> fieldMappers,
		ThreadPool threadPool) {
	super(names, 1f, fieldType, docValues, searchAnalyzer, indexAnalyzer, postingsFormat, docValuesFormat, similarity, null,
			fieldDataSettings, indexSettings, multiFields, null);
	this.ontologySettings = oSettings;
	// Mappers are added to mappers map as they are used/created
	this.mappers = CopyOnWriteHashMap.copyOf(fieldMappers);
	this.threadPool = threadPool;
}
 
Example #11
Source File: OntologyMapper.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
public OntologyMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType,
		Settings indexSettings, MultiFields multiFields, OntologySettings oSettings,
		Map<String, StringFieldMapper> fieldMappers) {
	super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, null);
	this.ontologySettings = oSettings;
	// Dynamic mappers are added to mappers map as they are used/created
	this.mappers = CopyOnWriteHashMap.copyOf(fieldMappers);
}
 
Example #12
Source File: OntologyMapper.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
public OntologyMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, Settings indexSettings, MultiFields multiFields, OntologySettings oSettings,
		Map<String, StringFieldMapper> fieldMappers,
		ThreadPool threadPool) {
	super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, null);
	this.ontologySettings = oSettings;
	// Dynamic mappers are added to mappers map as they are used/created
	this.mappers = CopyOnWriteHashMap.copyOf(fieldMappers);
	this.threadPool = threadPool;
}
 
Example #13
Source File: FieldTypeLookup.java    From crate with Apache License 2.0 5 votes vote down vote up
private FieldTypeLookup(CopyOnWriteHashMap<String, MappedFieldType> fullNameToFieldType,
                        CopyOnWriteHashMap<String, String> aliasToConcreteName,
                        CopyOnWriteHashMap<String, Set<String>> fullNameToTypes) {
    this.fullNameToFieldType = fullNameToFieldType;
    this.aliasToConcreteName = aliasToConcreteName;
    this.fullNameToTypes = fullNameToTypes;
}
 
Example #14
Source File: FieldTypeLookup.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Return a new instance that contains the union of this instance and the field types
 * from the provided mappers. If a field already exists, its field type will be updated
 * to use the new type from the given field mapper. Similarly if an alias already
 * exists, it will be updated to reference the field type from the new mapper.
 */
public FieldTypeLookup copyAndAddAll(String type,
                                     Collection<FieldMapper> fieldMappers,
                                     Collection<FieldAliasMapper> fieldAliasMappers,
                                     boolean updateAllTypes) {
    Objects.requireNonNull(type, "type must not be null");
    if (MapperService.DEFAULT_MAPPING.equals(type)) {
        throw new IllegalArgumentException("Default mappings should not be added to the lookup");
    }

    CopyOnWriteHashMap<String, MappedFieldType> fullName = this.fullNameToFieldType;
    CopyOnWriteHashMap<String, String> aliases = this.aliasToConcreteName;
    CopyOnWriteHashMap<String, Set<String>> fullNameToTypes = this.fullNameToTypes;

    for (FieldMapper fieldMapper : fieldMappers) {
        MappedFieldType fieldType = fieldMapper.fieldType();
        MappedFieldType fullNameFieldType = fullName.get(fieldType.name());

        if (!Objects.equals(fieldType, fullNameFieldType)) {
            validateField(type, fullNameFieldType, fieldType, aliases, updateAllTypes);
            fullName = fullName.copyAndPut(fieldType.name(), fieldType);
        }
        fullNameToTypes = addType(fullNameToTypes, fieldType.name(), type);
    }

    for (FieldAliasMapper fieldAliasMapper : fieldAliasMappers) {
        String aliasName = fieldAliasMapper.name();
        String path = fieldAliasMapper.path();

        validateAlias(aliasName, path, aliases, fullName);
        aliases = aliases.copyAndPut(aliasName, path);
    }

    return new FieldTypeLookup(fullName, aliases, fullNameToTypes);
}
 
Example #15
Source File: FieldNameAnalyzer.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public FieldNameAnalyzer(Map<String, Analyzer> analyzers) {
    super(Analyzer.PER_FIELD_REUSE_STRATEGY);
    this.analyzers = CopyOnWriteHashMap.copyOf(analyzers);
}
 
Example #16
Source File: FieldTypeLookup.java    From crate with Apache License 2.0 4 votes vote down vote up
FieldTypeLookup() {
    fullNameToFieldType = new CopyOnWriteHashMap<>();
    aliasToConcreteName = new CopyOnWriteHashMap<>();
    fullNameToTypes = new CopyOnWriteHashMap<>();
}