You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by be...@apache.org on 2006/01/20 14:35:02 UTC

svn commit: r370806 - in /db/derby/code/trunk/java/drda/org/apache/derby/impl/drda: DRDAConnThread.java NetworkServerControlImpl.java

Author: bernt
Date: Fri Jan 20 05:34:53 2006
New Revision: 370806

URL: http://svn.apache.org/viewcvs?rev=370806&view=rev
Log:
DERBY-815 Prevent unneeded object creation and excessive decoding in parseSQLDTA_work(), submitted by Dyre Tjeldvoll

Modified:
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java?rev=370806&r1=370805&r2=370806&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java Fri Jan 20 05:34:53 2006
@@ -62,7 +62,7 @@
 
 public class DRDAConnThread extends Thread {
 
-	private static final String leftBrace = "{";
+    private static final String leftBrace = "{";
 	private static final String rightBrace = "}";
 	private static final byte NULL_VALUE = (byte)0xff;
 	private static final String SYNTAX_ERR = "42X01";
@@ -160,6 +160,16 @@
 	// public key generated by Deffie-Hellman algorithm, to be passed to the encrypter,
 	// as well as used to initialize the cipher
 	private byte[] myPublicKey;
+
+    // Some byte[] constants that are frequently written into messages. It is more efficient to 
+    // use these constants than to convert from a String each time 
+    // (This replaces the qryscraft_ and notQryscraft_ static exception objects.)
+    private static final byte[] eod00000 = { '0', '0', '0', '0', '0' };
+    private static final byte[] eod02000 = { '0', '2', '0', '0', '0' };
+    private static final byte[] nullSQLState = { ' ', ' ', ' ', ' ', ' ' };
+    private static final byte[] errD4_D6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // 12x0 
+    private static final byte[] warn0_warnA = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };  // 11x ' '
+
 	// constructor
 	/**
 	 * Create a new Thread for processing session requests
@@ -5048,7 +5058,7 @@
 	{
 		writer.createDssObject();
 		writer.startDdm(CodePoint.SQLCARD);
-		writeSQLCAGRP(null, 0, 0, 0);
+        writeSQLCAGRP(nullSQLState, 0, 0, 0);
 		writer.endDdmAndDss();
 	}
 	/**
@@ -5154,39 +5164,28 @@
 	private void writeSQLCAGRP(SQLException e, int sqlcode, int updateCount,
 			long rowCount) throws DRDAProtocolException
 	{
-		String sqlerrmc;
-		String sqlState;
-		SQLException nextException = null;
+        if (e == null) {
+            // Forwarding to the optimized version when there is no
+            // exception object
+            writeSQLCAGRP(nullSQLState, sqlcode, updateCount, rowCount);
+            return;
+        }
 
 		if (rowCount < 0 && updateCount < 0)
 		{
 			writer.writeByte(CodePoint.NULLDATA);
 			return;
 		}
-		if (e == null)
-		{
-			sqlerrmc = null;
-			sqlState = "     "; // set to 5 blanks when no error
-		}
-		else {
-			
-			if (sqlcode < 0)
-			{
-				if (SanityManager.DEBUG && server.debugOutput) 
-				{
-					trace("handle SQLException here");
-					trace("reason is: "+e.getMessage());
-					trace("SQLState is: "+e.getSQLState());
-					trace("vendorCode is: "+e.getErrorCode());
-					trace("nextException is: "+e.getNextException());
-					server.consoleExceptionPrint(e);
-					trace("wrapping SQLException into SQLCARD...");
-				}
-			}
-			sqlState = e.getSQLState();
-			sqlerrmc = buildSqlerrmc(e);
 			
-		}
+        if (SanityManager.DEBUG && server.debugOutput && sqlcode < 0) {
+            trace("handle SQLException here");
+            trace("reason is: "+e.getMessage());
+            trace("SQLState is: "+e.getSQLState());
+            trace("vendorCode is: "+e.getErrorCode());
+            trace("nextException is: "+e.getNextException());
+            server.consoleExceptionPrint(e);
+            trace("wrapping SQLException into SQLCARD...");
+        }
 		
 		//null indicator
 		writer.writeByte(0);
@@ -5195,14 +5194,68 @@
 		writer.writeInt(sqlcode);
 
 		// SQLSTATE
-		writer.writeString(sqlState);
+        writer.writeString(e.getSQLState());
 
 		// SQLERRPROC
-		writer.writeString(server.prdId);
+        // Write the byte[] constant rather than the string, for efficiency
+        writer.writeBytes(server.prdIdBytes_);
 
 		// SQLCAXGRP
-		writeSQLCAXGRP(updateCount, rowCount, sqlerrmc, nextException);
+        writeSQLCAXGRP(updateCount, rowCount, buildSqlerrmc(e), e.getNextException());
 	}
+
+    /**
+     * Same as writeSQLCAGRP, but optimized for the case
+         * when there is no real exception, i.e. the exception is null, or "End
+         * of data"
+     *
+     * SQLCAGRP : FDOCA EARLY GROUP
+     * SQL Communcations Area Group Description
+     *
+     * FORMAT FOR SQLAM <= 6
+     *   SQLCODE; DRDA TYPE I4; ENVLID 0x02; Length Override 4
+     *   SQLSTATE; DRDA TYPE FCS; ENVLID 0x30; Length Override 5
+     *   SQLERRPROC; DRDA TYPE FCS; ENVLID 0x30; Length Override 8
+     *   SQLCAXGRP; DRDA TYPE N-GDA; ENVLID 0x52; Length Override 0
+     *
+     * FORMAT FOR SQLAM >= 7
+     *   SQLCODE; DRDA TYPE I4; ENVLID 0x02; Length Override 4
+     *   SQLSTATE; DRDA TYPE FCS; ENVLID 0x30; Length Override 5
+     *   SQLERRPROC; DRDA TYPE FCS; ENVLID 0x30; Length Override 8
+     *   SQLCAXGRP; DRDA TYPE N-GDA; ENVLID 0x52; Length Override 0
+     *   SQLDIAGGRP; DRDA TYPE N-GDA; ENVLID 0x56; Length Override 0
+     *
+     * @param sqlState     SQLState (already converted to UTF8)
+     * @param sqlcode    sqlcode
+         * @param updateCount
+         * @param rowCount
+     * 
+     * @exception DRDAProtocolException
+     */
+
+    private void writeSQLCAGRP(byte[] sqlState, int sqlcode, 
+                               int updateCount, long rowCount) throws DRDAProtocolException
+    {
+        if (rowCount < 0 && updateCount < 0) {
+            writer.writeByte(CodePoint.NULLDATA);
+            return;
+        }
+        
+        //null indicator
+        writer.writeByte(0);
+        
+        // SQLCODE
+        writer.writeInt(sqlcode);
+
+        // SQLSTATE
+        writer.writeBytes(sqlState);
+
+        // SQLERRPROC
+        writer.writeBytes(server.prdIdBytes_);
+
+        // SQLCAXGRP (Uses null as sqlerrmc since there is no error)
+        writeSQLCAXGRP(updateCount, rowCount, null, null);
+    }
 	
 	
 	// Delimiters for SQLERRMC values.
@@ -5404,12 +5457,9 @@
 		// SQL ERRD3 - updateCount
 		writer.writeInt(updateCount);
 		// SQL ERRD4 - D6 (12 bytes)
-		byte[] byteArray = new byte[1];
-		byteArray[0] = 0;
-		writer.writeScalarPaddedBytes(byteArray, 12, (byte) 0);
+        writer.writeBytes(errD4_D6); // byte[] constant
 		// WARN0-WARNA (11 bytes)
-		byteArray[0] = ' ';  //CLI tests need this to be blank
-		writer.writeScalarPaddedBytes(byteArray, 11, (byte) ' ');
+        writer.writeBytes(warn0_warnA); // byte[] constant
 	}
 
 	/**
@@ -6083,7 +6133,7 @@
 			// Send ResultSet warnings if there are any
 			SQLWarning sqlw = (rs != null)? rs.getWarnings(): null;
 			if (sqlw == null)
-				writeSQLCAGRP(null, 0, -1, -1);
+                writeSQLCAGRP(nullSQLState, 0, -1, -1);
 			else
 				writeSQLCAGRP(sqlw, sqlw.getErrorCode(), 1, -1);
 
@@ -6314,18 +6364,7 @@
 		writer.endDdmAndDss();
 		return true;
 	}
-    
-    // The use of static exception objects is normally a bad idea,
-    // but qryscraft_ and notQryscraft_ are not true exceptions and
-    // will not be thrown. They are being used as containers for
-    // passing data to the writeSQLCAGRP() method, and since this
-    // happens frequently we would like to avoid creating a new
-    // SQLException each time, (which is expensive because the
-    // Throwable ctor will fill in the stack trace).
-    private static final SQLException qryscraft_ = 
-        new SQLException("End of Data", "00000");
-    private static final SQLException notQryscraft_ =
-        new SQLException("End of Data", "02000");
+
 
 	/**
 	 * Done data
@@ -6380,10 +6419,10 @@
 		// We return sqlcode 0 in this case, as the DB2 server does.
 		boolean isQRYSCRAFT = (stmt.getQryscrorn() == CodePoint.QRYSCRAFT);
 
-		// sqlstate 02000 for end of data.
-        writeSQLCAGRP((isQRYSCRAFT ? qryscraft_ : notQryscraft_),
-                      (isQRYSCRAFT ? 0 : 100), 0, stmt.rowCount);
-
+        // Using sqlstate 00000 or 02000 for end of data.
+                writeSQLCAGRP((isQRYSCRAFT ? eod00000 : eod02000),
+                              (isQRYSCRAFT ? 0 : 100), 0, stmt.rowCount);
+                
 		writer.writeByte(CodePoint.NULLDATA);
 		// does all this fit in one QRYDTA
 		if (writer.getOffset() >= blksize)

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java?rev=370806&r1=370805&r2=370806&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java Fri Jan 20 05:34:53 2006
@@ -146,6 +146,8 @@
 	protected static String att_extnam;
 	protected static String att_srvrlslv; 
 	protected static String prdId;
+    protected static byte[] prdIdBytes_;
+    
 	private static String buildNumber;
 	// we will use single or mixed, not double byte to reduce traffic on the
 	// wire, this is in keeping with JCC
@@ -345,7 +347,9 @@
 		
 		prdId += drdaMaintStr;
 		att_srvrlslv = prdId + "/" + myPVH.getVersionBuildString(false);
-		
+                // Precompute this to save some cycles
+                prdIdBytes_ = prdId.getBytes(DEFAULT_ENCODING);
+ 
 		if (SanityManager.DEBUG)
 		{
 			if (majorStr.length() > 2  ||