db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer CollectionPrefetcher.java
Date Thu, 20 Feb 2003 21:37:12 GMT
thma        2003/02/20 13:37:12

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        CollectionPrefetcher.java
  Log:
  CollectionPrefetcher behaved differently than RsIterator wrt. extents (fix by Mark Rowell)
  
  Revision  Changes    Path
  1.8       +155 -148  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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- CollectionPrefetcher.java	29 Dec 2002 08:51:49 -0000	1.7
  +++ CollectionPrefetcher.java	20 Feb 2003 21:37:12 -0000	1.8
  @@ -80,166 +80,173 @@
   public class CollectionPrefetcher extends RelationshipPrefetcherImpl
   {
   
  -	/**
  -	 * Constructor for CollectionPrefetcher.
  -	 * @param aBroker
  -	 * @param anOrd
  -	 */
  -	public CollectionPrefetcher(PersistenceBroker aBroker, ObjectReferenceDescriptor anOrd)
  -	{
  -		super(aBroker, anOrd);
  -	}
  -
  -	/**
  -	 * 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();
  -	    FieldDescriptor fkFields[] = cds.getForeignKeyFieldDescriptors(getItemClassDescriptor());
  -		Criteria crit;
  -
  -		if (fkFields.length == 1)
  -		{
  -			crit = buildPrefetchCriteriaSingleKey(owners, fkFields[0]);
  -		}
  -		else
  -		{
  -			crit = buildPrefetchCriteriaMultipleKeys(owners, fkFields);
  -		}
  -
  -		// check if collection must be ordered
  -		if (!cds.getOrderBy().isEmpty())
  -		{
  -			Iterator iter = cds.getOrderBy().iterator();
  -			while (iter.hasNext())
  -			{
  -				crit.addOrderBy((FieldHelper) iter.next());
  -			}
  -		}
  -
  -		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);
  -		}
  -
  -		return crit;
  -	}
  -
  -	/**
  -	 * Build the Criteria using multiple ORs
  -	 * @param owners
  -	 * @param fkFields
  -	 * @return Criteria
  -	 */
  -	private Criteria buildPrefetchCriteriaMultipleKeys(Collection owners, FieldDescriptor
fkFields[])
  -	{
  +    /**
  +     * Constructor for CollectionPrefetcher.
  +     * @param aBroker
  +     * @param anOrd
  +     */
  +    public CollectionPrefetcher(PersistenceBroker aBroker, ObjectReferenceDescriptor anOrd)
  +    {
  +        super(aBroker, anOrd);
  +    }
  +
  +    /**
  +     * 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();
  +        FieldDescriptor fkFields[] = cds.getForeignKeyFieldDescriptors(getItemClassDescriptor());
  +        Criteria crit;
  +
  +        if (fkFields.length == 1)
  +        {
  +            crit = buildPrefetchCriteriaSingleKey(owners, fkFields[0]);
  +        }
  +        else
  +        {
  +            crit = buildPrefetchCriteriaMultipleKeys(owners, fkFields);
  +        }
  +
  +        // check if collection must be ordered
  +        if (!cds.getOrderBy().isEmpty())
  +        {
  +            Iterator iter = cds.getOrderBy().iterator();
  +            while (iter.hasNext())
  +            {
  +                crit.addOrderBy((FieldHelper) iter.next());
  +            }
  +        }
  +
  +        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);
  +        }
  +
  +        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())
           {
               Criteria c = new Criteria();
               owner = iter.next();
  -            pkVal = getBroker().serviceBrokerHelper().getKeyValues(getOwnerClassDescriptor(),
owner);      
  -            for (int i=0;i < pkVal.length;i++)
  +            pkVal =
  +                getBroker().serviceBrokerHelper().getKeyValues(getOwnerClassDescriptor(),
owner);
  +            for (int i = 0; i < pkVal.length; i++)
               {
  -                c.addEqualTo(fkFields[i].getAttributeName(),pkVal[i]);
  +                c.addEqualTo(fkFields[i].getAttributeName(), pkVal[i]);
               }
  -            
  +
               crit.addOrCriteria(c);
           }
   
           return crit;
  -	}
  +    }
   
  -	/**
  -	 * associate the batched Children with their owner object <br>
  -	 * loop over children
  -	 */
  -	protected void associateBatched(Collection owners, Collection children)
  -	{
  -		ClassDescriptor cld = getItemClassDescriptor();
  -		Iterator iter = children.iterator();
  -		Object owner, relatedObject;
  -		Object fkValues[];
  -		Identity id;
  -		PersistentField field = getObjectReferenceDescriptor().getPersistentField();
  -
  -		while (iter.hasNext())
  -		{
  -			relatedObject = iter.next();
  -			fkValues = getObjectReferenceDescriptor().getForeignKeyValues(relatedObject, cld);
  -			id = new Identity(getOwnerClassDescriptor().getClassOfObject(), fkValues);
  -			owner = getBroker().getObjectByIdentity(id);
  -
  -			if (Collection.class.isAssignableFrom(field.getType()))
  -			{
  -				Collection relatedData = (Collection) field.get(owner);
  -				if (relatedData == null)
  -				{
  -					try
  -					{
  -						// instantiate and set the owners collection
  -						relatedData = (Collection) children.getClass().newInstance();
  -						field.set(owner, relatedData);
  -					}
  -					catch (Exception e)
  -					{
  -						getLogger().error("Can't create new Collection for owner", e);
  -					}
  -				}
  -				relatedData.add(relatedObject);
  -			}
  -			else
  -			{
  -				getLogger().error("prefetch works with Collections only");
  -			}
  -
  -		}
  -	}
  -
  -	/**
  -	 * @see org.apache.ojb.broker.accesslayer.RelationshipPrefetcher#prefetchRelationship(Collection)
  -	 */
  -	public void prefetchRelationship(Collection owners)
  -	{
  -		if (getObjectReferenceDescriptor().getPersistentField().getType().isArray())
  -		{
  -			throw new PersistenceBrokerException("Prefetch does not yet work with Arrays");
  -		}
  -		else
  -		{
  -			super.prefetchRelationship(owners);
  -		}
  -	}
  +    /**
  +     * associate the batched Children with their owner object <br>
  +     * loop over children
  +     */
  +    protected void associateBatched(Collection owners, Collection children)
  +    {
  +        ClassDescriptor cld = getItemClassDescriptor();
  +        Iterator iter = children.iterator();
  +        Object owner, relatedObject;
  +        Object fkValues[];
  +        Identity id;
  +        PersistentField field = getObjectReferenceDescriptor().getPersistentField();
  +
  +        while (iter.hasNext())
  +        {
  +            relatedObject = iter.next();
  +            fkValues = getObjectReferenceDescriptor().getForeignKeyValues(relatedObject,
cld);
  +            id =
  +                new Identity(
  +                    getBroker().getTopLevelClass(getOwnerClassDescriptor().getClassOfObject()),
  +                    fkValues);
  +            owner = getBroker().getObjectByIdentity(id);
  +
  +            if (Collection.class.isAssignableFrom(field.getType()))
  +            {
  +                Collection relatedData = (Collection) field.get(owner);
  +                if (relatedData == null)
  +                {
  +                    try
  +                    {
  +                        // instantiate and set the owners collection
  +                        relatedData = (Collection) children.getClass().newInstance();
  +                        field.set(owner, relatedData);
  +                    }
  +                    catch (Exception e)
  +                    {
  +                        getLogger().error("Can't create new Collection for owner", e);
  +                    }
  +                }
  +                relatedData.add(relatedObject);
  +            }
  +            else
  +            {
  +                getLogger().error("prefetch works with Collections only");
  +            }
  +
  +        }
  +    }
  +
  +    /**
  +     * @see org.apache.ojb.broker.accesslayer.RelationshipPrefetcher#prefetchRelationship(Collection)
  +     */
  +    public void prefetchRelationship(Collection owners)
  +    {
  +        if (getObjectReferenceDescriptor().getPersistentField().getType().isArray())
  +        {
  +            throw new PersistenceBrokerException("Prefetch does not yet work with Arrays");
  +        }
  +        else
  +        {
  +            super.prefetchRelationship(owners);
  +        }
  +    }
   }
  
  
  

Mime
View raw message