db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mi...@apache.org
Subject svn commit: r634436 - /db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/sort/UniqueWithDuplicateNullsMergeSort.java
Date Thu, 06 Mar 2008 22:35:31 GMT
Author: mikem
Date: Thu Mar  6 14:35:30 2008
New Revision: 634436

URL: http://svn.apache.org/viewvc?rev=634436&view=rev
Log:
DERBY-3330

contributed by Anurag Shekhar
committing a modified version of sortercomments.diff patch.  This change just
updates the comments associated with the class and routine in this file - no
code changes.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/sort/UniqueWithDuplicateNullsMergeSort.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/sort/UniqueWithDuplicateNullsMergeSort.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/sort/UniqueWithDuplicateNullsMergeSort.java?rev=634436&r1=634435&r2=634436&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/sort/UniqueWithDuplicateNullsMergeSort.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/sort/UniqueWithDuplicateNullsMergeSort.java
Thu Mar  6 14:35:30 2008
@@ -25,18 +25,29 @@
 import org.apache.derby.iapi.types.DataValueDescriptor;
 
 /**
- * This class extends and customizes MergeSort to support almost unique index.
+ * This class extends and customizes MergeSort to support unique indexes with
+ * duplicate nulls.
  * It overrides compare method to consider keypart - 1 parts of the keys while
  * comparing (only for non null keys).
  */
 final class UniqueWithDuplicateNullsMergeSort extends MergeSort {
     
     /**
-     * Compares two sets of keys. If all the parts of the keys are not null
-     * keys.length - 1 part is compared other wise all the parts are compared.
-     * This methods assumes that last part is location.
+     * Compares two keys. 
+     *
+     * If all the parts of the keys are not null then the leading 
+     * (keys.length - 1) parts are compared, else if no part of the key
+     * is null then all parts of the key are compared (keys.length).
+     *
+     * This behavior is useful for implementing unique constraints where
+     * multiple null values are allowed, but uniqueness must still be 
+     * guaranteed for keys with no null values.   In this case the leading
+     * parts of the key are the user key columns, while the last column
+     * is a system provided column which is guaranteed unique per base row.
+     *
      * @param r1 keys 
      * @param r2 keys
+     *
      * @return 0 for duplicates non zero for distinct keys 
      */
     protected int compare(DataValueDescriptor[] r1, DataValueDescriptor[] r2)
@@ -45,12 +56,24 @@
         int colsToCompare = columnOrdering.length;
         int r;
 
-        // Compare the columns specified in the column
-        // ordering array.
+        // Compare the columns specified in the column ordering array.
         boolean nonull = true;
         for (int i = 0; i < colsToCompare; i++) {
+            //if there are any nulls in the row nonull will be false
+            //
+            //if there was no nulls in the row and we are about to 
+            //compare the last field (all fields except for the location
+            //are same), treat them as duplicate.   This is used by caller
+            //to implement unique key while ignoring case of keys with
+            //null values.
+            //
+            //if at least one field was null, go ahead and compare the 
+            //location too.  This is used to provide proper sorting of
+            //duplicate keys with nulls, they must be ordered properly 
+            //according to the last field also.
             if (i == colsToCompare - 1 && nonull)
                 return 0;
+
             // Get columns to compare.
             int colid = columnOrderingMap[i];
             boolean nullsLow = columnOrderingNullsLowMap[i];
@@ -64,6 +87,7 @@
                 else
                     return -r;
             } else {
+                //set nonull to false if the fields are equal and null
                 if (r1[colid].isNull())
                     nonull = false;
             }
@@ -73,5 +97,4 @@
         // all compared equal.  So return that the rows compare equal.
         return 0;
     }
-    
 }



Mime
View raw message