Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-18326 Use instance identity to track persistent collections
Browse files Browse the repository at this point in the history
mbladel committed Jan 21, 2025
1 parent 87850a7 commit 03d473a
Showing 5 changed files with 243 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -79,6 +79,8 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
private String sessionFactoryUuid;
private boolean allowLoadOutsideTransaction;

private int instanceId;

/**
* Not called by Hibernate, but used by non-JDK serialization,
* eg. SOAP libraries.
@@ -679,6 +681,7 @@ public final boolean unsetSession(SharedSessionContractImplementor currentSessio
&& session.getLoadQueryInfluencers().hasEnabledFilters() ) {
LOG.enabledFiltersWhenDetachFromSession( collectionInfoString( getRole(), getKey() ) );
}
instanceId = -1;
session = null;
}
return true;
@@ -1362,4 +1365,13 @@ public void setOwner(Object owner) {
this.owner = owner;
}

@Override
public int $$_hibernate_getInstanceId() {
return instanceId;
}

@Override
public void $$_hibernate_setInstanceId(int instanceId) {
this.instanceId = instanceId;
}
}
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

import org.hibernate.HibernateException;
import org.hibernate.Incubating;
import org.hibernate.engine.spi.InstanceIdentity;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
@@ -52,7 +53,7 @@
* @author Gavin King
*/
@Incubating
public interface PersistentCollection<E> extends LazyInitializable {
public interface PersistentCollection<E> extends LazyInitializable, InstanceIdentity {
/**
* Get the owning entity. Note that the owner is only
* set during the flush cycle, and when a new collection
Original file line number Diff line number Diff line change
@@ -55,7 +55,8 @@
import org.hibernate.event.spi.PostLoadEventListener;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.internal.util.collections.IdentityMap;
import org.hibernate.internal.util.collections.InstanceIdentityMap;
import org.hibernate.internal.util.collections.StandardStack;
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister;
@@ -131,7 +132,11 @@ the following fields are used in all circumstances, and are not worth (or not su
private IdentityHashMap<Object, PersistentCollection<?>> arrayHolders;

// Identity map of CollectionEntry instances, by the collection wrapper
private IdentityMap<PersistentCollection<?>, CollectionEntry> collectionEntries;
private InstanceIdentityMap<PersistentCollection<?>, CollectionEntry> collectionEntries;

// current collection instance id and stack of reusable ones
private StandardStack<Integer> reusableCollectionInstanceIds = null;
private int currentCollectionInstanceId;

// Collection wrappers, by the CollectionKey
private HashMap<CollectionKey, PersistentCollection<?>> collectionsByKey;
@@ -255,7 +260,7 @@ public void clear() {

final SharedSessionContractImplementor session = getSession();
if ( collectionEntries != null ) {
IdentityMap.onEachKey( collectionEntries, k -> k.unsetSession( session ) );
collectionEntries.forEach( (k, v) -> k.unsetSession( session ) );
}

arrayHolders = null;
@@ -267,6 +272,8 @@ public void clear() {
collectionsByKey = null;
nonlazyCollections = null;
collectionEntries = null;
currentCollectionInstanceId = 0;
reusableCollectionInstanceIds = null;
unownedCollections = null;
nullifiableEntityKeys = null;
deletedUnloadedEntityKeys = null;
@@ -613,7 +620,7 @@ public boolean isEntryFor(Object entity) {

@Override
public CollectionEntry getCollectionEntry(PersistentCollection<?> coll) {
return collectionEntries == null ? null : collectionEntries.get( coll );
return collectionEntries == null ? null : collectionEntries.get( coll.$$_hibernate_getInstanceId(), coll );
}

@Override
@@ -725,7 +732,7 @@ public EntityEntry addReferenceEntry(

@Override
public boolean containsCollection(PersistentCollection<?> collection) {
return collectionEntries != null && collectionEntries.containsKey( collection );
return collectionEntries != null && collectionEntries.containsKey( collection.$$_hibernate_getInstanceId(), collection );
}

@Override
@@ -1082,8 +1089,7 @@ public void replaceCollection(CollectionPersister persister, PersistentCollectio
"Replacement of not directly accessible collection found: " + oldCollection.getRole() );
}
assert !collection.isDirectlyAccessible();
final IdentityMap<PersistentCollection<?>, CollectionEntry> collectionEntries = getOrInitializeCollectionEntries();
final CollectionEntry oldEntry = collectionEntries.remove( oldCollection );
final CollectionEntry oldEntry = collectionEntries.remove( oldCollection.$$_hibernate_getInstanceId(), oldCollection );
final CollectionEntry entry;
if ( oldEntry.getLoadedPersister() != null ) {
// This is an already existing/loaded collection so ensure the loadedPersister is initialized
@@ -1093,7 +1099,7 @@ public void replaceCollection(CollectionPersister persister, PersistentCollectio
// A newly wrapped collection
entry = new CollectionEntry( persister, collection );
}
collectionEntries.put( collection, entry );
putCollectionEntry( collection, entry );
final Object key = collection.getKey();
if ( key != null ) {
final CollectionKey collectionKey = new CollectionKey( entry.getLoadedPersister(), key );
@@ -1112,7 +1118,7 @@ public void replaceCollection(CollectionPersister persister, PersistentCollectio
* @param key The key of the collection's entry.
*/
private void addCollection(PersistentCollection<?> coll, CollectionEntry entry, Object key) {
getOrInitializeCollectionEntries().put( coll, entry );
putCollectionEntry( coll, entry );
final CollectionKey collectionKey = new CollectionKey( entry.getLoadedPersister(), key );
final PersistentCollection<?> old = addCollectionByKey( collectionKey, coll );
if ( old != null ) {
@@ -1125,11 +1131,12 @@ private void addCollection(PersistentCollection<?> coll, CollectionEntry entry,
}
}

private IdentityMap<PersistentCollection<?>, CollectionEntry> getOrInitializeCollectionEntries() {
private void putCollectionEntry(PersistentCollection<?> collection, CollectionEntry entry) {
if ( this.collectionEntries == null ) {
this.collectionEntries = IdentityMap.instantiateSequenced( INIT_COLL_SIZE );
this.collectionEntries = new InstanceIdentityMap<>();
}
return this.collectionEntries;
collection.$$_hibernate_setInstanceId( nextCollectionInstanceId() );
this.collectionEntries.put( collection, entry );
}

/**
@@ -1140,7 +1147,7 @@ private IdentityMap<PersistentCollection<?>, CollectionEntry> getOrInitializeCol
*/
private void addCollection(PersistentCollection<?> collection, CollectionPersister persister) {
final CollectionEntry ce = new CollectionEntry( persister, collection );
getOrInitializeCollectionEntries().put( collection, ce );
putCollectionEntry( collection, ce );
}

@Override
@@ -1375,11 +1382,17 @@ public int getNumberOfManagedEntities() {
return collectionEntries;
}

private int nextCollectionInstanceId() {
return reusableCollectionInstanceIds != null && !reusableCollectionInstanceIds.isEmpty() ?
reusableCollectionInstanceIds.pop() :
currentCollectionInstanceId++;
}

@Override
public void forEachCollectionEntry(BiConsumer<PersistentCollection<?>, CollectionEntry> action, boolean concurrent) {
if ( collectionEntries != null ) {
if ( concurrent ) {
for ( Entry<PersistentCollection<?>,CollectionEntry> entry : IdentityMap.concurrentEntries( collectionEntries ) ) {
for ( Entry<PersistentCollection<?>,CollectionEntry> entry : collectionEntries.toArray() ) {
action.accept( entry.getKey(), entry.getValue() );
}
}
@@ -2032,7 +2045,7 @@ public static StatefulPersistenceContext deserialize(
final PersistentCollection<?> pc = (PersistentCollection<?>) ois.readObject();
final CollectionEntry ce = CollectionEntry.deserialize( ois, session );
pc.setCurrentSession( session );
rtn.getOrInitializeCollectionEntries().put( pc, ce );
rtn.putCollectionEntry( pc, ce );
}

count = ois.readInt();
@@ -2174,7 +2187,17 @@ public int getCollectionEntriesSize() {

@Override
public CollectionEntry removeCollectionEntry(PersistentCollection<?> collection) {
return collectionEntries == null ? null : collectionEntries.remove(collection);
if ( collectionEntries != null ) {
final int instanceId = collection.$$_hibernate_getInstanceId();
if ( reusableCollectionInstanceIds == null ) {
reusableCollectionInstanceIds = new StandardStack<>();
}
reusableCollectionInstanceIds.push( instanceId );
return collectionEntries.remove( instanceId, collection );
}
else {
return null;
}
}

@Override
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
import org.hibernate.event.spi.PersistContext;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.EntityPrinter;
import org.hibernate.internal.util.collections.IdentityMap;
import org.hibernate.internal.util.collections.InstanceIdentityMap;
import org.hibernate.persister.entity.EntityPersister;

import org.jboss.logging.Logger;
@@ -190,7 +190,7 @@ private void prepareCollectionFlushes(PersistenceContext persistenceContext) thr
persistenceContext.getCollectionEntries();
if ( collectionEntries != null ) {
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> entry :
( (IdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).entryArray() ) {
( (InstanceIdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).toArray() ) {
entry.getValue().preFlush( entry.getKey() );
}
}
@@ -271,7 +271,7 @@ private int flushCollections(final EventSource session, final PersistenceContext
}
else {
count = collectionEntries.size();
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> me : ( (IdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).entryArray() ) {
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> me : ( (InstanceIdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).toArray() ) {
final CollectionEntry ce = me.getValue();
if ( !ce.isReached() && !ce.isIgnore() ) {
Collections.processUnreachableCollection( me.getKey(), session );
Loading

0 comments on commit 03d473a

Please sign in to comment.