HHH-13587 Make StatefulPersistenceContext#entitiesByUniqueKey lazily initialized as well
This commit is contained in:
parent
05b888e0c0
commit
7531ed793a
|
@ -13,6 +13,7 @@ import java.io.ObjectOutputStream;
|
|||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
|
@ -157,7 +158,6 @@ public class StatefulPersistenceContext implements PersistenceContext {
|
|||
this.session = session;
|
||||
|
||||
entitiesByKey = new HashMap<>( INIT_COLL_SIZE );
|
||||
entitiesByUniqueKey = new HashMap<>( INIT_COLL_SIZE );
|
||||
entitySnapshotsByKey = new HashMap<>( INIT_COLL_SIZE );
|
||||
|
||||
entityEntryContext = new EntityEntryContext( this );
|
||||
|
@ -244,7 +244,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
|
|||
|
||||
arrayHolders = null;
|
||||
entitiesByKey.clear();
|
||||
entitiesByUniqueKey.clear();
|
||||
entitiesByUniqueKey = null;
|
||||
entityEntryContext.clear();
|
||||
parentsByChild = null;
|
||||
entitySnapshotsByKey.clear();
|
||||
|
@ -406,12 +406,15 @@ public class StatefulPersistenceContext implements PersistenceContext {
|
|||
@Override
|
||||
public Object removeEntity(EntityKey key) {
|
||||
final Object entity = entitiesByKey.remove( key );
|
||||
if ( entitiesByUniqueKey != null ) {
|
||||
final Iterator itr = entitiesByUniqueKey.values().iterator();
|
||||
while ( itr.hasNext() ) {
|
||||
if ( itr.next() == entity ) {
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear all parent cache
|
||||
parentsByChild = null;
|
||||
entitySnapshotsByKey.remove( key );
|
||||
|
@ -427,11 +430,14 @@ public class StatefulPersistenceContext implements PersistenceContext {
|
|||
|
||||
@Override
|
||||
public Object getEntity(EntityUniqueKey euk) {
|
||||
return entitiesByUniqueKey.get( euk );
|
||||
return entitiesByUniqueKey == null ? null : entitiesByUniqueKey.get( euk );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addEntity(EntityUniqueKey euk, Object entity) {
|
||||
if ( entitiesByUniqueKey == null ) {
|
||||
entitiesByUniqueKey = new HashMap<>( INIT_COLL_SIZE );
|
||||
}
|
||||
entitiesByUniqueKey.put( euk, entity );
|
||||
}
|
||||
|
||||
|
@ -1528,6 +1534,10 @@ public class StatefulPersistenceContext implements PersistenceContext {
|
|||
oos.writeObject( entry.getValue() );
|
||||
}
|
||||
|
||||
if ( entitiesByUniqueKey == null ) {
|
||||
oos.writeInt( 0 );
|
||||
}
|
||||
else {
|
||||
oos.writeInt( entitiesByUniqueKey.size() );
|
||||
if ( LOG.isTraceEnabled() ) {
|
||||
LOG.trace( "Starting serialization of [" + entitiesByUniqueKey.size() + "] entitiesByUniqueKey entries" );
|
||||
|
@ -1536,6 +1546,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
|
|||
entry.getKey().serialize( oos );
|
||||
oos.writeObject( entry.getValue() );
|
||||
}
|
||||
}
|
||||
|
||||
if ( proxiesByKey == null ) {
|
||||
oos.writeInt( 0 );
|
||||
|
@ -1655,10 +1666,12 @@ public class StatefulPersistenceContext implements PersistenceContext {
|
|||
if ( LOG.isTraceEnabled() ) {
|
||||
LOG.trace( "Starting deserialization of [" + count + "] entitiesByUniqueKey entries" );
|
||||
}
|
||||
if ( count != 0 ) {
|
||||
rtn.entitiesByUniqueKey = new HashMap<>( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
|
||||
for ( int i = 0; i < count; i++ ) {
|
||||
rtn.entitiesByUniqueKey.put( EntityUniqueKey.deserialize( ois, session ), ois.readObject() );
|
||||
}
|
||||
}
|
||||
|
||||
count = ois.readInt();
|
||||
if ( LOG.isTraceEnabled() ) {
|
||||
|
|
Loading…
Reference in New Issue