KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > loader > entity > BatchingEntityLoader


1 //$Id: BatchingEntityLoader.java,v 1.5 2005/05/27 03:54:00 oneovthafew Exp $
2
package org.hibernate.loader.entity;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.LockMode;
11 import org.hibernate.MappingException;
12 import org.hibernate.engine.SessionFactoryImplementor;
13 import org.hibernate.engine.SessionImplementor;
14 import org.hibernate.loader.Loader;
15 import org.hibernate.persister.entity.EntityPersister;
16 import org.hibernate.persister.entity.OuterJoinLoadable;
17 import org.hibernate.type.Type;
18 import org.hibernate.util.ArrayHelper;
19
20 /**
21  * "Batch" loads entities, using multiple primary key values in the
22  * SQL <tt>where</tt> clause.
23  *
24  * @see EntityLoader
25  * @author Gavin King
26  */

27 public class BatchingEntityLoader implements UniqueEntityLoader {
28
29     private final Loader[] loaders;
30     private final int[] batchSizes;
31     private final EntityPersister persister;
32     private final Type idType;
33
34     public BatchingEntityLoader(EntityPersister persister, int[] batchSizes, Loader[] loaders) {
35         this.batchSizes = batchSizes;
36         this.loaders = loaders;
37         this.persister = persister;
38         idType = persister.getIdentifierType();
39     }
40
41     private Object JavaDoc getObjectFromList(List JavaDoc results, Serializable JavaDoc id, SessionImplementor session) {
42         // get the right object from the list ... would it be easier to just call getEntity() ??
43
Iterator JavaDoc iter = results.iterator();
44         while ( iter.hasNext() ) {
45             Object JavaDoc obj = iter.next();
46             final boolean equal = idType.isEqual(
47                     id,
48                     session.getContextEntityIdentifier(obj),
49                     session.getEntityMode(),
50                     session.getFactory()
51             );
52             if ( equal ) return obj;
53         }
54         return null;
55     }
56
57     public Object JavaDoc load(Serializable JavaDoc id, Object JavaDoc optionalObject, SessionImplementor session)
58     throws HibernateException {
59         
60         Serializable JavaDoc[] batch = session.getPersistenceContext()
61             .getBatchFetchQueue()
62             .getEntityBatch( persister, id, batchSizes[0], session.getEntityMode() );
63         
64         for ( int i=0; i<batchSizes.length-1; i++) {
65             final int smallBatchSize = batchSizes[i];
66             if ( batch[smallBatchSize-1]!=null ) {
67                 Serializable JavaDoc[] smallBatch = new Serializable JavaDoc[smallBatchSize];
68                 System.arraycopy(batch, 0, smallBatch, 0, smallBatchSize);
69                 final List JavaDoc results = loaders[i].loadEntityBatch(
70                         session,
71                         smallBatch,
72                         idType,
73                         optionalObject,
74                         persister.getEntityName(),
75                         id,
76                         persister
77                 );
78                 return getObjectFromList(results, id, session); //EARLY EXIT
79
}
80         }
81         
82         return ( (UniqueEntityLoader) loaders[batchSizes.length-1] ).load(id, optionalObject, session);
83
84     }
85
86     public static UniqueEntityLoader createBatchingEntityLoader(
87         final OuterJoinLoadable persister,
88         final int maxBatchSize,
89         final LockMode lockMode,
90         final SessionFactoryImplementor factory,
91         final Map JavaDoc enabledFilters)
92     throws MappingException {
93
94         if ( maxBatchSize>1 ) {
95             int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
96             Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
97             for ( int i=0; i<batchSizesToCreate.length; i++ ) {
98                 loadersToCreate[i] = new EntityLoader(persister, batchSizesToCreate[i], lockMode, factory, enabledFilters);
99             }
100             return new BatchingEntityLoader(persister, batchSizesToCreate, loadersToCreate);
101         }
102         else {
103             return new EntityLoader(persister, lockMode, factory, enabledFilters);
104         }
105     }
106
107 }
108
Popular Tags