1 package org.hibernate.engine; 3 4 import java.io.Serializable ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.Map ; 8 9 import org.apache.commons.collections.SequencedHashMap; 10 import org.hibernate.EntityMode; 11 import org.hibernate.collection.PersistentCollection; 12 import org.hibernate.persister.collection.CollectionPersister; 13 import org.hibernate.persister.entity.EntityPersister; 14 import org.hibernate.util.MarkerObject; 15 16 23 public class BatchFetchQueue { 24 25 public static final Object MARKER = new MarkerObject("MARKER"); 26 27 private final Map batchLoadableEntityKeys = new SequencedHashMap(8); 31 private final Map subselectsByEntityKey = new HashMap (8); 34 private final PersistenceContext context; 36 37 public BatchFetchQueue(PersistenceContext context) { 38 this.context = context; 39 } 40 41 public void clear() { 42 batchLoadableEntityKeys.clear(); 43 subselectsByEntityKey.clear(); 44 } 45 46 public SubselectFetch getSubselect(EntityKey key) { 47 return (SubselectFetch) subselectsByEntityKey.get(key); 48 } 49 50 public void addSubselect(EntityKey key, SubselectFetch subquery) { 51 subselectsByEntityKey.put(key, subquery); 52 } 53 54 public void clearSubselects() { 55 subselectsByEntityKey.clear(); 56 } 57 58 63 public void removeBatchLoadableEntityKey(EntityKey key) { 64 if ( key.isBatchLoadable() ) batchLoadableEntityKeys.remove(key); 65 } 66 67 73 public void removeSubselect(EntityKey key) { 74 subselectsByEntityKey.remove(key); 75 } 76 77 81 public void addBatchLoadableEntityKey(EntityKey key) { 82 if ( key.isBatchLoadable() ) batchLoadableEntityKeys.put(key, MARKER); 83 } 84 85 92 public Serializable [] getCollectionBatch( 93 final CollectionPersister collectionPersister, 94 final Serializable id, 95 final int batchSize, 96 final EntityMode entityMode 97 ) { 98 Serializable [] keys = new Serializable [batchSize]; 99 keys[0] = id; 100 int i = 1; 101 int end = -1; 103 boolean checkForEnd = false; 104 Iterator iter = context.getCollectionEntries().entrySet().iterator(); while ( iter.hasNext() ) { 109 Map.Entry me = (Map.Entry ) iter.next(); 110 111 CollectionEntry ce = (CollectionEntry) me.getValue(); 112 PersistentCollection collection = (PersistentCollection) me.getKey(); 113 if ( !collection.wasInitialized() && ce.getLoadedPersister() == collectionPersister ) { 114 115 if ( checkForEnd && i == end ) return keys; 117 119 final boolean isEqual = collectionPersister.getKeyType().isEqual( 120 id, 121 ce.getLoadedKey(), 122 entityMode, 123 collectionPersister.getFactory() 124 ); 125 126 if ( isEqual ) { 127 end = i; 128 } 130 else { 131 keys[i++] = ce.getLoadedKey(); 132 } 134 135 if ( i == batchSize ) { 136 i = 1; if (end!=-1) checkForEnd = true; 138 } 139 } 140 141 } 142 return keys; } 144 145 155 public Serializable [] getEntityBatch( 156 final EntityPersister persister, 157 final Serializable id, 158 final int batchSize, 159 final EntityMode entityMode 160 ) { 161 Serializable [] ids = new Serializable [batchSize]; 162 ids[0] = id; int i = 1; 164 int end = -1; 165 boolean checkForEnd = false; 166 Iterator iter = batchLoadableEntityKeys.keySet().iterator(); 168 while ( iter.hasNext() ) { 169 170 EntityKey key = (EntityKey) iter.next(); 171 if ( key.getEntityName().equals( persister.getEntityName() ) ) { 173 if ( checkForEnd && i == end ) return ids; 175 177 if ( persister.getIdentifierType().isEqual( id, key.getIdentifier(), entityMode ) ) { 178 end = i; 179 } 181 else { 182 ids[i++] = key.getIdentifier(); 183 } 185 186 if ( i == batchSize ) { 187 i = 1; if (end!=-1) checkForEnd = true; 189 } 190 191 } 192 193 } 194 return ids; } 196 197 } 198 | Popular Tags |