HHH-15479 Removw megamorphic calls

This commit is contained in:
Andrea Boriero 2022-09-03 15:29:27 +02:00 committed by Christian Beikov
parent 6156751188
commit 16f865f100
1 changed files with 30 additions and 91 deletions

View File

@ -90,20 +90,9 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
} }
private final UniqueSemantic uniqueSemantic; private final UniqueSemantic uniqueSemantic;
private final ResultHandler<R> resultHandler;
public ListResultsConsumer(UniqueSemantic uniqueSemantic) { public ListResultsConsumer(UniqueSemantic uniqueSemantic) {
this.uniqueSemantic = uniqueSemantic; this.uniqueSemantic = uniqueSemantic;
if ( uniqueSemantic == UniqueSemantic.FILTER ) {
resultHandler = ListResultsConsumer::deDuplicationHandling;
}
else if ( uniqueSemantic == UniqueSemantic.ASSERT ) {
resultHandler = ListResultsConsumer::duplicationErrorHandling;
}
else {
resultHandler = ListResultsConsumer::applyAll;
}
} }
private static class Results<R> { private static class Results<R> {
@ -114,19 +103,12 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
this.resultJavaType = resultJavaType; this.resultJavaType = resultJavaType;
} }
private boolean contains(R result) { public boolean addUnique(R result) {
for ( int i = 0; i < results.size(); i++ ) { for ( int i = 0; i < results.size(); i++ ) {
if ( resultJavaType.areEqual( results.get( i ), result ) ) { if ( resultJavaType.areEqual( results.get( i ), result ) ) {
return true; return false;
} }
} }
return false;
}
public boolean addUnique(R result) {
if ( contains( result ) ) {
return false;
}
results.add( result ); results.add( result );
return true; return true;
} }
@ -156,7 +138,6 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
} }
return false; return false;
} }
} }
@Override @Override
@ -180,15 +161,7 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
typeConfiguration typeConfiguration
); );
final ResultHandler<R> resultHandlerToUse; final boolean isEnityResultType = domainResultJavaType instanceof EntityJavaType;
boolean isEnityResultType = domainResultJavaType instanceof EntityJavaType;
if ( uniqueSemantic == UniqueSemantic.ALLOW && isEnityResultType ) {
resultHandlerToUse = ListResultsConsumer::deDuplicationHandling;
}
else {
resultHandlerToUse = this.resultHandler;
}
final Results<R> results; final Results<R> results;
if ( ( uniqueSemantic == UniqueSemantic.ALLOW || uniqueSemantic == UniqueSemantic.FILTER ) && isEnityResultType ) { if ( ( uniqueSemantic == UniqueSemantic.ALLOW || uniqueSemantic == UniqueSemantic.FILTER ) && isEnityResultType ) {
@ -198,10 +171,33 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
results = new Results<>( domainResultJavaType ); results = new Results<>( domainResultJavaType );
} }
while ( rowProcessingState.next() ) { if ( this.uniqueSemantic == UniqueSemantic.FILTER
final R row = rowReader.readRow( rowProcessingState, processingOptions ); || this.uniqueSemantic == UniqueSemantic.ASSERT && rowProcessingState.hasCollectionInitializers
resultHandlerToUse.handle( row, results, rowProcessingState ); || this.uniqueSemantic == UniqueSemantic.ALLOW && isEnityResultType ) {
rowProcessingState.finishRowProcessing(); while ( rowProcessingState.next() ) {
results.addUnique( rowReader.readRow( rowProcessingState, processingOptions ) );
rowProcessingState.finishRowProcessing();
}
}
else if ( this.uniqueSemantic == UniqueSemantic.ASSERT ) {
while ( rowProcessingState.next() ) {
if ( !results.addUnique( rowReader.readRow( rowProcessingState, processingOptions ) ) ) {
throw new HibernateException(
String.format(
Locale.ROOT,
"Duplicate row was found and `%s` was specified",
UniqueSemantic.ASSERT
)
);
}
rowProcessingState.finishRowProcessing();
}
}
else {
while ( rowProcessingState.next() ) {
results.add( rowReader.readRow( rowProcessingState, processingOptions ) );
rowProcessingState.finishRowProcessing();
}
} }
try { try {
@ -245,63 +241,6 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
throw new IllegalStateException( "Should not reach this" ); throw new IllegalStateException( "Should not reach this" );
} }
/**
* Essentially a tri-consumer for applying the different duplication strategies.
*
* @see UniqueSemantic
*/
@FunctionalInterface
private interface ResultHandler<R> {
void handle(R result, Results<R> results, RowProcessingStateStandardImpl rowProcessingState);
}
public static <R> void deDuplicationHandling(
R result,
Results<R> results,
RowProcessingStateStandardImpl rowProcessingState) {
withDuplicationCheck(
result,
results,
rowProcessingState,
false
);
}
private static <R> void withDuplicationCheck(
R result,
Results<R> results,
RowProcessingStateStandardImpl rowProcessingState,
boolean throwException) {
if ( !results.addUnique( result ) && throwException && !rowProcessingState.hasCollectionInitializers ) {
throw new HibernateException(
String.format(
Locale.ROOT,
"Duplicate row was found and `%s` was specified",
UniqueSemantic.ASSERT
)
);
}
}
public static <R> void duplicationErrorHandling(
R result,
Results<R> results,
RowProcessingStateStandardImpl rowProcessingState) {
withDuplicationCheck(
result,
results,
rowProcessingState,
true
);
}
public static <R> void applyAll(
R result,
Results<R> results,
RowProcessingStateStandardImpl rowProcessingState) {
results.add( result );
}
private JavaType<R> resolveDomainResultJavaType( private JavaType<R> resolveDomainResultJavaType(
Class<R> domainResultResultJavaType, Class<R> domainResultResultJavaType,
List<JavaType<?>> resultJavaTypes, List<JavaType<?>> resultJavaTypes,