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/broker ObjectCacheTest.java
Date Wed, 31 Dec 2003 12:01:57 GMT
arminw      2003/12/31 04:01:57

  Modified:    src/java/org/apache/ojb/broker/cache ObjectCacheFactory.java
                        ObjectCacheEmptyImpl.java
                        ObjectCacheDefaultImpl.java
               src/test/org/apache/ojb/broker ObjectCacheTest.java
  Log:
  - fix bug in ObjectCacheDefaultImpl
  - fix documentation bug (for 'timeout' property)
  - add more comments
  
  Revision  Changes    Path
  1.15      +5 -4      db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheFactory.java
  
  Index: ObjectCacheFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheFactory.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ObjectCacheFactory.java	3 Oct 2003 12:53:35 -0000	1.14
  +++ ObjectCacheFactory.java	31 Dec 2003 12:01:56 -0000	1.15
  @@ -76,8 +76,6 @@
   {
       private static ObjectCacheFactory singleton = new ObjectCacheFactory();
   
  -    private ObjectCache defaultCache;
  -
       /**
        * Get the ObjectCacheFactory instance.
        */
  @@ -91,13 +89,16 @@
       }
   
       /**
  -     * Creates a new {@link InternObjectCache} instance.
  +     * Creates a new {@link ObjectCache} instance. Each <tt>ObjectCache</tt>
  +     * implementation was wrapped by a {@link CacheDistributor} instance and if
  +     * cache filtering is enabled by a {@link CacheFilterRegistry} instance too.
        *
        * @param broker The PB instance to associate with the cache instance
        */
       public ObjectCache createObjectCache(PersistenceBroker broker)
       {
           ObjectCache objectCache;
  +        ObjectCache defaultCache = null;
           try
           {
               getLogger().info("Start creating new ObjectCache instance");
  
  
  
  1.10      +6 -5      db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheEmptyImpl.java
  
  Index: ObjectCacheEmptyImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheEmptyImpl.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ObjectCacheEmptyImpl.java	17 Sep 2003 23:00:15 -0000	1.9
  +++ ObjectCacheEmptyImpl.java	31 Dec 2003 12:01:56 -0000	1.10
  @@ -64,9 +64,10 @@
   /**
    * This is an 'empty' ObjectCache implementation.
    * Useful when caching was not desired.
  - * To support 'circular references' this implementation
  - * use a temporary map while store and delete operation
  - * (this may change in further versions).
  + * <br/>
  + * NOTE: This implementation does not prevent infinite loops caused by
  + * 'circular references' of loaded object graphs.
  + * (this will change in versions &gt; 1.0).
    *
    * <p>
    * Implementation configuration properties:
  @@ -131,7 +132,7 @@
   
       public String toString()
       {
  -        ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
  +        ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
           return buf.toString();
       }
   }
  
  
  
  1.16      +24 -16    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.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- ObjectCacheDefaultImpl.java	16 Dec 2003 21:12:50 -0000	1.15
  +++ ObjectCacheDefaultImpl.java	31 Dec 2003 12:01:56 -0000	1.16
  @@ -54,6 +54,7 @@
    * <http://www.apache.org/>.
    */
   
  +import java.lang.ref.Reference;
   import java.lang.ref.SoftReference;
   import java.util.Hashtable;
   import java.util.Map;
  @@ -91,9 +92,12 @@
    * <tr>
    *     <td>timeout</td>
    *     <td>
  - *          Lifetime of the cached objects in milliseconds.
  + *          Lifetime of the cached objects in seconds.
    *          If expired the cached object was not returned
  - *          on lookup call (and removed from cache).
  + *          on lookup call (and removed from cache). Default timeout
  + *          value is 900 seconds.
  + *          <br/>NOTE: Objects internal cached via {@link SoftReference}, so
  + *          lifetime of cached object is limited by the GC too.
    *    </td>
    * </tr>
    * </table>
  @@ -144,8 +148,7 @@
       {
           if ((obj != null))
           {
  -            SoftReference ref = new SoftReference(new CacheEntry(obj));
  -            objectTable.put(oid, ref);
  +            objectTable.put(oid, new CacheEntry(obj));
           }
       }
   
  @@ -156,24 +159,29 @@
       public Object lookup(Identity oid)
       {
           hitCount++;
  -        CacheEntry entry = null;
  -        SoftReference ref = (SoftReference) objectTable.get(oid);
  -        if (ref != null)
  +        Object result = null;
  +
  +        CacheEntry entry = (CacheEntry) objectTable.get(oid);
  +        if (entry != null)
           {
  -            entry = (CacheEntry) ref.get();
  -            if (entry == null || entry.lifetime < System.currentTimeMillis())
  +            result = entry.ref.get();
  +            if (result == null || entry.lifetime < System.currentTimeMillis())
               {
  +                /*
  +                cached object was removed by gc or lifetime was exhausted
  +                remove CacheEntry from map
  +                */
                   gcCount++;
  -                objectTable.remove(oid);    // Soft-referenced Object reclaimed by GC
  -                // timeout, so set null
  -                entry = null;
  +                objectTable.remove(oid);
  +                // make sure that we return null
  +                result = null;
               }
           }
           else
           {
               failCount++;
           }
  -        return entry != null ? entry.object : null;
  +        return result;
       }
   
       /**
  @@ -209,11 +217,11 @@
       class CacheEntry
       {
           long lifetime;
  -        Object object;
  +        Reference ref;
   
           public CacheEntry(Object object)
           {
  -            this.object = object;
  +            this.ref = new SoftReference(object);
               lifetime = System.currentTimeMillis() + timeout;
           }
       }
  
  
  
  1.12      +56 -12    db-ojb/src/test/org/apache/ojb/broker/ObjectCacheTest.java
  
  Index: ObjectCacheTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/ObjectCacheTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ObjectCacheTest.java	13 Dec 2003 19:16:53 -0000	1.11
  +++ ObjectCacheTest.java	31 Dec 2003 12:01:56 -0000	1.12
  @@ -141,17 +141,61 @@
       {
           TestObjectDefaultCache obj = new TestObjectDefaultCache();
           PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -        broker.beginTransaction();
  -        broker.store(obj);
  -        broker.commitTransaction();
  -
  -        Identity oid = new Identity(obj, broker);
  -        obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid);
  -        assertNotNull(obj);
  -
  -        Thread.sleep(5000);
  -        obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid);
  -        assertNull(obj);
  +        try
  +        {
  +            broker.beginTransaction();
  +            broker.store(obj);
  +            broker.commitTransaction();
  +
  +            Identity oid = new Identity(obj, broker);
  +            obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid);
  +            assertNotNull(obj);
  +
  +            Thread.sleep(5000);
  +            obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid);
  +            assertNull(obj);
  +        }
  +        finally
  +        {
  +            if(broker != null) broker.close();
  +        }
  +    }
  +
  +    public void testObjectCacheDefaultImpl() throws Exception
  +    {
  +        String name = "testObjectCacheDefaultImpl_"+System.currentTimeMillis();
  +        TestObjectDefaultCache obj = new TestObjectDefaultCache();
  +        obj.setName(name);
  +        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +        try
  +        {
  +            broker.beginTransaction();
  +            broker.store(obj);
  +            broker.commitTransaction();
  +
  +            Identity oid = new Identity(obj, broker);
  +            obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid);
  +            assertNotNull(obj);
  +            assertEquals(name, obj.getName());
  +
  +            // modify name
  +            String new_name = "modified_"+name;
  +            obj.setName(new_name);
  +            obj = (TestObjectDefaultCache) broker.getObjectByIdentity(oid);
  +            assertNotNull(obj);
  +            assertEquals("current version of cache should return the modified object",
new_name, obj.getName());
  +
  +            broker.removeFromCache(oid);
  +            obj = (TestObjectDefaultCache) broker.serviceObjectCache().lookup(oid);
  +            assertNull("Should be removed from cache", obj);
  +            obj = (TestObjectDefaultCache) broker.getObjectByIdentity(oid);
  +            assertNotNull(obj);
  +            assertEquals("Should return the unmodified object", name, obj.getName());
  +        }
  +        finally
  +        {
  +            if(broker != null) broker.close();
  +        }
       }
   
       /**
  
  
  

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