db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer ReferencePrefetcher.java CollectionPrefetcher.java
Date Sun, 29 Dec 2002 08:51:50 GMT
brj         2002/12/29 00:51:50

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        ReferencePrefetcher.java CollectionPrefetcher.java
  Log:
  improved handling for multiple keys
  
  Revision  Changes    Path
  1.4       +80 -28    jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/ReferencePrefetcher.java
  
  Index: ReferencePrefetcher.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/ReferencePrefetcher.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ReferencePrefetcher.java	20 Oct 2002 15:50:57 -0000	1.3
  +++ ReferencePrefetcher.java	29 Dec 2002 08:51:49 -0000	1.4
  @@ -54,9 +54,9 @@
    * <http://www.apache.org/>.
    */
   
  +import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Iterator;
  -import java.util.Vector;
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBroker;
  @@ -117,35 +117,87 @@
   	protected Query buildPrefetchQuery(Collection owners)
   	{
   		ObjectReferenceDescriptor ord = getObjectReferenceDescriptor();
  -		Criteria crit = new Criteria();
  -		ClassDescriptor refCld = getDescriptorRepository().getDescriptorFor(ord.getItemClass());
  -		FieldDescriptor pkFields[] = refCld.getPkFields();
  -		FieldDescriptor fkFields[] = ord.getForeignKeyFieldDescriptors(getOwnerClassDescriptor());
  -		Collection fkValues;
  -		Iterator iter;
  -		Object fkVal;
  +        FieldDescriptor pkFields[] = getItemClassDescriptor().getPkFields();
  +        Criteria crit;
   
  -		for (int i = 0; i < fkFields.length; i++)
  -		{
  -			fkValues = new Vector();
  -			iter = owners.iterator();
  -			while (iter.hasNext())
  -			{
  -				fkVal = getObjectReferenceDescriptor().getForeignKeyValues(iter.next(),getOwnerClassDescriptor())[i];
  -				if (fkVal != null)
  -				{
  -					fkValues.add(fkVal);
  -				}	
  -			}
  -
  -			// create IN (...) for each key field
  -			if (!fkValues.isEmpty())
  -			{
  -				crit.addIn(pkFields[i].getAttributeName(), fkValues);
  -			}	
  -		}
  +        if (pkFields.length == 1)
  +        {
  +            crit = buildPrefetchCriteriaSingleKey(owners, pkFields[0]);
  +        }    
  +        else
  +        {
  +            crit = buildPrefetchCriteriaMultipleKeys(owners, pkFields);
  +        }
   
   		return QueryFactory.newQuery(ord.getItemClass(), crit);
   	}
  +
  +    /**
  +     * Build the Criteria using IN(...) for single keys
  +     * @param owners
  +     * @param pkField
  +     * @return Criteria
  +     */
  +    private Criteria buildPrefetchCriteriaSingleKey(Collection owners, FieldDescriptor
pkField)
  +    {
  +        Criteria crit = new Criteria();
  +        Collection fkValues = new ArrayList();
  +        Iterator iter = owners.iterator();
  +        Object fkVal;
  +        Object owner;
  +
  +        while (iter.hasNext())
  +        {
  +            owner = iter.next();
  +            fkVal = getObjectReferenceDescriptor().getForeignKeyValues(owner, getOwnerClassDescriptor())[0];
  +            if (fkVal != null)
  +            {
  +                fkValues.add(fkVal);
  +            }
  +        }
  +
  +        // create IN (...) for the single key field
  +        if (!fkValues.isEmpty())
  +        {
  +            crit.addIn(pkField.getAttributeName(), fkValues);
  +        }
  +
  +        return crit;
  +    }
  +    
  +    /**
  +     * Build the Criteria using multiple ORs
  +     * @param owners
  +     * @param pkFields
  +     * @return Criteria
  +     */
  +    private Criteria buildPrefetchCriteriaMultipleKeys(Collection owners, FieldDescriptor
pkFields[])
  +    {
  +        Criteria crit = new Criteria();
  +        Iterator iter = owners.iterator();
  +        Object[] fkVal;
  +        Object owner;
  +        
  +        while (iter.hasNext())
  +        {
  +            Criteria c = new Criteria();
  +            owner = iter.next();
  +            fkVal = getObjectReferenceDescriptor().getForeignKeyValues(owner, getOwnerClassDescriptor());
  +            for (int i=0;i < fkVal.length;i++)
  +            {
  +                if (fkVal[i] != null)
  +                {
  +                    c.addEqualTo(pkFields[i].getAttributeName(),fkVal[i]);
  +                }
  +            }
  +            
  +            if (!c.isEmpty())
  +            {
  +                crit.addOrCriteria(c);
  +            }
  +        }
  +
  +        return crit;
  +    }    
   
   }
  
  
  
  1.7       +73 -25    jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java
  
  Index: CollectionPrefetcher.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CollectionPrefetcher.java	28 Dec 2002 10:54:09 -0000	1.6
  +++ CollectionPrefetcher.java	29 Dec 2002 08:51:49 -0000	1.7
  @@ -54,9 +54,9 @@
    * <http://www.apache.org/>.
    */
   
  +import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Iterator;
  -import java.util.Vector;
   
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBroker;
  @@ -91,47 +91,95 @@
   	}
   
   	/**
  -	 * Build the query (using IN(...) )to perform a batched read
  -	 * get orderBy settings from CollectionDescriptor
  +	 * Build the query to perform a batched read get orderBy settings from
  +	 * CollectionDescriptor
   	 * @param owners Collection containing all objects of the ONE side
   	 */
   	protected Query buildPrefetchQuery(Collection owners)
   	{
   		CollectionDescriptor cds = (CollectionDescriptor) getObjectReferenceDescriptor();
  -		Criteria crit = new Criteria();
  -		ClassDescriptor refCld = getDescriptorRepository().getDescriptorFor(cds.getItemClass());
  -		FieldDescriptor pkFields[] = getOwnerClassDescriptor().getPkFields();
  -		FieldDescriptor fkFields[] = cds.getForeignKeyFieldDescriptors(refCld);
  -		Collection pkValues;
  -		Iterator iter;
  +	    FieldDescriptor fkFields[] = cds.getForeignKeyFieldDescriptors(getItemClassDescriptor());
  +		Criteria crit;
   
  -		for (int i = 0; i < pkFields.length; i++)
  +		if (fkFields.length == 1)
  +		{
  +			crit = buildPrefetchCriteriaSingleKey(owners, fkFields[0]);
  +		}
  +		else
   		{
  -			pkValues = new Vector();
  -			iter = owners.iterator();
  +			crit = buildPrefetchCriteriaMultipleKeys(owners, fkFields);
  +		}
  +
  +		// check if collection must be ordered
  +		if (!cds.getOrderBy().isEmpty())
  +		{
  +			Iterator iter = cds.getOrderBy().iterator();
   			while (iter.hasNext())
   			{
  -				pkValues.add(this.getBroker().serviceBrokerHelper().getKeyValues(getOwnerClassDescriptor(),
iter.next())[i]);
  +				crit.addOrderBy((FieldHelper) iter.next());
   			}
  +		}
   
  -			// create IN (...) for each key field
  -			if (!pkValues.isEmpty())
  -			{
  -				crit.addIn(fkFields[i].getAttributeName(), pkValues);
  -			}
  +		return QueryFactory.newQuery(cds.getItemClass(), crit);
  +	}
  +
  +	/**
  +	 * Build the Criteria using IN(...) for single keys
  +	 * @param owners
  +	 * @param fkField
  +	 * @return Criteria
  +	 */
  +	private Criteria buildPrefetchCriteriaSingleKey(Collection owners, FieldDescriptor fkField)
  +	{
  +		Criteria crit = new Criteria();
  +		Collection pkValues = new ArrayList();
  +		Iterator iter = owners.iterator();
  +		Object pkVal;
  +		Object owner;
  +
  +		while (iter.hasNext())
  +		{
  +			owner = iter.next();
  +			pkVal = getBroker().serviceBrokerHelper().getKeyValues(getOwnerClassDescriptor(), owner)[0];
  +			pkValues.add(pkVal);
  +		}
  +
  +		// create IN (...) for the single key field
  +		if (!pkValues.isEmpty())
  +		{
  +			crit.addIn(fkField.getAttributeName(), pkValues);
   		}
   
  -        // check if collection must be ordered
  -        if (!cds.getOrderBy().isEmpty())
  +		return crit;
  +	}
  +
  +	/**
  +	 * Build the Criteria using multiple ORs
  +	 * @param owners
  +	 * @param fkFields
  +	 * @return Criteria
  +	 */
  +	private Criteria buildPrefetchCriteriaMultipleKeys(Collection owners, FieldDescriptor
fkFields[])
  +	{
  +        Criteria crit = new Criteria();
  +        Iterator iter = owners.iterator();
  +        Object[] pkVal;
  +        Object owner;
  +        
  +        while (iter.hasNext())
           {
  -            iter = cds.getOrderBy().iterator();
  -            while (iter.hasNext())
  +            Criteria c = new Criteria();
  +            owner = iter.next();
  +            pkVal = getBroker().serviceBrokerHelper().getKeyValues(getOwnerClassDescriptor(),
owner);      
  +            for (int i=0;i < pkVal.length;i++)
               {
  -                crit.addOrderBy((FieldHelper)iter.next());
  +                c.addEqualTo(fkFields[i].getAttributeName(),pkVal[i]);
               }
  +            
  +            crit.addOrCriteria(c);
           }
   
  -		return QueryFactory.newQuery(cds.getItemClass(), crit);
  +        return crit;
   	}
   
   	/**
  
  
  

Mime
View raw message