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/test/org/apache/ojb repository_database.xml
Date Wed, 04 Aug 2004 00:12:41 GMT
arminw      2004/08/03 17:12:41

  Modified:    src/doc/forrest/src/documentation/content/xdocs/docu/guides
                        objectcache.xml
               src/java/org/apache/ojb/broker/cache
                        ObjectCacheDefaultImpl.java
               src/test/org/apache/ojb repository_database.xml
  Log:
  add new flag for ObjectCacheDefaultImpl, allows to determine how
  the used key for cached objects will be build.
  
  Revision  Changes    Path
  1.2       +19 -2     db-ojb/src/doc/forrest/src/documentation/content/xdocs/docu/guides/objectcache.xml
  
  Index: objectcache.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/doc/forrest/src/documentation/content/xdocs/docu/guides/objectcache.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- objectcache.xml	20 Jun 2004 09:12:33 -0000	1.1
  +++ objectcache.xml	4 Aug 2004 00:12:41 -0000	1.2
  @@ -295,11 +295,28 @@
                               </p>
                           </td>
                       </tr>
  +                    <tr>
  +                        <td>cachingKeyType</td>
  +                        <td>
  +                            Determines how the key was build for the cached objects:
  +                            <br/>
  +                            0 - Identity object was used as key, this was the <em>default</em>
setting.
  +                            <br/>
  +                            1 - Idenity + jcdAlias name was used as key. Useful when the
same object metadata model
  +                            (DescriptorRepository instance) are used for different databases
(JdbcConnectionDescriptor)
  +                            <br/>
  +                            2 - Identity + model (DescriptorRepository) was used as key.
Useful when different metadata
  +                            model (DescriptorRepository instance) are used for the same
database. Keep in mind that there
  +                            was no synchronization between cached objects with same Identity
but different metadata model.
  +                            <br/>
  +                            3 - all together (Idenity + jcdAlias + model)
  +                        </td>
  +                    </tr>
                   </table>
   
   
                   <p>
  -                    Recommendation:
  +                    <strong>Recommendation:</strong>
                       <br/>If you take care of cache synchronization and be aware of
dirty
                       reads, this implementation is useful for read-only or less update
                       centric classes.
  
  
  
  1.25      +64 -8     db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheDefaultImpl.java
  
  Index: ObjectCacheDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheDefaultImpl.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- ObjectCacheDefaultImpl.java	11 Jun 2004 22:04:47 -0000	1.24
  +++ ObjectCacheDefaultImpl.java	4 Aug 2004 00:12:41 -0000	1.25
  @@ -26,10 +26,12 @@
   
   import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.commons.lang.builder.ToStringStyle;
  +import org.apache.commons.lang.builder.HashCodeBuilder;
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PBStateEvent;
   import org.apache.ojb.broker.PBStateListener;
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
   /**
  @@ -50,11 +52,13 @@
    * Implementation configuration properties:
    * </p>
    * <p/>
  + *
    * <table cellspacing="2" cellpadding="2" border="3" frame="box">
    * <tr>
    * <td><strong>Property Key</strong></td>
    * <td><strong>Property Values</strong></td>
    * </tr>
  + *
    * <tr>
    * <td>timeout</td>
    * <td>
  @@ -69,6 +73,7 @@
    * </p>
    * </td>
    * </tr>
  + *
    * <tr>
    * <td>autoSync</td>
    * <td>
  @@ -90,6 +95,24 @@
    * </p>
    * </td>
    * </tr>
  + *
  + * <tr>
  + * <td>cachingKeyType</td>
  + * <td>
  + * Determines how the key was build for the cached objects:
  + * <br/>
  + * 0 - Identity object was used as key, this was the <em>default</em> setting.
  + * <br/>
  + * 1 - Idenity + jcdAlias name was used as key. Useful when the same object metadata model
  + * (DescriptorRepository instance) are used for different databases (JdbcConnectionDescriptor)
  + * <br/>
  + * 2 - Identity + model (DescriptorRepository) was used as key. Useful when different metadata
  + * model (DescriptorRepository instance) are used for the same database. Keep in mind that
there
  + * was no synchronization between cached objects with same Identity but different metadata
model.
  + * <br/>
  + * 3 - all together (1+2)
  + * </td>
  + * </tr>
    * </table>
    * <p/>
    * <br/>
  @@ -101,6 +124,7 @@
   {
       public static final String TIMEOUT_PROP = "timeout";
       public static final String AUTOSYNC_PROP = "autoSync";
  +    public static final String CACHING_KEY_TYPE_PROP = "cachingKeyType";
       /**
        * static Map held all cached objects
        */
  @@ -120,12 +144,21 @@
       private long timeout = 1000 * 60 * 15;
   
       /**
  -     *
  +     * Determines how the key was build for the cached objects:
  +     * <br/>
  +     * 0 - Identity object was used as key
  +     * 1 - Idenity + jcdAlias name was used as key
  +     * 2 - Identity + model (DescriptorRepository) was used as key
  +     * 3 - all together (1+2)
        */
  +    private int cachingKeyType;
  +
  +
       public ObjectCacheDefaultImpl(PersistenceBroker broker, Properties prop)
       {
           this.broker = broker;
           timeout = prop == null ? timeout : (Long.parseLong(prop.getProperty(TIMEOUT_PROP,
"" + (60 * 15))) * 1000);
  +        cachingKeyType = prop == null ? 0 : (Integer.parseInt(prop.getProperty(CACHING_KEY_TYPE_PROP,
"0")));
   
           useAutoSync = prop == null ?
                   false : (Boolean.valueOf((prop.getProperty(AUTOSYNC_PROP, "false")).trim())).booleanValue();
  @@ -163,7 +196,7 @@
           if ((obj != null))
           {
               traceIdentity(oid);
  -            objectTable.put(oid, new CacheEntry(obj, oid, queue));
  +            objectTable.put(buildKey(oid), new CacheEntry(obj, oid, queue));
           }
       }
   
  @@ -177,8 +210,8 @@
           hitCount++;
           Object result = null;
   
  -        CacheEntry entry = (CacheEntry) objectTable.get(oid);
  -        if (entry != null)
  +        CacheEntry entry = (CacheEntry) objectTable.get(buildKey(oid));
  +        if (entry != null && entry.oid.getObjectsTopLevelClass().equals(oid.getObjectsTopLevelClass()))
           {
               result = entry.get();
               if (result == null || entry.lifetime < System.currentTimeMillis())
  @@ -217,7 +250,7 @@
           if (oid != null)
           {
               removeTracedIdentity(oid);
  -            objectTable.remove(oid);
  +            objectTable.remove(buildKey(oid));
           }
       }
   
  @@ -257,8 +290,31 @@
           for (Iterator iterator = identitiesInWork.iterator(); iterator.hasNext();)
           {
               oid = (Identity) iterator.next();
  -            objectTable.remove(oid);
  +            objectTable.remove(buildKey(oid));
  +        }
  +    }
  +
  +    private Integer buildKey(Identity oid)
  +    {
  +        int key = 0;
  +        switch(cachingKeyType)
  +        {
  +            case 0:
  +                key = oid.hashCode();
  +                break;
  +            case 1:
  +                key = new HashCodeBuilder().append(broker.getPBKey().getAlias().hashCode()).append(oid.hashCode()).toHashCode();
  +                break;
  +            case 2:
  +                key = new HashCodeBuilder().append(broker.getDescriptorRepository().hashCode()).append(oid.hashCode()).toHashCode();
  +                break;
  +            case 3:
  +                key = new HashCodeBuilder().append(broker.getDescriptorRepository().hashCode()).append(broker.getPBKey().getAlias().hashCode()).append(oid.hashCode()).toHashCode();
  +                break;
  +            default:
  +                throw new OJBRuntimeException("Unexpected error, 'cacheType ="+cachingKeyType+"'
was not supported");
           }
  +        return new Integer(key);
       }
   
       public void beforeRollback(PBStateEvent event)
  @@ -302,7 +358,7 @@
           while ((sv = (CacheEntry) queue.poll()) != null)
           {
               removeTracedIdentity(sv.oid);
  -            objectTable.remove(sv.oid); // we can access private data!
  +            objectTable.remove(buildKey(sv.oid)); // we can access private data!
           }
       }
   
  
  
  
  1.24      +2 -1      db-ojb/src/test/org/apache/ojb/repository_database.xml
  
  Index: repository_database.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_database.xml,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- repository_database.xml	29 Jun 2004 17:20:04 -0000	1.23
  +++ repository_database.xml	4 Aug 2004 00:12:41 -0000	1.24
  @@ -49,6 +49,7 @@
           <object-cache class="org.apache.ojb.broker.cache.ObjectCacheDefaultImpl">
               <attribute attribute-name="timeout" attribute-value="900"/>
               <attribute attribute-name="autoSync" attribute-value="true"/>
  +            <attribute attribute-name="cachingKeyType" attribute-value="0"/>
           </object-cache>
   
           <connection-pool
  
  
  

---------------------------------------------------------------------
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