Skip to content

Commit 65698be

Browse files
committed
work on batch load stuff
1. use typesafe logging methods 2. use 'var' 3. don't use Array.newInstance() when new Object[] works
1 parent 99145ec commit 65698be

12 files changed

+268
-225
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public Join addJoin(JoinTable joinTable, Table table, boolean noDelayInPkColumnC
208208
* with correctly typed sub-properties for the metamodel.
209209
*/
210210
public static void handleGenericComponentProperty(Property property, MemberDetails memberDetails, MetadataBuildingContext context) {
211-
if ( property.getValue() instanceof final Component component ) {
211+
if ( property.getValue() instanceof Component component ) {
212212
final var collector = context.getMetadataCollector();
213213
if ( component.isGeneric() && component.getPropertySpan() > 0
214214
&& collector.getGenericComponent( component.getComponentClass() ) == null ) {
@@ -312,7 +312,7 @@ public void doSecondPass(Map<String, PersistentClass> persistentClasses) {
312312
final var element = initializedCollection.getElement().copy();
313313
setTypeName( element, memberDetails.getElementType().getName() );
314314
if ( initializedCollection instanceof IndexedCollection indexedCollection ) {
315-
final Value index = indexedCollection.getIndex().copy();
315+
final var index = indexedCollection.getIndex().copy();
316316
if ( memberDetails.getMapKeyType() != null ) {
317317
setTypeName( index, memberDetails.getMapKeyType().getName() );
318318
}

hibernate-core/src/main/java/org/hibernate/internal/log/IncubationLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @author Steve Ebersole
2020
*/
2121
@MessageLogger(projectCode = "HHH")
22-
@ValidIdRange(min = 90006001, max = 90007000)
22+
@ValidIdRange(min = 90006001, max = 90006100)
2323
@Internal
2424
public interface IncubationLogger {
2525
String CATEGORY = SubSystemLogging.BASE + ".incubating";

hibernate-core/src/main/java/org/hibernate/loader/ast/internal/AbstractCollectionBatchLoader.java

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@
55
package org.hibernate.loader.ast.internal;
66

77
import org.hibernate.collection.spi.PersistentCollection;
8-
import org.hibernate.engine.spi.CollectionEntry;
98
import org.hibernate.engine.spi.CollectionKey;
109
import org.hibernate.engine.spi.LoadQueryInfluencers;
11-
import org.hibernate.engine.spi.PersistenceContext;
1210
import org.hibernate.engine.spi.SessionFactoryImplementor;
1311
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1412
import org.hibernate.internal.build.AllowReflection;
1513
import org.hibernate.loader.ast.spi.CollectionBatchLoader;
1614
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
1715
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
1816
import org.hibernate.metamodel.mapping.ValuedModelPart;
19-
import org.hibernate.metamodel.mapping.internal.IdClassEmbeddable;
2017
import org.hibernate.sql.results.internal.ResultsHelper;
2118

22-
import java.lang.reflect.Array;
2319

2420
import static org.hibernate.loader.ast.internal.MultiKeyLoadHelper.hasSingleId;
2521
import static org.hibernate.loader.ast.internal.MultiKeyLoadHelper.trimIdBatch;
@@ -78,63 +74,57 @@ public int getKeyJdbcCount() {
7874

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

77+
private CollectionKey collectionKey(Object key) {
78+
return new CollectionKey( getLoadable().getCollectionDescriptor(), key );
79+
}
80+
8181
@Override
8282
public PersistentCollection<?> load(Object key, SharedSessionContractImplementor session) {
8383
if ( MULTI_KEY_LOAD_LOGGER.isTraceEnabled() ) {
84-
MULTI_KEY_LOAD_LOGGER.trace( "Batch fetching collection: "
85-
+ collectionInfoString( getLoadable(), key ) );
84+
MULTI_KEY_LOAD_LOGGER.batchFetchingCollection(
85+
collectionInfoString( getLoadable(), key ) );
8686
}
8787

88-
final Object[] keys = resolveKeysToInitialize( key, session );
89-
88+
final var keys = resolveKeysToInitialize( key, session );
9089
if ( hasSingleId( keys ) ) {
9190
return singleKeyLoader.load( key, session );
9291
}
93-
9492
initializeKeys( key, keys, session );
95-
9693
finishInitializingKeys( keys, session );
9794

98-
final CollectionKey collectionKey = new CollectionKey( getLoadable().getCollectionDescriptor(), key );
99-
return session.getPersistenceContext().getCollection( collectionKey );
95+
return session.getPersistenceContext().getCollection( collectionKey( key ) );
10096
}
10197

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

104100
protected void finishInitializingKey(Object key, SharedSessionContractImplementor session) {
105-
if ( key == null ) {
106-
return;
107-
}
108-
109-
if ( MULTI_KEY_LOAD_LOGGER.isTraceEnabled() ) {
110-
MULTI_KEY_LOAD_LOGGER.trace( "Finishing initializing batch-fetched collection: "
111-
+ collectionInfoString( attributeMapping, key ) );
112-
}
101+
if ( key != null ) {
102+
if ( MULTI_KEY_LOAD_LOGGER.isTraceEnabled() ) {
103+
MULTI_KEY_LOAD_LOGGER.finishingInitializingBatchFetchedCollection(
104+
collectionInfoString( attributeMapping, key ) );
105+
}
113106

114-
final PersistenceContext persistenceContext = session.getPersistenceContext();
115-
final CollectionKey collectionKey = new CollectionKey( getLoadable().getCollectionDescriptor(), key );
116-
final PersistentCollection<?> collection = persistenceContext.getCollection( collectionKey );
117-
if ( !collection.wasInitialized() ) {
118-
final CollectionEntry entry = persistenceContext.getCollectionEntry( collection );
119-
collection.initializeEmptyCollection( entry.getLoadedPersister() );
120-
ResultsHelper.finalizeCollectionLoading(
121-
persistenceContext,
122-
entry.getLoadedPersister(),
123-
collection,
124-
key,
125-
true
126-
);
107+
final var persistenceContext = session.getPersistenceContext();
108+
final var collection = persistenceContext.getCollection( collectionKey( key ) );
109+
if ( !collection.wasInitialized() ) {
110+
final var entry = persistenceContext.getCollectionEntry( collection );
111+
collection.initializeEmptyCollection( entry.getLoadedPersister() );
112+
ResultsHelper.finalizeCollectionLoading(
113+
persistenceContext,
114+
entry.getLoadedPersister(),
115+
collection,
116+
key,
117+
true
118+
);
119+
}
127120
}
128-
129121
}
130122

131123
@AllowReflection
132124
Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) {
133125
final int length = getDomainBatchSize();
134-
final Object[] keysToInitialize = (Object[]) Array.newInstance(
135-
getKeyType( getLoadable().getKeyDescriptor().getKeyPart() ),
136-
length
137-
);
126+
final var keyType = getKeyType( getLoadable().getKeyDescriptor().getKeyPart() );
127+
final Object[] keysToInitialize = new Object[length];
138128
session.getPersistenceContextInternal().getBatchFetchQueue()
139129
.collectBatchLoadableCollectionKeys(
140130
length,
@@ -148,13 +138,11 @@ Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImp
148138

149139
protected Class<?> getKeyType(ValuedModelPart keyPart) {
150140
if ( keyPart instanceof NonAggregatedIdentifierMapping nonAggregatedIdentifierMapping ) {
151-
final IdClassEmbeddable idClassEmbeddable = nonAggregatedIdentifierMapping.getIdClassEmbeddable();
141+
final var idClassEmbeddable = nonAggregatedIdentifierMapping.getIdClassEmbeddable();
152142
if ( idClassEmbeddable != null ) {
153143
return idClassEmbeddable.getMappedJavaType().getJavaTypeClass();
154144
}
155145
}
156146
return keyPart.getJavaType().getJavaTypeClass();
157147
}
158-
159-
160148
}

hibernate-core/src/main/java/org/hibernate/loader/ast/internal/AbstractEntityBatchLoader.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.hibernate.Hibernate;
88
import org.hibernate.LockMode;
99
import org.hibernate.LockOptions;
10-
import org.hibernate.engine.spi.EntityKey;
1110
import org.hibernate.engine.spi.LoadQueryInfluencers;
1211
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1312
import org.hibernate.loader.ast.spi.EntityBatchLoader;
@@ -25,7 +24,7 @@ public abstract class AbstractEntityBatchLoader<T>
2524

2625
public AbstractEntityBatchLoader(EntityMappingType entityDescriptor, LoadQueryInfluencers influencers) {
2726
super( entityDescriptor, influencers.getSessionFactory() );
28-
this.singleIdLoader = new SingleIdEntityLoaderStandardImpl<>( entityDescriptor, influencers );
27+
singleIdLoader = new SingleIdEntityLoaderStandardImpl<>( entityDescriptor, influencers );
2928
}
3029

3130
protected abstract void initializeEntities(
@@ -38,31 +37,29 @@ protected abstract void initializeEntities(
3837

3938
protected abstract Object[] resolveIdsToInitialize(Object id, SharedSessionContractImplementor session);
4039

41-
@Override
40+
@Override
4241
public final T load(
4342
Object id,
4443
Object entityInstance,
4544
LockOptions lockOptions,
4645
Boolean readOnly,
4746
SharedSessionContractImplementor session) {
4847
if ( MULTI_KEY_LOAD_LOGGER.isTraceEnabled() ) {
49-
MULTI_KEY_LOAD_LOGGER.trace( "Batch fetching entity: "
50-
+ infoString( getLoadable(), id ) );
48+
MULTI_KEY_LOAD_LOGGER.batchFetchingEntity( infoString( getLoadable(), id ) );
5149
}
5250

53-
final Object[] ids = resolveIdsToInitialize( id, session );
51+
final var ids = resolveIdsToInitialize( id, session );
5452
return load( id, ids, hasSingleId( ids ), entityInstance, lockOptions, readOnly, session );
5553
}
5654

57-
@Override
55+
@Override
5856
public T load(
5957
Object id,
6058
Object entityInstance,
6159
LockOptions lockOptions,
6260
SharedSessionContractImplementor session) {
6361
if ( MULTI_KEY_LOAD_LOGGER.isTraceEnabled() ) {
64-
MULTI_KEY_LOAD_LOGGER.trace( "Batch fetching entity: "
65-
+ infoString( getLoadable(), id ) );
62+
MULTI_KEY_LOAD_LOGGER.batchFetchingEntity( infoString( getLoadable(), id ) );
6663
}
6764

6865
final Object[] ids = resolveIdsToInitialize( id, session );
@@ -91,11 +88,11 @@ private T load(
9188
if ( hasSingleId || lockOptions.getLockMode() != LockMode.NONE ) {
9289
return singleIdLoader.load( id, entityInstance, lockOptions, readOnly, session );
9390
}
94-
95-
initializeEntities( ids, id, entityInstance, lockOptions, readOnly, session );
96-
97-
final EntityKey entityKey = session.generateEntityKey( id, getLoadable().getEntityPersister() );
98-
//noinspection unchecked
99-
return (T) session.getPersistenceContext().getEntity( entityKey );
91+
else {
92+
initializeEntities( ids, id, entityInstance, lockOptions, readOnly, session );
93+
final var entityKey = session.generateEntityKey( id, getLoadable().getEntityPersister() );
94+
//noinspection unchecked
95+
return (T) session.getPersistenceContext().getEntity( entityKey );
96+
}
10097
}
10198
}

0 commit comments

Comments
 (0)