HHH-14024 add QueryPlanCreator functional interface

simplifies customization of HQLQueryPlan instantiation
This commit is contained in:
gavinking 2020-05-18 11:26:18 +02:00 committed by Steve Ebersole
parent 4a7f9ec0d6
commit 92b559abda
3 changed files with 15 additions and 15 deletions

View File

@ -30,6 +30,7 @@ import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.cache.spi.TimestampsCacheFactory;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.query.spi.HQLQueryPlan;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.loader.BatchFetchStyle;
@ -468,7 +469,7 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilderImplement
final StandardServiceRegistry serviceRegistry = metadata.getMetadataBuildingOptions().getServiceRegistry();
BytecodeProvider bytecodeProvider = serviceRegistry.getService( BytecodeProvider.class );
addSessionFactoryObservers( new SessionFactoryObserverForBytecodeEnhancer( bytecodeProvider ) );
return new SessionFactoryImpl( metadata, buildSessionFactoryOptions() );
return new SessionFactoryImpl( metadata, buildSessionFactoryOptions(), HQLQueryPlan::new );
}
@Override

View File

@ -18,7 +18,6 @@ import java.util.concurrent.TimeUnit;
import org.hibernate.Filter;
import org.hibernate.MappingException;
import org.hibernate.QueryException;
import org.hibernate.Session;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -43,6 +42,11 @@ import org.hibernate.stat.spi.StatisticsImplementor;
public class QueryPlanCache implements Serializable {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( QueryPlanCache.class );
@FunctionalInterface
public interface QueryPlanCreator {
HQLQueryPlan createQueryPlan(String queryString, boolean shallow, Map<String, Filter> enabledFilters, SessionFactoryImplementor factory);
}
/**
* The default strong reference count.
*/
@ -53,6 +57,7 @@ public class QueryPlanCache implements Serializable {
public static final int DEFAULT_QUERY_PLAN_MAX_COUNT = 2048;
private final SessionFactoryImplementor factory;
private QueryPlanCreator queryPlanCreator;
/**
* the cache of the actual plans...
@ -78,8 +83,9 @@ public class QueryPlanCache implements Serializable {
* @param factory The SessionFactory
*/
@SuppressWarnings("deprecation")
public QueryPlanCache(final SessionFactoryImplementor factory) {
public QueryPlanCache(final SessionFactoryImplementor factory, QueryPlanCreator queryPlanCreator) {
this.factory = factory;
this.queryPlanCreator = queryPlanCreator;
Integer maxParameterMetadataCount = ConfigurationHelper.getInteger(
Environment.QUERY_PLAN_CACHE_PARAMETER_METADATA_MAX_SIZE,
@ -153,7 +159,7 @@ public class QueryPlanCache implements Serializable {
final long startTime = ( stats ) ? System.nanoTime() : 0L;
LOG.tracev( "Unable to locate HQL query plan in cache; generating ({0})", queryString );
value = createHQLQueryPlan( queryString, shallow, enabledFilters, factory );
value = queryPlanCreator.createQueryPlan( queryString, shallow, enabledFilters, factory );
if ( stats ) {
final long endTime = System.nanoTime();
@ -173,10 +179,6 @@ public class QueryPlanCache implements Serializable {
return value;
}
protected HQLQueryPlan createHQLQueryPlan(String queryString, boolean shallow, Map<String, Filter> enabledFilters, SessionFactoryImplementor factory) {
return new HQLQueryPlan( queryString, shallow, enabledFilters, factory );
}
/**
* Get the query plan for the given collection HQL filter fragment, creating it and caching it if not already cached
*

View File

@ -198,13 +198,10 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
private final transient SessionBuilder defaultSessionOpenOptions;
private final transient SessionBuilder temporarySessionOpenOptions;
public SessionFactoryImpl(final MetadataImplementor metadata, SessionFactoryOptions options) {
this( metadata, options, QueryPlanCache::new );
}
protected SessionFactoryImpl(
public SessionFactoryImpl(
final MetadataImplementor metadata,
SessionFactoryOptions options, Function<SessionFactoryImplementor, QueryPlanCache> queryPlanCacheFunction) {
SessionFactoryOptions options,
QueryPlanCache.QueryPlanCreator queryPlanCacheFunction) {
LOG.debug( "Building session factory" );
this.sessionFactoryOptions = options;
@ -265,7 +262,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
LOG.debugf( "Session factory constructed with filter configurations : %s", filters );
LOG.debugf( "Instantiating session factory with properties: %s", properties );
this.queryPlanCache = queryPlanCacheFunction.apply( this );
this.queryPlanCache = new QueryPlanCache( this, queryPlanCacheFunction );
class IntegratorObserver implements SessionFactoryObserver {
private ArrayList<Integrator> integrators = new ArrayList<>();