jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1025643 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join: JoinMerger.java QueryEngine.java
Date Wed, 20 Oct 2010 16:55:11 GMT
Author: jukka
Date: Wed Oct 20 16:55:11 2010
New Revision: 1025643

URL: http://svn.apache.org/viewvc?rev=1025643&view=rev
Log:
JCR-2715: Improved join query performance

Handle query offset and limit only after sorting.

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/JoinMerger.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/JoinMerger.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/JoinMerger.java?rev=1025643&r1=1025642&r2=1025643&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/JoinMerger.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/JoinMerger.java
Wed Oct 20 16:55:11 2010
@@ -140,8 +140,7 @@ abstract class JoinMerger {
         }
     }
 
-    public QueryResult merge(
-            RowIterator leftRows, RowIterator rightRows, long offset, long limit)
+    public QueryResult merge(RowIterator leftRows, RowIterator rightRows)
             throws RepositoryException {
         RowIterator joinRows;
         if (JCR_JOIN_TYPE_RIGHT_OUTER.equals(type)) {
@@ -172,16 +171,6 @@ abstract class JoinMerger {
             boolean outer = JCR_JOIN_TYPE_LEFT_OUTER.equals(type);
             joinRows = mergeLeft(leftRows, map, outer);
         }
-        while ((offset-- > 0 || limit == 0) && joinRows.hasNext()) {
-            joinRows.nextRow();
-        }
-        if (limit > 0) {
-            List<Row> rows = new ArrayList<Row>((int) limit);
-            for (int i = 0; i < limit && joinRows.hasNext(); i++) {
-                rows.add(joinRows.nextRow());
-            }
-            joinRows = new RowIteratorAdapter(rows);
-        }
         return new SimpleQueryResult(columnNames, selectorNames, joinRows);
     }
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java?rev=1025643&r1=1025642&r2=1025643&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java
Wed Oct 20 16:55:11 2010
@@ -216,9 +216,9 @@ public class QueryEngine {
             rightRows = new RowIteratorAdapter(list);
         }
 
-        QueryResult result = merger.merge(
-                new RowIteratorAdapter(leftRows), rightRows, offset, limit);
-        return sort(result, orderings);
+        QueryResult result =
+            merger.merge(new RowIteratorAdapter(leftRows), rightRows);
+        return sort(result, orderings, offset, limit);
     }
 
     private String toSqlConstraint(Constraint constraint)
@@ -357,19 +357,8 @@ public class QueryEngine {
             columnMap.keySet().toArray(new String[columnMap.size()]);
 
         NodeIterator nodes = query.execute().getNodes();
-        while ((offset-- > 0 || limit == 0) && nodes.hasNext()) {
-            nodes.next();
-        }
-        if (limit > 0) {
-            List<Node> list = new ArrayList<Node>((int) limit);
-            for (int i = 0; i < limit && nodes.hasNext(); i++) {
-                list.add(nodes.nextNode());
-            }
-            nodes = new NodeIteratorAdapter(list);
-        }
-
         final String selectorName = selector.getSelectorName();
-        RangeIterator rows = new RangeIteratorAdapter(nodes) {
+        RowIterator rows = new RowIteratorAdapter(nodes) {
             @Override
             public Object next() {
                 Node node = (Node) super.next();
@@ -378,9 +367,9 @@ public class QueryEngine {
             }
         };
 
-        QueryResult result = new SimpleQueryResult(
-                columnNames, selectorNames, new RowIteratorAdapter(rows));
-        return sort(result, orderings);
+        QueryResult result =
+            new SimpleQueryResult(columnNames, selectorNames, rows);
+        return sort(result, orderings, offset, limit);
     }
 
     private Map<String, PropertyValue> getColumnMap(
@@ -451,12 +440,16 @@ public class QueryEngine {
      *
      * @param result original query results
      * @param orderings QOM orderings
+     * @param offset result offset
+     * @param limit result limit
      * @return sorted query results
      * @throws RepositoryException if the results can not be sorted
      */
-    public QueryResult sort(QueryResult result, final Ordering[] orderings)
-            throws RepositoryException {
-        if (orderings != null && orderings.length > 0) {
+    public QueryResult sort(
+            QueryResult result, final Ordering[] orderings,
+            long offset, long limit) throws RepositoryException {
+        if ((orderings != null && orderings.length > 0)
+                || offset != 0 || limit >= 0) {
             List<Row> rows = new ArrayList<Row>();
 
             RowIterator iterator = result.getRows();
@@ -464,7 +457,18 @@ public class QueryEngine {
                 rows.add(iterator.nextRow());
             }
 
-            Collections.sort(rows, new RowComparator(orderings));
+            if (orderings != null && orderings.length > 0) {
+                Collections.sort(rows, new RowComparator(orderings));
+            }
+
+            if (offset != 0 || limit >= 0) {
+                int from = (int) offset;
+                int to = rows.size();
+                if (limit >= 0 && offset + limit < to) {
+                    to = (int) (offset + limit);
+                }
+                rows = rows.subList(from, to);
+            }
 
             return new SimpleQueryResult(
                     result.getColumnNames(), result.getSelectorNames(),



Mime
View raw message