db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/cache ObjectCacheTwoLevelImpl.java
Date Sat, 12 Mar 2005 01:13:05 GMT
arminw      2005/03/11 17:13:05

  Modified:    src/java/org/apache/ojb/broker/cache Tag: OJB_1_0_RELEASE
                        ObjectCacheTwoLevelImpl.java
  Log:
  copy persistent object fields to a Map, using field names as key, instead of caching "flat"
persistent object instances
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.5   +96 -22    db-ojb/src/java/org/apache/ojb/broker/cache/Attic/ObjectCacheTwoLevelImpl.java
  
  Index: ObjectCacheTwoLevelImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/Attic/ObjectCacheTwoLevelImpl.java,v
  retrieving revision 1.1.2.4
  retrieving revision 1.1.2.5
  diff -u -r1.1.2.4 -r1.1.2.5
  --- ObjectCacheTwoLevelImpl.java	10 Mar 2005 17:55:30 -0000	1.1.2.4
  +++ ObjectCacheTwoLevelImpl.java	12 Mar 2005 01:13:05 -0000	1.1.2.5
  @@ -134,6 +134,15 @@
           }
       }
   
  +    /**
  +     * Returns the {@link org.apache.ojb.broker.PersistenceBroker} instance associated
with
  +     * this cache instance.
  +     */
  +    public PersistenceBrokerImpl getBroker()
  +    {
  +        return broker;
  +    }
  +
       private void setupApplicationCache(PersistenceBroker broker, Properties prop)
       {
           if(log.isDebugEnabled()) log.debug("Start setup application cache for broker "
+ broker);
  @@ -201,10 +210,27 @@
           return result;
       }
   
  -    private void putToApplicationCache(Identity oid, Object obj)
  +    private boolean putToApplicationCache(Identity oid, Object obj, boolean cacheIfNew)
       {
  -        Object target = copyStrategy.write(broker, obj);
  -        getApplicationCache().cache(oid, target);
  +        /*
  +        we allow to reuse cached objects, so lookup the old cache object
  +        and forward it to the CopyStrategy
  +        */
  +        Object oldTarget = null;
  +        if(!cacheIfNew)
  +        {
  +            oldTarget = getApplicationCache().lookup(oid);
  +        }
  +        Object target = copyStrategy.write(broker, obj, oldTarget);
  +        if(cacheIfNew)
  +        {
  +            return getApplicationCache().cacheIfNew(oid, target);
  +        }
  +        else
  +        {
  +            getApplicationCache().cache(oid, target);
  +            return false;
  +        }
       }
   
       /**
  @@ -214,7 +240,6 @@
       public void resetSessionCache()
       {
           sessionCache.clear();
  -        // enabledReadCache = false;
           invokeCounter = 0;
       }
   
  @@ -249,7 +274,7 @@
                       */
                       if(ProxyHelper.isMaterialized(result))
                       {
  -                        putToApplicationCache(entry.oid, ProxyHelper.getRealObject(result));
  +                        putToApplicationCache(entry.oid, ProxyHelper.getRealObject(result),
false);
                           // set the new type after the object was pushed to application
cache
                           entry.type = typeAfterProcess;
                       }
  @@ -270,7 +295,7 @@
           // pass new materialized objects immediately to application cache
           if(type == TYPE_NEW_MATERIALIZED)
           {
  -            boolean result = getApplicationCache().cacheIfNew(oid, obj);
  +            boolean result = putToApplicationCache(oid, obj, true);
               CacheEntry entry = new CacheEntry(oid, obj, TYPE_CACHED_READ, queue);
               if(result)
               {
  @@ -332,7 +357,19 @@
           return result;
       }
   
  -    private void materializeFullObject(Object target)
  +    /**
  +     * This cache implementation cache only "flat" objects (persistent objects without
any
  +     * references), so when {@link #lookup(org.apache.ojb.broker.Identity)} a cache object
  +     * it needs full materialization (assign all referenced objects) before the cache returns
  +     * the object. The materialization of the referenced objects based on the auto-XXX
settings
  +     * specified in the metadata mapping.
  +     * <br/>
  +     * Override this method if needed in conjunction with a user-defined
  +     * {@link org.apache.ojb.broker.cache.ObjectCacheTwoLevelImpl.CopyStrategy}.
  +     *
  +     * @param target The "flat" object for full materialization
  +     */
  +    public void materializeFullObject(Object target)
       {
           ClassDescriptor cld = broker.getClassDescriptor(target.getClass());
           // don't force, let OJB use the user settings
  @@ -370,7 +407,7 @@
   
       public boolean cacheIfNew(Identity oid, Object obj)
       {
  -        boolean result = getApplicationCache().cacheIfNew(oid, obj);
  +        boolean result = putToApplicationCache(oid, obj, true);
           if(result)
           {
               CacheEntry entry = new CacheEntry(oid, obj, TYPE_CACHED_READ, queue);
  @@ -517,13 +554,30 @@
   
       public interface CopyStrategy
       {
  +        /**
  +         * Called when an object is read from the application cache (second level cache)
  +         * before the object is full materialized, see {@link ObjectCacheTwoLevelImpl#materializeFullObject(Object)}.
  +         *
  +         * @param broker The current used {@link org.apache.ojb.broker.PersistenceBroker}
instance.
  +         * @param obj The object read from the application cache.
  +         * @return A copy of the object.
  +         */
           public Object read(PersistenceBroker broker, Object obj);
   
  -        public Object write(PersistenceBroker broker, Object obj);
  +        /**
  +         * Called before an object is written to the application cache (second level cache).
  +         *
  +         * @param broker The current used {@link org.apache.ojb.broker.PersistenceBroker}
instance.
  +         * @param obj The object to cache in application cache.
  +         * @param oldObject The old cache object or <em>null</em>
  +         * @return A copy of the object to write to application cache.
  +         */
  +        public Object write(PersistenceBroker broker, Object obj, Object oldObject);
       }
   
       public static class CopyStrategyImpl implements CopyStrategy
       {
  +        static final String CLASS_NAME_STR = "ojbClassName11";
   
           public CopyStrategyImpl()
           {
  @@ -531,29 +585,49 @@
   
           public Object read(PersistenceBroker broker, Object obj)
           {
  -            return makeCopy(broker, obj);
  -        }
  -
  -        public Object write(PersistenceBroker broker, Object obj)
  -        {
  -            return makeCopy(broker, obj);
  +            HashMap source = (HashMap) obj;
  +            String className = (String) source.get(CLASS_NAME_STR);
  +            ClassDescriptor cld = broker.getDescriptorRepository().getDescriptorFor(className);
  +            Object target = ClassHelper.buildNewObjectInstance(cld);
  +            // perform main object values
  +            FieldDescriptor[] flds = cld.getFieldDescriptions();
  +            FieldDescriptor fld = null;
  +            for(int i = 0; i < flds.length; i++)
  +            {
  +                fld = flds[i];
  +                // read the field value
  +                Object value = source.get(fld.getPersistentField().getName());
  +                // copy the field value
  +                value = fld.getJdbcType().getFieldType().copy(value);
  +                // now make a field-conversion to java-type, because we only
  +                // the sql type of the field
  +                value = fld.getFieldConversion().sqlToJava(value);
  +                // set the copied field value in new object
  +                fld.getPersistentField().set(target, value);
  +            }
  +            return target;
           }
   
  -        private Object makeCopy(PersistenceBroker broker, Object obj)
  +        public Object write(PersistenceBroker broker, Object obj, Object oldObject)
           {
               ClassDescriptor cld = broker.getClassDescriptor(obj.getClass());
  -            Object target = ClassHelper.buildNewObjectInstance(cld);
  +            // we store field values by name in a Map
  +            HashMap target = oldObject != null ? (HashMap) oldObject : new HashMap();
               // perform main object values
               FieldDescriptor[] flds = cld.getFieldDescriptions();
               FieldDescriptor fld = null;
               for(int i = 0; i < flds.length; i++)
               {
                   fld = flds[i];
  -                // get copy of the field
  -                Object copy = fld.copyFieldValue(obj);
  -                // set the copied field value in new object
  -                fld.getPersistentField().set(target, copy);
  +                // get the value
  +                Object value = fld.getPersistentField().get(obj);
  +                // convert value to a supported sql type
  +                value = fld.getFieldConversion().javaToSql(value);
  +                // copy the sql type
  +                value = fld.getJdbcType().getFieldType().copy(value);
  +                target.put(fld.getPersistentField().getName(), value);
               }
  +            target.put(CLASS_NAME_STR, obj.getClass().getName());
               return target;
           }
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message