You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2011/06/02 16:39:19 UTC

svn commit: r1130577 - in /incubator/lcf/trunk: ./ framework/core/src/main/java/org/apache/manifoldcf/core/database/ framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/ framework/pull-agent/src/main/java/org/apache/manifoldcf/crawler/re...

Author: kwright
Date: Thu Jun  2 14:39:18 2011
New Revision: 1130577

URL: http://svn.apache.org/viewvc?rev=1130577&view=rev
Log:
Fix for CONNECTORS-205

Modified:
    incubator/lcf/trunk/CHANGES.txt
    incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/BaseTable.java
    incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceDerby.java
    incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceHSQLDB.java
    incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceMySQL.java
    incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfacePostgreSQL.java
    incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/IDBInterface.java
    incubator/lcf/trunk/framework/pull-agent/src/main/java/org/apache/manifoldcf/crawler/repository/RepositoryHistoryManager.java

Modified: incubator/lcf/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/CHANGES.txt?rev=1130577&r1=1130576&r2=1130577&view=diff
==============================================================================
--- incubator/lcf/trunk/CHANGES.txt (original)
+++ incubator/lcf/trunk/CHANGES.txt Thu Jun  2 14:39:18 2011
@@ -3,6 +3,11 @@ $Id$
 
 ======================= 0.3-dev =========================
 
+CONNECTORS-205: Changed the database interface distinct-on abstraction
+to also include the order-by clause, so we can write something that will
+work for HSQLDB.
+(Karl Wright)
+
 CONNECTORS-203: Move to Java 1.5 compilation and conventions.
 (Erlend GarĂ¥sen, Tommaso Teofili, Karl Wright et al)
 

Modified: incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/BaseTable.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/BaseTable.java?rev=1130577&r1=1130576&r2=1130577&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/BaseTable.java (original)
+++ incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/BaseTable.java Thu Jun  2 14:39:18 2011
@@ -335,16 +335,20 @@ public class BaseTable
   * Specifically, for each combination of the specified distinct fields in the result, only the first such row is included in the final
   * result.
   *@param outputParameters is a blank list into which to put parameters.  Null may be used if the baseParameters parameter is null.
-  *@param baseQuery is the base query, which can either be tables and where clause, or can be another SELECT in parens,
-  * e.g. "(SELECT ...) t3"
+  *@param baseQuery is the base query, which is another SELECT statement, without parens,
+  * e.g. "SELECT ..."
   *@param baseParameters are the parameters corresponding to the baseQuery.
-  *@param distinctFields are the fields to consider to be distinct.
-  *@param otherFields are the rest of the fields to return, keyed by the AS name, value being the column value, e.g. "value AS key"
+  *@param distinctFields are the fields to consider to be distinct.  These should all be keys in otherFields below.
+  *@param orderFields are the otherfield keys that determine the ordering.
+  *@param orderFields ascending are true for orderFields that are ordered as ASC, false for DESC.  
+  *@param otherFields are the rest of the fields to return, keyed by the AS name, value being the base query column value, e.g. "value AS key"
   *@return a revised query that performs the necessary DISTINCT ON operation.  The list outputParameters will also be appropriately filled in.
   */
-  public String constructDistinctOnClause(List outputParameters, String baseQuery, List baseParameters, String[] distinctFields, Map otherFields)
+  public String constructDistinctOnClause(List outputParameters, String baseQuery, List baseParameters,
+    String[] distinctFields, String[] orderFields, boolean[] orderFieldsAscending, Map<String,String> otherFields)
   {
-    return dbInterface.constructDistinctOnClause(outputParameters,baseQuery,baseParameters,distinctFields,otherFields);
+    return dbInterface.constructDistinctOnClause(outputParameters,baseQuery,baseParameters,distinctFields,
+      orderFields,orderFieldsAscending,otherFields);
   }
 
   /** Obtain the maximum number of individual items that should be

Modified: incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceDerby.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceDerby.java?rev=1130577&r1=1130576&r2=1130577&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceDerby.java (original)
+++ incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceDerby.java Thu Jun  2 14:39:18 2011
@@ -1084,11 +1084,13 @@ public class DBInterfaceDerby extends Da
   * e.g. "SELECT ..."
   *@param baseParameters are the parameters corresponding to the baseQuery.
   *@param distinctFields are the fields to consider to be distinct.  These should all be keys in otherFields below.
+  *@param orderFields are the otherfield keys that determine the ordering.
+  *@param orderFields ascending are true for orderFields that are ordered as ASC, false for DESC.  
   *@param otherFields are the rest of the fields to return, keyed by the AS name, value being the base query column value, e.g. "value AS key"
   *@return a revised query that performs the necessary DISTINCT ON operation.  The list outputParameters will also be appropriately filled in.
   */
-  public String constructDistinctOnClause(List outputParameters, String baseQuery, List baseParameters, String[] distinctFields,
-    Map<String,String> otherFields)
+  public String constructDistinctOnClause(List outputParameters, String baseQuery, List baseParameters,
+    String[] distinctFields, String[] orderFields, boolean[] orderFieldsAscending, Map<String,String> otherFields)
   {
     // Derby does not really support this functionality.
     // We could hack a workaround, along the following lines:
@@ -1124,6 +1126,33 @@ public class DBInterfaceDerby extends Da
       sb.append("txxx1.").append(columnValue).append(" AS ").append(fieldName);
     }
     sb.append(" FROM (").append(baseQuery).append(") txxx1");
+    if (distinctFields.length > 0 || orderFields.length > 0)
+    {
+      sb.append(" ORDER BY ");
+      int k = 0;
+      int i = 0;
+      while (i < distinctFields.length)
+      {
+        if (k > 0)
+          sb.append(",");
+        sb.append(distinctFields[i]).append(" ASC");
+        k++;
+        i++;
+      }
+      i = 0;
+      while (i < orderFields.length)
+      {
+        if (k > 0)
+          sb.append(",");
+        sb.append(orderFields[i]).append(" ");
+        if (orderFieldsAscending[i])
+          sb.append("ASC");
+        else
+          sb.append("DESC");
+        i++;
+        k++;
+      }
+    }
     return sb.toString();
   }
 

Modified: incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceHSQLDB.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceHSQLDB.java?rev=1130577&r1=1130576&r2=1130577&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceHSQLDB.java (original)
+++ incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceHSQLDB.java Thu Jun  2 14:39:18 2011
@@ -885,29 +885,27 @@ public class DBInterfaceHSQLDB extends D
   * e.g. "SELECT ..."
   *@param baseParameters are the parameters corresponding to the baseQuery.
   *@param distinctFields are the fields to consider to be distinct.  These should all be keys in otherFields below.
+  *@param orderFields are the otherfield keys that determine the ordering.
+  *@param orderFields ascending are true for orderFields that are ordered as ASC, false for DESC.  
   *@param otherFields are the rest of the fields to return, keyed by the AS name, value being the base query column value, e.g. "value AS key"
   *@return a revised query that performs the necessary DISTINCT ON operation.  The list outputParameters will also be appropriately filled in.
   */
   public String constructDistinctOnClause(List outputParameters, String baseQuery, List baseParameters,
-    String[] distinctFields, Map<String,String> otherFields)
+    String[] distinctFields, String[] orderFields, boolean[] orderFieldsAscending, Map<String,String> otherFields)
   {
-    // HSQLDB does not really support this functionality.
-    // We could hack a workaround, along the following lines:
+    // For HSQLDB, we want to generate the following:
+    // WITH ct01 ( ... otherfields ... ) AS ( ... baseQuery ... )
+    //   SELECT * FROM (SELECT DISTINCT ... distinctFields ... FROM ct01) AS ct02,
+    //   LATERAL ( SELECT ... otherfields ... FROM ct01 WHERE ... distinctFields = ct02.distinctField ... ORDER BY ??? LIMIT 1) AS ct03
     //
-    // SELECT
-    //   t1.bucket, t1.bytecount, t1.windowstart, t1.windowend
-    // FROM
-    //   (xxx) t1
-    // WHERE
-    //   t1.bytecount=( SELECT t2.bytecount FROM (xxx) t2 WHERE
-    //     t2.bucket = t1.bucket LIMIT 1 ) AND
-    //   t1.windowstart=( SELECT t2.windowstart FROM (xxx) t2 WHERE
-    //     t2.bucket = t1.bucket LIMIT 1 ) AND
-    //   t1.windowend=( SELECT t2.windowend FROM (xxx) t2 WHERE
-    //     t2.bucket = t1.bucket LIMIT 1 )
+    // The problem is that this requires the ORDER BY to not appear in the base query but appear in the LATERAL clause.  The
+    // only solution involves pulling the ORDER BY into this abstraction, so that the base query does not include it and it gets added to
+    // the end in the appropriate place by this method.
     //
-    // However, the cost of doing 3 identical and very costly queries is likely to be too high for this to be viable.
-
+    // If we change the abstraction in that way, the ordering criteria should be a List of column names, and a parallel List of Booleans.
+    // We'll have to be very careful to apply the otherfields mapping to the temporary table also, and the order-by will need to be in terms
+    // of the TARGET column names, not the source column names.
+    
     // Copy arguments
     if (baseParameters != null)
       outputParameters.addAll(baseParameters);
@@ -925,6 +923,33 @@ public class DBInterfaceHSQLDB extends D
       sb.append("txxx1.").append(columnValue).append(" AS ").append(fieldName);
     }
     sb.append(" FROM (").append(baseQuery).append(") txxx1");
+    if (distinctFields.length > 0 || orderFields.length > 0)
+    {
+      sb.append(" ORDER BY ");
+      int k = 0;
+      int i = 0;
+      while (i < distinctFields.length)
+      {
+        if (k > 0)
+          sb.append(",");
+        sb.append(distinctFields[i]).append(" ASC");
+        k++;
+        i++;
+      }
+      i = 0;
+      while (i < orderFields.length)
+      {
+        if (k > 0)
+          sb.append(",");
+        sb.append(orderFields[i]).append(" ");
+        if (orderFieldsAscending[i])
+          sb.append("ASC");
+        else
+          sb.append("DESC");
+        i++;
+        k++;
+      }
+    }
     return sb.toString();
   }
 

Modified: incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceMySQL.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceMySQL.java?rev=1130577&r1=1130576&r2=1130577&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceMySQL.java (original)
+++ incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfaceMySQL.java Thu Jun  2 14:39:18 2011
@@ -598,11 +598,13 @@ public class DBInterfaceMySQL extends Da
   * e.g. "SELECT ..."
   *@param baseParameters are the parameters corresponding to the baseQuery.
   *@param distinctFields are the fields to consider to be distinct.  These should all be keys in otherFields below.
+  *@param orderFields are the otherfield keys that determine the ordering.
+  *@param orderFields ascending are true for orderFields that are ordered as ASC, false for DESC.  
   *@param otherFields are the rest of the fields to return, keyed by the AS name, value being the base query column value, e.g. "value AS key"
   *@return a revised query that performs the necessary DISTINCT ON operation.  The list outputParameters will also be appropriately filled in.
   */
   public String constructDistinctOnClause(List outputParameters, String baseQuery, List baseParameters,
-    String[] distinctFields, Map<String,String> otherFields)
+    String[] distinctFields, String[] orderFields, boolean[] orderFieldsAscending, Map<String,String> otherFields)
   {
     // I don't know whether MySql supports this functionality or not.
     // MHL
@@ -623,6 +625,33 @@ public class DBInterfaceMySQL extends Da
       sb.append("txxx1.").append(columnValue).append(" AS ").append(fieldName);
     }
     sb.append(" FROM (").append(baseQuery).append(") txxx1");
+    if (distinctFields.length > 0 || orderFields.length > 0)
+    {
+      sb.append(" ORDER BY ");
+      int k = 0;
+      int i = 0;
+      while (i < distinctFields.length)
+      {
+        if (k > 0)
+          sb.append(",");
+        sb.append(distinctFields[i]).append(" ASC");
+        k++;
+        i++;
+      }
+      i = 0;
+      while (i < orderFields.length)
+      {
+        if (k > 0)
+          sb.append(",");
+        sb.append(orderFields[i]).append(" ");
+        if (orderFieldsAscending[i])
+          sb.append("ASC");
+        else
+          sb.append("DESC");
+        i++;
+        k++;
+      }
+    }
     return sb.toString();
   }
 

Modified: incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfacePostgreSQL.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfacePostgreSQL.java?rev=1130577&r1=1130576&r2=1130577&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfacePostgreSQL.java (original)
+++ incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/database/DBInterfacePostgreSQL.java Thu Jun  2 14:39:18 2011
@@ -931,11 +931,13 @@ public class DBInterfacePostgreSQL exten
   * e.g. "SELECT ..."
   *@param baseParameters are the parameters corresponding to the baseQuery.
   *@param distinctFields are the fields to consider to be distinct.  These should all be keys in otherFields below.
+  *@param orderFields are the otherfield keys that determine the ordering.
+  *@param orderFields ascending are true for orderFields that are ordered as ASC, false for DESC.  
   *@param otherFields are the rest of the fields to return, keyed by the AS name, value being the base query column value, e.g. "value AS key"
   *@return a revised query that performs the necessary DISTINCT ON operation.  The list outputParameters will also be appropriately filled in.
   */
   public String constructDistinctOnClause(List outputParameters, String baseQuery, List baseParameters,
-    String[] distinctFields, Map<String,String> otherFields)
+    String[] distinctFields, String[] orderFields, boolean[] orderFieldsAscending, Map<String,String> otherFields)
   {
     // Copy arguments
     if (baseParameters != null)
@@ -962,6 +964,33 @@ public class DBInterfacePostgreSQL exten
       sb.append("txxx1.").append(columnValue).append(" AS ").append(fieldName);
     }
     sb.append(" FROM (").append(baseQuery).append(") txxx1");
+    if (distinctFields.length > 0 || orderFields.length > 0)
+    {
+      sb.append(" ORDER BY ");
+      int k = 0;
+      i = 0;
+      while (i < distinctFields.length)
+      {
+        if (k > 0)
+          sb.append(",");
+        sb.append(distinctFields[i]).append(" ASC");
+        k++;
+        i++;
+      }
+      i = 0;
+      while (i < orderFields.length)
+      {
+        if (k > 0)
+          sb.append(",");
+        sb.append(orderFields[i]).append(" ");
+        if (orderFieldsAscending[i])
+          sb.append("ASC");
+        else
+          sb.append("DESC");
+        i++;
+        k++;
+      }
+    }
     return sb.toString();
   }
 

Modified: incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/IDBInterface.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/IDBInterface.java?rev=1130577&r1=1130576&r2=1130577&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/IDBInterface.java (original)
+++ incubator/lcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/IDBInterface.java Thu Jun  2 14:39:18 2011
@@ -294,11 +294,13 @@ public interface IDBInterface
   * e.g. "SELECT ..."
   *@param baseParameters are the parameters corresponding to the baseQuery.
   *@param distinctFields are the fields to consider to be distinct.  These should all be keys in otherFields below.
+  *@param orderFields are the otherfield keys that determine the ordering.
+  *@param orderFields ascending are true for orderFields that are ordered as ASC, false for DESC.  
   *@param otherFields are the rest of the fields to return, keyed by the AS name, value being the base query column value, e.g. "value AS key"
   *@return a revised query that performs the necessary DISTINCT ON operation.  The list outputParameters will also be appropriately filled in.
   */
   public String constructDistinctOnClause(List outputParameters, String baseQuery, List baseParameters,
-    String[] distinctFields, Map<String,String> otherFields);
+    String[] distinctFields, String[] orderFields, boolean[] orderFieldsAscending, Map<String,String> otherFields);
   
   /** Obtain the maximum number of individual items that should be
   * present in an IN clause.  Exceeding this amount will potentially cause the query performance

Modified: incubator/lcf/trunk/framework/pull-agent/src/main/java/org/apache/manifoldcf/crawler/repository/RepositoryHistoryManager.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/pull-agent/src/main/java/org/apache/manifoldcf/crawler/repository/RepositoryHistoryManager.java?rev=1130577&r1=1130576&r2=1130577&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/pull-agent/src/main/java/org/apache/manifoldcf/crawler/repository/RepositoryHistoryManager.java (original)
+++ incubator/lcf/trunk/framework/pull-agent/src/main/java/org/apache/manifoldcf/crawler/repository/RepositoryHistoryManager.java Thu Jun  2 14:39:18 2011
@@ -401,7 +401,7 @@ public class RepositoryHistoryManager ex
     sb.append(" AND t1a.").append(startTimeField).append("<t0a.").append(endTimeField)
       .append(" AND t1a.").append(endTimeField).append(">t0a.").append(endTimeField).append("-").append(intervalString);
     addCriteria(sb,list,"t1a.",connectionName,filterCriteria,true);
-    sb.append(") t6a GROUP BY bucket,windowstart,windowend) t2 ORDER BY bucket ASC, activitycount DESC");
+    sb.append(") t6a GROUP BY bucket,windowstart,windowend) t2");
 
     Map otherColumns = new HashMap();
     otherColumns.put("idbucket","bucket");
@@ -411,7 +411,8 @@ public class RepositoryHistoryManager ex
     
     StringBuilder newsb = new StringBuilder("SELECT * FROM (");
     ArrayList newList = new ArrayList();
-    newsb.append(constructDistinctOnClause(newList,sb.toString(),list,new String[]{"idbucket"},otherColumns)).append(") t4");
+    newsb.append(constructDistinctOnClause(newList,sb.toString(),list,new String[]{"idbucket"},
+      new String[]{"activitycount"},new boolean[]{false},otherColumns)).append(") t4");
     addOrdering(newsb,new String[]{"activitycount","starttime","endtime","idbucket"},sort);
     addLimits(newsb,startRow,maxRowCount);
     return performQuery(newsb.toString(),newList,null,null,maxRowCount);
@@ -509,7 +510,7 @@ public class RepositoryHistoryManager ex
     sb.append(" AND t1a.").append(startTimeField).append("<t0a.").append(endTimeField)
       .append(" AND t1a.").append(endTimeField).append(">t0a.").append(endTimeField).append("-").append(intervalString);
     addCriteria(sb,list,"t1a.",connectionName,filterCriteria,true);
-    sb.append(") t6a GROUP BY bucket,windowstart,windowend) t2 ORDER BY bucket ASC, bytecount DESC");
+    sb.append(") t6a GROUP BY bucket,windowstart,windowend) t2");
     
     Map otherColumns = new HashMap();
     otherColumns.put("idbucket","bucket");
@@ -518,7 +519,8 @@ public class RepositoryHistoryManager ex
     otherColumns.put("endtime","windowend");
     StringBuilder newsb = new StringBuilder("SELECT * FROM (");
     ArrayList newList = new ArrayList();
-    newsb.append(constructDistinctOnClause(newList,sb.toString(),list,new String[]{"idbucket"},otherColumns)).append(") t4");
+    newsb.append(constructDistinctOnClause(newList,sb.toString(),list,new String[]{"idbucket"},
+      new String[]{"bytecount"},new boolean[]{false},otherColumns)).append(") t4");
     addOrdering(newsb,new String[]{"bytecount","starttime","endtime","idbucket"},sort);
     addLimits(newsb,startRow,maxRowCount);
     return performQuery(newsb.toString(),newList,null,null,maxRowCount);