[hibernate-commits] Hibernate SVN: r11401 - branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Wed Apr 11 10:08:11 EDT 2007
Author: steve.ebersole at jboss.com
Date: 2007-04-11 10:08:11 -0400 (Wed, 11 Apr 2007)
New Revision: 11401
Modified:
branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading/CollectionLoadContext.java
branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading/LoadContexts.java
Log:
HHH-2553 : load contexts
Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading/CollectionLoadContext.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading/CollectionLoadContext.java 2007-04-11 14:07:13 UTC (rev 11400)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading/CollectionLoadContext.java 2007-04-11 14:08:11 UTC (rev 11401)
@@ -3,11 +3,12 @@
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.Comparator;
+import java.util.Set;
+import java.util.HashSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -40,7 +41,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.
@@ -89,7 +90,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 );
@@ -125,7 +126,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 {
@@ -142,27 +144,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
@@ -172,6 +153,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
@@ -180,10 +166,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();
}
@@ -205,8 +193,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() );
}
@@ -319,10 +313,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: branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading/LoadContexts.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading/LoadContexts.java 2007-04-11 14:07:13 UTC (rev 11400)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/engine/loading/LoadContexts.java 2007-04-11 14:08:11 UTC (rev 11401)
@@ -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