db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bri...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker/dirty DirtyTest.java
Date Thu, 08 Jul 2004 01:58:34 GMT
brianm      2004/07/07 18:58:34

  Modified:    src/java/org/apache/ojb/broker/dirty
                        DirtyPersistenceBrokerFactory.java Entry.java
                        ManagedEntry.java PersistenceAwareEntry.java
                        StateListener.java Tracker.java
               src/java/org/apache/ojb/broker/util IdentityHashMap.java
               src/test/org/apache/ojb/broker/dirty DirtyTest.java
  Log:
  Fix bug in pre-jdk1.4 IdentityHashMap
  
  Handle situation in Dirty stuff where a persistent-dirty object gets re-inserted after deletion.
  
  Revision  Changes    Path
  1.3       +1 -1      db-ojb/src/java/org/apache/ojb/broker/dirty/DirtyPersistenceBrokerFactory.java
  
  Index: DirtyPersistenceBrokerFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/DirtyPersistenceBrokerFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DirtyPersistenceBrokerFactory.java	8 Jul 2004 00:47:04 -0000	1.2
  +++ DirtyPersistenceBrokerFactory.java	8 Jul 2004 01:58:34 -0000	1.3
  @@ -34,7 +34,7 @@
   {
       private static final PersistenceBroker wrap(PersistenceBroker broker)
       {
  -        Tracker t = new Tracker();
  +        Tracker t = new Tracker(broker);
   
           broker.addListener(new LifeCycleListener(t));
           broker.addListener(new StateListener(t));
  
  
  
  1.3       +7 -5      db-ojb/src/java/org/apache/ojb/broker/dirty/Entry.java
  
  Index: Entry.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/Entry.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Entry.java	8 Jul 2004 00:47:04 -0000	1.2
  +++ Entry.java	8 Jul 2004 01:58:34 -0000	1.3
  @@ -1,7 +1,5 @@
   package org.apache.ojb.broker.dirty;
   
  -import org.apache.ojb.broker.PersistenceBroker;
  -
   /**
    * Interface to provide polymorphism between the different ways of handling
    * persistent objects.
  @@ -9,13 +7,17 @@
   interface Entry
   {
       /**
  -     * prepare for commit (typically store() changed attributes to the pb provided)
  -     * @param broker PB about to commit
  +     * prepare for commit
        */
  -    void prepare(PersistenceBroker broker);
  +    void prepare();
   
       /**
        * Restore changed attributes after rollback
        */
       void restore();
  +
  +    /**
  +     * Specified when a persistent entry is deleted (PERSISTENT_DELETED state)
  +     */
  +    void setDeleted();
   }
  
  
  
  1.2       +13 -5     db-ojb/src/java/org/apache/ojb/broker/dirty/ManagedEntry.java
  
  Index: ManagedEntry.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/ManagedEntry.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ManagedEntry.java	8 Jul 2004 00:47:04 -0000	1.1
  +++ ManagedEntry.java	8 Jul 2004 01:58:34 -0000	1.2
  @@ -14,12 +14,11 @@
    */
   package org.apache.ojb.broker.dirty;
   
  -import org.apache.ojb.otm.states.State;
  -import org.apache.ojb.broker.metadata.MetadataManager;
  +import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
  +import org.apache.ojb.broker.metadata.MetadataManager;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
  -import org.apache.ojb.broker.PersistenceBroker;
   
   /**
    * Provide copy-on-read and compare-on-commit style state management for
  @@ -31,10 +30,13 @@
   class ManagedEntry implements Entry
   {
       private Object[] fields;
  +    private PersistenceBroker broker;
       private Object original;
  +    private boolean deleted = false;
   
  -    public ManagedEntry(Object original)
  +    public ManagedEntry(PersistenceBroker broker, Object original)
       {
  +        this.broker = broker;
           this.original = original;
           MetadataManager man = MetadataManager.getInstance();
           ClassDescriptor cd = man.getRepository().getDescriptorFor(original.getClass());
  @@ -48,8 +50,9 @@
           }
       }
   
  -    public void prepare(PersistenceBroker broker)
  +    public void prepare()
       {
  +        if (deleted) return;
           MetadataManager man = MetadataManager.getInstance();
           ClassDescriptor cd = man.getRepository().getDescriptorFor(original.getClass());
           FieldDescriptor[] fds = cd.getFieldDescriptions();
  @@ -95,5 +98,10 @@
                   pd.set(original, fields[i]);
               }
           }
  +    }
  +
  +    public void setDeleted()
  +    {
  +        this.deleted = true;
       }
   }
  
  
  
  1.2       +11 -4     db-ojb/src/java/org/apache/ojb/broker/dirty/PersistenceAwareEntry.java
  
  Index: PersistenceAwareEntry.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/PersistenceAwareEntry.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PersistenceAwareEntry.java	8 Jul 2004 00:47:04 -0000	1.1
  +++ PersistenceAwareEntry.java	8 Jul 2004 01:58:34 -0000	1.2
  @@ -18,17 +18,19 @@
   
   class PersistenceAwareEntry implements Entry
   {
  +    private PersistenceBroker broker;
       private PersistenceAware aware;
       private boolean dirty = false;
  +    private boolean deleted = false;
   
  -    PersistenceAwareEntry(PersistenceAware aware)
  +    PersistenceAwareEntry(PersistenceBroker broker, PersistenceAware aware)
       {
  -
  +        this.broker = broker;
           this.aware = aware;
       }
  -    public void prepare(PersistenceBroker broker)
  +    public void prepare()
       {
  -        if (dirty)
  +        if (dirty && !deleted)
           {
               broker.store(aware);
           }
  @@ -37,6 +39,11 @@
       public void restore()
       {
           aware.restore();
  +    }
  +
  +    public void setDeleted()
  +    {
  +        this.deleted = true;
       }
   
       public void makeDirty()
  
  
  
  1.3       +1 -1      db-ojb/src/java/org/apache/ojb/broker/dirty/StateListener.java
  
  Index: StateListener.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/StateListener.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StateListener.java	8 Jul 2004 00:47:04 -0000	1.2
  +++ StateListener.java	8 Jul 2004 01:58:34 -0000	1.3
  @@ -48,7 +48,7 @@
        */
       public void beforeCommit(PBStateEvent event)
       {
  -        tracker.prepare(event.getTriggeringBroker());
  +        tracker.prepare();
       }
   
       /**
  
  
  
  1.3       +27 -5     db-ojb/src/java/org/apache/ojb/broker/dirty/Tracker.java
  
  Index: Tracker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/Tracker.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Tracker.java	8 Jul 2004 00:47:04 -0000	1.2
  +++ Tracker.java	8 Jul 2004 01:58:34 -0000	1.3
  @@ -15,8 +15,9 @@
   package org.apache.ojb.broker.dirty;
   
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.Identity;
  +import org.apache.ojb.broker.util.IdentityHashMap;
   import org.apache.ojb.broker.core.proxy.ProxyHelper;
  -import org.apache.ojb.otm.states.State;
   
   import java.util.HashSet;
   import java.util.Iterator;
  @@ -27,8 +28,18 @@
   class Tracker
   {
       private final HashSet dirties = new HashSet();
  +
  +    /** Identity -> Entry */
  +    private final IdentityHashMap entries = new IdentityHashMap();
  +
       private boolean flushing = false;
       private boolean transactional = true;
  +    private PersistenceBroker broker;
  +
  +    Tracker(PersistenceBroker broker)
  +    {
  +        this.broker = broker;
  +    }
   
       /**
        * transaction started, begin tracking changes
  @@ -38,6 +49,7 @@
           flushing = false;
           transactional = true;
           dirties.clear();
  +        entries.clear();
       }
   
       public void rollback()
  @@ -49,21 +61,24 @@
               entry.restore();
           }
           dirties.clear();
  +        entries.clear();
       }
   
       /**
        * About to prepare, flush dirty objects!
        */
  -    public void prepare(PersistenceBroker broker)
  +    public void prepare()
       {
           flushing = true;
           for (Iterator iterator = dirties.iterator(); iterator.hasNext();)
           {
               Entry entry = (Entry) iterator.next();
  -            entry.prepare(broker);
  +            entry.prepare();
           }
           flushing = false;
           transactional = false;
  +        dirties.clear();
  +        entries.clear();
       }
   
       /**
  @@ -92,6 +107,9 @@
       public void deleted(Object source)
       {
           if (!transactional) return;
  +        Entry entry = (Entry) entries.get(source);
  +        dirties.remove(entry);
  +        entry.setDeleted();
       }
   
       /**
  @@ -106,15 +124,19 @@
       private void track(Object source)
       {
           if (ProxyHelper.isProxy(source)) return;
  +        Entry entry = null;
           if (source instanceof PersistenceAware)
           {
               PersistenceAware aware = (PersistenceAware) source;
  -            aware.setStateReporter(new StateReporterImpl(this, new PersistenceAwareEntry(aware)));
  +            entry = new PersistenceAwareEntry(broker, aware);
  +            aware.setStateReporter(new StateReporterImpl(this, (PersistenceAwareEntry)
entry));
           }
           else
           {
  -            dirties.add(new ManagedEntry(source));
  +            entry = new ManagedEntry(broker, source);
  +            dirties.add(entry);
           }
  +        this.entries.put(source, entry);
       }
   
       void makeDirty(Entry entry)
  
  
  
  1.9       +8 -1      db-ojb/src/java/org/apache/ojb/broker/util/IdentityHashMap.java
  
  Index: IdentityHashMap.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/IdentityHashMap.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- IdentityHashMap.java	4 Apr 2004 23:53:37 -0000	1.8
  +++ IdentityHashMap.java	8 Jul 2004 01:58:34 -0000	1.9
  @@ -37,7 +37,14 @@
   
   		public boolean equals(final Object o)
   		{
  -			return (o == m_key);
  +            if (o instanceof IdentityKey)
  +            {
  +                return ((IdentityKey)o).m_key == m_key;
  +            }
  +            else
  +            {
  +			    return (o == m_key);
  +            }
   		}
   
   		public int hashCode()
  
  
  
  1.3       +47 -0     db-ojb/src/test/org/apache/ojb/broker/dirty/DirtyTest.java
  
  Index: DirtyTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/dirty/DirtyTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DirtyTest.java	8 Jul 2004 00:47:05 -0000	1.2
  +++ DirtyTest.java	8 Jul 2004 01:58:34 -0000	1.3
  @@ -163,4 +163,51 @@
   
           pb.close();
       }
  +
  +
  +    public void testDeleteDirtyManagedObject()
  +    {
  +        PersistenceAwarePerson p = new PersistenceAwarePerson();
  +        p.setFirstname("brian");
  +        PersistenceBroker pb = DirtyPersistenceBrokerFactory.defaultPersistenceBroker();
  +        pb.store(p);
  +
  +        pb.clearCache();
  +
  +        pb.beginTransaction();
  +        p = (PersistenceAwarePerson) pb.getObjectByIdentity(new Identity(p, pb));
  +        p.setFirstname("Doug");
  +        pb.delete(p);
  +        pb.commitTransaction();
  +
  +        pb.clearCache();
  +
  +        p = (PersistenceAwarePerson) pb.getObjectByIdentity(new Identity(p, pb));
  +        assertNull(p);
  +
  +        pb.close();
  +    }
  +
  +    public void testDeleteDirtyAwareObject()
  +    {
  +        Person p = new Person();
  +        p.setFirstname("brian");
  +        PersistenceBroker pb = DirtyPersistenceBrokerFactory.defaultPersistenceBroker();
  +        pb.store(p);
  +
  +        pb.clearCache();
  +
  +        pb.beginTransaction();
  +        p = (Person) pb.getObjectByIdentity(new Identity(p, pb));
  +        p.setFirstname("Doug");
  +        pb.delete(p);
  +        pb.commitTransaction();
  +
  +        pb.clearCache();
  +
  +        p = (Person) pb.getObjectByIdentity(new Identity(p, pb));
  +        assertNull(p);
  +
  +        pb.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