db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1478932 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/db/ engine/org/apache/derby/iapi/sql/compile/ engine/org/apache/derby/iapi/sql/conn/ engine/org/apache/derby/impl/sql/ engine/org/apache/derby/impl/sql/compile/ engine/or...
Date Fri, 03 May 2013 19:16:44 GMT
Author: rhillegas
Date: Fri May  3 19:16:33 2013
New Revision: 1478932

URL: http://svn.apache.org/r1478932
Log:
DERBY-6211: Cleanup optimizer trace support.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/db/OptimizerTrace.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/OptTrace.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/DefaultOptTrace.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/Level2OptimizerImpl.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerImpl.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/db/OptimizerTrace.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/db/OptimizerTrace.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/db/OptimizerTrace.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/db/OptimizerTrace.java Fri May 
3 19:16:33 2013
@@ -21,11 +21,16 @@
 
 package org.apache.derby.iapi.db;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.sql.SQLException;
 
+import org.apache.derby.iapi.sql.compile.OptTrace;
 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
 import org.apache.derby.iapi.sql.conn.ConnectionUtil;
 
+import org.apache.derby.impl.sql.compile.DefaultOptTrace;
+
 /**
   <P>
   This  class provides static methods for controlling the
@@ -35,68 +40,47 @@ import org.apache.derby.iapi.sql.conn.Co
 public class OptimizerTrace
 {
 	/**
-	 * Control whether or not optimizer trace is on.
-	 *
-	 * @param onOrOff    Whether to turn optimizer trace on (true) or off (false).
+	 * Turn default optimizer tracing on or off.
 	 *
-	 * @return Whether or not the call was successful.  (false will be returned when optimizer
tracing is not supported.)
+	 * @param onOrOff    Whether to turn optimizer tracing on (true) or off (false).
 	 */
-	public static boolean setOptimizerTrace(boolean onOrOff)
+	public static void setOptimizerTrace( boolean onOrOff )
 	{
-		boolean retCode = false;
-
-		try
-		{
-			// Get the current language connection context.  This is associated
-			// with the current database.
-			LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
-			retCode = lcc.setOptimizerTrace(onOrOff);
-		}
-		catch (Throwable t)
-		{
-			// eat all exceptions, simply return false
-		}
+        OptTrace    optimizerTracer = onOrOff ? new DefaultOptTrace() : null;
 
-		return retCode;
+        setOptimizerTracer( optimizerTracer );
 	}
 
-    /**
-     * Null out the optimizer trace.
-     */
-    public  static  void    nullifyTrace()  throws SQLException
-    {
-        ConnectionUtil.getCurrentLCC().setOptimizerTraceOutput( null );
-    }
-
 	/**
-	 * Control whether or not optimizer trace is generated in html.
-	 *
-	 * @param onOrOff    Whether or not optimizer trace will be in html (true) or not (false).
+	 * Install an optimizer tracer (to enable tracing) or uninstall the current optimizer tracer
+     * (to disable tracing).
 	 *
-	 * @return Whether or not the call was successful.  (false will be returned when optimizer
tracing is not supported.)
+	 * @param tracer    Null if tracing is being turned off, otherwise an optimizer tracer
 	 */
-	public static boolean setOptimizerTraceHtml(boolean onOrOff)
+	public static   void setOptimizerTracer( OptTrace tracer )
 	{
-		boolean retCode = false;
-
 		try
 		{
-			// Get the current language connection context.  This is associated
-			// with the current database.
-			LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
-			retCode = lcc.setOptimizerTraceHtml(onOrOff);
+            ConnectionUtil.getCurrentLCC().setOptimizerTracer( tracer );
 		}
-		catch (Throwable t)
+		catch (Throwable t) {}
+	}
+
+	/**
+	 * Get the current optimizer tracer, if any.
+	 */
+	public static   OptTrace getOptimizerTracer()
+	{
+		try
 		{
-			// eat all exceptions, simply return false
+            return ConnectionUtil.getCurrentLCC().getOptimizerTracer();
 		}
-
-		return retCode;
+		catch (Throwable t) { return null; }
 	}
 
+
 	/**
-	 * Get the optimizer trace output for the last optimized query as a String.  If optimizer
trace
-	 * html is on, then the String will contain the html tags.
+	 * Get the optimizer trace output for the last optimized query as a String.
 	 *
 	 * @return The optimizer trace output for the last optimized query as a String.
 	 *    Null will be returned if optimizer trace output is off or not supported 
@@ -111,39 +95,23 @@ public class OptimizerTrace
 			// Get the current language connection context.  This is associated
 			// with the current database.
 			LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
-			retCode = lcc.getOptimizerTraceOutput();
-		}
-		catch (Throwable t)
-		{
-			// eat all exceptions, simply return null
-		}
+            OptTrace    tracer = lcc.getOptimizerTracer();
 
-		return retCode;
-	}
+            if ( tracer != null )
+            {
+                StringWriter    sw = new StringWriter();
+                PrintWriter     pw = new PrintWriter( sw );
+
+                tracer.printToWriter( pw );
+                pw.flush();
+                sw.flush();
 
-	/**
-	 * Send the optimizer trace output for the last optimized query to a file with a .html extension.
 
-	 * If optimizer trace html is on, then the output will contain the html tags.
-	 *
-	 * @param fileName    The name of the file to write to.  (.html extension will be added.)
-	 *
-	 * @return Whether or not the request was successful.
-	 *    false mayl be returned for a number of reasons, including if optimizer trace output
is off or not supported 
-	 *    or no trace output was found or an exception occurred.
-	 */
-	public static boolean writeOptimizerTraceOutputHtml(String fileName)
-	{
-		boolean retCode = true;
-
-		try
-		{
-		String output = getOptimizerTraceOutput();
-		//RESOLVEOPTIMIZERTRACE - need to write out the html
+                retCode = sw.toString();
+            }
 		}
 		catch (Throwable t)
 		{
-			// eat all exceptions, simply return false
-			retCode = false;
+			// eat all exceptions, simply return null
 		}
 
 		return retCode;

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/OptTrace.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/OptTrace.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/OptTrace.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/OptTrace.java Fri May
 3 19:16:33 2013
@@ -21,6 +21,8 @@
 
 package org.apache.derby.iapi.sql.compile;
 
+import java.io.PrintWriter;
+
 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
 import org.apache.derby.iapi.util.JBitSet;
 
@@ -41,8 +43,11 @@ public  interface   OptTrace
     //
     ////////////////////////////////////////////////////////////////////////
 
+    /** Start the start of tracing a statement. */
+    public  void    traceStartStatement( String statementText );
+
     /** Start optimizer tracing. */
-    public  void    traceStart( long timeOptimizationStarted );
+    public  void    traceStart( long timeOptimizationStarted, int optimizerID );
 
     /** Say that the optimizer ran out of time. */
     public  void    traceTimeout( long currentTime, CostEstimate bestCost );
@@ -60,7 +65,7 @@ public  interface   OptTrace
     public  void    traceNoBestPlan();
 
     /** Say that we're modifying access paths. */
-    public  void    traceModifyingAccessPaths();
+    public  void    traceModifyingAccessPaths( int optimizerID );
 
     /** Say that we short-circuited a join order. */
     public  void    traceShortCircuiting( boolean timeExceeded, Optimizable thisOpt, int
joinPosition );
@@ -209,4 +214,7 @@ public  interface   OptTrace
     /** Report the cost based on index statistics. */
     public  void    traceCostIncludingStatsForIndex( CostEstimate cost, int tableNumber );
     
+    /** Print the trace so far. */
+    public  void    printToWriter( PrintWriter out );
+
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
Fri May  3 19:16:33 2013
@@ -23,6 +23,7 @@ package org.apache.derby.iapi.sql.conn;
 
 import org.apache.derby.iapi.services.context.Context;
 import org.apache.derby.iapi.services.io.FormatableBitSet;
+import org.apache.derby.iapi.sql.compile.OptTrace;
 import org.apache.derby.iapi.db.Database;
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.sql.compile.CompilerContext;
@@ -911,57 +912,24 @@ public interface LanguageConnectionConte
 	    throws StandardException;
 
 	/**
-	 * Control whether or not optimizer trace is on.
+	 * Install an optimizer tracer (to enable tracing) or uninstall the current optimizer tracer
+     * (to disable tracing).
 	 *
-	 * @param onOrOff    Whether to turn optimizer trace on (true) or off (false).
-	 *
-	 * @return Whether or not the call was successful.  (false will be returned when optimizer
tracing is not supported.)
+	 * @param tracer    Null if tracing is being turned off, otherwise an optimizer tracer
 	 */
-	public boolean setOptimizerTrace(boolean onOrOff);
-
-	/** 
-	 * Get whether or not optimizer trace is on.
-	 *
-	 * @return Whether or not optimizer trace is on.
-	 */
-	public boolean getOptimizerTrace();
+	public void setOptimizerTracer( OptTrace tracer );
 
 	/**
-	 * Control whether or not optimizer trace is generated in html.
-	 *
-	 * @param onOrOff    Whether or not optimizer trace will be in html (true) or not (false).
-	 *
-	 * @return Whether or not the call was successful.  (false will be returned when optimizer
tracing is not supported.)
+	 * Get the optimizer tracer (could be null if we aren't tracing the optimizer).
 	 */
-	public boolean setOptimizerTraceHtml(boolean onOrOff);
+	public OptTrace getOptimizerTracer();
 
 	/** 
-	 * Get whether or not optimizer trace html is on.
-	 *
-	 * @return Whether or not optimizer trace html is on.
-	 */
-	public boolean getOptimizerTraceHtml();
-
-	/**
-	 * Get the optimizer trace output for the last optimized query as a String.  If optimizer
trace
-	 * html is on, then the String will contain the html tags.
+	 * Get whether or not optimizer trace is on.
 	 *
-	 * @return The optimizer trace output for the last optimized query as a String.
-	 *    Null will be returned if optimizer trace output is off or not supported 
-	 *    or no trace output was found or an exception occurred.
-	 */
-	public String getOptimizerTraceOutput();
-
-	/**
-	 * Set the optimizer trace output to the specified String.
-	 * (Done at the beginning of each statement.)
-	 */
-	public void setOptimizerTraceOutput(String startingText);
-
-	/**
-	 * Append the latest output to the optimizer trace output.
+	 * @return Whether or not optimizer trace is on.
 	 */
-	public void appendOptimizerTraceOutput(String output);
+	public boolean optimizerTracingIsOn();
 
     /**
 	  *	Reports whether there is any outstanding work in the transaction.

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java Fri May
 3 19:16:33 2013
@@ -188,9 +188,11 @@ public class GenericStatement
 				return preparedStmt;
 		}
 
-		// Clear the optimizer trace from the last statement
-		if (lcc.getOptimizerTrace())
-			lcc.setOptimizerTraceOutput(getSource() + "\n");
+		// Start a new optimizer trace for this statement
+		if (lcc.optimizerTracingIsOn())
+        {
+            lcc.getOptimizerTracer().traceStartStatement( getSource() );
+        }
 
 		beginTime = getCurrentTimeMillis(lcc);
 		/* beginTimestamp only meaningful if beginTime is meaningful.

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/DefaultOptTrace.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/DefaultOptTrace.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/DefaultOptTrace.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/DefaultOptTrace.java
Fri May  3 19:16:33 2013
@@ -21,8 +21,9 @@
 
 package org.apache.derby.impl.sql.compile;
 
+import java.io.PrintWriter;
+
 import org.apache.derby.iapi.services.sanity.SanityManager;
-import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
 import org.apache.derby.iapi.sql.compile.AccessPath;
 import org.apache.derby.iapi.sql.compile.CostEstimate;
 import org.apache.derby.iapi.sql.compile.JoinStrategy;
@@ -52,8 +53,7 @@ public  class   DefaultOptTrace implemen
     //
     ////////////////////////////////////////////////////////////////////////
 
-    private LanguageConnectionContext   _lcc;
-    private int                                     _optimizerID;
+    private StringBuffer    _buffer;
     
     ////////////////////////////////////////////////////////////////////////
     //
@@ -62,10 +62,9 @@ public  class   DefaultOptTrace implemen
     ////////////////////////////////////////////////////////////////////////
 
     /** Make a DefaultOptTrace */
-    public  DefaultOptTrace( LanguageConnectionContext lcc, int optimizerID )
+    public  DefaultOptTrace()
     {
-        _lcc = lcc;
-        _optimizerID = optimizerID;
+        _buffer = new StringBuffer();
     }
 
     ////////////////////////////////////////////////////////////////////////
@@ -74,13 +73,18 @@ public  class   DefaultOptTrace implemen
     //
     ////////////////////////////////////////////////////////////////////////
 
-    public  void    traceStart( long timeOptimizationStarted )
+    public  void    traceStartStatement( String statementText )
+    {
+        appendTraceString( statementText );
+    }
+    
+    public  void    traceStart( long timeOptimizationStarted, int optimizerID )
     {
         appendTraceString
             (
              "Optimization started at time " + 
              timeOptimizationStarted +
-             " using optimizer " + _optimizerID
+             " using optimizer " + optimizerID
              );
     }
 
@@ -114,9 +118,9 @@ public  class   DefaultOptTrace implemen
         appendTraceString( "No best plan found." );
     }
 
-    public  void    traceModifyingAccessPaths()
+    public  void    traceModifyingAccessPaths( int optimizerID )
     {
-        appendTraceString( "Modifying access paths using optimizer " + _optimizerID );
+        appendTraceString( "Modifying access paths using optimizer " + optimizerID );
     }
 
     public  void    traceShortCircuiting( boolean timeExceeded, Optimizable thisOpt, int
joinPosition )
@@ -473,6 +477,11 @@ public  class   DefaultOptTrace implemen
         appendTraceString( reportCostIncluding( "statistics for index being considered",
cost, tableNumber ) );
     }
 
+    public  void    printToWriter( PrintWriter out )
+    {
+        out.println( _buffer.toString() );
+    }
+    
     ////////////////////////////////////////////////////////////////////////
     //
     //	REPORTING MINIONS
@@ -552,7 +561,7 @@ public  class   DefaultOptTrace implemen
     /** Append a string to the optimizer trace */
     private void    appendTraceString( String traceString )
     {
-		_lcc.appendOptimizerTraceOutput(traceString + "\n");
+		_buffer.append( traceString + "\n" );
     }
 
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/Level2OptimizerImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/Level2OptimizerImpl.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/Level2OptimizerImpl.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/Level2OptimizerImpl.java
Fri May  3 19:16:33 2013
@@ -63,13 +63,10 @@ public class Level2OptimizerImpl extends
 			  joinStrategies, tableLockThreshold, requiredRowOrdering,
 			  numTablesInQuery);
 
-		// Remember whether or not optimizer trace is on;
-		optimizerTrace = lcc.getOptimizerTrace();
-		optimizerTraceHtml = lcc.getOptimizerTraceHtml();
 		this.lcc = lcc;
 
 		// Optimization started
-		if (optimizerTrace) { tracer().traceStart( timeOptimizationStarted ); }
+		if (tracingIsOn()) { tracer().traceStart( timeOptimizationStarted, hashCode() ); }
 	}
 
 	/** @see Optimizer#getLevel */

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerImpl.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerImpl.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerImpl.java Fri
May  3 19:16:33 2013
@@ -135,11 +135,6 @@ public class OptimizerImpl implements Op
 
 	private boolean	conglomerate_OneRowResultSet;
 
-	// optimizer trace
-	protected boolean optimizerTrace;
-	protected boolean optimizerTraceHtml;
-    private OptTrace    _tracer;
-
 	// max memory use per table
 	protected int maxMemoryPerTable;
 
@@ -359,7 +354,7 @@ public class OptimizerImpl implements Op
 		desiredJoinOrderFound = false;
 	}
 
-    public  boolean tracingIsOn() { return optimizerTrace; }
+    public  boolean tracingIsOn() { return lcc.optimizerTracingIsOn(); }
 
     public int getMaxMemoryPerTable()
     {
@@ -377,7 +372,7 @@ public class OptimizerImpl implements Op
 		/* Don't get any permutations if there is nothing to optimize */
 		if (numOptimizables < 1)
 		{
-			if (optimizerTrace) { tracer().traceVacuous(); }
+			if ( tracingIsOn() ) { tracer().traceVacuous(); }
 
 			endOfRoundCleanup();
 			return false;
@@ -407,7 +402,7 @@ public class OptimizerImpl implements Op
 			currentTime = System.currentTimeMillis();
 			timeExceeded = (currentTime - timeOptimizationStarted) > timeLimit;
 
-			if (optimizerTrace && timeExceeded) { tracer().traceTimeout( currentTime, bestCost
); }
+			if (tracingIsOn() && timeExceeded) { tracer().traceTimeout( currentTime, bestCost
); }
 		}
 
 		if (bestCost.isUninitialized() && foundABestPlan &&
@@ -563,7 +558,7 @@ public class OptimizerImpl implements Op
 			// order.
 			if (joinPosition < (numOptimizables - 1))
             {
-                if (optimizerTrace)
+                if (tracingIsOn())
                 {
                     tracer().traceShortCircuiting
                         (
@@ -792,7 +787,7 @@ public class OptimizerImpl implements Op
 					if ((nextOptimizable < numOptimizables) &&
 						!joinOrderMeetsDependencies(nextOptimizable))
 					{
-						if (optimizerTrace)
+						if (tracingIsOn())
                         {
                             tracer().traceSkippingJoinOrder
                                 ( nextOptimizable, joinPosition, ArrayUtil.copy( proposedJoinOrder
), (JBitSet) assignedTableMap.clone() );
@@ -803,7 +798,7 @@ public class OptimizerImpl implements Op
 						*/
 						if ( ! optimizableList.optimizeJoinOrder())
 						{
-							if (optimizerTrace) { tracer().traceIllegalUserJoinOrder(); }
+							if (tracingIsOn()) { tracer().traceIllegalUserJoinOrder(); }
 
 							throw StandardException.newException(
 								SQLState.LANG_ILLEGAL_FORCED_JOIN_ORDER);
@@ -828,12 +823,12 @@ public class OptimizerImpl implements Op
 					// Verify that the user specified a legal join order
 					if ( ! optimizableList.legalJoinOrder(numTablesInQuery))
 					{
-						if (optimizerTrace)  { tracer().traceIllegalUserJoinOrder(); }
+						if (tracingIsOn())  { tracer().traceIllegalUserJoinOrder(); }
 
 						throw StandardException.newException(SQLState.LANG_ILLEGAL_FORCED_JOIN_ORDER);
 					}
 
-					if (optimizerTrace) { tracer().traceUserJoinOrderOptimized(); }
+					if (tracingIsOn()) { tracer().traceUserJoinOrderOptimized(); }
 
 					desiredJoinOrderFound = true;
 				}
@@ -953,7 +948,7 @@ public class OptimizerImpl implements Op
 			optimizableList.getOptimizable(nextOptimizable).
 				getBestAccessPath().setCostEstimate((CostEstimate) null);
 
-			if (optimizerTrace)
+			if (tracingIsOn())
             {
                 tracer().traceJoinOrderConsideration
                     ( joinPosition, ArrayUtil.copy( proposedJoinOrder ), (JBitSet) assignedTableMap.clone()
);
@@ -1614,15 +1609,16 @@ public class OptimizerImpl implements Op
 					ce.singleScanRowCount());
 			}
 
-			if (optimizerTrace) { tracer().traceCostWithoutSortAvoidance( currentCost ); }
+			if (tracingIsOn())
 			{
+                tracer().traceCostWithoutSortAvoidance( currentCost );
 				if (curOpt.considerSortAvoidancePath()) { tracer().traceCostWithSortAvoidance( currentSortAvoidanceCost
); }
 			}
 				
 			/* Do we have a complete join order? */
 			if ( joinPosition == (numOptimizables - 1) )
 			{
-				if (optimizerTrace) { tracer().traceCompleteJoinOrder(); }
+				if (tracingIsOn()) { tracer().traceCompleteJoinOrder(); }
 
 				/* Add cost of sorting to non-sort-avoidance cost */
 				if (requiredRowOrdering != null)
@@ -1704,7 +1700,7 @@ public class OptimizerImpl implements Op
 										currentCost.singleScanRowCount()
 										);
 					
-					if (optimizerTrace) { tracer().traceSortCost( sortCost, currentCost ); }
+					if (tracingIsOn()) { tracer().traceSortCost( sortCost, currentCost ); }
 				}
 
 				/*
@@ -1772,7 +1768,7 @@ public class OptimizerImpl implements Op
 							bestRowOrdering, optimizableList) == 
 								RequiredRowOrdering.NOTHING_REQUIRED)
 					{
-						if (optimizerTrace) { tracer().traceCurrentPlanAvoidsSort( bestCost, currentSortAvoidanceCost
); }
+						if (tracingIsOn()) { tracer().traceCurrentPlanAvoidsSort( bestCost, currentSortAvoidanceCost
); }
 
 						if ((currentSortAvoidanceCost.compare(bestCost) <= 0)
 							|| bestCost.isUninitialized())
@@ -1802,7 +1798,7 @@ public class OptimizerImpl implements Op
 	{
 		foundABestPlan = true;
 
-		if (optimizerTrace) { tracer().traceCheapestPlanSoFar( planType, currentCost ); }
+		if (tracingIsOn()) { tracer().traceCheapestPlanSoFar( planType, currentCost ); }
 
 		/* Remember the current cost as best */
 		bestCost.setCost(currentCost);
@@ -1843,7 +1839,7 @@ public class OptimizerImpl implements Op
 				requiredRowOrdering.sortNeeded();
 		}
 
-		if (optimizerTrace)
+		if (tracingIsOn())
 		{
 			if (requiredRowOrdering != null)    { tracer().traceSortNeededForOrdering( planType, requiredRowOrdering
); }
             tracer().traceRememberingBestJoinOrder( joinPosition, ArrayUtil.copy( bestJoinOrder
), (JBitSet) assignedTableMap.clone() );
@@ -2163,7 +2159,7 @@ public class OptimizerImpl implements Op
 		// DERBY-1259.
 		if( ! optimizable.memoryUsageOK( estimatedCost.rowCount() / outerCost.rowCount(), maxMemoryPerTable))
 		{
-			if (optimizerTrace) { tracer().traceSkippingBecauseTooMuchMemory( maxMemoryPerTable );
}
+			if (tracingIsOn()) { tracer().traceSkippingBecauseTooMuchMemory( maxMemoryPerTable );
}
 			return;
 		}
 
@@ -2297,7 +2293,7 @@ public class OptimizerImpl implements Op
         if( ! optimizable.memoryUsageOK( estimatedCost.rowCount() / outerCost.rowCount(),
                                          maxMemoryPerTable))
 		{
-			if (optimizerTrace) { tracer().traceSkippingBecauseTooMuchMemory( maxMemoryPerTable );
}
+			if (tracingIsOn()) { tracer().traceSkippingBecauseTooMuchMemory( maxMemoryPerTable );
}
 			return;
 		}
 
@@ -2383,11 +2379,11 @@ public class OptimizerImpl implements Op
 	 */
 	public void modifyAccessPaths() throws StandardException
 	{
-		if (optimizerTrace) { tracer().traceModifyingAccessPaths(); }
+		if (tracingIsOn()) { tracer().traceModifyingAccessPaths( hashCode() ); }
 
 		if ( ! foundABestPlan)
 		{
-			if (optimizerTrace) { tracer().traceNoBestPlan(); }
+			if (tracingIsOn()) { tracer().traceNoBestPlan(); }
 
 			throw StandardException.newException(SQLState.LANG_NO_BEST_PLAN_FOUND);
 		}
@@ -2752,11 +2748,6 @@ public class OptimizerImpl implements Op
 	}
 
     /** Get the trace machinery */
-    public  OptTrace    tracer()
-    {
-        if ( _tracer == null ) { _tracer = new DefaultOptTrace( lcc,  hashCode() ); }
-
-        return _tracer;
-    }
+    public  OptTrace    tracer()    { return lcc.getOptimizerTracer(); }
 
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
Fri May  3 19:16:33 2013
@@ -41,6 +41,7 @@ import org.apache.derby.iapi.db.Database
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.sql.compile.CompilerContext;
 import org.apache.derby.iapi.sql.compile.OptimizerFactory;
+import org.apache.derby.iapi.sql.compile.OptTrace;
 import org.apache.derby.iapi.sql.compile.ASTVisitor;
 import org.apache.derby.iapi.sql.conn.Authorizer;
 import org.apache.derby.iapi.error.ExceptionSeverity;
@@ -273,10 +274,7 @@ public class GenericLanguageConnectionCo
     private ArrayList triggerTables;
 
     // OptimizerTrace
-    private boolean optimizerTrace;
-    private boolean optimizerTraceHtml;
-    private String lastOptimizerTraceOutput;
-    private String optimizerTraceOutput;
+    private OptTrace    optimizerTracer;
 
     //// Support for AUTOINCREMENT
 
@@ -2994,82 +2992,24 @@ public class GenericLanguageConnectionCo
     }
 
     /**
-     * @see LanguageConnectionContext#setOptimizerTrace
+     * @see LanguageConnectionContext#setOptimizerTracer
      */
-    public boolean setOptimizerTrace(boolean onOrOff)
+    public void setOptimizerTracer( OptTrace tracer )
     {
-        if (of == null)
-        {
-            return false;
-        }
-        if (! of.supportsOptimizerTrace())
-        {
-            return false;
-        }
-        optimizerTrace = onOrOff;
-        return true;
-    }
-
-    /**
-     * @see LanguageConnectionContext#getOptimizerTrace
-     */
-    public boolean getOptimizerTrace()
-    {
-        return optimizerTrace;
-    }
-
-    /**
-     * @see LanguageConnectionContext#setOptimizerTraceHtml
-     */
-    public boolean setOptimizerTraceHtml(boolean onOrOff)
-    {
-        if (of == null)
-        {
-            return false;
-        }
-        if (! of.supportsOptimizerTrace())
-        {
-            return false;
-        }
-        optimizerTraceHtml = onOrOff;
-        return true;
-    }
-
-    /**
-     * @see LanguageConnectionContext#getOptimizerTraceHtml
-     */
-    public boolean getOptimizerTraceHtml()
-    {
-        return optimizerTraceHtml;
+        optimizerTracer = tracer;
     }
 
     /**
-     * @see LanguageConnectionContext#setOptimizerTraceOutput
+     * @see LanguageConnectionContext#getOptimizerTracer
      */
-    public void setOptimizerTraceOutput(String startingText)
-    {
-        if (optimizerTrace)
-        {
-            lastOptimizerTraceOutput = optimizerTraceOutput;
-            optimizerTraceOutput = startingText;
-        }
-    }
-
-    /**
-     * @see LanguageConnectionContext#appendOptimizerTraceOutput
-     */
-    public void appendOptimizerTraceOutput(String output)
-    {
-        optimizerTraceOutput = 
-            (optimizerTraceOutput == null) ? output : optimizerTraceOutput + output;
-    }
-
+	public OptTrace getOptimizerTracer() { return optimizerTracer; }
+    
     /**
-     * @see LanguageConnectionContext#getOptimizerTraceOutput
+     * @see LanguageConnectionContext#optimizerTracingIsOn
      */
-    public String getOptimizerTraceOutput()
+    public boolean optimizerTracingIsOn()
     {
-        return lastOptimizerTraceOutput;
+        return (optimizerTracer != null);
     }
 
     /**

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java?rev=1478932&r1=1478931&r2=1478932&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java
(original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java
Fri May  3 19:16:33 2013
@@ -21,10 +21,11 @@
 
 package org.apache.derby.impl.tools.optional;
 
-import java.io.FileWriter;
+import java.io.PrintWriter;
 import java.sql.SQLException;
 
 import org.apache.derby.iapi.db.OptimizerTrace;
+import org.apache.derby.iapi.sql.compile.OptTrace;
 import org.apache.derby.iapi.sql.dictionary.OptionalTool;
 
 /**
@@ -75,7 +76,7 @@ public	class   OptimizerTracer  implemen
 
     /**
      * <p>
-     * Dump the optimizer trace and turn off tracing. Takes optional parameters:
+     * Print the optimizer trace and turn off tracing. Takes optional parameters:
      * </p>
      *
      * <ul>
@@ -85,30 +86,34 @@ public	class   OptimizerTracer  implemen
     public  void    unloadTool( String... configurationParameters )
         throws SQLException
     {
-        String  trace = OptimizerTrace.getOptimizerTraceOutput();
-        if ( trace == null ) { trace = ""; }
-
-        OptimizerTrace.nullifyTrace();
+        try {
+            OptTrace    tracer = OptimizerTrace.getOptimizerTracer();
+            boolean     needsClosing = false;
+
+            PrintWriter pw;
+            if (
+                (configurationParameters != null) &&
+                (configurationParameters.length > 0)
+                )
+            {
+                pw = new PrintWriter( configurationParameters[ 0 ] );
+                needsClosing = true;
+            }
+            else { pw = new PrintWriter( System.out ); }
         
-        if (
-            (configurationParameters != null) &&
-            (configurationParameters.length > 0)
-            )
-        {
-            try {
-                FileWriter    writer = new FileWriter( configurationParameters[ 0 ] );
+            if ( tracer != null )
+            {
+                tracer.printToWriter( pw );
+                pw.flush();
+            }
 
-                writer.write( trace );
-                writer.flush();
-                writer.close();
-            } catch (Exception e) { throw wrap( e ); }
+            if ( needsClosing ) { pw.close(); }
         }
-        else
+        catch (Exception e) { throw wrap( e ); }
+        finally
         {
-            System.out.println( trace );
+            OptimizerTrace.setOptimizerTracer( null );
         }
-
-        OptimizerTrace.setOptimizerTrace( false );
     }
 
     ////////////////////////////////////////////////////////////////////////



Mime
View raw message