Skip to content

Commit

Permalink
HHH-8211 - Checkstyle and FindBugs fix-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed May 21, 2013
1 parent f207311 commit 286800e
Show file tree
Hide file tree
Showing 21 changed files with 570 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class CollectionLoadContext {

private final LoadContexts loadContexts;
private final ResultSet resultSet;
private Set localLoadingCollectionKeys = new HashSet();
private Set<CollectionKey> localLoadingCollectionKeys = new HashSet<CollectionKey>();

/**
* Creates a collection load context for the given result set.
Expand Down Expand Up @@ -119,12 +119,13 @@ public PersistentCollection getLoadingCollection(final CollectionPersister persi
if ( collection != null ) {
if ( collection.wasInitialized() ) {
LOG.trace( "Collection already initialized; ignoring" );
return null; // ignore this row of results! Note the early exit
// ignore this row of results! Note the early exit
return null;
}
LOG.trace( "Collection not yet initialized; initializing" );
}
else {
Object owner = loadContexts.getPersistenceContext().getCollectionOwner( key, persister );
final Object owner = loadContexts.getPersistenceContext().getCollectionOwner( key, persister );
final boolean newlySavedEntity = owner != null
&& loadContexts.getPersistenceContext().getEntry( owner ).getStatus() != Status.LOADING;
if ( newlySavedEntity ) {
Expand Down Expand Up @@ -162,7 +163,7 @@ public PersistentCollection getLoadingCollection(final CollectionPersister persi
* @param persister The persister for which to complete loading.
*/
public void endLoadingCollections(CollectionPersister persister) {
SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
if ( !loadContexts.hasLoadingCollectionEntries()
&& localLoadingCollectionKeys.isEmpty() ) {
return;
Expand All @@ -174,17 +175,17 @@ public void endLoadingCollections(CollectionPersister persister) {
// internal loadingCollections map for matches and store those matches
// in a temp collection. the temp collection is then used to "drive"
// the #endRead processing.
List matches = null;
Iterator iter = localLoadingCollectionKeys.iterator();
while ( iter.hasNext() ) {
final CollectionKey collectionKey = (CollectionKey) iter.next();
List<LoadingCollectionEntry> matches = null;
final Iterator itr = localLoadingCollectionKeys.iterator();
while ( itr.hasNext() ) {
final CollectionKey collectionKey = (CollectionKey) itr.next();
final LoadingCollectionEntry lce = loadContexts.locateLoadingCollectionEntry( collectionKey );
if ( lce == null ) {
LOG.loadingCollectionKeyNotFound( collectionKey );
}
else if ( lce.getResultSet() == resultSet && lce.getPersister() == persister ) {
if ( matches == null ) {
matches = new ArrayList();
matches = new ArrayList<LoadingCollectionEntry>();
}
matches.add( lce );
if ( lce.getCollection().getOwner() == null ) {
Expand All @@ -201,7 +202,7 @@ else if ( lce.getResultSet() == resultSet && lce.getPersister() == persister ) {

// todo : i'd much rather have this done from #endLoadingCollection(CollectionPersister,LoadingCollectionEntry)...
loadContexts.unregisterLoadingCollectionXRef( collectionKey );
iter.remove();
itr.remove();
}
}

Expand All @@ -217,29 +218,35 @@ else if ( lce.getResultSet() == resultSet && lce.getPersister() == persister ) {
}
}

private void endLoadingCollections(CollectionPersister persister, List matchedCollectionEntries) {
private void endLoadingCollections(CollectionPersister persister, List<LoadingCollectionEntry> matchedCollectionEntries) {
final boolean debugEnabled = LOG.isDebugEnabled();
if ( matchedCollectionEntries == null ) {
if ( debugEnabled ) LOG.debugf( "No collections were found in result set for role: %s", persister.getRole() );
if ( debugEnabled ) {
LOG.debugf( "No collections were found in result set for role: %s", persister.getRole() );
}
return;
}

final int count = matchedCollectionEntries.size();
if ( debugEnabled ) LOG.debugf("%s collections were found in result set for role: %s", count, persister.getRole());
if ( debugEnabled ) {
LOG.debugf( "%s collections were found in result set for role: %s", count, persister.getRole() );
}

for ( int i = 0; i < count; i++ ) {
LoadingCollectionEntry lce = ( LoadingCollectionEntry ) matchedCollectionEntries.get( i );
endLoadingCollection( lce, persister );
for ( LoadingCollectionEntry matchedCollectionEntry : matchedCollectionEntries ) {
endLoadingCollection( matchedCollectionEntry, persister );
}

if ( debugEnabled ) LOG.debugf( "%s collections initialized for role: %s", count, persister.getRole() );
if ( debugEnabled ) {
LOG.debugf( "%s collections initialized for role: %s", count, persister.getRole() );
}
}

private void endLoadingCollection(LoadingCollectionEntry lce, CollectionPersister persister) {
LOG.tracev( "Ending loading collection [{0}]", lce );
final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();

boolean hasNoQueuedAdds = lce.getCollection().endRead(); // warning: can cause a recursive calls! (proxy initialization)
// warning: can cause a recursive calls! (proxy initialization)
final boolean hasNoQueuedAdds = lce.getCollection().endRead();

if ( persister.getCollectionType().hasHolder() ) {
getLoadContext().getPersistenceContext().addCollectionHolder( lce.getCollection() );
Expand All @@ -255,25 +262,28 @@ private void endLoadingCollection(LoadingCollectionEntry lce, CollectionPersiste
// getLoadContext().getPersistenceContext().getBatchFetchQueue().removeBatchLoadableCollection(ce);
// }
}



boolean addToCache = hasNoQueuedAdds && // there were no queued additions
persister.hasCache() && // and the role has a cache
session.getCacheMode().isPutEnabled() &&
!ce.isDoremove(); // and this is not a forced initialization during flush
// add to cache if:
boolean addToCache =
// there were no queued additions
hasNoQueuedAdds
// and the role has a cache
&& persister.hasCache()
// and this is not a forced initialization during flush
&& session.getCacheMode().isPutEnabled() && !ce.isDoremove();
if ( addToCache ) {
addCollectionToCache( lce, persister );
}

if ( LOG.isDebugEnabled() ) {
LOG.debugf(
"Collection fully initialized: %s",
MessageHelper.collectionInfoString(persister, lce.getCollection(), lce.getKey(), session)
MessageHelper.collectionInfoString( persister, lce.getCollection(), lce.getKey(), session )
);
}
if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
session.getFactory().getStatisticsImplementor().loadCollection(persister.getRole());
session.getFactory().getStatisticsImplementor().loadCollection( persister.getRole() );
}
}

Expand Down Expand Up @@ -302,7 +312,8 @@ private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersiste
// currently this works in conjuction with the check on
// DefaultInitializeCollectionEventHandler.initializeCollectionFromCache() (which makes sure to not read from
// cache with enabled filters).
return; // EARLY EXIT!!!!!
// EARLY EXIT!!!!!
return;
}

final Object version;
Expand All @@ -315,7 +326,7 @@ private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersiste
// about its owner, that owner should be the same instance as associated with the PC, but we do the
// resolution against the PC anyway just to be safe since the lookup should not be costly.
if ( lce.getCollection() != null ) {
Object linkedOwner = lce.getCollection().getOwner();
final Object linkedOwner = lce.getCollection().getOwner();
if ( linkedOwner != null ) {
final Serializable ownerKey = persister.getOwnerEntityPersister().getIdentifier( linkedOwner, session );
collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( ownerKey, persister );
Expand All @@ -335,11 +346,11 @@ private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersiste
version = null;
}

CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
CacheKey cacheKey = session.generateCacheKey( lce.getKey(), persister.getKeyType(), persister.getRole() );
boolean put = persister.getCacheAccessStrategy().putFromLoad(
final CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
final CacheKey cacheKey = session.generateCacheKey( lce.getKey(), persister.getKeyType(), persister.getRole() );
final boolean put = persister.getCacheAccessStrategy().putFromLoad(
cacheKey,
persister.getCacheEntryStructure().structure(entry),
persister.getCacheEntryStructure().structure( entry ),
session.getTimestamp(),
version,
factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
Expand All @@ -360,7 +371,7 @@ void cleanup() {


@Override
public String toString() {
public String toString() {
return super.toString() + "<rs=" + resultSet + ">";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class EntityLoadContext {

private final LoadContexts loadContexts;
private final ResultSet resultSet;
private final List hydratingEntities = new ArrayList( 20 ); // todo : need map? the prob is a proper key, right?
// todo : need map? the prob is a proper key, right?
private final List hydratingEntities = new ArrayList( 20 );

public EntityLoadContext(LoadContexts loadContexts, ResultSet resultSet) {
this.loadContexts = loadContexts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,26 @@
import java.util.Map;
import java.util.Set;

import org.jboss.logging.Logger;

import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.collections.IdentityMap;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.pretty.MessageHelper;

/**
* Maps {@link ResultSet result-sets} to specific contextual data related to processing that result set
* <p/>
* Implementation note: internally an {@link IdentityMap} is used to maintain the mappings mainly because I'd
* rather not be dependent upon potentially bad {@link Object#equals} and {@link Object#hashCode} implementations on
* the JDBC result sets
* <p/>
* Considering the JDBC-redesign work, would further like this contextual info not mapped separately, but available
* based on the result set being processed. This would also allow maintaining a single mapping as we could reliably
* get notification of the result-set closing...
*
* @author Steve Ebersole
*/
public class LoadContexts {

private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, LoadContexts.class.getName());
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( LoadContexts.class );

private final PersistenceContext persistenceContext;
private Map<ResultSet,CollectionLoadContext> collectionLoadContexts;
Expand Down Expand Up @@ -101,11 +94,11 @@ private SessionImplementor getSession() {
*/
public void cleanup(ResultSet resultSet) {
if ( collectionLoadContexts != null ) {
CollectionLoadContext collectionLoadContext = collectionLoadContexts.remove( resultSet );
final CollectionLoadContext collectionLoadContext = collectionLoadContexts.remove( resultSet );
collectionLoadContext.cleanup();
}
if ( entityLoadContexts != null ) {
EntityLoadContext entityLoadContext = entityLoadContexts.remove( resultSet );
final EntityLoadContext entityLoadContext = entityLoadContexts.remove( resultSet );
entityLoadContext.cleanup();
}
}
Expand Down Expand Up @@ -191,7 +184,7 @@ public CollectionLoadContext getCollectionLoadContext(ResultSet resultSet) {
* @return The loading collection, or null if not found.
*/
public PersistentCollection locateLoadingCollection(CollectionPersister persister, Serializable ownerKey) {
LoadingCollectionEntry lce = locateLoadingCollectionEntry( new CollectionKey( persister, ownerKey ) );
final LoadingCollectionEntry lce = locateLoadingCollectionEntry( new CollectionKey( persister, ownerKey ) );
if ( lce != null ) {
if ( LOG.isTraceEnabled() ) {
LOG.tracef(
Expand Down Expand Up @@ -246,13 +239,13 @@ void unregisterLoadingCollectionXRef(CollectionKey key) {
if ( !hasRegisteredLoadingCollectionEntries() ) {
return;
}
xrefLoadingCollectionEntries.remove(key);
}
xrefLoadingCollectionEntries.remove( key );
}

@SuppressWarnings( {"UnusedDeclaration"})
Map getLoadingCollectionXRefs() {
return xrefLoadingCollectionEntries;
}
return xrefLoadingCollectionEntries;
}


/**
Expand All @@ -271,7 +264,7 @@ LoadingCollectionEntry locateLoadingCollectionEntry(CollectionKey key) {
return null;
}
LOG.tracev( "Attempting to locate loading collection entry [{0}] in any result-set context", key );
LoadingCollectionEntry rtn = xrefLoadingCollectionEntries.get( key );
final LoadingCollectionEntry rtn = xrefLoadingCollectionEntries.get( key );
if ( rtn == null ) {
LOG.tracev( "Collection [{0}] not located in load context", key );
}
Expand All @@ -291,6 +284,13 @@ LoadingCollectionEntry locateLoadingCollectionEntry(CollectionKey key) {
// Entity load contexts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// * currently, not yet used...

/**
* Currently unused
*
* @param resultSet The result set
*
* @return The entity load context
*/
@SuppressWarnings( {"UnusedDeclaration"})
public EntityLoadContext getEntityLoadContext(ResultSet resultSet) {
EntityLoadContext context = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class LoadingCollectionEntry {
private final Serializable key;
private final PersistentCollection collection;

public LoadingCollectionEntry(
LoadingCollectionEntry(
ResultSet resultSet,
CollectionPersister persister,
Serializable key,
Expand All @@ -68,6 +68,7 @@ public PersistentCollection getCollection() {
return collection;
}

@Override
public String toString() {
return getClass().getName() + "<rs=" + resultSet + ", coll=" + MessageHelper.collectionInfoString( persister.getRole(), key ) + ">@" + Integer.toHexString( hashCode() );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/

/**
* Internal classes used to track loading of data, potentially across multiple ResultSets
*/
package org.hibernate.engine.loading.internal;
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public class Association {
private final String associationPath;
private final String role;

/**
* Constructs a association defining what is to be fetched.
*
* @param owner The entity owning the association
* @param associationPath The path of the association, from the entity
*/
public Association(EntityPersister owner, String associationPath) {
this.owner = owner;
this.associationPath = associationPath;
Expand Down
Loading

0 comments on commit 286800e

Please sign in to comment.