Coverage Report - org.apache.ojb.broker.accesslayer.PlainPrefetcher
 
Classes in this File Line Coverage Branch Coverage Complexity
PlainPrefetcher
N/A
N/A
3.333
 
 1  
 package org.apache.ojb.broker.accesslayer;
 2  
 
 3  
 /* Copyright 2003-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.HashMap;
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.ojb.broker.Identity;
 26  
 import org.apache.ojb.broker.PersistenceBroker;
 27  
 import org.apache.ojb.broker.cache.ObjectCache;
 28  
 import org.apache.ojb.broker.core.PersistenceBrokerImpl;
 29  
 import org.apache.ojb.broker.core.proxy.IndirectionHandler;
 30  
 import org.apache.ojb.broker.core.proxy.ProxyHelper;
 31  
 import org.apache.ojb.broker.query.Query;
 32  
 
 33  
 /**
 34  
  * Prefetcher for plain list of objects (no relations).
 35  
  *
 36  
  * @author <a href="mailto:olegnitz@apache.org">Oleg Nitz</a>
 37  
  * @version $Id: PlainPrefetcher.java,v 1.1 2007-08-24 22:17:30 ewestfal Exp $
 38  
  */
 39  
 public class PlainPrefetcher extends BasePrefetcher
 40  
 {
 41  
 
 42  
     public PlainPrefetcher(PersistenceBrokerImpl aBroker, Class anItemClass)
 43  
     {
 44  
         super(aBroker, anItemClass);
 45  
     }
 46  
 
 47  
     public void prepareRelationshipSettings()
 48  
     {
 49  
         // no op
 50  
     }
 51  
 
 52  
     public void restoreRelationshipSettings()
 53  
     {
 54  
         // no op
 55  
     }
 56  
 
 57  
     protected void associateBatched(Collection proxies, Collection realSubjects)
 58  
     {
 59  
         PersistenceBroker pb = getBroker();
 60  
         IndirectionHandler handler;
 61  
         Identity id;
 62  
         Object proxy;
 63  
         Object realSubject;
 64  
         HashMap realSubjectsMap = new HashMap(realSubjects.size());
 65  
 
 66  
         for (Iterator it = realSubjects.iterator(); it.hasNext(); )
 67  
         {
 68  
             realSubject = it.next();
 69  
             realSubjectsMap.put(pb.serviceIdentity().buildIdentity(realSubject), realSubject);
 70  
         }
 71  
 
 72  
         for (Iterator it = proxies.iterator(); it.hasNext(); )
 73  
         {
 74  
             proxy = it.next();
 75  
             handler = ProxyHelper.getIndirectionHandler(proxy);
 76  
 
 77  
             if (handler == null)
 78  
             {
 79  
                 continue;
 80  
             }
 81  
 
 82  
             id = handler.getIdentity();
 83  
             realSubject = realSubjectsMap.get(id);
 84  
             if (realSubject != null)
 85  
             {
 86  
                 handler.setRealSubject(realSubject);
 87  
             }
 88  
         }
 89  
     }
 90  
 
 91  
     /**
 92  
      * Build the multiple queries for one relationship because of limitation of IN(...)
 93  
      * @param proxies Collection containing all proxy objects to load
 94  
      * @param realSubjects Collection where real subjects found in the cache should be added.
 95  
      */
 96  
     protected Query[] buildPrefetchQueries(Collection proxies, Collection realSubjects)
 97  
     {
 98  
         Collection queries = new ArrayList();
 99  
         Collection idsSubset;
 100  
         Object proxy;
 101  
         IndirectionHandler handler;
 102  
         Identity id;
 103  
         Class realClass;
 104  
         HashMap classToIds = new HashMap();
 105  
         Class topLevelClass = getItemClassDescriptor().getClassOfObject();
 106  
         PersistenceBroker pb = getBroker();
 107  
         ObjectCache cache = pb.serviceObjectCache();
 108  
 
 109  
         for (Iterator it = proxies.iterator(); it.hasNext(); )
 110  
         {
 111  
             proxy = it.next();
 112  
             handler = ProxyHelper.getIndirectionHandler(proxy);
 113  
 
 114  
             if (handler == null)
 115  
             {
 116  
                 continue;
 117  
             }
 118  
             
 119  
             id = handler.getIdentity();
 120  
             if (cache.lookup(id) != null)
 121  
             {
 122  
                 realSubjects.add(pb.getObjectByIdentity(id));
 123  
                 continue;
 124  
             }
 125  
             realClass = id.getObjectsRealClass();
 126  
             if (realClass == null)
 127  
             {
 128  
                 realClass = Object.class; // to remember that the real class is unknown
 129  
             }
 130  
             idsSubset = (HashSet) classToIds.get(realClass);
 131  
             if (idsSubset == null)
 132  
             {
 133  
                 idsSubset = new HashSet();
 134  
                 classToIds.put(realClass, idsSubset);
 135  
             }
 136  
             idsSubset.add(id);
 137  
             if (idsSubset.size() == pkLimit)
 138  
             {
 139  
                 Query query;
 140  
                 if (realClass == Object.class)
 141  
                 {
 142  
                     query = buildPrefetchQuery(topLevelClass, idsSubset, true);
 143  
                 }
 144  
                 else
 145  
                 {
 146  
                     query = buildPrefetchQuery(realClass, idsSubset, false);
 147  
                 }
 148  
                 queries.add(query);
 149  
                 idsSubset.clear();
 150  
             }
 151  
         }
 152  
 
 153  
         for (Iterator it = classToIds.entrySet().iterator(); it.hasNext(); )
 154  
         {
 155  
             Map.Entry entry = (Map.Entry) it.next();
 156  
             realClass = (Class) entry.getKey();
 157  
             idsSubset = (HashSet) entry.getValue();
 158  
             if (idsSubset.size() > 0)
 159  
             {
 160  
                 Query query;
 161  
                 if (realClass == Object.class)
 162  
                 {
 163  
                     query = buildPrefetchQuery(topLevelClass, idsSubset, true);
 164  
                 }
 165  
                 else
 166  
                 {
 167  
                     query = buildPrefetchQuery(realClass, idsSubset, false);
 168  
                 }
 169  
                 queries.add(query);
 170  
             }
 171  
         }
 172  
 
 173  
         return (Query[]) queries.toArray(new Query[queries.size()]);
 174  
     }
 175  
 
 176  
     protected Query buildPrefetchQuery(Class clazz, Collection ids, boolean withExtents)
 177  
     {
 178  
         Query query = buildPrefetchQuery(clazz, ids,
 179  
                 getDescriptorRepository().getDescriptorFor(clazz).getPkFields());
 180  
         query.setWithExtents(withExtents);
 181  
         return query;
 182  
     }
 183  
 }