Skip to content

Commit 8073079

Browse files
committed
HHH-18326 Use instance identity to track persistent collections
1 parent 2302b42 commit 8073079

File tree

5 files changed

+232
-20
lines changed

5 files changed

+232
-20
lines changed

hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
7979
private String sessionFactoryUuid;
8080
private boolean allowLoadOutsideTransaction;
8181

82+
private transient int instanceId;
83+
8284
/**
8385
* Not called by Hibernate, but used by non-JDK serialization,
8486
* eg. SOAP libraries.
@@ -1362,4 +1364,13 @@ public void setOwner(Object owner) {
13621364
this.owner = owner;
13631365
}
13641366

1367+
@Override
1368+
public int $$_hibernate_getInstanceId() {
1369+
return instanceId;
1370+
}
1371+
1372+
@Override
1373+
public void $$_hibernate_setInstanceId(int instanceId) {
1374+
this.instanceId = instanceId;
1375+
}
13651376
}

hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentCollection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.hibernate.HibernateException;
1313
import org.hibernate.Incubating;
14+
import org.hibernate.engine.spi.InstanceIdentity;
1415
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1516
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
1617
import org.hibernate.persister.collection.CollectionPersister;
@@ -52,7 +53,7 @@
5253
* @author Gavin King
5354
*/
5455
@Incubating
55-
public interface PersistentCollection<E> extends LazyInitializable {
56+
public interface PersistentCollection<E> extends LazyInitializable, InstanceIdentity {
5657
/**
5758
* Get the owning entity. Note that the owner is only
5859
* set during the flush cycle, and when a new collection

hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import org.hibernate.event.spi.PostLoadEventListener;
5656
import org.hibernate.internal.CoreMessageLogger;
5757
import org.hibernate.internal.util.collections.CollectionHelper;
58-
import org.hibernate.internal.util.collections.IdentityMap;
58+
import org.hibernate.internal.util.collections.InstanceIdentityMap;
5959
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
6060
import org.hibernate.persister.collection.CollectionPersister;
6161
import org.hibernate.persister.entity.EntityPersister;
@@ -131,7 +131,10 @@ the following fields are used in all circumstances, and are not worth (or not su
131131
private IdentityHashMap<Object, PersistentCollection<?>> arrayHolders;
132132

133133
// Identity map of CollectionEntry instances, by the collection wrapper
134-
private IdentityMap<PersistentCollection<?>, CollectionEntry> collectionEntries;
134+
private InstanceIdentityMap<PersistentCollection<?>, CollectionEntry> collectionEntries;
135+
136+
// Current collection instance id
137+
private transient int currentCollectionInstanceId;
135138

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

256259
final SharedSessionContractImplementor session = getSession();
257260
if ( collectionEntries != null ) {
258-
IdentityMap.onEachKey( collectionEntries, k -> k.unsetSession( session ) );
261+
collectionEntries.forEach( (k, v) -> k.unsetSession( session ) );
259262
}
260263

261264
arrayHolders = null;
@@ -267,6 +270,7 @@ public void clear() {
267270
collectionsByKey = null;
268271
nonlazyCollections = null;
269272
collectionEntries = null;
273+
currentCollectionInstanceId = 0;
270274
unownedCollections = null;
271275
nullifiableEntityKeys = null;
272276
deletedUnloadedEntityKeys = null;
@@ -613,7 +617,7 @@ public boolean isEntryFor(Object entity) {
613617

614618
@Override
615619
public CollectionEntry getCollectionEntry(PersistentCollection<?> coll) {
616-
return collectionEntries == null ? null : collectionEntries.get( coll );
620+
return collectionEntries == null ? null : collectionEntries.get( coll.$$_hibernate_getInstanceId(), coll );
617621
}
618622

619623
@Override
@@ -725,7 +729,7 @@ public EntityEntry addReferenceEntry(
725729

726730
@Override
727731
public boolean containsCollection(PersistentCollection<?> collection) {
728-
return collectionEntries != null && collectionEntries.containsKey( collection );
732+
return collectionEntries != null && collectionEntries.containsKey( collection.$$_hibernate_getInstanceId(), collection );
729733
}
730734

731735
@Override
@@ -1082,8 +1086,7 @@ public void replaceCollection(CollectionPersister persister, PersistentCollectio
10821086
"Replacement of not directly accessible collection found: " + oldCollection.getRole() );
10831087
}
10841088
assert !collection.isDirectlyAccessible();
1085-
final IdentityMap<PersistentCollection<?>, CollectionEntry> collectionEntries = getOrInitializeCollectionEntries();
1086-
final CollectionEntry oldEntry = collectionEntries.remove( oldCollection );
1089+
final CollectionEntry oldEntry = collectionEntries.remove( oldCollection.$$_hibernate_getInstanceId(), oldCollection );
10871090
final CollectionEntry entry;
10881091
if ( oldEntry.getLoadedPersister() != null ) {
10891092
// This is an already existing/loaded collection so ensure the loadedPersister is initialized
@@ -1093,7 +1096,7 @@ public void replaceCollection(CollectionPersister persister, PersistentCollectio
10931096
// A newly wrapped collection
10941097
entry = new CollectionEntry( persister, collection );
10951098
}
1096-
collectionEntries.put( collection, entry );
1099+
putCollectionEntry( collection, entry );
10971100
final Object key = collection.getKey();
10981101
if ( key != null ) {
10991102
final CollectionKey collectionKey = new CollectionKey( entry.getLoadedPersister(), key );
@@ -1112,7 +1115,7 @@ public void replaceCollection(CollectionPersister persister, PersistentCollectio
11121115
* @param key The key of the collection's entry.
11131116
*/
11141117
private void addCollection(PersistentCollection<?> coll, CollectionEntry entry, Object key) {
1115-
getOrInitializeCollectionEntries().put( coll, entry );
1118+
putCollectionEntry( coll, entry );
11161119
final CollectionKey collectionKey = new CollectionKey( entry.getLoadedPersister(), key );
11171120
final PersistentCollection<?> old = addCollectionByKey( collectionKey, coll );
11181121
if ( old != null ) {
@@ -1125,11 +1128,12 @@ private void addCollection(PersistentCollection<?> coll, CollectionEntry entry,
11251128
}
11261129
}
11271130

1128-
private IdentityMap<PersistentCollection<?>, CollectionEntry> getOrInitializeCollectionEntries() {
1131+
private void putCollectionEntry(PersistentCollection<?> collection, CollectionEntry entry) {
11291132
if ( this.collectionEntries == null ) {
1130-
this.collectionEntries = IdentityMap.instantiateSequenced( INIT_COLL_SIZE );
1133+
this.collectionEntries = new InstanceIdentityMap<>();
11311134
}
1132-
return this.collectionEntries;
1135+
collection.$$_hibernate_setInstanceId( nextCollectionInstanceId() );
1136+
this.collectionEntries.put( collection, entry );
11331137
}
11341138

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

11461150
@Override
@@ -1375,11 +1379,15 @@ public int getNumberOfManagedEntities() {
13751379
return collectionEntries;
13761380
}
13771381

1382+
private int nextCollectionInstanceId() {
1383+
return currentCollectionInstanceId++;
1384+
}
1385+
13781386
@Override
13791387
public void forEachCollectionEntry(BiConsumer<PersistentCollection<?>, CollectionEntry> action, boolean concurrent) {
13801388
if ( collectionEntries != null ) {
13811389
if ( concurrent ) {
1382-
for ( Entry<PersistentCollection<?>,CollectionEntry> entry : IdentityMap.concurrentEntries( collectionEntries ) ) {
1390+
for ( Entry<PersistentCollection<?>,CollectionEntry> entry : collectionEntries.toArray() ) {
13831391
action.accept( entry.getKey(), entry.getValue() );
13841392
}
13851393
}
@@ -2032,7 +2040,7 @@ public static StatefulPersistenceContext deserialize(
20322040
final PersistentCollection<?> pc = (PersistentCollection<?>) ois.readObject();
20332041
final CollectionEntry ce = CollectionEntry.deserialize( ois, session );
20342042
pc.setCurrentSession( session );
2035-
rtn.getOrInitializeCollectionEntries().put( pc, ce );
2043+
rtn.putCollectionEntry( pc, ce );
20362044
}
20372045

20382046
count = ois.readInt();
@@ -2174,7 +2182,12 @@ public int getCollectionEntriesSize() {
21742182

21752183
@Override
21762184
public CollectionEntry removeCollectionEntry(PersistentCollection<?> collection) {
2177-
return collectionEntries == null ? null : collectionEntries.remove(collection);
2185+
if ( collectionEntries != null ) {
2186+
return collectionEntries.remove( collection.$$_hibernate_getInstanceId(), collection );
2187+
}
2188+
else {
2189+
return null;
2190+
}
21782191
}
21792192

21802193
@Override

hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.hibernate.event.spi.PersistContext;
3535
import org.hibernate.internal.CoreMessageLogger;
3636
import org.hibernate.internal.util.EntityPrinter;
37-
import org.hibernate.internal.util.collections.IdentityMap;
37+
import org.hibernate.internal.util.collections.InstanceIdentityMap;
3838
import org.hibernate.persister.entity.EntityPersister;
3939

4040
import org.jboss.logging.Logger;
@@ -190,7 +190,7 @@ private void prepareCollectionFlushes(PersistenceContext persistenceContext) thr
190190
persistenceContext.getCollectionEntries();
191191
if ( collectionEntries != null ) {
192192
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> entry :
193-
( (IdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).entryArray() ) {
193+
( (InstanceIdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).toArray() ) {
194194
entry.getValue().preFlush( entry.getKey() );
195195
}
196196
}
@@ -271,7 +271,7 @@ private int flushCollections(final EventSource session, final PersistenceContext
271271
}
272272
else {
273273
count = collectionEntries.size();
274-
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> me : ( (IdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).entryArray() ) {
274+
for ( Map.Entry<PersistentCollection<?>, CollectionEntry> me : ( (InstanceIdentityMap<PersistentCollection<?>, CollectionEntry>) collectionEntries ).toArray() ) {
275275
final CollectionEntry ce = me.getValue();
276276
if ( !ce.isReached() && !ce.isIgnore() ) {
277277
Collections.processUnreachableCollection( me.getKey(), session );

0 commit comments

Comments
 (0)