/** * Return a copy of the provided map. */ public static <K, V> CopyOnWriteHashMap<K, V> copyOf(Map<? extends K, ? extends V> map) { if (map instanceof CopyOnWriteHashMap) { // no need to copy in that case @SuppressWarnings("unchecked") final CopyOnWriteHashMap<K, V> cowMap = (CopyOnWriteHashMap<K, V>) map; return cowMap; } else { return new CopyOnWriteHashMap<K, V>().copyAndPutAll(map); } }
@Override Leaf<K, V> put(K key, int hash, int hashBits, V value, MutableValueInt newValue) { assert hashBits <= 0 : hashBits; int slot = -1; for (int i = 0; i < keys.length; i++) { if (key.equals(keys[i])) { slot = i; break; } } final K[] keys2; final V[] values2; if (slot < 0) { keys2 = appendElement(keys, key); values2 = appendElement(values, value); newValue.value = 1; } else { keys2 = replace(keys, slot, key); values2 = replace(values, slot, value); } return new Leaf<>(keys2, values2); }
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); } }
/** Returns the field for the given field */ public MappedFieldType get(String field) { String concreteField = aliasToConcreteName.getOrDefault(field, field); return fullNameToFieldType.get(concreteField); }
ObjectMapper(String name, String fullPath, boolean enabled, Nested nested, Dynamic dynamic, Boolean includeInAll, Map<String, Mapper> mappers, Settings settings) { super(name); assert settings != null; Version indexCreatedVersion = Version.indexCreated(settings); if (indexCreatedVersion.onOrAfter(Version.V_5_0_0_beta1)) { if (name.isEmpty()) { throw new IllegalArgumentException("name cannot be empty string"); } } this.fullPath = fullPath; this.enabled = enabled; this.nested = nested; this.dynamic = dynamic; this.includeInAll = includeInAll; 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)); }
@Override public Iterator<MappedFieldType> iterator() { return fullNameToFieldType.values().iterator(); } }
FieldTypeLookup() { fullNameToFieldType = new CopyOnWriteHashMap<>(); aliasToConcreteName = new CopyOnWriteHashMap<>(); fullNameToTypes = new CopyOnWriteHashMap<>(); }
@Override public boolean containsKey(Object key) { // works fine since null values are not supported return get(key) != null; }
public <K1 extends K, V1 extends V> CopyOnWriteHashMap<K, V> copyAndPutAll(Iterable<Entry<K1, V1>> entries) { CopyOnWriteHashMap<K, V> result = this; for (Entry<K1, V1> entry : entries) { result = result.copyAndPut(entry.getKey(), entry.getValue()); } return result; }
public <K1 extends K, V1 extends V> CopyOnWriteHashMap<K, V> copyAndPutAll(Stream<Entry<K1, V1>> entries) { return copyAndPutAll(entries::iterator); }
/** * Same as {@link #copyAndRemove(Object)} but for an arbitrary number of entries. */ public CopyOnWriteHashMap<K, V> copyAndRemoveAll(Collection<?> keys) { CopyOnWriteHashMap<K, V> result = this; for (Object key : keys) { result = result.copyAndRemove(key); } return result; }
private InnerNode<K, V> putNew(K key, int hash6, int slot, V value) { final long mask2 = mask | (1L << hash6); final K[] keys2 = insertElement(keys, key, slot); final Object[] subNodes2 = insertElement(subNodes, value, slot); return new InnerNode<>(mask2, keys2, subNodes2); }
public FieldNameAnalyzer(Map<String, Analyzer> analyzers) { super(Analyzer.PER_FIELD_REUSE_STRATEGY); this.analyzers = CopyOnWriteHashMap.copyOf(analyzers); }
/** * 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()); } } }
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)); }
builder.field("include_in_root", true); } else if (mappers.isEmpty() && custom == null) { // only write the object content type if there are no properties, otherwise, it is automatically detected builder.field("type", CONTENT_TYPE); Mapper[] sortedMappers = mappers.values().stream().toArray(size -> new Mapper[size]); Arrays.sort(sortedMappers, new Comparator<Mapper>() { @Override
@Override public Iterator<Mapper> iterator() { return mappers.values().iterator(); }
/** * Remove the given key from this map. The current hash table is not modified. */ public CopyOnWriteHashMap<K, V> copyAndRemove(Object key) { if (key == null) { throw new IllegalArgumentException("null keys are not supported"); } final int hash = key.hashCode(); final InnerNode<K, V> newRoot = root.remove(key, hash); if (root == newRoot) { return this; } else { return new CopyOnWriteHashMap<>(newRoot, size - 1); } }
public Mapper getMapper(String field) { return mappers.get(field); }