HHH-18863 change the way enum types -> values are cached to avoid lookups

This commit is contained in:
Gavin King 2024-11-20 18:31:58 +01:00
parent ece2981ae8
commit de3408662c
3 changed files with 27 additions and 25 deletions

View File

@ -504,12 +504,25 @@ public final class Context {
return enumTypesByValue; return enumTypesByValue;
} }
public void addEnumValue(String type, String value) { public void addEnumValue(
enumTypesByValue.computeIfAbsent( value, s -> new TreeSet<>() ).add( type ); String qualifiedTypeName, String shortTypeName,
@Nullable String outerTypeQualifiedName, @Nullable String outerShortTypeName,
String value) {
addEnumValue( qualifiedTypeName, value );
addEnumValue( qualifiedTypeName, qualifiedTypeName + '.' + value );
addEnumValue( qualifiedTypeName, shortTypeName + '.' + value );
if ( outerShortTypeName != null ) {
addEnumValue( qualifiedTypeName, outerShortTypeName + '.' + shortTypeName + '.' + value );
addEnumValue( qualifiedTypeName, outerShortTypeName + '$' + shortTypeName + '.' + value );
addEnumValue( qualifiedTypeName, outerTypeQualifiedName + '$' + shortTypeName + '.' + value );
}
} }
@Nullable private void addEnumValue(String qualifiedTypeName, String value) {
public TypeElement entityType(String entityName) { enumTypesByValue.computeIfAbsent( value, s -> new TreeSet<>() ).add( qualifiedTypeName );
}
public @Nullable TypeElement entityType(String entityName) {
final Elements elementUtils = getElementUtils(); final Elements elementUtils = getElementUtils();
final String qualifiedName = qualifiedNameForEntityName(entityName); final String qualifiedName = qualifiedNameForEntityName(entityName);
if ( qualifiedName != null ) { if ( qualifiedName != null ) {

View File

@ -643,9 +643,15 @@ public class HibernateProcessor extends AbstractProcessor {
final DeclaredType declaredType = (DeclaredType) type; final DeclaredType declaredType = (DeclaredType) type;
final TypeElement fieldType = (TypeElement) declaredType.asElement(); final TypeElement fieldType = (TypeElement) declaredType.asElement();
if ( fieldType.getKind() == ElementKind.ENUM ) { if ( fieldType.getKind() == ElementKind.ENUM ) {
for (Element enumMember : fieldType.getEnclosedElements() ) { for ( Element enumMember : fieldType.getEnclosedElements() ) {
if ( enumMember.getKind() == ElementKind.ENUM_CONSTANT ) { if ( enumMember.getKind() == ElementKind.ENUM_CONSTANT ) {
final Element enclosingElement = fieldType.getEnclosingElement();
final boolean hasOuterType =
enclosingElement.getKind().isClass() || enclosingElement.getKind().isInterface();
context.addEnumValue( fieldType.getQualifiedName().toString(), context.addEnumValue( fieldType.getQualifiedName().toString(),
fieldType.getSimpleName().toString(),
hasOuterType ? ((TypeElement) enclosingElement).getQualifiedName().toString() : null,
hasOuterType ? enclosingElement.getSimpleName().toString() : null,
enumMember.getSimpleName().toString() ); enumMember.getSimpleName().toString() );
} }
} }

View File

@ -57,7 +57,6 @@ import static org.hibernate.internal.util.StringHelper.qualify;
import static org.hibernate.internal.util.StringHelper.root; import static org.hibernate.internal.util.StringHelper.root;
import static org.hibernate.internal.util.StringHelper.split; import static org.hibernate.internal.util.StringHelper.split;
import static org.hibernate.internal.util.StringHelper.unroot; import static org.hibernate.internal.util.StringHelper.unroot;
import static org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.addAllowedEnumLiteralsToEnumTypesMap;
import static org.hibernate.processor.util.Constants.JAVA_OBJECT; import static org.hibernate.processor.util.Constants.JAVA_OBJECT;
/** /**
@ -90,7 +89,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
private final Types typeUtil; private final Types typeUtil;
private final Filer filer; private final Filer filer;
private final Map<String, String> entityNameMappings; private final Map<String, String> entityNameMappings;
private final Map<String, Set<String>> allowedEnumLiteralsToEnumTypeNames; private final Map<String, Set<String>> enumTypesByValue;
public ProcessorSessionFactory( public ProcessorSessionFactory(
ProcessingEnvironment processingEnvironment, ProcessingEnvironment processingEnvironment,
@ -100,23 +99,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
typeUtil = processingEnvironment.getTypeUtils(); typeUtil = processingEnvironment.getTypeUtils();
filer = processingEnvironment.getFiler(); filer = processingEnvironment.getFiler();
this.entityNameMappings = entityNameMappings; this.entityNameMappings = entityNameMappings;
final Map<String, Set<String>> allowedEnumLiteralsToEnumTypeNames = new HashMap<>( enumTypesByValue.size() << 2 ); this.enumTypesByValue = enumTypesByValue;
for ( Map.Entry<String, Set<String>> entry : enumTypesByValue.entrySet() ) {
final String enumConstantName = entry.getKey();
for ( String enumClassName : entry.getValue() ) {
final TypeElement enumTypeElement = elementUtil.getTypeElement( enumClassName );
if ( enumTypeElement != null ) {
addAllowedEnumLiteralsToEnumTypesMap(
allowedEnumLiteralsToEnumTypeNames,
enumConstantName,
enumTypeElement.getSimpleName().toString(),
elementUtil.getBinaryName( enumTypeElement ).toString(),
enumClassName
);
}
}
}
this.allowedEnumLiteralsToEnumTypeNames = allowedEnumLiteralsToEnumTypeNames;
} }
@Override @Override
@ -233,7 +216,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Nullable @Override @Nullable
Set<String> getEnumTypesForValue(String value) { Set<String> getEnumTypesForValue(String value) {
final Set<String> result = allowedEnumLiteralsToEnumTypeNames.get( value); final Set<String> result = enumTypesByValue.get(value);
if ( result != null ) { if ( result != null ) {
return result; return result;
} }