Skip to content

Commit

Permalink
very minor code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Jan 14, 2025
1 parent 24a6f2a commit 206ccd3
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public abstract class AbstractCollectionBatchLoader implements CollectionBatchLo
private final LoadQueryInfluencers influencers;
private final SessionFactoryImplementor sessionFactory;

private final int keyJdbcCount;

final CollectionLoaderSingleKey singleKeyLoader;

public AbstractCollectionBatchLoader(
Expand All @@ -46,15 +44,14 @@ public AbstractCollectionBatchLoader(
this.domainBatchSize = domainBatchSize;
this.attributeMapping = attributeMapping;

this.keyJdbcCount = attributeMapping.getJdbcTypeCount();
this.sessionFactory = sessionFactory;
this.influencers = influencers;

singleKeyLoader = new CollectionLoaderSingleKey( getLoadable(), getInfluencers(), getSessionFactory() );
}

@Override
public int getDomainBatchSize() {
public final int getDomainBatchSize() {
return domainBatchSize;
}

Expand All @@ -71,10 +68,6 @@ public SessionFactoryImplementor getSessionFactory() {
return sessionFactory;
}

public int getKeyJdbcCount() {
return keyJdbcCount;
}

abstract void initializeKeys(Object key, Object[] keysToInitialize, SharedSessionContractImplementor session);

@Override
Expand All @@ -85,75 +78,70 @@ public PersistentCollection<?> load(Object key, SharedSessionContractImplementor
}

final Object[] keys = resolveKeysToInitialize( key, session );

if ( hasSingleId( keys ) ) {
return singleKeyLoader.load( key, session );
}

initializeKeys( key, keys, session );

finishInitializingKeys( keys, session );

final CollectionKey collectionKey = new CollectionKey( getLoadable().getCollectionDescriptor(), key );
return session.getPersistenceContext().getCollection( collectionKey );
else {
initializeKeys( key, keys, session );
finishInitializingKeys( keys, session );
final CollectionKey collectionKey = new CollectionKey( getLoadable().getCollectionDescriptor(), key );
return session.getPersistenceContext().getCollection( collectionKey );
}
}

abstract void finishInitializingKeys(Object[] key, SharedSessionContractImplementor session);

protected void finishInitializingKey(Object key, SharedSessionContractImplementor session) {
if ( key == null ) {
return;
}

if ( MULTI_KEY_LOAD_LOGGER.isDebugEnabled() ) {
MULTI_KEY_LOAD_LOGGER.debugf( "Finishing initializing batch-fetched collection: %s.%s",
attributeMapping.getNavigableRole().getFullPath(), key );
}
if ( key != null ) {
if ( MULTI_KEY_LOAD_LOGGER.isDebugEnabled() ) {
MULTI_KEY_LOAD_LOGGER.debugf( "Finishing initializing batch-fetched collection: %s.%s",
attributeMapping.getNavigableRole().getFullPath(), key );
}

final PersistenceContext persistenceContext = session.getPersistenceContext();
final CollectionKey collectionKey = new CollectionKey( getLoadable().getCollectionDescriptor(), key );
final PersistentCollection<?> collection = persistenceContext.getCollection( collectionKey );
if ( !collection.wasInitialized() ) {
final CollectionEntry entry = persistenceContext.getCollectionEntry( collection );
collection.initializeEmptyCollection( entry.getLoadedPersister() );
ResultsHelper.finalizeCollectionLoading(
persistenceContext,
entry.getLoadedPersister(),
collection,
key,
true
);
final PersistenceContext persistenceContext = session.getPersistenceContext();
final CollectionKey collectionKey = new CollectionKey( getLoadable().getCollectionDescriptor(), key );
final PersistentCollection<?> collection = persistenceContext.getCollection( collectionKey );
if ( !collection.wasInitialized() ) {
final CollectionEntry entry = persistenceContext.getCollectionEntry( collection );
collection.initializeEmptyCollection( entry.getLoadedPersister() );
ResultsHelper.finalizeCollectionLoading(
persistenceContext,
entry.getLoadedPersister(),
collection,
key,
true
);
}
}

}

@AllowReflection
Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) {
final int length = getDomainBatchSize();
final Object[] keysToInitialize = (Object[]) Array.newInstance(
getKeyType( getLoadable().getKeyDescriptor().getKeyPart() ),
length
);
final Object[] keysToInitialize = createKeyArray();
session.getPersistenceContextInternal().getBatchFetchQueue()
.collectBatchLoadableCollectionKeys(
length,
getDomainBatchSize(),
(index, key) -> keysToInitialize[index] = key,
keyBeingLoaded,
getLoadable()
);
// now trim down the array to the number of keys we found
return trimIdBatch( length, keysToInitialize );
return trimIdBatch( getDomainBatchSize(), keysToInitialize );
}

private Object[] createKeyArray() {
final Class<?> elementType = getKeyType( getLoadable().getKeyDescriptor().getKeyPart() );
return (Object[]) Array.newInstance( elementType, getDomainBatchSize() );
}

protected Class<?> getKeyType(ValuedModelPart keyPart) {
if ( keyPart instanceof NonAggregatedIdentifierMapping ) {
final IdClassEmbeddable idClassEmbeddable = ( (NonAggregatedIdentifierMapping) keyPart ).getIdClassEmbeddable();
if ( keyPart instanceof NonAggregatedIdentifierMapping nonAggregatedIdentifierMapping ) {
final IdClassEmbeddable idClassEmbeddable = nonAggregatedIdentifierMapping.getIdClassEmbeddable();
if ( idClassEmbeddable != null ) {
return idClassEmbeddable.getMappedJavaType().getJavaTypeClass();
}
}
return keyPart.getJavaType().getJavaTypeClass();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public final T load(
}

final Object[] ids = resolveIdsToInitialize( id, session );

return load( id, ids, hasSingleId( ids ), entityInstance, lockOptions, readOnly, session );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.select.QuerySpec;
Expand All @@ -34,6 +35,7 @@
import org.hibernate.sql.results.spi.ListResultsConsumer;

import static org.hibernate.loader.ast.internal.MultiKeyLoadHelper.hasSingleId;
import static org.hibernate.loader.ast.internal.MultiKeyLoadHelper.resolveArrayJdbcMapping;
import static org.hibernate.loader.ast.internal.MultiKeyLoadHelper.trimIdBatch;
import static org.hibernate.loader.ast.internal.MultiKeyLoadLogging.MULTI_KEY_LOAD_LOGGER;

Expand Down Expand Up @@ -71,11 +73,7 @@ public CollectionBatchLoaderArrayParam(
final Class<?> jdbcJavaTypeClass = jdbcMapping.getJdbcJavaType().getJavaTypeClass();
keyDomainType = getKeyType( keyDescriptor.getKeyPart() );

arrayJdbcMapping = MultiKeyLoadHelper.resolveArrayJdbcMapping(
jdbcMapping,
jdbcJavaTypeClass,
getSessionFactory()
);
arrayJdbcMapping = resolveArrayJdbcMapping( jdbcMapping, jdbcJavaTypeClass, getSessionFactory() );

jdbcParameter = new JdbcParameterImpl( arrayJdbcMapping );
sqlSelect = LoaderSelectBuilder.createSelectBySingleArrayParameter(
Expand All @@ -91,12 +89,16 @@ public CollectionBatchLoaderArrayParam(
final TableGroup tableGroup = querySpec.getFromClause().getRoots().get( 0 );
attributeMapping.applySoftDeleteRestrictions( tableGroup, querySpec::applyPredicate );

jdbcSelectOperation = getSessionFactory().getJdbcServices()
.getJdbcEnvironment()
.getSqlAstTranslatorFactory()
.buildSelectTranslator( getSessionFactory(), sqlSelect )
.translate( JdbcParameterBindings.NO_BINDINGS, QueryOptions.NONE );
jdbcSelectOperation =
getSqlAstTranslatorFactory()
.buildSelectTranslator( getSessionFactory(), sqlSelect )
.translate( JdbcParameterBindings.NO_BINDINGS, QueryOptions.NONE );
}

private SqlAstTranslatorFactory getSqlAstTranslatorFactory() {
return getSessionFactory().getJdbcServices().getJdbcEnvironment().getSqlAstTranslatorFactory();
}

@Override
public PersistentCollection<?> load(Object keyBeingLoaded, SharedSessionContractImplementor session) {
final ForeignKeyDescriptor keyDescriptor = getLoadable().getKeyDescriptor();
Expand All @@ -122,19 +124,11 @@ private PersistentCollection<?> loadEmbeddable(
);
}

final int length = getDomainBatchSize();
final Object[] keysToInitialize = (Object[]) Array.newInstance(
jdbcParameter.getExpressionType()
.getSingleJdbcMapping()
.getJdbcJavaType()
.getJavaTypeClass()
.getComponentType(),
length
);
final Object[] embeddedKeys = (Object[]) Array.newInstance( keyDomainType, length );
final Object[] keysToInitialize = createKeyArray();
final Object[] embeddedKeys = createEmbeddedKeyArray();
session.getPersistenceContextInternal().getBatchFetchQueue()
.collectBatchLoadableCollectionKeys(
length,
getDomainBatchSize(),
(index, key) ->
keyDescriptor.forEachJdbcValue( key, (i, value, jdbcMapping) -> {
keysToInitialize[index] = value;
Expand All @@ -144,25 +138,33 @@ private PersistentCollection<?> loadEmbeddable(
keyBeingLoaded,
getLoadable()
);
// now trim down the array to the number of keys we found
final Object[] keys = trimIdBatch( length, keysToInitialize );

// now trim down the array to the number of keys we found
final Object[] keys = trimIdBatch( getDomainBatchSize(), keysToInitialize );
if ( hasSingleId( keys ) ) {
return singleKeyLoader.load( keyBeingLoaded, session );
}

initializeKeys( keyBeingLoaded, keys, session );

for ( Object initializedKey : embeddedKeys ) {
if ( initializedKey != null ) {
finishInitializingKey( initializedKey, session );
else {
initializeKeys( keyBeingLoaded, keys, session );
for ( Object initializedKey : embeddedKeys ) {
if ( initializedKey != null ) {
finishInitializingKey( initializedKey, session );
}
}
return session.getPersistenceContext()
.getCollection( new CollectionKey( getLoadable().getCollectionDescriptor(), keyBeingLoaded ) );
}
final CollectionKey collectionKey = new CollectionKey(
getLoadable().getCollectionDescriptor(),
keyBeingLoaded
);
return session.getPersistenceContext().getCollection( collectionKey );
}

private Object[] createEmbeddedKeyArray() {
return (Object[]) Array.newInstance( keyDomainType, getDomainBatchSize() );
}

private Object[] createKeyArray() {
final Class<?> elementType =
jdbcParameter.getExpressionType().getSingleJdbcMapping().getJdbcJavaType()
.getJavaTypeClass().getComponentType();
return (Object[]) Array.newInstance( elementType, getDomainBatchSize() );
}

@Override
Expand All @@ -180,25 +182,21 @@ void initializeKeys(Object key, Object[] keysToInitialize, SharedSessionContract
assert jdbcParameter != null;

final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(1);
jdbcParameterBindings.addBinding(
jdbcParameter,
new JdbcParameterBindingImpl( arrayJdbcMapping, keysToInitialize )
);

final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(
session.getPersistenceContext().getBatchFetchQueue(),
sqlSelect,
JdbcParametersList.singleton( jdbcParameter ),
jdbcParameterBindings
);
jdbcParameterBindings.addBinding( jdbcParameter,
new JdbcParameterBindingImpl( arrayJdbcMapping, keysToInitialize ) );

final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler =
SubselectFetch.createRegistrationHandler(
session.getPersistenceContext().getBatchFetchQueue(),
sqlSelect,
JdbcParametersList.singleton( jdbcParameter ),
jdbcParameterBindings
);

session.getJdbcServices().getJdbcSelectExecutor().list(
jdbcSelectOperation,
jdbcParameterBindings,
new ExecutionContextWithSubselectFetchHandler(
session,
subSelectFetchableKeysHandler
),
new ExecutionContextWithSubselectFetchHandler( session, subSelectFetchableKeysHandler ),
RowTransformerStandardImpl.instance(),
ListResultsConsumer.UniqueSemantic.FILTER
);
Expand All @@ -215,24 +213,28 @@ void finishInitializingKeys(Object[] keys, SharedSessionContractImplementor sess
@AllowReflection
Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) {
final ForeignKeyDescriptor keyDescriptor = getLoadable().getKeyDescriptor();
if( keyDescriptor.isEmbedded()){
if ( keyDescriptor.isEmbedded() ) {
assert keyDescriptor.getJdbcTypeCount() == 1;
final int length = getDomainBatchSize();
final Object[] keysToInitialize = (Object[]) Array.newInstance( keyDescriptor.getSingleJdbcMapping().getJdbcJavaType().getJavaTypeClass(), length );
final Object[] keysToInitialize = (Object[]) createForeignKeyArray( keyDescriptor );
session.getPersistenceContextInternal().getBatchFetchQueue()
.collectBatchLoadableCollectionKeys(
length,
getDomainBatchSize(),
(index, key) ->
keyDescriptor.forEachJdbcValue( key, (i, value, jdbcMapping) -> {
keysToInitialize[index] = value;
}, session )
,
keyDescriptor.forEachJdbcValue( key, (i, value, jdbcMapping) ->
keysToInitialize[index] = value, session ),
keyBeingLoaded,
getLoadable()
);
// now trim down the array to the number of keys we found
return trimIdBatch( length, keysToInitialize );
return trimIdBatch( getDomainBatchSize(), keysToInitialize );
}
return super.resolveKeysToInitialize( keyBeingLoaded, session );
else {
return super.resolveKeysToInitialize( keyBeingLoaded, session );
}
}

private Object createForeignKeyArray(ForeignKeyDescriptor keyDescriptor) {
final Class<?> elementType = keyDescriptor.getSingleJdbcMapping().getJdbcJavaType().getJavaTypeClass();
return Array.newInstance( elementType, getDomainBatchSize() );
}
}
Loading

0 comments on commit 206ccd3

Please sign in to comment.