db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker MtoNMapping.java
Date Mon, 22 Dec 2003 16:11:26 GMT
brj         2003/12/22 08:11:26

  Modified:    src/java/org/apache/ojb/broker/accesslayer Tag:
                        OJB_BRANCH_1_0 RelationshipPrefetcherFactory.java
                        CollectionPrefetcher.java
               src/test/org/apache/ojb/broker Tag: OJB_BRANCH_1_0
                        MtoNMapping.java
  Added:       src/java/org/apache/ojb/broker/accesslayer Tag:
                        OJB_BRANCH_1_0 MtoNCollectionPrefetcher.java
  Log:
  support for prefetching m:n relationships
  including testcase
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.2.2.1   +10 -2     db-ojb/src/java/org/apache/ojb/broker/accesslayer/RelationshipPrefetcherFactory.java
  
  Index: RelationshipPrefetcherFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/RelationshipPrefetcherFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.2.2.1
  diff -u -r1.2 -r1.2.2.1
  --- RelationshipPrefetcherFactory.java	8 Dec 2003 21:13:19 -0000	1.2
  +++ RelationshipPrefetcherFactory.java	22 Dec 2003 16:11:26 -0000	1.2.2.1
  @@ -75,7 +75,15 @@
       {
           if (ord instanceof CollectionDescriptor)
           {
  -            return new CollectionPrefetcher(aBroker, ord);                
  +            CollectionDescriptor cds = (CollectionDescriptor)ord;
  +            if (cds.isMtoNRelation())
  +            {
  +                return new MtoNCollectionPrefetcher(aBroker, cds);                    
           
  +            }
  +            else
  +            {
  +                return new CollectionPrefetcher(aBroker, cds);                
  +            }
           }
           else
           {    
  
  
  
  1.20.2.1  +52 -43    db-ojb/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java
  
  Index: CollectionPrefetcher.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java,v
  retrieving revision 1.20
  retrieving revision 1.20.2.1
  diff -u -r1.20 -r1.20.2.1
  --- CollectionPrefetcher.java	14 Dec 2003 11:03:15 -0000	1.20
  +++ CollectionPrefetcher.java	22 Dec 2003 16:11:26 -0000	1.20.2.1
  @@ -60,6 +60,7 @@
   import java.util.HashMap;
   import java.util.HashSet;
   import java.util.Iterator;
  +import java.util.List;
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.ManageableCollection;
  @@ -163,43 +164,40 @@
       protected void associateBatched(Collection owners, Collection children)
       {
           CollectionDescriptor cds = getCollectionDescriptor();
  -        ClassDescriptor cld;
           PersistentField field = cds.getPersistentField();
           PersistenceBroker pb = getBroker();
           Class ownerTopLevelClass = pb.getTopLevelClass(getOwnerClassDescriptor().getClassOfObject());
       
           Class collectionClass = cds.getCollectionClass(); // this collection type will
be used:
  -        Object owner, relatedObject;
  -        Object fkValues[];
  -        Identity id;
           HashMap ownerIdsToLists = new HashMap();
  -        ArrayList list;
  -        Object result;
   
  +        // initialize the owner list map
  +        for (Iterator it = owners.iterator(); it.hasNext();)
  +        {
  +            Object owner = it.next();
  +            ownerIdsToLists.put(new Identity(owner,pb), new ArrayList());
  +        }
  +        
  +        // build the children lists for the owners
           for (Iterator it = children.iterator(); it.hasNext();)
           {
  -            relatedObject = it.next();
  +            Object child = it.next();
               // BRJ: use cld for real class, relatedObject could be Proxy
  -            cld = getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(relatedObject));
  -            fkValues = cds.getForeignKeyValues(relatedObject, cld);
  -            id = new Identity(null, ownerTopLevelClass, fkValues);
  -            list = (ArrayList) ownerIdsToLists.get(id);
  -            if (list == null)
  -            {
  -                list = new ArrayList();
  -                ownerIdsToLists.put(id, list);
  -            }
  -            list.add(relatedObject);
  +            ClassDescriptor cld = getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(child));
  +            
  +            Object[] fkValues = cds.getForeignKeyValues(child, cld);
  +            Identity ownerId = new Identity(null, ownerTopLevelClass, fkValues);
  +            List list = (List) ownerIdsToLists.get(ownerId);
  +            list.add(child);
           }
   
  +        // connect children list to owners  
           for (Iterator it = owners.iterator(); it.hasNext();)
           {
  -            owner = it.next();
  -            id = new Identity(owner, pb);
  -            list = (ArrayList) ownerIdsToLists.get(id);
  -            if (list == null)
  -            {
  -                list = new ArrayList();
  -            }
  +            Object result;
  +            Object owner = it.next();
  +            Identity ownerId = new Identity(owner, pb);
  +            List list = (List) ownerIdsToLists.get(ownerId);
  +                       
               if ((collectionClass == null) && field.getType().isArray())
               {
                   int length = list.size();
  @@ -212,23 +210,7 @@
               }
               else
               {
  -                ManageableCollection col;
  -
  -                if (collectionClass == null)
  -                {
  -                    col = new RemovalAwareCollection();
  -                }
  -                else
  -                {
  -                    try
  -                    {
  -                        col = (ManageableCollection) collectionClass.newInstance();
  -                    }
  -                    catch (Exception e)
  -                    {
  -                        throw new OJBRuntimeException("Can't create new Collection for
owner", e);
  -                    }
  -                }
  +                ManageableCollection col = createCollection(collectionClass);
                   for (Iterator it2 = list.iterator(); it2.hasNext();)
                   {
                       col.ojbAdd(it2.next());
  @@ -237,7 +219,6 @@
               }
   
               Object value = field.get(owner);
  -
               if ((value instanceof CollectionProxy) && (result instanceof Collection))
               {
                   ((CollectionProxy) value).setData((Collection) result);
  @@ -247,6 +228,34 @@
                   field.set(owner, result);
               }
           }
  +    }
  +
  +    /**
  +     * Create a Collection of class collectionClass
  +     * if collectionClass is null return a RemovalAwareCollection
  +     * @param collectionClass
  +     * @return
  +     */
  +    protected ManageableCollection createCollection(Class collectionClass)
  +    {
  +        ManageableCollection col;
  +
  +        if (collectionClass == null)
  +        {
  +            col = new RemovalAwareCollection();
  +        }
  +        else
  +        {
  +            try
  +            {
  +                col = (ManageableCollection) collectionClass.newInstance();
  +            }
  +            catch (Exception e)
  +            {
  +                throw new OJBRuntimeException("Can't create new Collection for owner",
e);
  +            }
  +        }
  +        return col;
       }
   
       protected CollectionDescriptor getCollectionDescriptor()
  
  
  
  No                   revision
  
  Index: CollectionPrefetcher.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java,v
  retrieving revision 1.20
  retrieving revision 1.20.2.1
  diff -u -r1.20 -r1.20.2.1
  --- CollectionPrefetcher.java	14 Dec 2003 11:03:15 -0000	1.20
  +++ CollectionPrefetcher.java	22 Dec 2003 16:11:26 -0000	1.20.2.1
  @@ -60,6 +60,7 @@
   import java.util.HashMap;
   import java.util.HashSet;
   import java.util.Iterator;
  +import java.util.List;
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.ManageableCollection;
  @@ -163,43 +164,40 @@
       protected void associateBatched(Collection owners, Collection children)
       {
           CollectionDescriptor cds = getCollectionDescriptor();
  -        ClassDescriptor cld;
           PersistentField field = cds.getPersistentField();
           PersistenceBroker pb = getBroker();
           Class ownerTopLevelClass = pb.getTopLevelClass(getOwnerClassDescriptor().getClassOfObject());
       
           Class collectionClass = cds.getCollectionClass(); // this collection type will
be used:
  -        Object owner, relatedObject;
  -        Object fkValues[];
  -        Identity id;
           HashMap ownerIdsToLists = new HashMap();
  -        ArrayList list;
  -        Object result;
   
  +        // initialize the owner list map
  +        for (Iterator it = owners.iterator(); it.hasNext();)
  +        {
  +            Object owner = it.next();
  +            ownerIdsToLists.put(new Identity(owner,pb), new ArrayList());
  +        }
  +        
  +        // build the children lists for the owners
           for (Iterator it = children.iterator(); it.hasNext();)
           {
  -            relatedObject = it.next();
  +            Object child = it.next();
               // BRJ: use cld for real class, relatedObject could be Proxy
  -            cld = getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(relatedObject));
  -            fkValues = cds.getForeignKeyValues(relatedObject, cld);
  -            id = new Identity(null, ownerTopLevelClass, fkValues);
  -            list = (ArrayList) ownerIdsToLists.get(id);
  -            if (list == null)
  -            {
  -                list = new ArrayList();
  -                ownerIdsToLists.put(id, list);
  -            }
  -            list.add(relatedObject);
  +            ClassDescriptor cld = getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(child));
  +            
  +            Object[] fkValues = cds.getForeignKeyValues(child, cld);
  +            Identity ownerId = new Identity(null, ownerTopLevelClass, fkValues);
  +            List list = (List) ownerIdsToLists.get(ownerId);
  +            list.add(child);
           }
   
  +        // connect children list to owners  
           for (Iterator it = owners.iterator(); it.hasNext();)
           {
  -            owner = it.next();
  -            id = new Identity(owner, pb);
  -            list = (ArrayList) ownerIdsToLists.get(id);
  -            if (list == null)
  -            {
  -                list = new ArrayList();
  -            }
  +            Object result;
  +            Object owner = it.next();
  +            Identity ownerId = new Identity(owner, pb);
  +            List list = (List) ownerIdsToLists.get(ownerId);
  +                       
               if ((collectionClass == null) && field.getType().isArray())
               {
                   int length = list.size();
  @@ -212,23 +210,7 @@
               }
               else
               {
  -                ManageableCollection col;
  -
  -                if (collectionClass == null)
  -                {
  -                    col = new RemovalAwareCollection();
  -                }
  -                else
  -                {
  -                    try
  -                    {
  -                        col = (ManageableCollection) collectionClass.newInstance();
  -                    }
  -                    catch (Exception e)
  -                    {
  -                        throw new OJBRuntimeException("Can't create new Collection for
owner", e);
  -                    }
  -                }
  +                ManageableCollection col = createCollection(collectionClass);
                   for (Iterator it2 = list.iterator(); it2.hasNext();)
                   {
                       col.ojbAdd(it2.next());
  @@ -237,7 +219,6 @@
               }
   
               Object value = field.get(owner);
  -
               if ((value instanceof CollectionProxy) && (result instanceof Collection))
               {
                   ((CollectionProxy) value).setData((Collection) result);
  @@ -247,6 +228,34 @@
                   field.set(owner, result);
               }
           }
  +    }
  +
  +    /**
  +     * Create a Collection of class collectionClass
  +     * if collectionClass is null return a RemovalAwareCollection
  +     * @param collectionClass
  +     * @return
  +     */
  +    protected ManageableCollection createCollection(Class collectionClass)
  +    {
  +        ManageableCollection col;
  +
  +        if (collectionClass == null)
  +        {
  +            col = new RemovalAwareCollection();
  +        }
  +        else
  +        {
  +            try
  +            {
  +                col = (ManageableCollection) collectionClass.newInstance();
  +            }
  +            catch (Exception e)
  +            {
  +                throw new OJBRuntimeException("Can't create new Collection for owner",
e);
  +            }
  +        }
  +        return col;
       }
   
       protected CollectionDescriptor getCollectionDescriptor()
  
  
  
  No                   revision
  
  Index: CollectionPrefetcher.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java,v
  retrieving revision 1.20
  retrieving revision 1.20.2.1
  diff -u -r1.20 -r1.20.2.1
  --- CollectionPrefetcher.java	14 Dec 2003 11:03:15 -0000	1.20
  +++ CollectionPrefetcher.java	22 Dec 2003 16:11:26 -0000	1.20.2.1
  @@ -60,6 +60,7 @@
   import java.util.HashMap;
   import java.util.HashSet;
   import java.util.Iterator;
  +import java.util.List;
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.ManageableCollection;
  @@ -163,43 +164,40 @@
       protected void associateBatched(Collection owners, Collection children)
       {
           CollectionDescriptor cds = getCollectionDescriptor();
  -        ClassDescriptor cld;
           PersistentField field = cds.getPersistentField();
           PersistenceBroker pb = getBroker();
           Class ownerTopLevelClass = pb.getTopLevelClass(getOwnerClassDescriptor().getClassOfObject());
       
           Class collectionClass = cds.getCollectionClass(); // this collection type will
be used:
  -        Object owner, relatedObject;
  -        Object fkValues[];
  -        Identity id;
           HashMap ownerIdsToLists = new HashMap();
  -        ArrayList list;
  -        Object result;
   
  +        // initialize the owner list map
  +        for (Iterator it = owners.iterator(); it.hasNext();)
  +        {
  +            Object owner = it.next();
  +            ownerIdsToLists.put(new Identity(owner,pb), new ArrayList());
  +        }
  +        
  +        // build the children lists for the owners
           for (Iterator it = children.iterator(); it.hasNext();)
           {
  -            relatedObject = it.next();
  +            Object child = it.next();
               // BRJ: use cld for real class, relatedObject could be Proxy
  -            cld = getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(relatedObject));
  -            fkValues = cds.getForeignKeyValues(relatedObject, cld);
  -            id = new Identity(null, ownerTopLevelClass, fkValues);
  -            list = (ArrayList) ownerIdsToLists.get(id);
  -            if (list == null)
  -            {
  -                list = new ArrayList();
  -                ownerIdsToLists.put(id, list);
  -            }
  -            list.add(relatedObject);
  +            ClassDescriptor cld = getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(child));
  +            
  +            Object[] fkValues = cds.getForeignKeyValues(child, cld);
  +            Identity ownerId = new Identity(null, ownerTopLevelClass, fkValues);
  +            List list = (List) ownerIdsToLists.get(ownerId);
  +            list.add(child);
           }
   
  +        // connect children list to owners  
           for (Iterator it = owners.iterator(); it.hasNext();)
           {
  -            owner = it.next();
  -            id = new Identity(owner, pb);
  -            list = (ArrayList) ownerIdsToLists.get(id);
  -            if (list == null)
  -            {
  -                list = new ArrayList();
  -            }
  +            Object result;
  +            Object owner = it.next();
  +            Identity ownerId = new Identity(owner, pb);
  +            List list = (List) ownerIdsToLists.get(ownerId);
  +                       
               if ((collectionClass == null) && field.getType().isArray())
               {
                   int length = list.size();
  @@ -212,23 +210,7 @@
               }
               else
               {
  -                ManageableCollection col;
  -
  -                if (collectionClass == null)
  -                {
  -                    col = new RemovalAwareCollection();
  -                }
  -                else
  -                {
  -                    try
  -                    {
  -                        col = (ManageableCollection) collectionClass.newInstance();
  -                    }
  -                    catch (Exception e)
  -                    {
  -                        throw new OJBRuntimeException("Can't create new Collection for
owner", e);
  -                    }
  -                }
  +                ManageableCollection col = createCollection(collectionClass);
                   for (Iterator it2 = list.iterator(); it2.hasNext();)
                   {
                       col.ojbAdd(it2.next());
  @@ -237,7 +219,6 @@
               }
   
               Object value = field.get(owner);
  -
               if ((value instanceof CollectionProxy) && (result instanceof Collection))
               {
                   ((CollectionProxy) value).setData((Collection) result);
  @@ -247,6 +228,34 @@
                   field.set(owner, result);
               }
           }
  +    }
  +
  +    /**
  +     * Create a Collection of class collectionClass
  +     * if collectionClass is null return a RemovalAwareCollection
  +     * @param collectionClass
  +     * @return
  +     */
  +    protected ManageableCollection createCollection(Class collectionClass)
  +    {
  +        ManageableCollection col;
  +
  +        if (collectionClass == null)
  +        {
  +            col = new RemovalAwareCollection();
  +        }
  +        else
  +        {
  +            try
  +            {
  +                col = (ManageableCollection) collectionClass.newInstance();
  +            }
  +            catch (Exception e)
  +            {
  +                throw new OJBRuntimeException("Can't create new Collection for owner",
e);
  +            }
  +        }
  +        return col;
       }
   
       protected CollectionDescriptor getCollectionDescriptor()
  
  
  
  1.1.2.1   +0 -0      db-ojb/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java
  
  Index: MtoNCollectionPrefetcher.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java,v
  retrieving revision 1.1
  retrieving revision 1.1.2.1
  diff -u -r1.1 -r1.1.2.1
  
  
  
  No                   revision
  No                   revision
  1.13.2.1  +46 -0     db-ojb/src/test/org/apache/ojb/broker/MtoNMapping.java
  
  Index: MtoNMapping.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/MtoNMapping.java,v
  retrieving revision 1.13
  retrieving revision 1.13.2.1
  diff -u -r1.13 -r1.13.2.1
  --- MtoNMapping.java	2 Jun 2003 12:52:28 -0000	1.13
  +++ MtoNMapping.java	22 Dec 2003 16:11:26 -0000	1.13.2.1
  @@ -1,5 +1,6 @@
   package org.apache.ojb.broker;
   
  +import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.List;
  @@ -9,6 +10,7 @@
   
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.query.QueryFactory;
   
   /** This TestClass tests OJB facilities to work with polymorphism.
  @@ -231,6 +233,48 @@
           assertNotNull(col);
       }
   
  +    /** test loading of m:n mapped object nets with prefetch*/
  +    public void testMNLoadingPrefetch() throws Exception
  +    {
  +        Criteria crit;
  +        QueryByCriteria qry;
  +        Collection col1, col2;
  +        
  +        broker.clearCache();
  +
  +        crit = new Criteria();
  +        crit.addLessThan("id",new Integer(6));
  +        qry = QueryFactory.newQuery(Project.class, crit);
  +        qry.addOrderByAscending("id");
  +        col1 = broker.getCollectionByQuery(qry);
  +        assertNotNull(col1);
  +
  +        broker.clearCache();
  +        
  +        crit = new Criteria();
  +        crit.addLessThan("id",new Integer(6));
  +        qry = QueryFactory.newQuery(Project.class, crit);
  +        qry.addOrderByAscending("id");
  +        qry.addPrefetchedRelationship("persons");
  +        col2 = broker.getCollectionByQuery(qry);
  +        assertNotNull(col2);
  +        
  +        assertEquals("Same size",col1.size(), col2.size());
  +
  +        Iterator it1 = col1.iterator();
  +        Iterator it2 = col2.iterator();
  +        
  +        while (it1.hasNext() && it2.hasNext())
  +        {
  +            Project p1 = (Project)it1.next();
  +            Project p2 = (Project)it2.next();
  +            
  +            assertEquals("Same Title", p1.getTitle(), p2.getTitle());
  +            assertEquals("Same Number of Persons", p1.getPersons().size(), p2.getPersons().size());
  +            assertEquals("Same toString", p1.toString(), p2.toString());
  +        }
  +    }
  +    
   
       /** test loading of m:n unidirectionally mapped objects*/
       public void testMNLoadingUnidirectional() throws Exception
  @@ -288,10 +332,12 @@
           int count = projects.size();
   
           Project proj = new Project();
  +        proj.setPersons(new ArrayList());
           proj.setTitle("MARS");
           proj.setDescription("colonization of planet Mars");
   
           p.getProjects().add(proj);
  +        proj.getPersons().add(p);
           assertEquals(count + 1, p.getProjects().size());
   
           broker.beginTransaction();
  
  
  

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