db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1141005 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql: catalog/DataDictionaryImpl.java compile/CreateTriggerNode.java
Date Wed, 29 Jun 2011 08:44:28 GMT
Author: kahatlen
Date: Wed Jun 29 08:44:28 2011
New Revision: 1141005

URL: http://svn.apache.org/viewvc?rev=1141005&view=rev
Log:
DERBY-5293: Replace bubble sort in DataDictionaryImpl and CreateTriggerNode with Collections.sort()

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateTriggerNode.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java?rev=1141005&r1=1141004&r2=1141005&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/DataDictionaryImpl.java
Wed Jun 29 08:44:28 2011
@@ -155,6 +155,8 @@ import org.apache.derby.iapi.util.IdUtil
 
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.GregorianCalendar;
 import java.util.Hashtable;
 import java.util.HashMap;
@@ -4684,6 +4686,20 @@ public final class	DataDictionaryImpl
 		return list;
 	}
 
+    /**
+     * Comparator that can be used for sorting lists of column references
+     * on the position they have in the SQL query string.
+     */
+    private static final Comparator OFFSET_COMPARATOR = new Comparator() {
+        public int compare(Object o1, Object o2) {
+            // Return negative int, zero, or positive int if the first column
+            // reference has an offset which is smaller than, equal to, or
+            // greater than the offset of the second column reference.
+            return ((ColumnReference) o1).getBeginOffset() -
+                    ((ColumnReference) o2).getBeginOffset();
+        }
+    };
+
 	/**
 	 * Get the trigger action string associated with the trigger after the
 	 * references to old/new transition tables/variables in trigger action
@@ -4818,7 +4834,7 @@ public final class	DataDictionaryImpl
 		Vector refs = visitor.getList();
 		/* we need to sort on position in string, beetle 4324
 		 */
-		QueryTreeNode[] cols = sortRefs(refs, true);
+		Collections.sort(refs, OFFSET_COMPARATOR);
 		
 		if (createTriggerTime) {
 			//The purpose of following array(triggerActionColsOnly) is to
@@ -4876,9 +4892,9 @@ public final class	DataDictionaryImpl
 			//in next version of 10.7 and 10.8. In 10.9, DERBY-1482 was
 			//reimplemented correctly and we started doing the collection and
 			//usage of trigger action columns again in 10.9
-			for (int i = 0; i < cols.length; i++)
+			for (int i = 0; i < refs.size(); i++)
 			{
-				ColumnReference ref = (ColumnReference) cols[i];
+				ColumnReference ref = (ColumnReference) refs.get(i);
 				/*
 				** Only occurrences of those OLD/NEW transition tables/variables 
 				** are of interest here.  There may be intermediate nodes in the 
@@ -4992,9 +5008,9 @@ public final class	DataDictionaryImpl
 		// turns into
 		//	DELETE FROM t WHERE c in 
 		//		(SELECT c FROM new TriggerOldTransitionTable OLD)
-		for (int i = 0; i < cols.length; i++)
+		for (int i = 0; i < refs.size(); i++)
 		{
-			ColumnReference ref = (ColumnReference) cols[i];				
+			ColumnReference ref = (ColumnReference) refs.get(i);
 			/*
 			** Only occurrences of those OLD/NEW transition tables/variables 
 			** are of interest here.  There may be intermediate nodes in the 
@@ -5266,43 +5282,6 @@ public final class	DataDictionaryImpl
 	    }
 	}
 
-	/*
-	** Sort the refs into array.
-	*/
-	private QueryTreeNode[] sortRefs(Vector refs, boolean isRow)
-	{
-		int size = refs.size();
-		QueryTreeNode[] sorted = (QueryTreeNode[])refs.toArray(new QueryTreeNode[size]);
-		int i = 0;
-		/* bubble sort
-		 */
-		QueryTreeNode temp;
-		for (i = 0; i < size - 1; i++)
-		{
-			temp = null;
-			for (int j = 0; j < size - i - 1; j++)
-			{
-				if ((isRow && 
-					 sorted[j].getBeginOffset() > 
-					 sorted[j+1].getBeginOffset()
-					) ||
-					(!isRow &&
-					 ((FromBaseTable) sorted[j]).getTableNameField().getBeginOffset() > 
-					 ((FromBaseTable) sorted[j+1]).getTableNameField().getBeginOffset()
-					))
-				{
-					temp = sorted[j];
-					sorted[j] = sorted[j+1];
-					sorted[j+1] = temp;
-				}
-			}
-			if (temp == null)		// sorted
-				break;
-		}
-
-		return sorted;
-	}
-
 	/**
 	 * Get a TriggerDescriptor given its UUID.
 	 *

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateTriggerNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateTriggerNode.java?rev=1141005&r1=1141004&r2=1141005&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateTriggerNode.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateTriggerNode.java
Wed Jun 29 08:44:28 2011
@@ -22,6 +22,8 @@
 package	org.apache.derby.impl.sql.compile;
 
 import java.sql.Timestamp;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Vector;
@@ -454,6 +456,20 @@ public class CreateTriggerNode extends D
 		return (isSessionSchema(triggerTableDescriptor.getSchemaName()) || actionNode.referencesSessionSchema());
 	}
 
+    /**
+     * Comparator that can be used for sorting lists of FromBaseTables
+     * on the position they have in the SQL query string.
+     */
+    private static final Comparator OFFSET_COMPARATOR = new Comparator() {
+        public int compare(Object o1, Object o2) {
+            // Return negative int, zero, or positive int if the offset of the
+            // first table is less than, equal to, or greater than the offset
+            // of the second table.
+            return ((FromBaseTable) o1).getTableNameField().getBeginOffset() -
+                    ((FromBaseTable) o2).getTableNameField().getBeginOffset();
+        }
+    };
+
 	/*
 	** BIND OLD/NEW TRANSITION TABLES/VARIABLES AND collect TRIGGER ACTION
 	** COLUMNS referenced through REFERECING CLAUSE in CREATE TRIGGER statement
@@ -587,11 +603,11 @@ public class CreateTriggerNode extends D
 			*/
 			CollectNodesVisitor visitor = new CollectNodesVisitor(FromBaseTable.class);
 			actionNode.accept(visitor);
-			Vector refs = visitor.getList();
-			QueryTreeNode[] tabs = sortRefs(refs, false);
-			for (int i = 0; i < tabs.length; i++)
+			Vector tabs = visitor.getList();
+			Collections.sort(tabs, OFFSET_COMPARATOR);
+			for (int i = 0; i < tabs.size(); i++)
 			{
-				FromBaseTable fromTable = (FromBaseTable) tabs[i];
+				FromBaseTable fromTable = (FromBaseTable) tabs.get(i);
 				String refTableName = fromTable.getTableName().getTableName();
 				String baseTableName = fromTable.getBaseTableName();
 				if ((baseTableName == null) ||
@@ -684,43 +700,6 @@ public class CreateTriggerNode extends D
 			return null;
 	}
 
-	/*
-	** Sort the refs into array.
-	*/
-	private QueryTreeNode[] sortRefs(Vector refs, boolean isRow)
-	{
-		int size = refs.size();
-		QueryTreeNode[] sorted = (QueryTreeNode[]) refs.toArray(new QueryTreeNode[size]);
-		int i = 0;
-		/* bubble sort
-		 */
-		QueryTreeNode temp;
-		for (i = 0; i < size - 1; i++)
-		{
-			temp = null;
-			for (int j = 0; j < size - i - 1; j++)
-			{
-				if ((isRow && 
-					 sorted[j].getBeginOffset() > 
-					 sorted[j+1].getBeginOffset()
-					) ||
-					(!isRow &&
-					 ((FromBaseTable) sorted[j]).getTableNameField().getBeginOffset() > 
-					 ((FromBaseTable) sorted[j+1]).getTableNameField().getBeginOffset()
-					))
-				{
-					temp = sorted[j];
-					sorted[j] = sorted[j+1];
-					sorted[j+1] = temp;
-				}
-			}
-			if (temp == null)		// sorted
-				break;
-		}
-
-		return sorted;
-	}
-
     /*
      * Forbid references to generated columns in the actions of BEFORE triggers.
      * This is DERBY-3948, enforcing the following section of the SQL standard:



Mime
View raw message