HHH-1775 - collection batch fetching
This commit is contained in:
parent
7cecc68fb1
commit
fb253b0297
|
@ -25,21 +25,19 @@ package org.hibernate.engine.spi;
|
|||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import org.hibernate.EntityMode;
|
||||
import org.hibernate.cache.spi.CacheKey;
|
||||
import org.hibernate.collection.spi.PersistentCollection;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.internal.util.MarkerObject;
|
||||
import org.hibernate.internal.util.collections.IdentityMap;
|
||||
import org.hibernate.persister.collection.CollectionPersister;
|
||||
import org.hibernate.persister.entity.EntityPersister;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* Tracks entity and collection keys that are available for batch
|
||||
|
@ -47,35 +45,35 @@ import org.jboss.logging.Logger;
|
|||
* can be re-used as a subquery for loading owned collections.
|
||||
*
|
||||
* @author Gavin King
|
||||
* @author Steve Ebersole
|
||||
* @author Guenther Demetz
|
||||
*/
|
||||
public class BatchFetchQueue {
|
||||
|
||||
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, BatchFetchQueue.class.getName() );
|
||||
|
||||
/**
|
||||
* Defines a sequence of {@link EntityKey} elements that are currently
|
||||
* elegible for batch-fetching.
|
||||
* <p/>
|
||||
* utilize a {@link LinkedHashMap} to maintain sequencing as well as uniqueness.
|
||||
* <p/>
|
||||
*/
|
||||
private final Map <String,LinkedHashSet<EntityKey>> batchLoadableEntityKeys = new HashMap <String,LinkedHashSet<EntityKey>>(8);
|
||||
|
||||
/**
|
||||
* cannot use PersistentCollection as keym because PersistentSet.hashCode() would force initialization immediately
|
||||
*/
|
||||
private final Map <CollectionPersister, LinkedHashMap <CollectionEntry, PersistentCollection>> batchLoadableCollections = new HashMap <CollectionPersister, LinkedHashMap <CollectionEntry, PersistentCollection>>(8);
|
||||
private final PersistenceContext context;
|
||||
|
||||
/**
|
||||
* A map of {@link SubselectFetch subselect-fetch descriptors} keyed by the
|
||||
* {@link EntityKey) against which the descriptor is registered.
|
||||
*/
|
||||
private final Map subselectsByEntityKey = new HashMap(8);
|
||||
private final Map<EntityKey, SubselectFetch> subselectsByEntityKey = new HashMap<EntityKey, SubselectFetch>(8);
|
||||
|
||||
/**
|
||||
* The owning persistence context.
|
||||
* Used to hold information about the entities that are currently eligible for batch-fetching. Ultimately
|
||||
* used by {@link #getEntityBatch} to build entity load batches.
|
||||
* <p/>
|
||||
* A Map structure is used to segment the keys by entity type since loading can only be done for a particular entity
|
||||
* type at a time.
|
||||
*/
|
||||
private final PersistenceContext context;
|
||||
private final Map <String,LinkedHashSet<EntityKey>> batchLoadableEntityKeys = new HashMap <String,LinkedHashSet<EntityKey>>(8);
|
||||
|
||||
/**
|
||||
* Used to hold information about the collections that are currently eligible for batch-fetching. Ultimately
|
||||
* used by {@link #getCollectionBatch} to build collection load batches.
|
||||
*/
|
||||
private final Map<String, LinkedHashMap<CollectionEntry, PersistentCollection>> batchLoadableCollections =
|
||||
new HashMap<String, LinkedHashMap <CollectionEntry, PersistentCollection>>(8);
|
||||
|
||||
/**
|
||||
* Constructs a queue for the given context.
|
||||
|
@ -95,6 +93,9 @@ public class BatchFetchQueue {
|
|||
subselectsByEntityKey.clear();
|
||||
}
|
||||
|
||||
|
||||
// sub-select support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
/**
|
||||
* Retrieve the fetch descriptor associated with the given entity key.
|
||||
*
|
||||
|
@ -103,7 +104,7 @@ public class BatchFetchQueue {
|
|||
* this entity key.
|
||||
*/
|
||||
public SubselectFetch getSubselect(EntityKey key) {
|
||||
return (SubselectFetch) subselectsByEntityKey.get(key);
|
||||
return subselectsByEntityKey.get( key );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -113,7 +114,7 @@ public class BatchFetchQueue {
|
|||
* @param subquery The fetch descriptor.
|
||||
*/
|
||||
public void addSubselect(EntityKey key, SubselectFetch subquery) {
|
||||
subselectsByEntityKey.put(key, subquery);
|
||||
subselectsByEntityKey.put( key, subquery );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -123,7 +124,7 @@ public class BatchFetchQueue {
|
|||
* need to load its collections)
|
||||
*/
|
||||
public void removeSubselect(EntityKey key) {
|
||||
subselectsByEntityKey.remove(key);
|
||||
subselectsByEntityKey.remove( key );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -135,6 +136,9 @@ public class BatchFetchQueue {
|
|||
subselectsByEntityKey.clear();
|
||||
}
|
||||
|
||||
|
||||
// entity batch support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
/**
|
||||
* If an EntityKey represents a batch loadable entity, add
|
||||
* it to the queue.
|
||||
|
@ -171,18 +175,83 @@ public class BatchFetchQueue {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param persister The persister for the entities being loaded.
|
||||
* @param id The identifier of the entity currently demanding load.
|
||||
* @param batchSize The maximum number of keys to return
|
||||
* @return an array of identifiers, of length batchSize (possibly padded with nulls)
|
||||
*/
|
||||
public Serializable[] getEntityBatch(
|
||||
final EntityPersister persister,
|
||||
final Serializable id,
|
||||
final int batchSize,
|
||||
final 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;
|
||||
|
||||
// TODO: this needn't exclude subclasses...
|
||||
|
||||
LinkedHashSet<EntityKey> set = batchLoadableEntityKeys.get( persister.getEntityName() );
|
||||
if ( set != null ) {
|
||||
for ( EntityKey key : set ) {
|
||||
if ( checkForEnd && i == end ) {
|
||||
//the first id found after the given id
|
||||
return ids;
|
||||
}
|
||||
if ( persister.getIdentifierType().isEqual( id, key.getIdentifier() ) ) {
|
||||
end = i;
|
||||
}
|
||||
else {
|
||||
if ( !isCached( key, persister ) ) {
|
||||
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
|
||||
}
|
||||
|
||||
private boolean isCached(EntityKey entityKey, EntityPersister persister) {
|
||||
if ( persister.hasCache() ) {
|
||||
CacheKey key = context.getSession().generateCacheKey(
|
||||
entityKey.getIdentifier(),
|
||||
persister.getIdentifierType(),
|
||||
entityKey.getEntityName()
|
||||
);
|
||||
return persister.getCacheAccessStrategy().get( key, context.getSession().getTimestamp() ) != null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// collection batch support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
/**
|
||||
* If an CollectionEntry represents a batch loadable collection, add
|
||||
* it to the queue.
|
||||
*/
|
||||
public void addBatchLoadableCollection(PersistentCollection collection, CollectionEntry ce) {
|
||||
LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get( ce.getLoadedPersister());
|
||||
if (map == null) {
|
||||
map = new LinkedHashMap<CollectionEntry, PersistentCollection>(8);
|
||||
batchLoadableCollections.put( ce.getLoadedPersister(), map);
|
||||
final CollectionPersister persister = ce.getLoadedPersister();
|
||||
|
||||
LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get( persister.getRole() );
|
||||
if ( map == null ) {
|
||||
map = new LinkedHashMap<CollectionEntry, PersistentCollection>( 16 );
|
||||
batchLoadableCollections.put( persister.getRole(), map );
|
||||
}
|
||||
map.put(ce, collection);
|
||||
map.put( ce, collection );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -191,9 +260,9 @@ public class BatchFetchQueue {
|
|||
* if necessary
|
||||
*/
|
||||
public void removeBatchLoadableCollection(CollectionEntry ce) {
|
||||
LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get( ce.getLoadedPersister());
|
||||
if (map != null) {
|
||||
map.remove(ce);
|
||||
LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get( ce.getLoadedPersister().getRole() );
|
||||
if ( map != null ) {
|
||||
map.remove( ce );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -209,25 +278,30 @@ public class BatchFetchQueue {
|
|||
final CollectionPersister collectionPersister,
|
||||
final Serializable id,
|
||||
final int batchSize) {
|
||||
|
||||
Serializable[] keys = new Serializable[batchSize];
|
||||
keys[0] = id;
|
||||
|
||||
int i = 1;
|
||||
//int count = 0;
|
||||
int end = -1;
|
||||
boolean checkForEnd = false;
|
||||
LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get(collectionPersister);
|
||||
if (map != null) {
|
||||
for (Entry<CollectionEntry, PersistentCollection> me : map.entrySet()) {
|
||||
CollectionEntry ce = me.getKey();
|
||||
PersistentCollection collection = me.getValue();
|
||||
if ( !collection.wasInitialized() ) { // should always be true
|
||||
|
||||
final LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get( collectionPersister.getRole() );
|
||||
if ( map != null ) {
|
||||
for ( Entry<CollectionEntry, PersistentCollection> me : map.entrySet() ) {
|
||||
final CollectionEntry ce = me.getKey();
|
||||
final PersistentCollection collection = me.getValue();
|
||||
|
||||
if ( collection.wasInitialized() ) {
|
||||
// should never happen
|
||||
LOG.warn( "Encountered initialized collection in BatchFetchQueue, this should not happen." );
|
||||
continue;
|
||||
}
|
||||
|
||||
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(),
|
||||
|
@ -250,72 +324,10 @@ public class BatchFetchQueue {
|
|||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
LOG.warn("Encountered initialized collection in BatchFetchQueue, this should not happen.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return keys; //we ran out of keys 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.
|
||||
*
|
||||
* @param persister The persister for the entities being loaded.
|
||||
* @param id The identifier of the entity currently demanding load.
|
||||
* @param batchSize The maximum number of keys to return
|
||||
* @return an array of identifiers, of length batchSize (possibly padded with nulls)
|
||||
*/
|
||||
public Serializable[] getEntityBatch(
|
||||
final EntityPersister persister,
|
||||
final Serializable id,
|
||||
final int batchSize,
|
||||
final 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;
|
||||
|
||||
LinkedHashSet<EntityKey> set = batchLoadableEntityKeys.get( persister.getEntityName() ); //TODO: this needn't exclude subclasses...
|
||||
if (set != null) {
|
||||
for (EntityKey key : set) {
|
||||
if ( checkForEnd && i == end ) {
|
||||
//the first id found after the given id
|
||||
return ids;
|
||||
}
|
||||
if ( persister.getIdentifierType().isEqual( id, key.getIdentifier() ) ) {
|
||||
end = i;
|
||||
}
|
||||
else {
|
||||
if ( !isCached( key, persister ) ) {
|
||||
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
|
||||
}
|
||||
|
||||
private boolean isCached(EntityKey entityKey, EntityPersister persister) {
|
||||
if ( persister.hasCache() ) {
|
||||
CacheKey key = context.getSession().generateCacheKey(
|
||||
entityKey.getIdentifier(),
|
||||
persister.getIdentifierType(),
|
||||
entityKey.getEntityName()
|
||||
);
|
||||
return persister.getCacheAccessStrategy().get( key, context.getSession().getTimestamp() ) != null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isCached(Serializable collectionKey, CollectionPersister persister) {
|
||||
if ( persister.hasCache() ) {
|
||||
CacheKey cacheKey = context.getSession().generateCacheKey(
|
||||
|
|
Loading…
Reference in New Issue