Skip to content

Commit

Permalink
HHH-3810 : : rename CopyCache to EventCache to be more generic
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_2@16663 1b8cb986-b30d-0410-93ca-fae66ebed9b2
  • Loading branch information
gbadner committed Jun 2, 2009
1 parent 4965452 commit 3df61e2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 65 deletions.
26 changes: 13 additions & 13 deletions src/org/hibernate/event/def/DefaultMergeEventListener.java
Expand Up @@ -71,7 +71,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
private static final Log log = LogFactory.getLog(DefaultMergeEventListener.class);

protected Map getMergeMap(Object anything) {
return ( ( CopyCache ) anything ).getMergeMap();
return ( ( EventCache ) anything ).invertMap();
}

/**
Expand All @@ -81,7 +81,7 @@ protected Map getMergeMap(Object anything) {
* @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
Expand Down Expand Up @@ -111,8 +111,8 @@ public void onMerge(MergeEvent event) throws HibernateException {
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();
Expand All @@ -132,7 +132,7 @@ protected CopyCache getTransientCopyCache(MergeEvent event, CopyCache copyCache)
);
}
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() );
Expand All @@ -141,7 +141,7 @@ else if ( copyEntry.getStatus() != Status.MANAGED && copyEntry.getStatus() != St
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:
Expand Down Expand Up @@ -170,7 +170,7 @@ protected void retryMergeTransientEntities(MergeEvent event, Map transientCopyCa
*/
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();

Expand All @@ -193,14 +193,14 @@ public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateExcepti
}

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;
Expand Down Expand Up @@ -262,7 +262,7 @@ protected void entityIsPersistent(MergeEvent event, Map copyCache) {
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);
Expand Down Expand Up @@ -296,7 +296,7 @@ protected Object mergeTransientEntity(Object entity, String entityName, Serializ
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 );
Expand Down Expand Up @@ -334,7 +334,7 @@ protected Object mergeTransientEntity(Object entity, String entityName, Serializ
"' 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+"]");
Expand Down Expand Up @@ -398,7 +398,7 @@ protected void entityIsDetached(MergeEvent event, Map copyCache) {
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 ) {
Expand Down
Expand Up @@ -34,36 +34,45 @@
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) {
Expand All @@ -74,9 +83,9 @@ public boolean containsKey(Object entity) {
}

/**
* 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) {
Expand All @@ -87,17 +96,17 @@ public boolean containsValue(Object copy) {
}

/**
* 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) {
Expand All @@ -108,23 +117,23 @@ public Object get(Object entity) {
}

/**
* 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
Expand All @@ -135,30 +144,30 @@ public Object put(Object entity, Object copy) {
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
*/
Expand All @@ -169,12 +178,12 @@ public void putAll(Map 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
Expand All @@ -183,62 +192,62 @@ public Object remove(Object entity) {
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 );
}
}

0 comments on commit 3df61e2

Please sign in to comment.