HHH-14650 AbstractRowReader: no need to pre-compute the entityInitializerByEntityReference cache in advance

This commit is contained in:
Sanne Grinovero 2021-06-03 10:07:19 +01:00
parent c22393d57b
commit fb69172a4b
1 changed files with 19 additions and 26 deletions

View File

@ -8,7 +8,6 @@ package org.hibernate.loader.plan.exec.process.internal;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -16,12 +15,9 @@ import java.util.Map;
import org.hibernate.engine.internal.TwoPhaseLoad; import org.hibernate.engine.internal.TwoPhaseLoad;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventSource; import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PostLoadEvent; import org.hibernate.event.spi.PostLoadEvent;
import org.hibernate.event.spi.PreLoadEvent; import org.hibernate.event.spi.PreLoadEvent;
import org.hibernate.event.spi.PreLoadEventListener;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.loader.plan.exec.process.spi.CollectionReferenceInitializer; import org.hibernate.loader.plan.exec.process.spi.CollectionReferenceInitializer;
@ -54,24 +50,10 @@ public abstract class AbstractRowReader implements RowReader {
// cache map for looking up EntityReferenceInitializer by EntityReference to help with resolving // cache map for looking up EntityReferenceInitializer by EntityReference to help with resolving
// bidirectional EntityReference and fetches. // bidirectional EntityReference and fetches.
private final Map<EntityReference,EntityReferenceInitializer> entityInitializerByEntityReference; private Map<EntityReference,EntityReferenceInitializer> entityInitializerByEntityReference;
public AbstractRowReader(ReaderCollector readerCollector) { public AbstractRowReader(ReaderCollector readerCollector) {
if ( CollectionHelper.isNotEmpty( readerCollector.getEntityReferenceInitializers() ) ) { this.entityReferenceInitializers = readerCollector.getEntityReferenceInitializers().toArray( EMPTY_REFERENCE_INITIALIZERS );
entityReferenceInitializers = readerCollector.getEntityReferenceInitializers().toArray( EMPTY_REFERENCE_INITIALIZERS );
entityInitializerByEntityReference =
new HashMap<EntityReference, EntityReferenceInitializer>( entityReferenceInitializers.length );
for ( EntityReferenceInitializer entityReferenceInitializer : entityReferenceInitializers ) {
entityInitializerByEntityReference.put(
entityReferenceInitializer.getEntityReference(),
entityReferenceInitializer
);
}
}
else {
entityReferenceInitializers = EMPTY_REFERENCE_INITIALIZERS;
entityInitializerByEntityReference = Collections.<EntityReference,EntityReferenceInitializer>emptyMap();
}
this.arrayReferenceInitializers = readerCollector.getArrayReferenceInitializers(); this.arrayReferenceInitializers = readerCollector.getArrayReferenceInitializers();
this.collectionReferenceInitializers = readerCollector.getNonArrayCollectionReferenceInitializers(); this.collectionReferenceInitializers = readerCollector.getNonArrayCollectionReferenceInitializers();
} }
@ -148,9 +130,7 @@ public abstract class AbstractRowReader implements RowReader {
FetchSource fetchSource) throws SQLException { FetchSource fetchSource) throws SQLException {
// Resolve any bidirectional entity references first. // Resolve any bidirectional entity references first.
for ( BidirectionalEntityReference bidirectionalEntityReference : fetchSource.getBidirectionalEntityReferences() ) { for ( BidirectionalEntityReference bidirectionalEntityReference : fetchSource.getBidirectionalEntityReferences() ) {
final EntityReferenceInitializer targetEntityReferenceInitializer = entityInitializerByEntityReference.get( final EntityReferenceInitializer targetEntityReferenceInitializer = getInitializerByEntityReference( bidirectionalEntityReference.getTargetEntityReference() );
bidirectionalEntityReference.getTargetEntityReference()
);
resolveEntityKey( resolveEntityKey(
resultSet, resultSet,
context, context,
@ -161,9 +141,7 @@ public abstract class AbstractRowReader implements RowReader {
for ( Fetch fetch : fetchSource.getFetches() ) { for ( Fetch fetch : fetchSource.getFetches() ) {
if ( EntityFetch.class.isInstance( fetch ) ) { if ( EntityFetch.class.isInstance( fetch ) ) {
final EntityFetch entityFetch = (EntityFetch) fetch; final EntityFetch entityFetch = (EntityFetch) fetch;
final EntityReferenceInitializer entityReferenceInitializer = entityInitializerByEntityReference.get( final EntityReferenceInitializer entityReferenceInitializer = getInitializerByEntityReference( entityFetch );
entityFetch
);
if ( entityReferenceInitializer != null ) { if ( entityReferenceInitializer != null ) {
resolveEntityKey( resolveEntityKey(
resultSet, resultSet,
@ -183,6 +161,21 @@ public abstract class AbstractRowReader implements RowReader {
} }
} }
private EntityReferenceInitializer getInitializerByEntityReference(EntityReference targetEntityReference) {
if ( entityInitializerByEntityReference == null ) {
entityInitializerByEntityReference = new HashMap<>( entityReferenceInitializers.length );
for ( EntityReferenceInitializer entityReferenceInitializer : entityReferenceInitializers ) {
entityInitializerByEntityReference.put(
entityReferenceInitializer.getEntityReference(),
entityReferenceInitializer
);
}
}
return entityInitializerByEntityReference.get(
targetEntityReference
);
}
@Override @Override
public void finishUp(ResultSetProcessingContextImpl context, List<AfterLoadAction> afterLoadActionList) { public void finishUp(ResultSetProcessingContextImpl context, List<AfterLoadAction> afterLoadActionList) {
final List<HydratedEntityRegistration> hydratedEntityRegistrations = context.getHydratedEntityRegistrationList(); final List<HydratedEntityRegistration> hydratedEntityRegistrations = context.getHydratedEntityRegistrationList();