Method from org.hibernate.engine.BatchFetchQueue Detail: |
public void addBatchLoadableEntityKey(EntityKey key) {
if ( key.isBatchLoadable() ) {
batchLoadableEntityKeys.put( key, MARKER );
}
}
If an EntityKey represents a batch loadable entity, add
it to the queue.
Note that the contract here is such that any key passed in should
previously have been been checked for existence within the
PersistenceContext ; failure to do so may cause the
referenced entity to be included in a batch even though it is
already associated with the PersistenceContext . |
public void addSubselect(EntityKey key,
SubselectFetch subquery) {
subselectsByEntityKey.put(key, subquery);
}
Adds a subselect fetch decriptor for the given entity key. |
public void clear() {
batchLoadableEntityKeys.clear();
subselectsByEntityKey.clear();
}
Clears all entries from this fetch queue. |
public void clearSubselects() {
subselectsByEntityKey.clear();
}
Clears all pending subselect fetches from the queue.
Called after flushing. |
public Serializable[] getCollectionBatch(CollectionPersister collectionPersister,
Serializable id,
int batchSize,
EntityMode entityMode) {
Serializable[] keys = new Serializable[batchSize];
keys[0] = id;
int i = 1;
//int count = 0;
int end = -1;
boolean checkForEnd = false;
// this only works because collection entries are kept in a sequenced
// map by persistence context (maybe we should do like entities and
// keep a separate sequences set...)
Iterator iter = context.getCollectionEntries().entrySet().iterator(); //TODO: calling entrySet on an IdentityMap is SLOW!!
while ( iter.hasNext() ) {
Map.Entry me = (Map.Entry) iter.next();
CollectionEntry ce = (CollectionEntry) me.getValue();
PersistentCollection collection = (PersistentCollection) me.getKey();
if ( !collection.wasInitialized() && ce.getLoadedPersister() == collectionPersister ) {
if ( checkForEnd && i == end ) {
return keys; //the first key found after the given key
}
//if ( end == -1 && count > batchSize*10 ) return keys; //try out ten batches, max
final boolean isEqual = collectionPersister.getKeyType().isEqual(
id,
ce.getLoadedKey(),
entityMode,
collectionPersister.getFactory()
);
if ( isEqual ) {
end = i;
//checkForEnd = false;
}
else if ( !isCached( ce.getLoadedKey(), collectionPersister, entityMode ) ) {
keys[i++] = ce.getLoadedKey();
//count++;
}
if ( i == batchSize ) {
i = 1; //end of array, start filling again from start
if ( end != -1 ) {
checkForEnd = true;
}
}
}
}
return keys; //we ran out of keys to try
}
Get a batch of uninitialized collection keys for a given role |
public Serializable[] getEntityBatch(EntityPersister persister,
Serializable id,
int batchSize,
EntityMode entityMode) {
Serializable[] ids = new Serializable[batchSize];
ids[0] = id; //first element of array is reserved for the actual instance we are loading!
int i = 1;
int end = -1;
boolean checkForEnd = false;
Iterator iter = batchLoadableEntityKeys.keySet().iterator();
while ( iter.hasNext() ) {
EntityKey key = (EntityKey) iter.next();
if ( key.getEntityName().equals( persister.getEntityName() ) ) { //TODO: this needn't exclude subclasses...
if ( checkForEnd && i == end ) {
//the first id found after the given id
return ids;
}
if ( persister.getIdentifierType().isEqual( id, key.getIdentifier(), entityMode ) ) {
end = i;
}
else {
if ( !isCached( key, persister, entityMode ) ) {
ids[i++] = key.getIdentifier();
}
}
if ( i == batchSize ) {
i = 1; //end of array, start filling again from start
if (end!=-1) checkForEnd = true;
}
}
}
return ids; //we ran out of ids to try
}
Get a batch of unloaded identifiers for this class, using a slightly
complex algorithm that tries to grab keys registered immediately after
the given key. |
public SubselectFetch getSubselect(EntityKey key) {
return (SubselectFetch) subselectsByEntityKey.get(key);
}
Retrieve the fetch descriptor associated with the given entity key. |
public void removeBatchLoadableEntityKey(EntityKey key) {
if ( key.isBatchLoadable() ) batchLoadableEntityKeys.remove(key);
}
After evicting or deleting or loading an entity, we don't
need to batch fetch it anymore, remove it from the queue
if necessary |
public void removeSubselect(EntityKey key) {
subselectsByEntityKey.remove(key);
}
After evicting or deleting an entity, we don't need to
know the query that was used to load it anymore (don't
call this after loading the entity, since we might still
need to load its collections) |