[hibernate-commits] Hibernate SVN: r11400 - trunk/Hibernate3/src/org/hibernate/engine/loading.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Apr 11 10:07:13 EDT 2007


Author: steve.ebersole at jboss.com
Date: 2007-04-11 10:07:13 -0400 (Wed, 11 Apr 2007)
New Revision: 11400

Modified:
   trunk/Hibernate3/src/org/hibernate/engine/loading/CollectionLoadContext.java
   trunk/Hibernate3/src/org/hibernate/engine/loading/LoadContexts.java
Log:
HHH-2553 : load contexts

Modified: trunk/Hibernate3/src/org/hibernate/engine/loading/CollectionLoadContext.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/engine/loading/CollectionLoadContext.java	2007-04-11 09:41:39 UTC (rev 11399)
+++ trunk/Hibernate3/src/org/hibernate/engine/loading/CollectionLoadContext.java	2007-04-11 14:07:13 UTC (rev 11400)
@@ -3,10 +3,11 @@
 import java.sql.ResultSet;
 import java.io.Serializable;
 import java.util.Map;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Iterator;
 import java.util.ArrayList;
+import java.util.Set;
+import java.util.HashSet;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -39,7 +40,7 @@
 
 	private final LoadContexts loadContexts;
 	private final ResultSet resultSet;
-	private final Map loadingCollections = new HashMap( 8 );
+	private Set localLoadingCollectionKeys = new HashSet();
 
 	/**
 	 * Creates a collection load context for the given result set.
@@ -88,7 +89,7 @@
 		if ( log.isTraceEnabled() ) {
 			log.trace( "starting attempt to find loading collection [" + MessageHelper.collectionInfoString( persister.getRole(), key ) + "]" );
 		}
-		final LoadingCollectionEntry loadingCollectionEntry = locateLoadingCollectionEntry( collectionKey );
+		final LoadingCollectionEntry loadingCollectionEntry = loadContexts.locateLoadingCollectionEntry( collectionKey );
 		if ( loadingCollectionEntry == null ) {
 			// look for existing collection as part of the persistence context
 			PersistentCollection collection = loadContexts.getPersistenceContext().getCollection( collectionKey );
@@ -124,7 +125,8 @@
 			}
 			collection.beforeInitialize( persister, -1 );
 			collection.beginRead();
-			loadingCollections.put( collectionKey, new LoadingCollectionEntry( resultSet, persister, key, collection ) );
+			localLoadingCollectionKeys.add( collectionKey );
+			loadContexts.registerLoadingCollectionEntry( collectionKey, new LoadingCollectionEntry( resultSet, persister, key, collection ) );
 			return collection;
 		}
 		else {
@@ -141,27 +143,6 @@
 		}
 	}
 
-	private LoadingCollectionEntry locateLoadingCollectionEntry(CollectionKey collectionKey) {
-		if ( log.isTraceEnabled() ) {
-			log.trace( "attempting to locate loading collection entry [" + collectionKey + "]" );
-		}
-		// first try our loading collections
-		LoadingCollectionEntry loadingCollectionEntry = getLocalLoadingCollectionEntry( collectionKey );
-		if ( loadingCollectionEntry == null ) {
-			// the loading collection is not associated with our result set, so check the other
-			// result sets registered with the load context...
-			loadingCollectionEntry = loadContexts.locateLoadingCollectionEntry( collectionKey, this );
-		}
-		return loadingCollectionEntry;
-	}
-
-	LoadingCollectionEntry getLocalLoadingCollectionEntry(CollectionKey key) {
-		if ( log.isTraceEnabled() ) {
-			log.trace( "attempting to locally locate loading collection entry [key=" + key + ", rs=" + resultSet + "]" );
-		}
-		return ( LoadingCollectionEntry ) loadingCollections.get( key );
-	}
-
 	/**
 	 * Finish the process of collection-loading for this bound result set.  Mainly this
 	 * involves cleaning up resources and notifying the collections that loading is
@@ -171,6 +152,11 @@
 	 */
 	public void endLoadingCollections(CollectionPersister persister) {
 		SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
+		if ( loadContexts.getLoadingCollectionEntryMap() == null
+				|| loadContexts.getLoadingCollectionEntryMap().isEmpty()
+				|| localLoadingCollectionKeys.isEmpty() ) {
+			return;
+		}
 
 		// in an effort to avoid concurrent-modification-exceptions (from
 		// potential recursive calls back through here as a result of the
@@ -179,10 +165,12 @@
 		// in a temp collection.  the temp collection is then used to "drive"
 		// the #endRead processing.
 		List matches = null;
-		Iterator iter = loadingCollections.values().iterator();
+		Iterator iter = loadContexts.getLoadingCollectionEntryMap().entrySet().iterator();
 		while ( iter.hasNext() ) {
-			LoadingCollectionEntry lce = (LoadingCollectionEntry) iter.next();
-			if ( lce.getResultSet() == resultSet && lce.getPersister() == persister) {
+			final Map.Entry mapEntry = ( Map.Entry ) iter.next();
+			final CollectionKey collectionKey = ( CollectionKey ) mapEntry.getKey();
+			final LoadingCollectionEntry lce = ( LoadingCollectionEntry ) mapEntry.getValue();
+			if ( localLoadingCollectionKeys.contains( collectionKey ) && lce.getResultSet() == resultSet && lce.getPersister() == persister) {
 				if ( matches == null ) {
 					matches = new ArrayList();
 				}
@@ -204,8 +192,14 @@
 	}
 
 	private void endLoadingCollections(CollectionPersister persister, List matchedCollectionEntries) {
-		final int count = ( matchedCollectionEntries == null ) ? 0 : matchedCollectionEntries.size();
+		if ( matchedCollectionEntries == null ) {
+			if ( log.isDebugEnabled() ) {
+				log.debug( "no collections were found in result set for role: " + persister.getRole() );
+			}
+			return;
+		}
 
+		final int count = matchedCollectionEntries.size();
 		if ( log.isDebugEnabled() ) {
 			log.debug( count + " collections were found in result set for role: " + persister.getRole() );
 		}
@@ -305,7 +299,7 @@
 				persister.getCacheEntryStructure().structure(entry),
 				session.getTimestamp(),
 				version,
-				( factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH )
+				factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
 		);
 
 		if ( put && factory.getStatistics().isStatisticsEnabled() ) {
@@ -314,10 +308,11 @@
 	}
 
 	void cleanup() {
-		if ( !loadingCollections.isEmpty() ) {
-			log.warn( "On CollectionLoadContext#clear, loadingCollections contained [" + loadingCollections.size() + "] entries" );
+		if ( !localLoadingCollectionKeys.isEmpty() ) {
+			log.warn( "On CollectionLoadContext#clear, loadingCollections contained [" + localLoadingCollectionKeys.size() + "] entries" );
 		}
-		loadingCollections.clear();
+		loadContexts.cleanupCollectionEntries( localLoadingCollectionKeys );
+		localLoadingCollectionKeys.clear();
 	}
 
 

Modified: trunk/Hibernate3/src/org/hibernate/engine/loading/LoadContexts.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/engine/loading/LoadContexts.java	2007-04-11 09:41:39 UTC (rev 11399)
+++ trunk/Hibernate3/src/org/hibernate/engine/loading/LoadContexts.java	2007-04-11 14:07:13 UTC (rev 11400)
@@ -2,7 +2,9 @@
 
 import java.sql.ResultSet;
 import java.util.Map;
+import java.util.Set;
 import java.util.Iterator;
+import java.util.HashMap;
 import java.io.Serializable;
 
 import org.apache.commons.logging.Log;
@@ -40,6 +42,8 @@
 	private Map collectionLoadContexts;
 	private Map entityLoadContexts;
 
+	private Map xrefLoadingCollectionEntries;
+
 	/**
 	 * Creates and binds this to the given persistence context.
 	 *
@@ -93,7 +97,7 @@
 	 * @return The loading collection, or null if not found.
 	 */
 	public PersistentCollection locateLoadingCollection(CollectionPersister persister, Serializable ownerKey) {
-		LoadingCollectionEntry lce = locateLoadingCollectionEntry( new CollectionKey( persister, ownerKey, getEntityMode() ), null ); // note: null because here we are interested in all contexts...
+		LoadingCollectionEntry lce = locateLoadingCollectionEntry( new CollectionKey( persister, ownerKey, getEntityMode() ) );
 		if ( lce != null ) {
 			if ( log.isTraceEnabled() ) {
 				log.trace( "returning loading collection:" + MessageHelper.collectionInfoString( persister, ownerKey, getSession().getFactory() ) );
@@ -118,34 +122,46 @@
 	 * being loaded by other {@link CollectionLoadContext}s/{@link ResultSet}s.
 	 *
 	 * @param key The collection key.
-	 * @param caller The collection load context making this call (for performance optimization)
 	 * @return The located entry; or null.
 	 */
-	LoadingCollectionEntry locateLoadingCollectionEntry(CollectionKey key, CollectionLoadContext caller) {
-		if ( collectionLoadContexts == null ) {
+	LoadingCollectionEntry locateLoadingCollectionEntry(CollectionKey key) {
+		if ( xrefLoadingCollectionEntries == null ) {
 			return null;
 		}
 		if ( log.isTraceEnabled() ) {
 			log.trace( "attempting to locate loading collection entry [" + key + "] in any result-set context" );
 		}
-		LoadingCollectionEntry rtn = null;
-		Iterator itr = collectionLoadContexts.values().iterator();
-		while ( itr.hasNext() ) {
-			final CollectionLoadContext collectionLoadContext = ( CollectionLoadContext ) itr.next();
-			if ( collectionLoadContext == caller ) {
-				continue;
+		LoadingCollectionEntry rtn = ( LoadingCollectionEntry ) xrefLoadingCollectionEntries.get( key );
+		if ( log.isTraceEnabled() ) {
+			if ( rtn == null ) {
+				log.trace( "collection [" + key + "] located in load context" );
 			}
-			rtn = collectionLoadContext.getLocalLoadingCollectionEntry( key );
-			if ( rtn != null ) {
-				if ( log.isTraceEnabled() ) {
-					log.trace( "collection [" + key + "] located in load context [" + collectionLoadContext + "]" );
-				}
-				break;
+			else {
+				log.trace( "collection [" + key + "] not located in load context" );
 			}
 		}
 		return rtn;
 	}
 
+	/*package*/void registerLoadingCollectionEntry(CollectionKey entryKey, LoadingCollectionEntry entry) {
+		if ( xrefLoadingCollectionEntries == null ) {
+			xrefLoadingCollectionEntries = new HashMap();
+		}
+		xrefLoadingCollectionEntries.put( entryKey, entry );
+	}
+
+	/*package*/Map getLoadingCollectionEntryMap() {
+		return xrefLoadingCollectionEntries;
+	}
+
+	/*package*/void cleanupCollectionEntries(Set entryKeys) {
+		Iterator itr = entryKeys.iterator();
+		while ( itr.hasNext() ) {
+			final CollectionKey entryKey = ( CollectionKey ) itr.next();
+			xrefLoadingCollectionEntries.remove( entryKey );
+		}
+	}
+
 	public EntityLoadContext getEntityLoadContext(ResultSet resultSet) {
 		EntityLoadContext context = null;
 		if ( entityLoadContexts == null ) {
@@ -180,5 +196,4 @@
 		return getSession().getEntityMode();
 	}
 
-
 }




More information about the hibernate-commits mailing list