HHH-5843 - Avoid useless branches during HQL parsing when trace logging is disabled

This commit is contained in:
Sanne Grinovero 2011-01-14 16:31:01 +00:00
parent af81f187b4
commit adbe3920f1
3 changed files with 48 additions and 36 deletions

View File

@ -55,6 +55,7 @@ public final class HqlParser extends HqlBaseParser {
* A logger for this class. * A logger for this class.
*/ */
private static final Logger log = LoggerFactory.getLogger( HqlParser.class ); private static final Logger log = LoggerFactory.getLogger( HqlParser.class );
private final boolean trace = log.isTraceEnabled();
private ParseErrorHandler parseErrorHandler; private ParseErrorHandler parseErrorHandler;
private ASTPrinter printer = getASTPrinter(); private ASTPrinter printer = getASTPrinter();
@ -77,23 +78,26 @@ public final class HqlParser extends HqlBaseParser {
// handle trace logging ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // handle trace logging ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private int traceDepth = 0; private int traceDepth = 0;
public void traceIn(String ruleName) { public void traceIn(String ruleName) {
if ( inputState.guessing > 0 ) { if (trace) {
return; if ( inputState.guessing > 0 ) {
return;
}
String prefix = StringHelper.repeat( '-', (traceDepth++ * 2) ) + "-> ";
log.trace( prefix + ruleName );
} }
String prefix = StringHelper.repeat( '-', (traceDepth++ * 2) ) + "-> ";
log.trace( prefix + ruleName );
} }
public void traceOut(String ruleName) { public void traceOut(String ruleName) {
if ( inputState.guessing > 0 ) { if (trace) {
return; if ( inputState.guessing > 0 ) {
return;
}
String prefix = "<-" + StringHelper.repeat( '-', (--traceDepth * 2) ) + " ";
log.trace( prefix + ruleName );
} }
String prefix = "<-" + StringHelper.repeat( '-', (--traceDepth * 2) ) + " ";
log.trace( prefix + ruleName );
} }
public void reportError(RecognitionException e) { public void reportError(RecognitionException e) {

View File

@ -110,7 +110,7 @@ import antlr.collections.AST;
* Implements methods used by the HQL->SQL tree transform grammar (a.k.a. the second phase). * Implements methods used by the HQL->SQL tree transform grammar (a.k.a. the second phase).
* <ul> * <ul>
* <li>Isolates the Hibernate API-specific code from the ANTLR generated code.</li> * <li>Isolates the Hibernate API-specific code from the ANTLR generated code.</li>
* <li>Handles the SQL framgents generated by the persisters in order to create the SELECT and FROM clauses, * <li>Handles the SQL fragments generated by the persisters in order to create the SELECT and FROM clauses,
* taking into account the joins and projections that are implied by the mappings (persister/queryable).</li> * taking into account the joins and projections that are implied by the mappings (persister/queryable).</li>
* <li>Uses SqlASTFactory to create customized AST nodes.</li> * <li>Uses SqlASTFactory to create customized AST nodes.</li>
* </ul> * </ul>
@ -119,6 +119,7 @@ import antlr.collections.AST;
*/ */
public class HqlSqlWalker extends HqlSqlBaseWalker implements ErrorReporter, ParameterBinder.NamedParameterSource { public class HqlSqlWalker extends HqlSqlBaseWalker implements ErrorReporter, ParameterBinder.NamedParameterSource {
private static final Logger log = LoggerFactory.getLogger( HqlSqlWalker.class ); private static final Logger log = LoggerFactory.getLogger( HqlSqlWalker.class );
private final boolean trace = log.isTraceEnabled();
private final QueryTranslatorImpl queryTranslatorImpl; private final QueryTranslatorImpl queryTranslatorImpl;
private final HqlParser hqlParser; private final HqlParser hqlParser;
@ -183,15 +184,17 @@ public class HqlSqlWalker extends HqlSqlBaseWalker implements ErrorReporter, Par
// handle trace logging ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // handle trace logging ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private int traceDepth = 0; private int traceDepth = 0;
public void traceIn(String ruleName, AST tree) { public void traceIn(String ruleName, AST tree) {
if ( inputState.guessing > 0 ) { if (trace) {
return; if ( inputState.guessing > 0 ) {
return;
}
String prefix = StringHelper.repeat( '-', (traceDepth++ * 2) ) + "-> ";
String traceText = ruleName + " (" + buildTraceNodeName(tree) + ")";
log.trace( prefix + traceText );
} }
String prefix = StringHelper.repeat( '-', (traceDepth++ * 2) ) + "-> ";
String traceText = ruleName + " (" + buildTraceNodeName(tree) + ")";
log.trace( prefix + traceText );
} }
private String buildTraceNodeName(AST tree) { private String buildTraceNodeName(AST tree) {
@ -201,11 +204,13 @@ public class HqlSqlWalker extends HqlSqlBaseWalker implements ErrorReporter, Par
} }
public void traceOut(String ruleName, AST tree) { public void traceOut(String ruleName, AST tree) {
if ( inputState.guessing > 0 ) { if (trace) {
return; if ( inputState.guessing > 0 ) {
return;
}
String prefix = "<-" + StringHelper.repeat( '-', (--traceDepth * 2) ) + " ";
log.trace( prefix + ruleName );
} }
String prefix = "<-" + StringHelper.repeat( '-', (--traceDepth * 2) ) + " ";
log.trace( prefix + ruleName );
} }
@ -583,7 +588,7 @@ public class HqlSqlWalker extends HqlSqlBaseWalker implements ErrorReporter, Par
if ( fromElements.size() == 1 ) { if ( fromElements.size() == 1 ) {
final FromElement fromElement = ( FromElement ) fromElements.get( 0 ); final FromElement fromElement = ( FromElement ) fromElements.get( 0 );
try { try {
log.trace( "attempting to resolve property [" + identText + "] as a non-qualified ref" ); if (trace) log.trace( "attempting to resolve property [" + identText + "] as a non-qualified ref" );
return fromElement.getPropertyMapping( identText ).toType( identText ) != null; return fromElement.getPropertyMapping( identText ).toType( identText ) != null;
} }
catch( QueryException e ) { catch( QueryException e ) {

View File

@ -33,7 +33,6 @@ import antlr.RecognitionException;
import antlr.collections.AST; import antlr.collections.AST;
import org.hibernate.QueryException; import org.hibernate.QueryException;
import org.hibernate.hql.ast.tree.FunctionNode; import org.hibernate.hql.ast.tree.FunctionNode;
import org.hibernate.hql.ast.tree.SqlNode;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.hibernate.util.StringHelper; import org.hibernate.util.StringHelper;
import org.hibernate.param.ParameterSpecification; import org.hibernate.param.ParameterSpecification;
@ -41,7 +40,6 @@ import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.hql.antlr.SqlGeneratorBase; import org.hibernate.hql.antlr.SqlGeneratorBase;
import org.hibernate.hql.antlr.SqlTokenTypes; import org.hibernate.hql.antlr.SqlTokenTypes;
import org.hibernate.hql.ast.tree.MethodNode;
import org.hibernate.hql.ast.tree.FromElement; import org.hibernate.hql.ast.tree.FromElement;
import org.hibernate.hql.ast.tree.Node; import org.hibernate.hql.ast.tree.Node;
import org.hibernate.hql.ast.tree.ParameterNode; import org.hibernate.hql.ast.tree.ParameterNode;
@ -60,14 +58,15 @@ import org.slf4j.LoggerFactory;
*/ */
public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter { public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
private static final Logger log = LoggerFactory.getLogger( SqlGenerator.class ); private static final Logger log = LoggerFactory.getLogger( SqlGenerator.class );
private final boolean trace = log.isTraceEnabled();
public static boolean REGRESSION_STYLE_CROSS_JOINS = false; public static boolean REGRESSION_STYLE_CROSS_JOINS = false;
/** /**
* all append invocations on the buf should go through this Output instance variable. * all append invocations on the buf should go through this Output instance variable.
* The value of this variable may be temporarily substitued by sql function processing code * The value of this variable may be temporarily substituted by sql function processing code
* to catch generated arguments. * to catch generated arguments.
* This is because sql function templates need arguments as seperate string chunks * This is because sql function templates need arguments as separate string chunks
* that will be assembled into the target dialect-specific function call. * that will be assembled into the target dialect-specific function call.
*/ */
private SqlWriter writer = new DefaultWriter(); private SqlWriter writer = new DefaultWriter();
@ -81,15 +80,17 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
// handle trace logging ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // handle trace logging ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private int traceDepth = 0; private int traceDepth = 0;
public void traceIn(String ruleName, AST tree) { public void traceIn(String ruleName, AST tree) {
if ( inputState.guessing > 0 ) { if (trace) {
return; if ( inputState.guessing > 0 ) {
return;
}
String prefix = StringHelper.repeat( '-', (traceDepth++ * 2) ) + "-> ";
String traceText = ruleName + " (" + buildTraceNodeName(tree) + ")";
log.trace( prefix + traceText );
} }
String prefix = StringHelper.repeat( '-', (traceDepth++ * 2) ) + "-> ";
String traceText = ruleName + " (" + buildTraceNodeName(tree) + ")";
log.trace( prefix + traceText );
} }
private String buildTraceNodeName(AST tree) { private String buildTraceNodeName(AST tree) {
@ -99,11 +100,13 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter {
} }
public void traceOut(String ruleName, AST tree) { public void traceOut(String ruleName, AST tree) {
if ( inputState.guessing > 0 ) { if (trace) {
return; if ( inputState.guessing > 0 ) {
return;
}
String prefix = "<-" + StringHelper.repeat( '-', (--traceDepth * 2) ) + " ";
log.trace( prefix + ruleName );
} }
String prefix = "<-" + StringHelper.repeat( '-', (--traceDepth * 2) ) + " ";
log.trace( prefix + ruleName );
} }
public List getCollectedParameters() { public List getCollectedParameters() {