Author: thomasm
Date: Thu Apr  5 09:20:55 2018
New Revision: 1828405

URL: http://svn.apache.org/viewvc?rev=1828405&view=rev
Log:
OAK-7390 QueryResult.getSize() can be slow for many or or union conditions

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryImpl.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/UnionQueryImpl.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/JoinImpl.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SelectorImpl.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SourceImpl.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryImpl.java?rev=1828405&r1=1828404&r2=1828405&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryImpl.java
 Thu Apr  5 09:20:55 2018
@@ -1261,7 +1261,7 @@ public class QueryImpl implements Query
             // "order by" was used, so we know the size
             return size;
         }
-        return Math.min(limit, source.getSize(precision, max));
+        return Math.min(limit, source.getSize(context.getBaseState(), 
precision, max));
     }
 
     @Override

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/UnionQueryImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/UnionQueryImpl.java?rev=1828405&r1=1828404&r2=1828405&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/UnionQueryImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/UnionQueryImpl.java
 Thu Apr  5 09:20:55 2018
@@ -173,14 +173,15 @@ public class UnionQueryImpl implements Q
     public long getSize(SizePrecision precision, long max) {
         // Note: for "unionAll == false", overlapping entries are counted twice
         // (this can result in a larger reported size, but it is not a 
security problem)
-        
-        // ensure the queries are both executed, otherwise the cursor is not 
set,
-        // and so the size would be -1
-        left.executeQuery().getRows().iterator().hasNext();
-        right.executeQuery().getRows().iterator().hasNext();
         long a = left.getSize(precision, max);
+        if (a < 0) {
+            return -1;
+        }
+        if (a >= limit) {
+            return limit;
+        }
         long b = right.getSize(precision, max);
-        if (a < 0 || b < 0) {
+        if (b < 0) {
             return -1;
         }
         long total = QueryImpl.saturatedAdd(a, b);

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/JoinImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/JoinImpl.java?rev=1828405&r1=1828404&r2=1828405&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/JoinImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/JoinImpl.java
 Thu Apr  5 09:20:55 2018
@@ -281,7 +281,7 @@ public class JoinImpl extends SourceImpl
     }
 
     @Override
-    public long getSize(SizePrecision precision, long max) {
+    public long getSize(NodeState rootState, SizePrecision precision, long 
max) {
         // we don't know
         return -1;
     }

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SelectorImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SelectorImpl.java?rev=1828405&r1=1828404&r2=1828405&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SelectorImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SelectorImpl.java
 Thu Apr  5 09:20:55 2018
@@ -806,9 +806,9 @@ public class SelectorImpl extends Source
     }
 
     @Override
-    public long getSize(SizePrecision precision, long max) {
+    public long getSize(NodeState rootState, SizePrecision precision, long 
max) {
         if (cursor == null) {
-            return -1;
+            execute(rootState);
         }
         return cursor.getSize(precision, max);
     }

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SourceImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SourceImpl.java?rev=1828405&r1=1828404&r2=1828405&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SourceImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SourceImpl.java
 Thu Apr  5 09:20:55 2018
@@ -165,9 +165,10 @@ public abstract class SourceImpl extends
     /**
      * Get the size if known.
      * 
+     * @param rootState the root node state (to initialize the cursor, if 
needed)
      * @param precision the required precision
      * @param max the maximum nodes read (for an exact size)
      * @return the size, or -1 if unknown
      */
-    public abstract long getSize(SizePrecision precision, long max);
+    public abstract long getSize(NodeState rootState, SizePrecision precision, 
long max);
 }


Reply via email to