[jboss-cvs] JBossCache/src-50/org/jboss/cache/pojo/impl ...

Ben Wang bwang at jboss.com
Sun Jul 16 00:04:41 EDT 2006


  User: bwang   
  Date: 06/07/16 00:04:41

  Modified:    src-50/org/jboss/cache/pojo/impl       
                        AdvisedPojoHandler.java CollectionClassHandler.java
                        InternalHelper.java ObjectGraphHandler.java
                        PojoCacheDelegate.java PojoCacheImpl.java
                        SerializableObjectHandler.java
  Log:
  Incremental refactoring towards re-mapping of internal graph.
  
  Revision  Changes    Path
  1.4       +46 -70    JBossCache/src-50/org/jboss/cache/pojo/impl/AdvisedPojoHandler.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: AdvisedPojoHandler.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/impl/AdvisedPojoHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- AdvisedPojoHandler.java	14 Jul 2006 17:56:51 -0000	1.3
  +++ AdvisedPojoHandler.java	16 Jul 2006 04:04:41 -0000	1.4
  @@ -36,11 +36,11 @@
   import java.lang.reflect.Field;
   
   /**
  - * Handling the Collection class management.
  + * Handling the advised pojo operations. No consideration of object graph here.
    *
    * @author Ben Wang
    *         Date: Aug 4, 2005
  - * @version $Id: AdvisedPojoHandler.java,v 1.3 2006/07/14 17:56:51 bwang Exp $
  + * @version $Id: AdvisedPojoHandler.java,v 1.4 2006/07/16 04:04:41 bwang Exp $
    */
   class AdvisedPojoHandler
   {
  @@ -48,17 +48,15 @@
      private PojoTreeCache cache_;
      private PojoCacheImpl pCache_;
      private InternalHelper internal_;
  -   private ObjectGraphHandler graphHandler_;
      private PojoUtil util_;
      private Observer observer_;
   
      public AdvisedPojoHandler(PojoCacheImpl pCache, InternalHelper internal,
  -                             ObjectGraphHandler graphHandler, PojoUtil util, Observer observer)
  +                             PojoUtil util, Observer observer)
      {
         pCache_ = pCache;
         cache_ = (PojoTreeCache)pCache_.getCache();
         internal_ = internal;
  -      graphHandler_ = graphHandler;
         util_ = util;
         observer_ = observer;
      }
  @@ -88,72 +86,8 @@
      void put(Fqn fqn, Object obj) throws CacheException
      {
         CachedType type = pCache_.getCachedType(obj.getClass());
  -      // add interceptor
  -      InstanceAdvisor advisor = ((Advised) obj)._getInstanceAdvisor();
  -      if (advisor == null)
  -         throw new RuntimeException("_putObject(): InstanceAdvisor is null for: " + obj);
  -
  -      // Step Check for cross references
  -      Interceptor interceptor = AopUtil.findCacheInterceptor(advisor);
  -      if (interceptor != null && graphHandler_.put(fqn, interceptor, type))
  -      {
  -         // found cross references
  -         return;
  -      }
  -
         // We have a clean slate then.
  -      _regularPutObject(fqn, obj, advisor, type);
  -   }
  -
  -   Object remove(Fqn fqn, Object result, Class clazz
  -                ) throws CacheException
  -   {
  -      CachedType type = pCache_.getCachedType(clazz);
  -      InstanceAdvisor advisor = ((Advised) result)._getInstanceAdvisor();
  -      for (Iterator i = type.getFields().iterator(); i.hasNext();)
  -      {
  -         Field field = (Field) (((FieldPersistentReference) i.next())).get();
  -         CachedType fieldType = pCache_.getCachedType(field.getType());
  -         Object value = null;
  -         if (!fieldType.isImmediate())
  -         {
  -            value = pCache_.removeObject(new Fqn(fqn, field.getName()));
  -
  -            // Check for Collection. If it is, we need to reset the original reference.
  -            if ((value instanceof Map || value instanceof List || value instanceof Set) &&
  -                 (value instanceof ClassProxy))
  -            {
  -               Interceptor interceptor = CollectionInterceptorUtil.getInterceptor((ClassProxy) value);
  -               Object origValue = ((AbstractCollectionInterceptor)interceptor).getCurrecntCopy();
  -               util_.collectionReplaceWithProxy(value, field, origValue);
  -            }
  -
  -         }
  -      }
  -
  -      // batch remove
  -      cache_.removeData(fqn);
  -      // Determine if we want to keep the interceptor for later use.
  -      CacheFieldInterceptor interceptor = (CacheFieldInterceptor) AopUtil.findCacheInterceptor(advisor);
  -      // Remember to remove the interceptor from in-memory object but make sure it belongs to me first.
  -      if (interceptor != null)
  -      {
  -         if (log.isDebugEnabled()) {
  -            log.debug("regularRemoveObject(): removed cache interceptor fqn: " + fqn + " interceptor: "+interceptor);
  -         }
  -         util_.detachInterceptor(advisor, interceptor, observer_);
  -      }
  -
  -      return null; // Not really null though.
  -   }
  -
  -   /*****************************************************************************************
  -    * private methods
  -    *******************************************************************************************/
  -
  -   private void _regularPutObject(Fqn fqn, Object obj, InstanceAdvisor advisor, CachedType type)
  -           throws CacheException
  -   {
  +      InstanceAdvisor advisor = ((Advised) obj)._getInstanceAdvisor();
         // TODO workaround for deserialiased objects
         if (advisor == null)
         {
  @@ -245,6 +179,48 @@
         }
      }
   
  +   Object remove(Fqn fqn, Object result, Class clazz
  +                ) throws CacheException
  +   {
  +      CachedType type = pCache_.getCachedType(clazz);
  +      InstanceAdvisor advisor = ((Advised) result)._getInstanceAdvisor();
  +      for (Iterator i = type.getFields().iterator(); i.hasNext();)
  +      {
  +         Field field = (Field) (((FieldPersistentReference) i.next())).get();
  +         CachedType fieldType = pCache_.getCachedType(field.getType());
  +         Object value = null;
  +         if (!fieldType.isImmediate())
  +         {
  +            value = pCache_.removeObject(new Fqn(fqn, field.getName()));
  +
  +            // Check for Collection. If it is, we need to reset the original reference.
  +            if ((value instanceof Map || value instanceof List || value instanceof Set) &&
  +                 (value instanceof ClassProxy))
  +            {
  +               Interceptor interceptor = CollectionInterceptorUtil.getInterceptor((ClassProxy) value);
  +               Object origValue = ((AbstractCollectionInterceptor)interceptor).getCurrecntCopy();
  +               util_.collectionReplaceWithProxy(value, field, origValue);
  +            }
  +
  +         }
  +      }
  +
  +      // batch remove
  +      cache_.removeData(fqn);
  +      // Determine if we want to keep the interceptor for later use.
  +      CacheFieldInterceptor interceptor = (CacheFieldInterceptor) AopUtil.findCacheInterceptor(advisor);
  +      // Remember to remove the interceptor from in-memory object but make sure it belongs to me first.
  +      if (interceptor != null)
  +      {
  +         if (log.isDebugEnabled()) {
  +            log.debug("regularRemoveObject(): removed cache interceptor fqn: " + fqn + " interceptor: "+interceptor);
  +         }
  +         util_.detachInterceptor(advisor, interceptor, observer_);
  +      }
  +
  +      return null; // Not really null though.
  +   }
  +
      private static boolean hasAnnotation(Class clazz, Advisor advisor, CachedType type)
      {
         return CachedType.hasAnnotation(clazz, advisor, type);
  
  
  
  1.5       +6 -43     JBossCache/src-50/org/jboss/cache/pojo/impl/CollectionClassHandler.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CollectionClassHandler.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/impl/CollectionClassHandler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- CollectionClassHandler.java	14 Jul 2006 17:56:51 -0000	1.4
  +++ CollectionClassHandler.java	16 Jul 2006 04:04:41 -0000	1.5
  @@ -26,27 +26,24 @@
   import java.util.Set;
   
   /**
  - * Handling the Collection class management.
  + * Handling the Collection class management. Has no consideration of object graph here.
    *
    * @author Ben Wang
    *         Date: Aug 4, 2005
  - * @version $Id: CollectionClassHandler.java,v 1.4 2006/07/14 17:56:51 bwang Exp $
  + * @version $Id: CollectionClassHandler.java,v 1.5 2006/07/16 04:04:41 bwang Exp $
    */
   class CollectionClassHandler
   {
  -   private final static Log log = LogFactory.getLog(CollectionClassHandler.class);
  +   private final Log log = LogFactory.getLog(CollectionClassHandler.class);
      private PojoTreeCache cache_;
      private PojoCacheImpl pCache_;
      private InternalHelper internal_;
  -   private ObjectGraphHandler graphHandler_;
   
  -   public CollectionClassHandler(PojoCacheImpl pCache, InternalHelper internal,
  -                                 ObjectGraphHandler graphHandler)
  +   public CollectionClassHandler(PojoCacheImpl pCache, InternalHelper internal)
      {
         pCache_ = pCache;
         cache_ = (PojoTreeCache)pCache_.getCache();
         internal_ = internal;
  -      graphHandler_ = graphHandler;
      }
   
      Object get(Fqn fqn, Class clazz, PojoReference pojoReference)
  @@ -82,46 +79,12 @@
         boolean isCollection = false;
   
         CachedType type = null;
  -      AbstractCollectionInterceptor interceptor = null;
         if (obj instanceof ClassProxy)
         {
  -         Class originalClaz = obj.getClass().getSuperclass();
  -         interceptor = CollectionInterceptorUtil.getInterceptor((ClassProxy) obj);
  -         type = pCache_.getCachedType(originalClaz);
  -      } else
  -      {
  -         type = pCache_.getCachedType(obj.getClass());
  +         throw new IllegalStateException("CollectionClassHandler.put(): obj is an ClassProxy instance "+ obj);
         }
   
  -      if (obj instanceof ClassProxy)
  -      {
  -         // A proxy here. We may have multiple references.
  -         if (interceptor == null)
  -         {
  -            if (log.isDebugEnabled())
  -            {
  -               log.debug("put(): null interceptor. Could be removed previously. " + fqn);
  -            }
  -         } else
  -         {
  -            if (interceptor.isAttached()) // If it is not attached, it is not active.
  -            {
  -               // Let's check for object graph, e.g., multiple and circular references first
  -               if (graphHandler_.put(fqn, interceptor, type))
  -               { // found cross references
  -                  return;
  -               }
  -            } else
  -            {
  -               // Re-attach the interceptor to this fqn.
  -               boolean copyToCache = true;
  -               interceptor.attach(fqn, copyToCache);
  -               internal_.putAopClazz(fqn, type.getType());
  -               internal_.setPojo(fqn, obj);
  -               return; // we are done
  -            }
  -         }
  -      }
  +      type = pCache_.getCachedType(obj.getClass());
   
         if (obj instanceof Map)
         {
  
  
  
  1.4       +1 -1      JBossCache/src-50/org/jboss/cache/pojo/impl/InternalHelper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: InternalHelper.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/impl/InternalHelper.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- InternalHelper.java	14 Jul 2006 16:35:48 -0000	1.3
  +++ InternalHelper.java	16 Jul 2006 04:04:41 -0000	1.4
  @@ -27,7 +27,7 @@
    */
   public class InternalHelper
   {
  -   private static Log log = LogFactory.getLog(InternalHelper.class.getName());
  +   private Log log = LogFactory.getLog(InternalHelper.class.getName());
      // This is an optimization flag to skip put lock when we are sure that it has been locked from
      // putObject. However, if later on there are transactional field updates, then we we will need
      // this off to protected the write lock.
  
  
  
  1.4       +28 -29    JBossCache/src-50/org/jboss/cache/pojo/impl/ObjectGraphHandler.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ObjectGraphHandler.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/impl/ObjectGraphHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- ObjectGraphHandler.java	14 Jul 2006 16:35:48 -0000	1.3
  +++ ObjectGraphHandler.java	16 Jul 2006 04:04:41 -0000	1.4
  @@ -11,23 +11,24 @@
   import org.apache.commons.logging.LogFactory;
   import org.jboss.aop.Advised;
   import org.jboss.aop.InstanceAdvisor;
  +import org.jboss.aop.proxy.ClassProxy;
   import org.jboss.aop.advice.Interceptor;
   import org.jboss.cache.CacheException;
   import org.jboss.cache.Fqn;
  -import org.jboss.cache.pojo.interceptors.dynamic.AbstractCollectionInterceptor;
   import org.jboss.cache.pojo.util.AopUtil;
   import org.jboss.cache.pojo.interceptors.dynamic.BaseInterceptor;
   import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
   import org.jboss.cache.pojo.PojoTreeCache;
   import org.jboss.cache.pojo.CachedType;
   import org.jboss.cache.pojo.PojoReference;
  +import org.jboss.cache.pojo.collection.CollectionInterceptorUtil;
   
   /**
    * Handle the object graph management.
    *
    * @author Ben Wang
    *         Date: Aug 4, 2005
  - * @version $Id: ObjectGraphHandler.java,v 1.3 2006/07/14 16:35:48 bwang Exp $
  + * @version $Id: ObjectGraphHandler.java,v 1.4 2006/07/16 04:04:41 bwang Exp $
    */
   class ObjectGraphHandler
   {
  @@ -43,10 +44,10 @@
         internal_ = internal;
      }
   
  -   Object get(Fqn fqn) throws CacheException
  +   Object get(Fqn fqn, Class clazz, PojoReference pojoReference) throws CacheException
      {
         // Note this is actually the aliasFqn, not the real fqn!
  -      String refFqn = internal_.getRefFqn(fqn);
  +      String refFqn = internal_.getRefFqn(pojoReference);
         Object obj;
         if (refFqn != null)
         {
  @@ -55,7 +56,7 @@
            // In addition, this op will not be recursive.
            if (log.isDebugEnabled())
            {
  -            log.debug("getObject(): obtain value from reference fqn: " + refFqn);
  +            log.debug("get(): obtain value from reference fqn: " + refFqn);
            }
            obj = pCache_.getObject(Fqn.fromString(refFqn));
            if (obj == null)
  @@ -68,27 +69,32 @@
         return null;
      }
   
  -   boolean put(Fqn fqn, Interceptor interceptor, CachedType type) throws CacheException
  +   boolean put(Fqn fqn, Object obj) throws CacheException
      {
  -      Fqn originalFqn = null;
  +      CachedType type = pCache_.getCachedType(obj.getClass());
   
  -      if (interceptor instanceof AbstractCollectionInterceptor)
  +      InstanceAdvisor advisor = null;
  +      Interceptor interceptor = null;
  +
  +      if(obj instanceof Advised)
         {
  -         // Special case for Collection class. If it is detached, we don't care.
  -         if (!((AbstractCollectionInterceptor) interceptor).isAttached())
  +         advisor = ((Advised) obj)._getInstanceAdvisor();
  +         if (advisor == null)
  +            throw new RuntimeException("put(): InstanceAdvisor is null for: " + obj);
  +         // Step Check for cross references
  +         interceptor = AopUtil.findCacheInterceptor(advisor);
  +      } else
            {
  -            return false;
  +         advisor = ((ClassProxy) obj)._getInstanceAdvisor();
  +         if (advisor == null)
  +            throw new RuntimeException("put(): InstanceAdvisor is null for: " + obj);
  +         interceptor = CollectionInterceptorUtil.getInterceptor((ClassProxy) obj);
            }
  -      }
  -      // ah, found something. So this will be multiple referenced.
  -      originalFqn = ((BaseInterceptor) interceptor).getFqn();
   
  -      if (originalFqn == null) return false;
  +      Fqn originalFqn = null;
   
  -      if (log.isDebugEnabled())
  -      {
  -         log.debug("handleObjectGraph(): fqn: " + fqn + " and " + originalFqn + " share the object.");
  -      }
  +      // ah, found something. So this will be multiple referenced.
  +      originalFqn = ((BaseInterceptor) interceptor).getFqn();
   
         // This will increment the ref count, reset, and add ref fqn in the current fqn node.
         setupRefCounting(fqn, originalFqn);
  @@ -96,11 +102,9 @@
         return true;
      }
   
  -   boolean remove(Fqn fqn, Object pojo)
  +   void remove(Fqn fqn, Object pojo)
              throws CacheException
      {
  -      boolean isTrue = false;
  -
         // Note this is actually the aliasFqn, not the real fqn!
         PojoReference pojoReference = internal_.getAopInstance(fqn);
         String refFqn = internal_.getRefFqn(pojoReference);
  @@ -114,7 +118,6 @@
            }
            removeFromReference(fqn, refFqn);
            internal_.cleanUp(fqn);
  -         isTrue = true;
         } else
         {
            if (InternalHelper.isReferenced(pojoReference))
  @@ -150,8 +153,6 @@
                  // reset the fqn in the indirect fqn map
                  internal_.setIndirectFqn(fqn.toString(), newFqn.toString());
   
  -               isTrue = true;
  -
                  if (log.isDebugEnabled())
                  {
                     log.debug("remove(): this node " + fqn + " is currently referenced by " +
  @@ -161,8 +162,6 @@
               }
            }
         }
  -
  -      return isTrue;
      }
   
      /**
  @@ -181,7 +180,7 @@
            {
               // No one is referring it so it is safe to remove
               // TODO we should make sure the parent nodes are also removed they are empty as well.
  -            pCache_._removeObject(Fqn.fromString(refFqn));
  +            pCache_.removeObject(Fqn.fromString(refFqn));
            }
         }
   
  
  
  
  1.5       +200 -145  JBossCache/src-50/org/jboss/cache/pojo/impl/PojoCacheDelegate.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: PojoCacheDelegate.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/impl/PojoCacheDelegate.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- PojoCacheDelegate.java	14 Jul 2006 17:56:51 -0000	1.4
  +++ PojoCacheDelegate.java	16 Jul 2006 04:04:41 -0000	1.5
  @@ -24,6 +24,7 @@
   import org.jboss.cache.pojo.PojoUtil;
   import org.jboss.cache.pojo.CachedType;
   import org.jboss.cache.pojo.PojoReference;
  +import org.jboss.cache.pojo.collection.CollectionInterceptorUtil;
   
   import java.lang.reflect.Field;
   import java.util.Collection;
  @@ -61,10 +62,10 @@
         cache_ = (PojoTreeCache)pCache_.getCache();
         internal_ = new InternalHelper(cache_);
         graphHandler_ = new ObjectGraphHandler(pCache_, internal_);
  -      collectionHandler_ = new CollectionClassHandler(pCache_, internal_, graphHandler_);
  +      collectionHandler_ = new CollectionClassHandler(pCache_, internal_);
         serializableHandler_ = new SerializableObjectHandler(pCache_, internal_);
         observer_ = observer;
  -      advisedHandler_ = new AdvisedPojoHandler(pCache_, internal_, graphHandler_, util_, observer_);
  +      advisedHandler_ = new AdvisedPojoHandler(pCache_, internal_, util_, observer_);
      }
   
      public void setBulkRemove(boolean bulk)
  @@ -77,16 +78,26 @@
         return ((Boolean) bulkRemove_.get()).booleanValue();
      }
   
  -   Object _getObject(Fqn fqn) throws CacheException
  +   public Object getObject(Fqn fqn) throws CacheException
      {
  +      // Remember not to print obj here since it will trigger the CacheFieldInterceptor.
  +
         // TODO Must we really to couple with BR? JBCACHE-669
         Object pojo = internal_.getPojoWithGravitation(fqn);
         if (pojo != null)
         {
            // we already have an advised instance
  +         if (log.isDebugEnabled())
  +         {
  +            log.debug("getObject(): fqn: " + fqn + " retrieved from existing instance directly. ");
  +         }
            return pojo;
         }
   
  +      if (log.isDebugEnabled())
  +      {
  +         log.debug("getObject(): fqn: " + fqn);
  +      }
         // OK. So we are here meaning that this is a failover or passivation since the transient
         // pojo instance is not around. Let's also make sure the right classloader is used
         // as well.
  @@ -97,7 +108,7 @@
            {
               cache_.getRegionManager().setUnmarshallingClassLoader(fqn);
            }
  -         return _getObjectInternal(fqn);
  +         return getObjectInternal(fqn);
         }
         finally
         {
  @@ -105,7 +116,159 @@
         }
      }
   
  -   private Object _getObjectInternal(Fqn fqn) throws CacheException
  +   /**
  +    * Note that caller of this method will take care of synchronization within the <code>fqn</code> sub-tree.
  +    *
  +    */
  +   public Object putObject(Fqn fqn, Object obj) throws CacheException
  +   {
  +      // Remember not to print obj here since it will trigger the CacheFieldInterceptor.
  +
  +      // Skip some un-necessary update if obj is the same class as the old one
  +      Object oldValue = internal_.getPojo(fqn);
  +      if (oldValue == obj)
  +      {
  +         if (log.isDebugEnabled())
  +         {
  +            log.debug("putObject(): fqn: " + fqn + " pojo is already in the cache. Returns right away.");
  +         }
  +         return obj;
  +      }
  +
  +      if (log.isDebugEnabled())
  +      {
  +         log.debug("putObject(): fqn: " + fqn);
  +      }
  +
  +      // remove old value before overwriting it. This is necessary to detach any interceptor.
  +      // TODO Or can we simply walk thru that somewhere? Well, there is also implication of Collection though
  +      pCache_.removeObject(fqn);
  +
  +      if (obj == null)
  +      {
  +         return oldValue; // we are done
  +      }
  +
  +      // Trigger bulk remove here for performance. Let's not worry about performance optimization yet.
  +      setBulkRemove(false);
  +
  +      if( (obj instanceof Advised || obj instanceof ClassProxy) && isMultipleReferenced(obj))
  +      {
  +        graphHandler_.put(fqn, obj);
  +      } else if (obj instanceof Advised)
  +      {
  +         advisedHandler_.put(fqn, obj);
  +      } else if (isCollection(obj))
  +      {
  +         collectionHandler_.put(fqn, obj);
  +         //
  +      } else
  +      {
  +         // must be Serializable, including primitive types
  +         serializableHandler_.put(fqn, obj);
  +      }
  +
  +      return oldValue;
  +   }
  +
  +   /**
  +    * Note that caller of this method will take care of synchronization within the <code>fqn</code> sub-tree.
  +    *
  +    * @param fqn
  +    * @return
  +    * @throws CacheException
  +    */
  +   public Object removeObject(Fqn fqn) throws CacheException
  +   {
  +      Class clazz = internal_.peekAopClazz(fqn);
  +      if (clazz == null)
  +      {
  +         if (log.isTraceEnabled())
  +         {
  +            log.trace("removeObject(): clasz is null. fqn: " + fqn + " No need to remove.");
  +         }
  +         return null;
  +      }
  +
  +      if (log.isDebugEnabled())
  +      {
  +         log.debug("removeObject(): removing object from fqn: " + fqn);
  +      }
  +
  +      Object result = pCache_.getObject(fqn);
  +      if (result == null)
  +      {
  +         // This is not a *Pojo*. Must be regular cache stuffs
  +         if (cache_.exists(fqn))
  +         {
  +            // TODO What do we do here. It can still have children pojo though.
  +            cache_.remove(fqn);
  +         }
  +         return null;
  +      }
  +
  +      // can check if we need to do any bulk remove. E.g., if there is no object graph.
  +/*
  +      if (getBulkRemove())
  +      {
  +         if (bulkRemove(fqn, result))
  +         {
  +            // Remember not to print obj here since it will trigger the CacheFieldInterceptor.
  +            if (log.isDebugEnabled())
  +            {
  +               log.debug("_removeObject(): fqn: " + fqn + "removing existing object in bulk.");
  +            }
  +
  +            return result;
  +         }
  +         setBulkRemove(false);
  +      }
  +*/
  +
  +      if( (result instanceof Advised || result instanceof ClassProxy) &&isMultipleReferenced(result))
  +      {
  +         graphHandler_.remove(fqn, result);
  +      }
  +      if (Advised.class.isAssignableFrom(clazz))
  +      {
  +         advisedHandler_.remove(fqn, result, clazz);
  +         internal_.cleanUp(fqn);
  +      } else if (isCollectionGet(clazz))
  +      {
  +         // We need to return the original reference
  +         result = collectionHandler_.remove(fqn, result);
  +         internal_.cleanUp(fqn);
  +      } else
  +      { // Just Serializable objects. Do a brute force remove is ok.
  +         serializableHandler_.remove();
  +         internal_.cleanUp(fqn);
  +      }
  +
  +      // remove the interceptor as well.
  +      return result;
  +   }
  +
  +   public Map findObjects(Fqn fqn) throws CacheException
  +   {
  +
  +      // Traverse from fqn to do getObject, if it return a pojo we then stop.
  +      Map map = new HashMap();
  +      Object pojo = getObject(fqn);
  +      if (pojo != null)
  +      {
  +         map.put(fqn, pojo); // we are done!
  +         return map;
  +      }
  +
  +      findChildObjects(fqn, map);
  +      if (log.isDebugEnabled())
  +      {
  +         log.debug("_findObjects(): Fqn: " + fqn + " size of pojos found: " + map.size());
  +      }
  +      return map;
  +   }
  +
  +   private Object getObjectInternal(Fqn fqn) throws CacheException
      {
         // the class attribute is implicitly stored as an immutable read-only attribute
         Class clazz = internal_.peekAopClazz(fqn);
  @@ -118,9 +281,6 @@
          */
         Object obj;
   
  -      // Check for both Advised and Collection classes for object graph.
  -      if ((obj = graphHandler_.get(fqn)) != null)
  -         return obj; // retrieved from internal ref node. We are done.
   
         PojoReference pojoReference = internal_.getAopInstance(fqn);
         if (pojoReference == null)
  @@ -128,7 +288,11 @@
            throw new RuntimeException("PojoCacheDelegate._getObject(): null PojoReference.");
         }
   
  -      if (Advised.class.isAssignableFrom(clazz))
  +      // Check for both Advised and Collection classes for object graph.
  +      if (isMultipleReferencedGet(pojoReference))
  +      {
  +         obj = graphHandler_.get(fqn, clazz, pojoReference); // retrieved from internal ref node. We are done.
  +      } else if (Advised.class.isAssignableFrom(clazz))
         {
            obj = advisedHandler_.get(fqn, clazz, pojoReference);
         } else if(isCollectionGet(clazz))
  @@ -144,6 +308,13 @@
         return obj;
      }
   
  +   private boolean isMultipleReferencedGet(PojoReference pojoReference) throws CacheException
  +   {
  +      if(internal_.getRefFqn(pojoReference) != null) return true;
  +
  +      return false;
  +   }
  +
      private boolean isCollectionGet(Class clazz)
      {
         if(Map.class.isAssignableFrom(clazz) || Collection.class.isAssignableFrom(clazz))
  @@ -152,52 +323,32 @@
         return false;
      }
   
  -   /**
  -    * Note that caller of this method will take care of synchronization within the <code>fqn</code> sub-tree.
  -    *
  -    * @param fqn
  -    * @param obj
  -    * @return
  -    * @throws CacheException
  -    */
  -   Object _putObject(Fqn fqn, Object obj) throws CacheException
  -   {
  -      // Skip some un-necessary update if obj is the same class as the old one
  -      Object oldValue = internal_.getPojo(fqn);
  -      if (oldValue == obj) return obj;  // value already in cache. return right away.
   
  -      // remove old value before overwriting it. This is necessary to detach any interceptor.
  -      // TODO Or can we simply walk thru that somewhere? Well, there is also implication of Collection though
  -      pCache_._removeObject(fqn, true);
  +   private boolean isMultipleReferenced(Object obj)
  +   {
  +      Interceptor interceptor = null;
  +      if(obj instanceof Advised)
  +      {
  +         InstanceAdvisor advisor = ((Advised) obj)._getInstanceAdvisor();
  +         if (advisor == null)
  +            throw new RuntimeException("_putObject(): InstanceAdvisor is null for: " + obj);
   
  -      if (obj == null)
  +         // Step Check for cross references
  +         interceptor = AopUtil.findCacheInterceptor(advisor);
  +      }else
         {
  -         return oldValue; // we are done
  +         interceptor = CollectionInterceptorUtil.getInterceptor((ClassProxy) obj);
         }
  +      if(interceptor == null) return false;
   
  -      // Trigger bulk remove here for performance. Let's not worry about performance optimization yet.
  -      setBulkRemove(false);
  +      Fqn originalFqn = null;
   
  -      // Remember not to print obj here since it will trigger the CacheFieldInterceptor.
  -      if (log.isDebugEnabled())
  -      {
  -         log.debug("putObject(): fqn: " + fqn);
  -      }
  +      // ah, found something. So this will be multiple referenced.
  +      originalFqn = ((BaseInterceptor) interceptor).getFqn();
   
  -      if (obj instanceof Advised)
  -      {
  -         advisedHandler_.put(fqn, obj);
  -      } else if (isCollection(obj))
  -      {
  -         collectionHandler_.put(fqn, obj);
  -         //
  -      } else
  -      {
  -         // must be Serializable, including primitive types
  -         serializableHandler_.put(fqn, obj);
  -      }
  +      if (originalFqn != null) return true;
   
  -      return oldValue;
  +      return false;
      }
   
      private boolean isCollection(Object obj)
  @@ -338,7 +489,7 @@
   
               // Need to do a getObject just in case this is a failover removeObject.
               if (value == null)
  -               value = _getObject(new Fqn(interceptor.getFqn(), field.getName()));
  +               value = getObject(new Fqn(interceptor.getFqn(), field.getName()));
   
               if (value == null) continue; // this is no brainer.
   
  @@ -389,102 +540,6 @@
         return CachedType.hasAnnotation(clazz, advisor, type);
      }
   
  -   /**
  -    * Note that caller of this method will take care of synchronization within the <code>fqn</code> sub-tree.
  -    *
  -    * @param fqn
  -    * @return
  -    * @throws CacheException
  -    */
  -   public Object _removeObject(Fqn fqn) throws CacheException
  -   {
  -      Class clazz = internal_.peekAopClazz(fqn);
  -      if (clazz == null)
  -      {
  -         if (log.isTraceEnabled())
  -         {
  -            log.trace("_removeObject(): clasz is null. fqn: " + fqn + " No need to remove.");
  -         }
  -         return null;
  -      }
  -
  -      if (log.isDebugEnabled())
  -      {
  -         log.debug("_removeObject(): removing object from fqn: " + fqn);
  -      }
  -
  -      Object result = pCache_.getObject(fqn);
  -      if (result == null)
  -      {
  -         // This is not a *Pojo*. Must be regular cache stuffs
  -         if (cache_.exists(fqn))
  -         {
  -            // TODO What do we do here. It can still have children pojo though.
  -            cache_.remove(fqn);
  -         }
  -         return null;
  -      }
  -
  -      // can check if we need to do any bulk remove. E.g., if there is no object graph.
  -      if (getBulkRemove())
  -      {
  -         if (bulkRemove(fqn, result))
  -         {
  -            // Remember not to print obj here since it will trigger the CacheFieldInterceptor.
  -            if (log.isDebugEnabled())
  -            {
  -               log.debug("_removeObject(): fqn: " + fqn + "removing existing object in bulk.");
  -            }
  -
  -            return result;
  -         }
  -         setBulkRemove(false);
  -      }
  -
  -      if (graphHandler_.remove(fqn, result))
  -      {
  -         return result;
  -      }
  -
  -      // Not multi-referenced
  -      if (Advised.class.isAssignableFrom(clazz))
  -      {
  -         advisedHandler_.remove(fqn, result, clazz);
  -      } else if (isCollectionGet(clazz))
  -      {
  -         // We need to return the original reference
  -         result = collectionHandler_.remove(fqn, result);
  -      } else
  -      { // Just Serializable objects. Do a brute force remove is ok.
  -         serializableHandler_.remove();
  -      }
  -
  -      internal_.cleanUp(fqn);
  -
  -      // remove the interceptor as well.
  -      return result;
  -   }
  -
  -   Map _findObjects(Fqn fqn) throws CacheException
  -   {
  -
  -      // Traverse from fqn to do getObject, if it return a pojo we then stop.
  -      Map map = new HashMap();
  -      Object pojo = _getObject(fqn);
  -      if (pojo != null)
  -      {
  -         map.put(fqn, pojo); // we are done!
  -         return map;
  -      }
  -
  -      findChildObjects(fqn, map);
  -      if (log.isDebugEnabled())
  -      {
  -         log.debug("_findObjects(): Fqn: " + fqn + " size of pojos found: " + map.size());
  -      }
  -      return map;
  -   }
  -
      private void findChildObjects(Fqn fqn, Map map) throws CacheException
      {
         // We need to traverse then
  @@ -495,7 +550,7 @@
            String obj = (String) aSet;
            Fqn newFqn = new Fqn(fqn, obj);
   
  -         Object pojo = _getObject(newFqn);
  +         Object pojo = getObject(newFqn);
            if (pojo != null)
            {
               map.put(newFqn, pojo);
  
  
  
  1.11      +30 -51    JBossCache/src-50/org/jboss/cache/pojo/impl/PojoCacheImpl.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: PojoCacheImpl.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/impl/PojoCacheImpl.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -b -r1.10 -r1.11
  --- PojoCacheImpl.java	14 Jul 2006 17:56:51 -0000	1.10
  +++ PojoCacheImpl.java	16 Jul 2006 04:04:41 -0000	1.11
  @@ -38,7 +38,7 @@
    * Implementation class for PojoCache interface
    *
    * @author Ben Wang
  - * @version $Id: PojoCacheImpl.java,v 1.10 2006/07/14 17:56:51 bwang Exp $
  + * @version $Id: PojoCacheImpl.java,v 1.11 2006/07/16 04:04:41 bwang Exp $
    */
   public class PojoCacheImpl implements PojoCache, Observer
   {
  @@ -87,9 +87,7 @@
      {
         try
         {
  -         notifyAttach(pojo, true);
            Object obj = putObject(Fqn.fromString(id), pojo);
  -         notifyAttach(pojo, false);
            return obj;
         } catch (CacheException e)
         {
  @@ -98,12 +96,26 @@
         }
      }
   
  +   /** This public API is called from internal package only.
  +    *
  +    */
  +   public Object putObject(Fqn id, Object pojo)
  +           throws CacheException
  +   {
  +      notifyAttach(pojo, true);
  +      Object obj = delegate_.putObject(id, pojo);
  +      notifyAttach(pojo, false);
  +      return obj;
  +   }
  +
      @Detach
      public Object detach(String id) throws PojoCacheException
      {
         try
         {
            Object pojo = find(id); // TODO need optimization here since it will be redundant here
  +         if(pojo == null) return pojo;
  +
            notifyDetach(pojo, true);
            Object obj = removeObject(Fqn.fromString(id));
            notifyDetach(pojo, false);
  @@ -115,6 +127,12 @@
         }
      }
   
  +   public Object removeObject(Fqn id) throws CacheException
  +   {
  +      delegate_.setBulkRemove(false);
  +      return delegate_.removeObject(id);
  +   }
  +
      public String getPojoID(Object pojo)
      {
         throw new RuntimeException("getPojoID not yet implemented");
  @@ -133,12 +151,18 @@
         }
      }
   
  +   public Object getObject(Fqn id)
  +           throws CacheException
  +   {
  +      return delegate_.getObject(id);
  +   }
  +
      @Find
      public Map findAll(String id) throws PojoCacheException
      {
         try
         {
  -         return findObjects(Fqn.fromString(id));
  +         return delegate_.findObjects(Fqn.fromString(id));
         } catch (CacheException e)
         {
            e.printStackTrace();  // TODO
  @@ -201,54 +225,9 @@
      }
   
   
  -   /********************************************************************************
  -    * Internal API for event notification
  -    ********************************************************************************/
  -
  -   public Object getObject(Fqn fqn) throws CacheException
  +   public Object XputObject(Fqn fqn, Object obj) throws CacheException
      {
  -      return delegate_._getObject(fqn);
  -   }
  -
  -   public Object putObject(Fqn fqn, Object obj) throws CacheException
  -   {
  -      return delegate_._putObject(fqn, obj);
  -   }
  -
  -   public Object removeObject(Fqn fqn) throws CacheException
  -   {
  -      return _removeObject(fqn, true);
  -   }
  -
  -   public Map findObjects(Fqn fqn) throws CacheException
  -   {
  -      return delegate_._findObjects(fqn);
  -   }
  -
  -   /**
  -    * Used by internal implementation. Not for general public.
  -    */
  -   public Object _removeObject(Fqn fqn) throws CacheException
  -   {
  -      boolean removeCacheInterceptor = true;
  -      return _removeObject(fqn, removeCacheInterceptor);
  -   }
  -
  -   /**
  -    * Used by internal implementation. Not for general public.
  -    */
  -   public Object _removeObject(Fqn fqn, boolean removeCacheInterceptor) throws CacheException
  -   {
  -      boolean evict = false;
  -      return _removeObject(fqn, removeCacheInterceptor, evict);
  -   }
  -
  -   public Object _removeObject(Fqn fqn, boolean removeCacheInterceptor, boolean evict) throws CacheException
  -   {
  -      // Don't trigger bulk remove now since there is still some problem with Collection class
  -      // when it is detached.
  -      delegate_.setBulkRemove(false);
  -      return delegate_._removeObject(fqn);
  +      return delegate_.putObject(fqn, obj);
      }
   
      /**
  
  
  
  1.5       +2 -2      JBossCache/src-50/org/jboss/cache/pojo/impl/SerializableObjectHandler.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: SerializableObjectHandler.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/impl/SerializableObjectHandler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- SerializableObjectHandler.java	14 Jul 2006 16:35:48 -0000	1.4
  +++ SerializableObjectHandler.java	16 Jul 2006 04:04:41 -0000	1.5
  @@ -22,7 +22,7 @@
    * Handle Serializable object cache management.
    *
    * @author Ben Wang
  - * @version $Id: SerializableObjectHandler.java,v 1.4 2006/07/14 16:35:48 bwang Exp $
  + * @version $Id: SerializableObjectHandler.java,v 1.5 2006/07/16 04:04:41 bwang Exp $
    */
   class SerializableObjectHandler
   {
  @@ -52,7 +52,7 @@
           // Note that JBoss Serialization can serialize any type now.
            if (log_.isDebugEnabled())
            {
  -            log_.debug("putObject(): obj (" + obj.getClass() + ") is non-advisable but serialize it anyway. "
  +            log_.debug("put(): obj (" + obj.getClass() + ") is non-advisable but serialize it anyway. "
               + "Note that if it is non-serializable we require to use JBoss Serialization.");
            }
   
  
  
  



More information about the jboss-cvs-commits mailing list