From ea859736c2f847ed8396ff105308210ec4e39097 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Wed, 8 Aug 2018 16:22:18 +0200 Subject: [PATCH] HHH-12896 Minor memory improvements in HQLQueryPlan --- .../engine/query/spi/HQLQueryPlan.java | 28 +++++++++++-------- .../internal/classic/QueryTranslatorImpl.java | 7 ++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/HQLQueryPlan.java b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/HQLQueryPlan.java index 704d8ff4b9..05b7f0152a 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/HQLQueryPlan.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/HQLQueryPlan.java @@ -48,11 +48,10 @@ import org.hibernate.type.Type; public class HQLQueryPlan implements Serializable { private static final CoreMessageLogger LOG = CoreLogging.messageLogger( HQLQueryPlan.class ); - // TODO : keep separate notions of QT[] here for shallow/non-shallow queries... + // TODO : keep separate notions of QT[] here for shallow/non-shallow queries... private final String sourceQuery; private final QueryTranslator[] translators; - private final String[] sqlStrings; private final ParameterMetadataImpl parameterMetadata; private final ReturnMetadata returnMetadata; @@ -60,7 +59,6 @@ public class HQLQueryPlan implements Serializable { private final Set enabledFilterNames; private final boolean shallow; - private final SessionFactoryImplementor factory; /** * We'll check the trace level only once per instance @@ -79,7 +77,7 @@ public class HQLQueryPlan implements Serializable { SessionFactoryImplementor factory) { this( hql, null, shallow, enabledFilters, factory, null ); } - + public HQLQueryPlan(String hql, boolean shallow, Map enabledFilters, SessionFactoryImplementor factory, EntityGraphQueryHint entityGraphQueryHint) { this( hql, null, shallow, enabledFilters, factory, entityGraphQueryHint ); @@ -95,17 +93,18 @@ public class HQLQueryPlan implements Serializable { EntityGraphQueryHint entityGraphQueryHint) { this.sourceQuery = hql; this.shallow = shallow; - this.factory = factory; - final Set copy = new HashSet<>(); - copy.addAll( enabledFilters.keySet() ); - this.enabledFilterNames = java.util.Collections.unmodifiableSet( copy ); + if ( enabledFilters.isEmpty() ) { + this.enabledFilterNames = Collections.emptySet(); + } + else { + this.enabledFilterNames = Collections.unmodifiableSet( new HashSet<>( enabledFilters.keySet() ) ); + } final String[] concreteQueryStrings = QuerySplitter.concreteQueries( hql, factory ); final int length = concreteQueryStrings.length; this.translators = new QueryTranslator[length]; - final List sqlStringList = new ArrayList<>(); final Set combinedQuerySpaces = new HashSet<>(); final Map querySubstitutions = factory.getSessionFactoryOptions().getQuerySubstitutions(); @@ -124,10 +123,8 @@ public class HQLQueryPlan implements Serializable { ( (FilterTranslator) translators[i] ).compile( collectionRole, querySubstitutions, shallow ); } combinedQuerySpaces.addAll( translators[i].getQuerySpaces() ); - sqlStringList.addAll( translators[i].collectSqlStrings() ); } - this.sqlStrings = ArrayHelper.toStringArray( sqlStringList ); this.querySpaces = combinedQuerySpaces; if ( length == 0 ) { @@ -166,8 +163,15 @@ public class HQLQueryPlan implements Serializable { return enabledFilterNames; } + /** + * This method should only be called for debugging purposes as it regenerates a new array every time. + */ public String[] getSqlStrings() { - return sqlStrings; + List sqlStrings = new ArrayList<>(); + for ( int i = 0; i < translators.length; i++ ) { + sqlStrings.addAll( translators[i].collectSqlStrings() ); + } + return ArrayHelper.toStringArray( sqlStrings ); } public Set getUtilizedFilterNames() { diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/classic/QueryTranslatorImpl.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/classic/QueryTranslatorImpl.java index 24126fe58d..9ed4be7538 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/classic/QueryTranslatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/classic/QueryTranslatorImpl.java @@ -268,10 +268,12 @@ public class QueryTranslatorImpl extends BasicLoader implements FilterTranslator return sqlString; } + @Override public List collectSqlStrings() { - return ArrayHelper.toList( new String[] {sqlString} ); + return Collections.singletonList( sqlString ); } + @Override public String getQueryString() { return queryString; } @@ -291,15 +293,18 @@ public class QueryTranslatorImpl extends BasicLoader implements FilterTranslator * * @return an array of Types. */ + @Override public Type[] getReturnTypes() { return actualReturnTypes; } + @Override public String[] getReturnAliases() { // return aliases not supported in classic translator! return NO_RETURN_ALIASES; } + @Override public String[][] getColumnNames() { return scalarColumnNames; }