Skip to content

Commit 83110b4

Browse files
committed
HHH-18863 allow index creation to be disabled in processor
also cache enum results read from index
1 parent 3cf2bc8 commit 83110b4

File tree

5 files changed

+113
-69
lines changed

5 files changed

+113
-69
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/Context.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public final class Context {
107107
private String[] includes = {"*"};
108108
private String[] excludes = {};
109109

110+
private boolean indexing = true;
111+
110112
private final Map<String, String> entityNameMappings = new HashMap<>();
111113
private final Map<String, Set<String>> enumTypesByValue = new HashMap<>();
112114

@@ -551,4 +553,12 @@ private void addEnumValue(String qualifiedTypeName, String value) {
551553
}
552554
return null;
553555
}
556+
557+
public void setIndexing(boolean index) {
558+
this.indexing = index;
559+
}
560+
561+
public boolean isIndexing() {
562+
return indexing;
563+
}
554564
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import static org.hibernate.processor.HibernateProcessor.EXCLUDE;
5555
import static org.hibernate.processor.HibernateProcessor.FULLY_ANNOTATION_CONFIGURED_OPTION;
5656
import static org.hibernate.processor.HibernateProcessor.INCLUDE;
57+
import static org.hibernate.processor.HibernateProcessor.INDEX;
5758
import static org.hibernate.processor.HibernateProcessor.LAZY_XML_PARSING;
5859
import static org.hibernate.processor.HibernateProcessor.ORM_XML_OPTION;
5960
import static org.hibernate.processor.HibernateProcessor.PERSISTENCE_XML_OPTION;
@@ -120,7 +121,8 @@
120121
ADD_GENERATED_ANNOTATION,
121122
ADD_SUPPRESS_WARNINGS_ANNOTATION,
122123
SUPPRESS_JAKARTA_DATA_METAMODEL,
123-
INCLUDE, EXCLUDE
124+
INCLUDE, EXCLUDE,
125+
INDEX
124126
})
125127
public class HibernateProcessor extends AbstractProcessor {
126128

@@ -190,6 +192,13 @@ public class HibernateProcessor extends AbstractProcessor {
190192
*/
191193
public static final String EXCLUDE = "exclude";
192194

195+
/**
196+
* Option to suppress creation of a filesystem-based index of entity
197+
* types and enums for use by the query validator. By default, and
198+
* index is created.
199+
*/
200+
public static final String INDEX = "index";
201+
193202
private static final boolean ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS = false;
194203

195204
public static final String ENTITY_INDEX = "entity.index";
@@ -282,6 +291,8 @@ && packagePresent(quarkusOrmPanachePackage) ) {
282291
context.setInclude( options.getOrDefault( INCLUDE, "*" ) );
283292
context.setExclude( options.getOrDefault( EXCLUDE, "" ) );
284293

294+
context.setIndexing( parseBoolean( options.get( INDEX ) ) );
295+
285296
return parseBoolean( options.get( FULLY_ANNOTATION_CONFIGURED_OPTION ) );
286297
}
287298

@@ -773,43 +784,45 @@ else if ( hasAnnotation( typeElement, EMBEDDABLE ) ) {
773784
}
774785

775786
private void writeIndex() {
776-
final ProcessingEnvironment processingEnvironment = context.getProcessingEnvironment();
777-
final Elements elementUtils = processingEnvironment.getElementUtils();
778-
context.getEntityNameMappings().forEach((entityName, className) -> {
779-
try (Writer writer = processingEnvironment.getFiler()
780-
.createResource(
781-
StandardLocation.SOURCE_OUTPUT,
782-
ENTITY_INDEX,
783-
entityName,
784-
elementUtils.getTypeElement( className )
785-
)
786-
.openWriter()) {
787-
writer.append(className);
788-
}
789-
catch (IOException e) {
790-
processingEnvironment.getMessager()
791-
.printMessage(Diagnostic.Kind.WARNING,
792-
"could not write entity index " + e.getMessage());
793-
}
794-
});
795-
context.getEnumTypesByValue().forEach((valueName, enumTypeNames) -> {
796-
try (Writer writer = processingEnvironment.getFiler()
797-
.createResource(
798-
StandardLocation.SOURCE_OUTPUT,
799-
ENTITY_INDEX,
800-
'.' + valueName,
801-
elementUtils.getTypeElement( enumTypeNames.iterator().next() )
802-
)
803-
.openWriter()) {
804-
for (String enumTypeName : enumTypeNames) {
805-
writer.append(enumTypeName).append(" ");
787+
if ( context.isIndexing() ) {
788+
final ProcessingEnvironment processingEnvironment = context.getProcessingEnvironment();
789+
final Elements elementUtils = processingEnvironment.getElementUtils();
790+
context.getEntityNameMappings().forEach( (entityName, className) -> {
791+
try (Writer writer = processingEnvironment.getFiler()
792+
.createResource(
793+
StandardLocation.SOURCE_OUTPUT,
794+
ENTITY_INDEX,
795+
entityName,
796+
elementUtils.getTypeElement( className )
797+
)
798+
.openWriter()) {
799+
writer.append( className );
806800
}
807-
}
808-
catch (IOException e) {
809-
processingEnvironment.getMessager()
810-
.printMessage(Diagnostic.Kind.WARNING,
811-
"could not write entity index " + e.getMessage());
812-
}
813-
});
801+
catch (IOException e) {
802+
processingEnvironment.getMessager()
803+
.printMessage( Diagnostic.Kind.WARNING,
804+
"could not write entity index " + e.getMessage() );
805+
}
806+
} );
807+
context.getEnumTypesByValue().forEach( (valueName, enumTypeNames) -> {
808+
try (Writer writer = processingEnvironment.getFiler()
809+
.createResource(
810+
StandardLocation.SOURCE_OUTPUT,
811+
ENTITY_INDEX,
812+
'.' + valueName,
813+
elementUtils.getTypeElement( enumTypeNames.iterator().next() )
814+
)
815+
.openWriter()) {
816+
for ( String enumTypeName : enumTypeNames ) {
817+
writer.append( enumTypeName ).append( " " );
818+
}
819+
}
820+
catch (IOException e) {
821+
processingEnvironment.getMessager()
822+
.printMessage( Diagnostic.Kind.WARNING,
823+
"could not write entity index " + e.getMessage() );
824+
}
825+
} );
826+
}
814827
}
815828
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMeta.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
104104
new WarningErrorHandler( context, getElement(), mirror, value, hql,
105105
reportErrors, checkHql ),
106106
ProcessorSessionFactory.create( context.getProcessingEnvironment(),
107-
context.getEntityNameMappings(), context.getEnumTypesByValue() )
107+
context.getEntityNameMappings(), context.getEnumTypesByValue(),
108+
context.isIndexing() )
108109
);
109110
if ( !isJakartaDataStyle()
110111
&& statement instanceof SqmSelectStatement<?> selectStatement ) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2451,7 +2451,7 @@ private void validateHql(
24512451
true,
24522452
new ErrorHandler( context, isLocal(method) ? method : element, mirror, value, hql ),
24532453
ProcessorSessionFactory.create( context.getProcessingEnvironment(),
2454-
context.getEntityNameMappings(), context.getEnumTypesByValue() )
2454+
context.getEntityNameMappings(), context.getEnumTypesByValue(), context.isIndexing() )
24552455
);
24562456
if ( statement != null ) {
24572457
if ( statement instanceof SqmSelectStatement ) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
7272
public static MockSessionFactory create(
7373
ProcessingEnvironment environment,
7474
Map<String,String> entityNameMappings,
75-
Map<String, Set<String>> enumTypesByValue) {
76-
return instance.make(environment, entityNameMappings, enumTypesByValue);
75+
Map<String, Set<String>> enumTypesByValue,
76+
boolean indexing) {
77+
return instance.make(environment, indexing, entityNameMappings, enumTypesByValue);
7778
}
7879

7980
static final Mocker<ProcessorSessionFactory> instance = Mocker.variadic(ProcessorSessionFactory.class);
@@ -88,16 +89,19 @@ public static MockSessionFactory create(
8889
private final Elements elementUtil;
8990
private final Types typeUtil;
9091
private final Filer filer;
92+
private final boolean indexing;
9193
private final Map<String, String> entityNameMappings;
9294
private final Map<String, Set<String>> enumTypesByValue;
9395

9496
public ProcessorSessionFactory(
9597
ProcessingEnvironment processingEnvironment,
98+
boolean indexing,
9699
Map<String,String> entityNameMappings,
97100
Map<String, Set<String>> enumTypesByValue) {
98101
elementUtil = processingEnvironment.getElementUtils();
99102
typeUtil = processingEnvironment.getTypeUtils();
100103
filer = processingEnvironment.getFiler();
104+
this.indexing = indexing;
101105
this.entityNameMappings = entityNameMappings;
102106
this.enumTypesByValue = enumTypesByValue;
103107
}
@@ -220,15 +224,25 @@ Set<String> getEnumTypesForValue(String value) {
220224
if ( result != null ) {
221225
return result;
222226
}
223-
try (Reader reader = filer.getResource(StandardLocation.SOURCE_OUTPUT, ENTITY_INDEX, value)
224-
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
225-
return Set.of(split(" ", buffered.readLine()));
227+
if ( indexing ) {
228+
final Set<String> indexed = getIndexedEnumTypesByValue(value);
229+
enumTypesByValue.put(value, indexed);
230+
return indexed;
231+
}
232+
//TODO: else do a full scan like in findEntityByUnqualifiedName()
233+
return null;
234+
}
235+
236+
private @Nullable Set<String> getIndexedEnumTypesByValue(String value) {
237+
try (Reader reader = filer.getResource( StandardLocation.SOURCE_OUTPUT, ENTITY_INDEX, value )
238+
.openReader( true ); BufferedReader buffered = new BufferedReader( reader )) {
239+
return Set.of( split( " ", buffered.readLine() ) );
226240
}
227241
catch (IOException ignore) {
228242
}
229-
try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, '.' + value)
230-
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
231-
return Set.of(split(" ", buffered.readLine()));
243+
try (Reader reader = filer.getResource( StandardLocation.CLASS_PATH, ENTITY_INDEX, '.' + value )
244+
.openReader( true ); BufferedReader buffered = new BufferedReader( reader )) {
245+
return Set.of( split( " ", buffered.readLine() ) );
232246
}
233247
catch (IOException ignore) {
234248
}
@@ -503,27 +517,13 @@ private TypeElement findEntityByUnqualifiedName(String entityName) {
503517
if ( cached != null ) {
504518
return cached;
505519
}
506-
final String qualifiedName = entityNameMappings.get(entityName);
507-
if ( qualifiedName != null ) {
508-
final TypeElement result = elementUtil.getTypeElement(qualifiedName);
509-
entityCache.put(entityName, result);
510-
return result;
511-
}
512-
try (Reader reader = filer.getResource( StandardLocation.SOURCE_OUTPUT, ENTITY_INDEX, entityName)
513-
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
514-
final TypeElement result = elementUtil.getTypeElement(buffered.readLine());
515-
entityCache.put(entityName, result);
516-
return result;
517-
}
518-
catch (IOException ignore) {
519-
}
520-
try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, entityName)
521-
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
522-
final TypeElement result = elementUtil.getTypeElement(buffered.readLine());
523-
entityCache.put(entityName, result);
524-
return result;
525-
}
526-
catch (IOException ignore) {
520+
521+
if ( indexing ) {
522+
final TypeElement indexedEntity = findIndexedEntityByQualifiedName( entityName );
523+
if ( indexedEntity != null ) {
524+
entityCache.put(entityName, indexedEntity);
525+
return indexedEntity;
526+
}
527527
}
528528

529529
TypeElement symbol =
@@ -543,6 +543,26 @@ private TypeElement findEntityByUnqualifiedName(String entityName) {
543543
return null;
544544
}
545545

546+
private @Nullable TypeElement findIndexedEntityByQualifiedName(String entityName) {
547+
final String qualifiedName = entityNameMappings.get(entityName);
548+
if ( qualifiedName != null ) {
549+
return elementUtil.getTypeElement(qualifiedName);
550+
}
551+
try (Reader reader = filer.getResource( StandardLocation.SOURCE_OUTPUT, ENTITY_INDEX, entityName)
552+
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
553+
return elementUtil.getTypeElement(buffered.readLine());
554+
}
555+
catch (IOException ignore) {
556+
}
557+
try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, entityName)
558+
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
559+
return elementUtil.getTypeElement(buffered.readLine());
560+
}
561+
catch (IOException ignore) {
562+
}
563+
return null;
564+
}
565+
546566
public static TypeElement findEntityByUnqualifiedName(String entityName, ModuleElement module) {
547567
for (Element element: module.getEnclosedElements()) {
548568
if (element.getKind() == ElementKind.PACKAGE) {

0 commit comments

Comments
 (0)