db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/otm/core ConcreteEditingContext.java
Date Sun, 29 Feb 2004 15:15:48 GMT
olegnitz    2004/02/29 07:15:48

  Modified:    src/java/org/apache/ojb/otm/core ConcreteEditingContext.java
  Log:
  Fixed the order of creation/deletion of dependent objects
  
  Revision  Changes    Path
  1.37      +72 -40    db-ojb/src/java/org/apache/ojb/otm/core/ConcreteEditingContext.java
  
  Index: ConcreteEditingContext.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/core/ConcreteEditingContext.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- ConcreteEditingContext.java	28 Feb 2004 21:44:57 -0000	1.36
  +++ ConcreteEditingContext.java	29 Feb 2004 15:15:48 -0000	1.37
  @@ -160,12 +160,12 @@
       public void insert(Identity oid, Object obj, int lock)
               throws LockingException
       {
  -        insertInternal(oid, obj, lock, true, false, false);
  +        insertInternal(oid, obj, lock, true, false, null);
       }
   
       private ContextEntry insertInternal(Identity oid, Object newObj, int lock,
                                           boolean mainObject, boolean isDependent,
  -                                        boolean addBeforeLast)
  +                                        Identity insertBeforeThis)
               throws LockingException
       {
           ContextEntry entry;
  @@ -233,9 +233,10 @@
                   }
               }
               _objects.put(oid, entry);
  -            if (addBeforeLast)
  +            if (insertBeforeThis != null)
               {
  -                _order.add(_order.size() - 1, oid);
  +                int insertIndex = _order.indexOf(insertBeforeThis);
  +                _order.add(insertIndex, oid);
               }
               else
               {
  @@ -283,7 +284,7 @@
           // if the inserted object is materialized
           if ((handler == null) && (newObj != null))
           {
  -            lockReachableObjects(newObj, lock);
  +            lockReachableObjects(oid, newObj, lock);
           }
   
           return entry;
  @@ -304,7 +305,7 @@
       {
           ContextEntry entry;
   
  -        entry = insertInternal(oid, object, LockType.WRITE_LOCK, true, false, false);
  +        entry = insertInternal(oid, object, LockType.WRITE_LOCK, true, false, null);
           if (entry != null)
           {
               entry.state = entry.state.deletePersistent();
  @@ -394,7 +395,7 @@
           // if the inserted object is materialized
           try
           {
  -            lockReachableObjects(object, lock);
  +            lockReachableObjects(oid, object, lock);
           }
           catch (LockingException ex)
           {
  @@ -462,7 +463,7 @@
           {
               return;
           }
  -        
  +
           removeCollectionProxyListeners();
   
           ConnectionManagerIF connMan = _pb.serviceConnectionManager();
  @@ -519,10 +520,10 @@
                               if (origFields == null)
                               {
                                   newObjects.addAll(
  -                                        handleDependentReferences(entry.object,
  +                                        handleDependentReferences(oid, entry.object,
                                           null, newFields[0], newFields[2]));
                                   newObjects.addAll(
  -                                        handleDependentCollections(entry.object,
  +                                        handleDependentCollections(oid, entry.object,
                                           null, newFields[1], newFields[3]));
                               }
                               else
  @@ -533,7 +534,7 @@
                                       entry.needsCacheSwizzle = true;
                                       lockManager.ensureLock(oid, _tx, LockType.WRITE_LOCK,
_pb);
                                       newObjects.addAll(
  -                                            handleDependentReferences(entry.object,
  +                                            handleDependentReferences(oid, entry.object,
                                               origFields[0], newFields[0], newFields[2]));
                                   }
   
  @@ -544,7 +545,7 @@
                                       entry.needsCacheSwizzle = !state.needsInsert();
                                       lockManager.ensureLock(oid, _tx, LockType.WRITE_LOCK,
_pb);
                                       newObjects.addAll(
  -                                            handleDependentCollections(entry.object,
  +                                            handleDependentCollections(oid, entry.object,
                                               origFields[1], newFields[1], newFields[3]));
                                       changedCollections.add(oid);
                                   }
  @@ -562,12 +563,10 @@
               }
               while (countNewObjects > 0);
   
  -            Identity[] order = (Identity[]) _order.toArray(new Identity[_order.size()]);
  -
               // Swizzle the context objects and the cache objects
  -            for (int i = 0; i < order.length; i++)
  +            for (Iterator it = _order.iterator(); it.hasNext(); )
               {
  -                Identity oid = order[i];
  +                Identity oid = (Identity) it.next();
                   ContextEntry entry = (ContextEntry) _objects.get(oid);
   
                   if (entry.needsCacheSwizzle)
  @@ -592,14 +591,16 @@
               do
               {
                   countCascadeDeleted = 0;
  -                for (int i = 0; i < order.length; i++)
  +                // Use intermediate new ArrayList(_order) because _order
  +                // may be changed during cascade delete
  +                for (Iterator it = (new ArrayList(_order)).iterator(); it.hasNext(); )
                   {
  -                    Identity oid = order[i];
  +                    Identity oid = (Identity) it.next();
                       ContextEntry entry = (ContextEntry) _objects.get(oid);
   
                       if (entry.state.isDeleted())
                       {
  -                        countCascadeDeleted += doCascadeDelete(entry.object);
  +                        countCascadeDeleted += doCascadeDelete(oid, entry.object);
                       }
                   }
               }
  @@ -609,9 +610,9 @@
               connMan.setBatchMode(true);
               try
               {
  -                for (int i = 0; i < order.length; i++)
  +                for (Iterator it = _order.iterator(); it.hasNext(); )
                   {
  -                    Identity oid = order[i];
  +                    Identity oid = (Identity) it.next();
                       ContextEntry entry = (ContextEntry) _objects.get(oid);
                       State state = entry.state;
   
  @@ -715,7 +716,7 @@
        * Lock all objects reachable via 1:N and N:1 relations,
        * @param lock The lock type to use
        */
  -    private void lockReachableObjects(Object object, int lock)
  +    private void lockReachableObjects(Identity oid, Object object, int lock)
           throws LockingException
       {
           boolean onlyDependants = !_tx.getKit().isImplicitLockingUsed();
  @@ -746,7 +747,7 @@
                   cntxRelObj = lookup(relOid);
                   if (cntxRelObj == null)
                   {
  -                    insertInternal(relOid, relObj, lock, false, isDependent, true);
  +                    insertInternal(relOid, relObj, lock, false, isDependent, oid);
                   }
                   else if (cntxRelObj != relObj)
                   {
  @@ -808,7 +809,7 @@
                       cntxRelObj = lookup(relOid);
                       if (cntxRelObj == null)
                       {
  -                        insertInternal(relOid, relObj, lock, false, isDependent, false);
  +                        insertInternal(relOid, relObj, lock, false, isDependent, null);
                           cntxRelObj = relObj;
                       }
                       else if (cntxRelObj != relObj)
  @@ -1194,7 +1195,7 @@
       /**
        * @return number of deleted objects: 1 or 0 (if the object is already deleted)
        */
  -    private int markDelete(Object oid)
  +    private int markDelete(Identity oid, Identity mainOid, boolean isCollection)
       {
           ContextEntry entry = (ContextEntry) _objects.get(oid);
   
  @@ -1211,6 +1212,29 @@
           else
           {
               entry.state = entry.state.deletePersistent();
  +            if (mainOid != null)
  +            {
  +                int dependentIndex = _order.indexOf(oid);
  +                int mainIndex = _order.indexOf(mainOid);
  +
  +                if (isCollection) // remove collection item before main obj
  +                {
  +                    if (dependentIndex > mainIndex)
  +                    {
  +                        _order.remove(dependentIndex);
  +                        _order.add(mainIndex, oid);
  +                    }
  +                }
  +                else // remove reference after main obj
  +                {
  +                    if (dependentIndex < mainIndex)
  +                    {
  +                        _order.remove(dependentIndex); // this causes mainIndex--
  +                        _order.add(mainIndex, oid);
  +                    }
  +                }
  +
  +            }
               return 1;
           }
       }
  @@ -1218,7 +1242,7 @@
       /**
        * Insert the object is not in the context and mark as new.
        */
  -    private void markNew(Object oid, Object object)
  +    private void markNew(Object oid, Object object, Identity insertBeforeThis)
       {
           ContextEntry entry = new ContextEntry(object);
   
  @@ -1230,7 +1254,15 @@
   
           entry.state = State.PERSISTENT_NEW;
           _objects.put(oid, entry);
  -        _order.add(oid);
  +        if (insertBeforeThis != null)
  +        {
  +            int insertIndex = _order.indexOf(insertBeforeThis);
  +            _order.add(insertIndex, oid);
  +        }
  +        else
  +        {
  +            _order.add(oid);
  +        }
       }
   
       /**
  @@ -1238,7 +1270,7 @@
        * Mark for deletion all nullified dependent references.
        * @return the list of created objects
        */
  -    private ArrayList handleDependentReferences(Object obj,
  +    private ArrayList handleDependentReferences(Identity oid, Object obj,
               Object[] origFields, Object[] newFields, Object[] newRefs)
       {
           ClassDescriptor mif = _pb.getClassDescriptor(obj.getClass());
  @@ -1251,8 +1283,8 @@
           for (Iterator it = refDescs.iterator(); it.hasNext(); count++, countRefs++)
           {
               ObjectReferenceDescriptor rds = (ObjectReferenceDescriptor) it.next();
  -            Object origOid = (origFields == null ? null : origFields[count]);
  -            Object newOid = newFields[count];
  +            Identity origOid = (origFields == null ? null : (Identity) origFields[count]);
  +            Identity newOid = (Identity) newFields[count];
   
               if (rds.getOtmDependent())
               {
  @@ -1263,14 +1295,14 @@
                       if (entry == null)
                       {
                           Object relObj = newRefs[countRefs];
  -                        markNew(newOid, relObj);
  +                        markNew(newOid, relObj, oid);
                           newObjects.add(newOid);
                       }
                   }
                   else if ((origOid != null) &&
                            ((newOid == null) || !newOid.equals(origOid)))
                   {
  -                    markDelete(origOid);
  +                    markDelete(origOid, oid, false);
                   }
               }
           }
  @@ -1282,7 +1314,7 @@
        * Mark for creation all objects that were included into dependent collections.
        * Mark for deletion all objects that were excluded from dependent collections.
        */
  -    private ArrayList handleDependentCollections(Object obj,
  +    private ArrayList handleDependentCollections(Identity oid, Object obj,
               Object[] origCollections, Object[] newCollections,
               Object[] newCollectionsOfObjects)
       {
  @@ -1305,11 +1337,11 @@
                   {
                       for (Iterator it2 = origList.iterator(); it2.hasNext(); )
                       {
  -                        Object origOid = it2.next();
  +                        Identity origOid = (Identity) it2.next();
   
                           if (!newList.contains(origOid))
                           {
  -                            markDelete(origOid);
  +                            markDelete(origOid, oid, true);
                           }
                       }
                   }
  @@ -1328,7 +1360,7 @@
                               ArrayList relCol = (ArrayList)
                                       newCollectionsOfObjects[count];
                               Object relObj = relCol.get(countElem);
  -                            markNew(newOid, relObj);
  +                            markNew(newOid, relObj, null);
                               newObjects.add(newOid);
                           }
                       }
  @@ -1343,7 +1375,7 @@
        * Mark for deletion all dependent objects (via references and collections).
        * @return the number of deleted objects
        */
  -    private int doCascadeDelete(Object obj)
  +    private int doCascadeDelete(Identity oid, Object obj)
       {
           ClassDescriptor mif = _pb.getClassDescriptor(ProxyHelper.getRealClass(obj));
           Collection refDescs = mif.getObjectReferenceDescriptors();
  @@ -1362,7 +1394,7 @@
                   if (relObj != null)
                   {
                       countCascadeDeleted +=
  -                            markDelete(new Identity(relObj, _pb));
  +                            markDelete(new Identity(relObj, _pb), oid, false);
                   }
               }
           }
  @@ -1398,7 +1430,7 @@
                       {
   
                           countCascadeDeleted +=
  -                                markDelete(new Identity(colIterator.next(), _pb));
  +                                markDelete(new Identity(colIterator.next(), _pb), oid,
true);
                       }
                   }
               }
  @@ -1516,7 +1548,7 @@
                       try
                       {
                           insertInternal(relOid, relObj, _lock, false,
  -                                       _cds.getOtmDependent(), false);
  +                                       _cds.getOtmDependent(), null);
                       }
                       catch (LockingException ex)
                       {
  
  
  

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