KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > loader > collection > BatchingCollectionInitializer


1 //$Id: BatchingCollectionInitializer.java,v 1.4 2005/06/13 20:10:19 oneovthafew Exp $
2
package org.hibernate.loader.collection;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.hibernate.HibernateException;
8 import org.hibernate.MappingException;
9 import org.hibernate.engine.SessionFactoryImplementor;
10 import org.hibernate.engine.SessionImplementor;
11 import org.hibernate.loader.Loader;
12 import org.hibernate.persister.collection.CollectionPersister;
13 import org.hibernate.persister.collection.QueryableCollection;
14 import org.hibernate.util.ArrayHelper;
15
16 /**
17  * "Batch" loads collections, using multiple foreign key values in the
18  * SQL <tt>where</tt> clause.
19  *
20  * @see BasicCollectionLoader
21  * @see OneToManyLoader
22  * @author Gavin King
23  */

24 public class BatchingCollectionInitializer implements CollectionInitializer {
25
26     private final Loader[] loaders;
27     private final int[] batchSizes;
28     private final CollectionPersister collectionPersister;
29
30     public BatchingCollectionInitializer(CollectionPersister collPersister, int[] batchSizes, Loader[] loaders) {
31         this.loaders = loaders;
32         this.batchSizes = batchSizes;
33         this.collectionPersister = collPersister;
34     }
35
36     public void initialize(Serializable JavaDoc id, SessionImplementor session)
37     throws HibernateException {
38         
39         Serializable JavaDoc[] batch = session.getPersistenceContext().getBatchFetchQueue()
40             .getCollectionBatch( collectionPersister, id, batchSizes[0], session.getEntityMode() );
41         
42         for ( int i=0; i<batchSizes.length-1; i++) {
43             final int smallBatchSize = batchSizes[i];
44             if ( batch[smallBatchSize-1]!=null ) {
45                 Serializable JavaDoc[] smallBatch = new Serializable JavaDoc[smallBatchSize];
46                 System.arraycopy(batch, 0, smallBatch, 0, smallBatchSize);
47                 loaders[i].loadCollectionBatch( session, smallBatch, collectionPersister.getKeyType() );
48                 return; //EARLY EXIT!
49
}
50         }
51         
52         loaders[batchSizes.length-1].loadCollection( session, id, collectionPersister.getKeyType() );
53
54     }
55
56     public static CollectionInitializer createBatchingOneToManyInitializer(
57         final QueryableCollection persister,
58         final int maxBatchSize,
59         final SessionFactoryImplementor factory,
60         final Map JavaDoc enabledFilters)
61     throws MappingException {
62
63         if ( maxBatchSize>1 ) {
64             int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
65             Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
66             for ( int i=0; i<batchSizesToCreate.length; i++ ) {
67                 loadersToCreate[i] = new OneToManyLoader(persister, batchSizesToCreate[i], factory, enabledFilters);
68             }
69             return new BatchingCollectionInitializer(persister, batchSizesToCreate, loadersToCreate);
70         }
71         else {
72             return new OneToManyLoader(persister, factory, enabledFilters);
73         }
74     }
75
76     public static CollectionInitializer createBatchingCollectionInitializer(
77         final QueryableCollection persister,
78         final int maxBatchSize,
79         final SessionFactoryImplementor factory,
80         final Map JavaDoc enabledFilters)
81     throws MappingException {
82
83         if ( maxBatchSize>1 ) {
84             int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
85             Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
86             for ( int i=0; i<batchSizesToCreate.length; i++ ) {
87                 loadersToCreate[i] = new BasicCollectionLoader(persister, batchSizesToCreate[i], factory, enabledFilters);
88             }
89             return new BatchingCollectionInitializer(persister, batchSizesToCreate, loadersToCreate);
90         }
91         else {
92             return new BasicCollectionLoader(persister, factory, enabledFilters);
93         }
94     }
95
96 }
97
Popular Tags