FileDocCategorySizeDatePackage
QueryPlanCache.javaAPI DocHibernate 3.2.58354Wed Jun 28 08:25:00 BST 2006org.hibernate.engine.query

QueryPlanCache

public class QueryPlanCache extends Object implements Serializable
Acts as a cache for compiled query plans, as well as query-parameter metadata.
author
Steve Ebersole

Fields Summary
private static final Log
log
private org.hibernate.engine.SessionFactoryImplementor
factory
private final org.hibernate.util.SimpleMRUCache
sqlParamMetadataCache
private final org.hibernate.util.SoftLimitMRUCache
planCache
Constructors Summary
public QueryPlanCache(org.hibernate.engine.SessionFactoryImplementor factory)


	   
		this.factory = factory;
	
Methods Summary
private ParameterMetadatabuildNativeSQLParameterMetadata(java.lang.String sqlString)

		ParamLocationRecognizer recognizer = ParamLocationRecognizer.parseLocations( sqlString );

		OrdinalParameterDescriptor[] ordinalDescriptors =
				new OrdinalParameterDescriptor[ recognizer.getOrdinalParameterLocationList().size() ];
		for ( int i = 0; i < recognizer.getOrdinalParameterLocationList().size(); i++ ) {
			final Integer position = ( Integer ) recognizer.getOrdinalParameterLocationList().get( i );
			ordinalDescriptors[i] = new OrdinalParameterDescriptor( i, null, position.intValue() );
		}

		Iterator itr = recognizer.getNamedParameterDescriptionMap().entrySet().iterator();
		Map namedParamDescriptorMap = new HashMap();
		while( itr.hasNext() ) {
			final Map.Entry entry = ( Map.Entry ) itr.next();
			final String name = ( String ) entry.getKey();
			final ParamLocationRecognizer.NamedParameterDescription description =
					( ParamLocationRecognizer.NamedParameterDescription ) entry.getValue();
			namedParamDescriptorMap.put(
					name ,
			        new NamedParameterDescriptor( name, null, description.buildPositionsArray(), description.isJpaStyle() )
			);
		}

		return new ParameterMetadata( ordinalDescriptors, namedParamDescriptorMap );
	
public FilterQueryPlangetFilterQueryPlan(java.lang.String filterString, java.lang.String collectionRole, boolean shallow, java.util.Map enabledFilters)

		FilterQueryPlanKey key = new FilterQueryPlanKey( filterString, collectionRole, shallow, enabledFilters );
		FilterQueryPlan plan = ( FilterQueryPlan ) planCache.get ( key );

		if ( plan == null ) {
			if ( log.isTraceEnabled() ) {
				log.trace( "unable to locate collection-filter query plan in cache; generating (" + collectionRole + " : " + filterString + ")" );
			}
			plan = new FilterQueryPlan( filterString, collectionRole, shallow, enabledFilters, factory );
		}
		else {
			if ( log.isTraceEnabled() ) {
				log.trace( "located collection-filter query plan in cache (" + collectionRole + " : " + filterString + ")" );
			}
		}

		planCache.put( key, plan );

		return plan;
	
public HQLQueryPlangetHQLQueryPlan(java.lang.String queryString, boolean shallow, java.util.Map enabledFilters)

		HQLQueryPlanKey key = new HQLQueryPlanKey( queryString, shallow, enabledFilters );
		HQLQueryPlan plan = ( HQLQueryPlan ) planCache.get ( key );

		if ( plan == null ) {
			if ( log.isTraceEnabled() ) {
				log.trace( "unable to locate HQL query plan in cache; generating (" + queryString + ")" );
			}
			plan = new HQLQueryPlan(queryString, shallow, enabledFilters, factory );
		}
		else {
			if ( log.isTraceEnabled() ) {
				log.trace( "located HQL query plan in cache (" + queryString + ")" );
			}
		}

		planCache.put( key, plan );

		return plan;
	
public NativeSQLQueryPlangetNativeSQLQueryPlan(org.hibernate.engine.query.sql.NativeSQLQuerySpecification spec)

		NativeSQLQueryPlan plan = ( NativeSQLQueryPlan ) planCache.get( spec );

		if ( plan == null ) {
			if ( log.isTraceEnabled() ) {
				log.trace( "unable to locate native-sql query plan in cache; generating (" + spec.getQueryString() + ")" );
			}
			plan = new NativeSQLQueryPlan( spec, factory );
		}
		else {
			if ( log.isTraceEnabled() ) {
				log.trace( "located native-sql query plan in cache (" + spec.getQueryString() + ")" );
			}
		}

		planCache.put( spec, plan );
		return plan;
	
public ParameterMetadatagetSQLParameterMetadata(java.lang.String query)



	    
		ParameterMetadata metadata = ( ParameterMetadata ) sqlParamMetadataCache.get( query );
		if ( metadata == null ) {
			// for native-sql queries, the param metadata is determined outside
			// any relation to a query plan, because query plan creation and/or
			// retreival for a native-sql query depends on all of the return
			// types having been set, which might not be the case up-front when
			// param metadata would be most useful
			metadata = buildNativeSQLParameterMetadata( query );
			sqlParamMetadataCache.put( query, metadata );
		}
		return metadata;