HHH-13587 Make StatefulPersistenceContext#arrayHolders lazily initialized as well

This commit is contained in:
Sanne Grinovero 2019-08-17 21:59:07 +01:00 committed by Sanne Grinovero
parent dbbc24c2e1
commit 05b888e0c0

View File

@ -162,7 +162,6 @@ public StatefulPersistenceContext(SharedSessionContractImplementor session) {
entityEntryContext = new EntityEntryContext( this ); entityEntryContext = new EntityEntryContext( this );
collectionsByKey = new HashMap<>( INIT_COLL_SIZE ); collectionsByKey = new HashMap<>( INIT_COLL_SIZE );
arrayHolders = new IdentityHashMap<>( INIT_COLL_SIZE );
} }
private ConcurrentMap<EntityKey, Object> getOrInitializeProxiesByKey() { private ConcurrentMap<EntityKey, Object> getOrInitializeProxiesByKey() {
@ -243,7 +242,7 @@ public void clear() {
IdentityMap.onEachKey( collectionEntries, k -> k.unsetSession( session ) ); IdentityMap.onEachKey( collectionEntries, k -> k.unsetSession( session ) );
} }
arrayHolders.clear(); arrayHolders = null;
entitiesByKey.clear(); entitiesByKey.clear();
entitiesByUniqueKey.clear(); entitiesByUniqueKey.clear();
entityEntryContext.clear(); entityEntryContext.clear();
@ -981,18 +980,21 @@ public void initializeNonLazyCollections() throws HibernateException {
@Override @Override
public PersistentCollection getCollectionHolder(Object array) { public PersistentCollection getCollectionHolder(Object array) {
return arrayHolders.get( array ); return arrayHolders == null ? null : arrayHolders.get( array );
} }
@Override @Override
public void addCollectionHolder(PersistentCollection holder) { public void addCollectionHolder(PersistentCollection holder) {
//TODO:refactor + make this method private //TODO:refactor + make this method private
if ( arrayHolders == null ) {
arrayHolders = new IdentityHashMap<>( INIT_COLL_SIZE );
}
arrayHolders.put( holder.getValue(), holder ); arrayHolders.put( holder.getValue(), holder );
} }
@Override @Override
public PersistentCollection removeCollectionHolder(Object array) { public PersistentCollection removeCollectionHolder(Object array) {
return arrayHolders.remove( array ); return arrayHolders != null ? arrayHolders.remove( array ) : null;
} }
@Override @Override
@ -1583,6 +1585,10 @@ public void serialize(ObjectOutputStream oos) throws IOException {
} }
} }
if ( arrayHolders == null ) {
oos.writeInt( 0 );
}
else {
oos.writeInt( arrayHolders.size() ); oos.writeInt( arrayHolders.size() );
if ( LOG.isTraceEnabled() ) { if ( LOG.isTraceEnabled() ) {
LOG.trace( "Starting serialization of [" + arrayHolders.size() + "] arrayHolders entries" ); LOG.trace( "Starting serialization of [" + arrayHolders.size() + "] arrayHolders entries" );
@ -1591,6 +1597,7 @@ public void serialize(ObjectOutputStream oos) throws IOException {
oos.writeObject( entry.getKey() ); oos.writeObject( entry.getKey() );
oos.writeObject( entry.getValue() ); oos.writeObject( entry.getValue() );
} }
}
if ( nullifiableEntityKeys == null ) { if ( nullifiableEntityKeys == null ) {
oos.writeInt( 0 ); oos.writeInt( 0 );
@ -1707,10 +1714,12 @@ public static StatefulPersistenceContext deserialize(
if ( LOG.isTraceEnabled() ) { if ( LOG.isTraceEnabled() ) {
LOG.trace( "Starting deserialization of [" + count + "] arrayHolders entries" ); LOG.trace( "Starting deserialization of [" + count + "] arrayHolders entries" );
} }
if ( count != 0 ) {
rtn.arrayHolders = new IdentityHashMap<>( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); rtn.arrayHolders = new IdentityHashMap<>( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
for ( int i = 0; i < count; i++ ) { for ( int i = 0; i < count; i++ ) {
rtn.arrayHolders.put( ois.readObject(), (PersistentCollection) ois.readObject() ); rtn.arrayHolders.put( ois.readObject(), (PersistentCollection) ois.readObject() );
} }
}
count = ois.readInt(); count = ois.readInt();
if ( LOG.isTraceEnabled() ) { if ( LOG.isTraceEnabled() ) {