Skip to content

Commit

Permalink
Add assertion that all field mapper names are interned (#87238)
Browse files Browse the repository at this point in the history
Follow up to #86301. Assert that all field names are interned so we don't
lose this optimization and fix `FieldAliasMapper.name` for it not to trip.
  • Loading branch information
original-brownbear authored Jun 9, 2022
1 parent 3dc4176 commit f15b414
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class Names {

public FieldAliasMapper(String simpleName, String name, String path) {
super(simpleName);
this.name = name;
this.name = Mapper.internFieldName(name);
this.path = path;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ private MappingLookup(
this.indexTimeScriptMappers = List.copyOf(indexTimeScriptMappers);

runtimeFields.stream().flatMap(RuntimeField::asMappedFieldTypes).map(MappedFieldType::name).forEach(this::validateDoesNotShadow);
assert assertMapperNamesInterned(this.fieldMappers, this.objectMappers);
}

private static boolean assertMapperNamesInterned(Map<String, Mapper> mappers, Map<String, ObjectMapper> objectMappers) {
mappers.forEach(MappingLookup::assertNamesInterned);
objectMappers.forEach(MappingLookup::assertNamesInterned);
return true;
}

private static void assertNamesInterned(String name, Mapper mapper) {
assert name == name.intern();
assert mapper.name() == mapper.name().intern();
assert mapper.simpleName() == mapper.simpleName().intern();
if (mapper instanceof ObjectMapper) {
((ObjectMapper) mapper).mappers.forEach(MappingLookup::assertNamesInterned);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ public void testBuildWithUnmappedField() {

protected MappedFieldType mockFieldType(String fieldName) {
MappedFieldType fieldType = mock(MappedFieldType.class);
when(fieldType.name()).thenReturn(fieldName);
when(fieldType.name()).thenReturn(fieldName.intern()); // intern field name to not trip assertions that ensure all field names are
// interned
NamedAnalyzer searchAnalyzer = new NamedAnalyzer("fieldSearchAnalyzer", AnalyzerScope.INDEX, new SimpleAnalyzer());
TextSearchInfo tsi = new TextSearchInfo(TextFieldMapper.Defaults.FIELD_TYPE, null, searchAnalyzer, searchAnalyzer);
when(fieldType.getTextSearchInfo()).thenReturn(tsi);
Expand Down

0 comments on commit f15b414

Please sign in to comment.