HHH-3810 : rename CopyCache to EventCache to be more generic

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@16665 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2009-06-02 09:30:14 +00:00
parent 77b959f5cc
commit 35a28509f2
2 changed files with 74 additions and 65 deletions

View File

@ -70,7 +70,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
private static final Logger log = LoggerFactory.getLogger(DefaultMergeEventListener.class);
protected Map getMergeMap(Object anything) {
return ( ( CopyCache ) anything ).getMergeMap();
return ( ( EventCache ) anything ).invertMap();
}
/**
@ -80,7 +80,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
* @throws HibernateException
*/
public void onMerge(MergeEvent event) throws HibernateException {
CopyCache copyCache = new CopyCache();
EventCache copyCache = new EventCache();
onMerge( event, copyCache );
// TODO: iteratively get transient entities and retry merge until one of the following conditions:
// 1) transientCopyCache.size() == 0
@ -110,8 +110,8 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
copyCache = null;
}
protected CopyCache getTransientCopyCache(MergeEvent event, CopyCache copyCache) {
CopyCache transientCopyCache = new CopyCache();
protected EventCache getTransientCopyCache(MergeEvent event, EventCache copyCache) {
EventCache transientCopyCache = new EventCache();
for ( Iterator it=copyCache.entrySet().iterator(); it.hasNext(); ) {
Map.Entry mapEntry = ( Map.Entry ) it.next();
Object entity = mapEntry.getKey();
@ -131,7 +131,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
);
}
else if ( copyEntry.getStatus() == Status.SAVING ) {
transientCopyCache.put( entity, copy, copyCache.isIncludedInMerge( entity ) );
transientCopyCache.put( entity, copy, copyCache.isOperatedOn( entity ) );
}
else if ( copyEntry.getStatus() != Status.MANAGED && copyEntry.getStatus() != Status.READ_ONLY ) {
throw new AssertionFailure( "Merged entity does not have status set to MANAGED or READ_ONLY; "+copy+" status="+copyEntry.getStatus() );
@ -140,7 +140,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
return transientCopyCache;
}
protected void retryMergeTransientEntities(MergeEvent event, Map transientCopyCache, CopyCache copyCache) {
protected void retryMergeTransientEntities(MergeEvent event, Map transientCopyCache, EventCache copyCache) {
// TODO: The order in which entities are saved may matter (e.g., a particular transient entity
// may need to be saved before other transient entities can be saved;
// Keep retrying the batch of transient entities until either:
@ -169,7 +169,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
*/
public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
final CopyCache copyCache = ( CopyCache ) copiedAlready;
final EventCache copyCache = ( EventCache ) copiedAlready;
final EventSource source = event.getSession();
final Object original = event.getOriginal();
@ -192,14 +192,14 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
}
if ( copyCache.containsKey( entity ) &&
( copyCache.isIncludedInMerge( entity ) ) ) {
( copyCache.isOperatedOn( entity ) ) ) {
log.trace("already in merge process");
event.setResult( entity );
}
else {
if ( copyCache.containsKey( entity ) ) {
log.trace("already in copyCache; setting in merge process");
copyCache.setIncludedInMerge( entity, true );
copyCache.setOperatedOn( entity, true );
}
event.setEntity( entity );
int entityState = -1;
@ -261,7 +261,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
final EventSource source = event.getSession();
final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
( ( CopyCache ) copyCache ).put( entity, entity, true ); //before cascade!
( ( EventCache ) copyCache ).put( entity, entity, true ); //before cascade!
cascadeOnMerge(source, persister, entity, copyCache);
copyValues(persister, entity, entity, source, copyCache);
@ -295,7 +295,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
persister.setIdentifier( copyCache.get( entity ), id, source.getEntityMode() );
}
else {
( ( CopyCache ) copyCache ).put( entity, persister.instantiate( id, source.getEntityMode() ), true ); //before cascade!
( ( EventCache ) copyCache ).put( entity, persister.instantiate( id, source.getEntityMode() ), true ); //before cascade!
//TODO: should this be Session.instantiate(Persister, ...)?
}
final Object copy = copyCache.get( entity );
@ -333,7 +333,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
"' from original entity is not in copyCache; " + propertyName + " =["+propertyFromEntity+"]");
throw ex;
}
if ( ( ( CopyCache ) copyCache ).isIncludedInMerge( propertyFromEntity ) ) {
if ( ( ( EventCache ) copyCache ).isOperatedOn( propertyFromEntity ) ) {
log.trace( "property '" + copyEntry.getEntityName() + "." + propertyName +
"' from original entity is in copyCache and is in the process of being merged; " +
propertyName + " =["+propertyFromEntity+"]");
@ -397,7 +397,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
entityIsTransient(event, copyCache);
}
else {
( ( CopyCache ) copyCache ).put( entity, result, true ); //before cascade!
( ( EventCache ) copyCache ).put( entity, result, true ); //before cascade!
final Object target = source.getPersistenceContext().unproxy(result);
if ( target == entity ) {

View File

@ -34,36 +34,45 @@ import org.hibernate.util.IdentityMap;
import org.hibernate.AssertionFailure;
/**
* CopyCache is intended to be the Map implementation used by
* {@link DefaultMergeEventListener} to keep track of entities and their copies
* being merged into the session. This implementation also tracks whether a
* an entity in the CopyCache is included in the merge. This allows a
* an entity and its copy to be added to a CopyCache before merge has cascaded
* to that entity.
* EventCache is a Map implementation that can be used by an event
* listener to keep track of entities involved in the operation
* being performed. This implementation allows entities to be added
* to the EventCache before the operation has cascaded to that
* entity.
* <p/>
* The following methods can be used by event listeners (and other
* classes) in the same package to add entities to an EventCache
* and indicate if the operation is being performed on the entity:<p/>
* {@link EventCache#put(Object entity, Object copy, boolean isOperatedOn)}
* <p/>
* The following method can be used by event listeners (and other
* classes) in the same package to indicate that the operation is being
* performed on an entity already in the EventCache:
* {@link EventCache#setOperatedOn(Object entity, boolean isOperatedOn)
*
* @author Gail Badner
*/
class CopyCache implements Map {
class EventCache implements Map {
private Map entityToCopyMap = IdentityMap.instantiate(10);
// key is an entity involved with the merge;
// key is an entity involved with the operation performed by the listener;
// value can be either a copy of the entity or the entity itself
private Map entityToIncludeInMergeFlagMap = IdentityMap.instantiate(10);
// key is an entity involved with the merge;
// value is a flag indicating if the entity is included in the merge
private Map entityToOperatedOnFlagMap = IdentityMap.instantiate(10);
// key is an entity involved with the operation performed by the listener;
// value is a flag indicating if the listener explicitly operates on the entity
/**
* Clears the CopyCache.
* Clears the EventCache.
*/
public void clear() {
entityToCopyMap.clear();
entityToIncludeInMergeFlagMap.clear();
entityToOperatedOnFlagMap.clear();
}
/**
* Returns true if this CopyCache contains a mapping for the specified entity.
* Returns true if this EventCache contains a mapping for the specified entity.
* @param entity must be non-null
* @return true if this CopyCache contains a mapping for the specified entity
* @return true if this EventCache contains a mapping for the specified entity
* @throws NullPointerException if entity is null
*/
public boolean containsKey(Object entity) {
@ -74,9 +83,9 @@ class CopyCache implements Map {
}
/**
* Returns true if this CopyCache maps one or more entities to the specified copy.
* Returns true if this EventCache maps one or more entities to the specified copy.
* @param copy must be non-null
* @return true if this CopyCache maps one or more entities to the specified copy
* @return true if this EventCache maps one or more entities to the specified copy
* @throws NullPointerException if copy is null
*/
public boolean containsValue(Object copy) {
@ -87,17 +96,17 @@ class CopyCache implements Map {
}
/**
* Returns a set view of the entity-to-copy mappings contained in this CopyCache.
* @return set view of the entity-to-copy mappings contained in this CopyCache
* Returns a set view of the entity-to-copy mappings contained in this EventCache.
* @return set view of the entity-to-copy mappings contained in this EventCache
*/
public Set entrySet() {
return entityToCopyMap.entrySet();
}
/**
* Returns the copy to which this CopyCache maps the specified entity.
* Returns the copy to which this EventCache maps the specified entity.
* @param entity must be non-null
* @return the copy to which this CopyCache maps the specified entity
* @return the copy to which this EventCache maps the specified entity
* @throws NullPointerException if entity is null
*/
public Object get(Object entity) {
@ -108,23 +117,23 @@ class CopyCache implements Map {
}
/**
* Returns true if this CopyCache contains no entity-copy mappings.
* @return true if this CopyCache contains no entity-copy mappings
* Returns true if this EventCache contains no entity-copy mappings.
* @return true if this EventCache contains no entity-copy mappings
*/
public boolean isEmpty() {
return entityToCopyMap.isEmpty();
}
/**
* Returns a set view of the entities contained in this CopyCache
* @return a set view of the entities contained in this CopyCache
* Returns a set view of the entities contained in this EventCache
* @return a set view of the entities contained in this EventCache
*/
public Set keySet() {
return entityToCopyMap.keySet();
}
/**
* Associates the specified entity with the specified copy in this CopyCache;
* Associates the specified entity with the specified copy in this EventCache;
* @param entity must be non-null
* @param copy must be non- null
* @return previous copy associated with specified entity, or null if
@ -135,30 +144,30 @@ class CopyCache implements Map {
if ( entity == null || copy == null ) {
throw new NullPointerException( "null entities and copies are not supported by " + getClass().getName() );
}
entityToIncludeInMergeFlagMap.put( entity, Boolean.FALSE );
entityToOperatedOnFlagMap.put( entity, Boolean.FALSE );
return entityToCopyMap.put( entity, copy );
}
/**
* Associates the specified entity with the specified copy in this CopyCache;
* Associates the specified entity with the specified copy in this EventCache;
* @param entity must be non-null
* @param copy must be non- null
* @param isIncludedInMerge indicates if the entity is included in merge
* @param isOperatedOn indicates if the operation is performed on the entity
*
* @return previous copy associated with specified entity, or null if
* there was no mapping for entity.
* @throws NullPointerException if entity or copy is null
*/
/* package-private */ Object put(Object entity, Object copy, boolean isIncludedInMerge) {
/* package-private */ Object put(Object entity, Object copy, boolean isOperatedOn) {
if ( entity == null || copy == null ) {
throw new NullPointerException( "null entities and copies are not supported by " + getClass().getName() );
}
entityToIncludeInMergeFlagMap.put( entity, Boolean.valueOf( isIncludedInMerge ) );
entityToOperatedOnFlagMap.put( entity, Boolean.valueOf( isOperatedOn ) );
return entityToCopyMap.put( entity, copy );
}
/**
* Copies all of the mappings from the specified map to this CopyCache
* Copies all of the mappings from the specified map to this EventCache
* @param map keys and values must be non-null
* @throws NullPointerException if any map keys or values are null
*/
@ -169,12 +178,12 @@ class CopyCache implements Map {
throw new NullPointerException( "null entities and copies are not supported by " + getClass().getName() );
}
entityToCopyMap.put( entry.getKey(), entry.getValue() );
entityToIncludeInMergeFlagMap.put( entry.getKey(), Boolean.FALSE );
entityToOperatedOnFlagMap.put( entry.getKey(), Boolean.FALSE );
}
}
/**
* Removes the mapping for this entity from this CopyCache if it is present
* Removes the mapping for this entity from this EventCache if it is present
* @param entity must be non-null
* @return previous value associated with specified entity, or null if there was no mapping for entity.
* @throws NullPointerException if entity is null
@ -183,62 +192,62 @@ class CopyCache implements Map {
if ( entity == null ) {
throw new NullPointerException( "null entities are not supported by " + getClass().getName() );
}
entityToIncludeInMergeFlagMap.remove( entity );
entityToOperatedOnFlagMap.remove( entity );
return entityToCopyMap.remove( entity );
}
/**
* Returns the number of entity-copy mappings in this CopyCache
* @return the number of entity-copy mappings in this CopyCache
* Returns the number of entity-copy mappings in this EventCache
* @return the number of entity-copy mappings in this EventCache
*/
public int size() {
return entityToCopyMap.size();
}
/**
* Returns a collection view of the entity copies contained in this CopyCache.
* @return a collection view of the entity copies contained in this CopyCache
* Returns a collection view of the entity copies contained in this EventCache.
* @return a collection view of the entity copies contained in this EventCache
*/
public Collection values() {
return entityToCopyMap.values();
}
/**
* Returns true if the specified entity is included in the merge.
* Returns true if the listener is performing the operation on the specified entity.
* @param entity must be non-null
* @return true if the specified entity is included in the merge.
* @return true if the listener is performing the operation on the specified entity.
* @throws NullPointerException if entity is null
*/
public boolean isIncludedInMerge(Object entity) {
public boolean isOperatedOn(Object entity) {
if ( entity == null ) {
throw new NullPointerException( "null entities are not supported by " + getClass().getName() );
}
return ( ( Boolean ) entityToIncludeInMergeFlagMap.get( entity ) ).booleanValue();
return ( ( Boolean ) entityToOperatedOnFlagMap.get( entity ) ).booleanValue();
}
/**
* Set flag to indicate if an entity is included in the merge.
* @param entity must be non-null and this CopyCache must contain a mapping for this entity
* @return true if the specified entity is included in the merge
* Set flag to indicate if the listener is performing the operation on the specified entity.
* @param entity must be non-null and this EventCache must contain a mapping for this entity
* @return true if the listener is performing the operation on the specified entity
* @throws NullPointerException if entity is null
* @throws AssertionFailure if this CopyCache does not contain a mapping for the specified entity
* @throws AssertionFailure if this EventCache does not contain a mapping for the specified entity
*/
/* package-private */ void setIncludedInMerge(Object entity, boolean isIncludedInMerge) {
/* package-private */ void setOperatedOn(Object entity, boolean isOperatedOn) {
if ( entity == null ) {
throw new NullPointerException( "null entities are not supported by " + getClass().getName() );
}
if ( ! entityToIncludeInMergeFlagMap.containsKey( entity ) ||
if ( ! entityToOperatedOnFlagMap.containsKey( entity ) ||
! entityToCopyMap.containsKey( entity ) ) {
throw new AssertionFailure( "called CopyCache.setInMergeProcess() for entity not found in CopyCache" );
throw new AssertionFailure( "called EventCache.setOperatedOn() for entity not found in EventCache" );
}
entityToIncludeInMergeFlagMap.put( entity, Boolean.valueOf( isIncludedInMerge ) );
entityToOperatedOnFlagMap.put( entity, Boolean.valueOf( isOperatedOn ) );
}
/**
* Returns the copy-entity mappings
* @return the copy-entity mappings
*/
public Map getMergeMap() {
public Map invertMap() {
return IdentityMap.invert( entityToCopyMap );
}
}