HHH-12917 Interning of strings for Filter definitions

This commit is contained in:
Sanne Grinovero 2018-08-17 12:26:29 +01:00 committed by Guillaume Smet
parent 6911efdfb7
commit 4fa0491341
2 changed files with 38 additions and 11 deletions

View File

@ -14,6 +14,8 @@ import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.sql.Template; import org.hibernate.sql.Template;
import static org.hibernate.internal.util.StringHelper.safeInterning;
/** /**
* Implementation of FilterHelper. * Implementation of FilterHelper.
* *
@ -44,23 +46,27 @@ public class FilterHelper {
filterCount = 0; filterCount = 0;
for ( final FilterConfiguration filter : filters ) { for ( final FilterConfiguration filter : filters ) {
filterAutoAliasFlags[filterCount] = false; filterAutoAliasFlags[filterCount] = false;
filterNames[filterCount] = filter.getName(); filterNames[filterCount] = safeInterning( filter.getName() );
filterConditions[filterCount] = filter.getCondition(); filterConditions[filterCount] = safeInterning( filter.getCondition() );
filterAliasTableMaps[filterCount] = filter.getAliasTableMap( factory ); filterAliasTableMaps[filterCount] = filter.getAliasTableMap( factory );
if ( ( filterAliasTableMaps[filterCount].isEmpty() || isTableFromPersistentClass( filterAliasTableMaps[filterCount] ) ) && filter if ( ( filterAliasTableMaps[filterCount].isEmpty() || isTableFromPersistentClass( filterAliasTableMaps[filterCount] ) ) && filter
.useAutoAliasInjection() ) { .useAutoAliasInjection() ) {
filterConditions[filterCount] = Template.renderWhereStringTemplate( filterConditions[filterCount] = safeInterning(
Template.renderWhereStringTemplate(
filter.getCondition(), filter.getCondition(),
FilterImpl.MARKER, FilterImpl.MARKER,
factory.getDialect(), factory.getDialect(),
factory.getSqlFunctionRegistry() factory.getSqlFunctionRegistry()
)
); );
filterAutoAliasFlags[filterCount] = true; filterAutoAliasFlags[filterCount] = true;
} }
filterConditions[filterCount] = StringHelper.replace( filterConditions[filterCount] = safeInterning(
StringHelper.replace(
filterConditions[filterCount], filterConditions[filterCount],
":", ":",
":" + filterNames[filterCount] + "." ":" + filterNames[filterCount] + "."
)
); );
filterCount++; filterCount++;
} }

View File

@ -886,4 +886,25 @@ public final class StringHelper {
return null; return null;
} }
} }
/**
* Return the interned form of a String, or null if the parameter is null.
* <p>
* Use with caution: excessive interning is known to cause issues.
* Best to use only with strings which are known to be long lived constants,
* and for which the chances of being actual duplicates is proven.
* (Even better: avoid needing interning by design changes such as reusing
* the known reference)
* @param string The string to intern.
* @return The interned string.
*/
public static String safeInterning(final String string) {
if ( string == null ) {
return null;
}
else {
return string.intern();
}
}
} }