jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1431411 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/index/p2/ main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/ test/java/org/apache/jackrabbit/oak/plugins/index/p2/
Date Thu, 10 Jan 2013 15:18:25 GMT
Author: thomasm
Date: Thu Jan 10 15:18:24 2013
New Revision: 1431411

URL: http://svn.apache.org/viewvc?rev=1431411&view=rev
Log:
OAK-547 Property2Index: getCost reads all nodes

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexLookup.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/ContentMirrorStoreStrategy.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/IndexStoreStrategy.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexLookup.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexLookup.java?rev=1431411&r1=1431410&r2=1431411&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexLookup.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexLookup.java
Thu Jan 10 15:18:24 2013
@@ -52,6 +52,8 @@ import org.apache.jackrabbit.oak.spi.sta
  * </pre>
  */
 public class Property2IndexLookup {
+    
+    private static final int MAX_COST = 100;
 
     private final IndexStoreStrategy store = new ContentMirrorStoreStrategy();
 
@@ -99,19 +101,12 @@ public class Property2IndexLookup {
     }
 
     public double getCost(String name, PropertyValue value) {
-        // TODO the cost method is currently reading all the data - 
-        // is not supposed to do that, it is only supposed to estimate
         NodeState state = getIndexDataNode(root, name);
         if (state == null) {
             return Double.POSITIVE_INFINITY;
         }
-        double cost;
-        if (value == null) {
-            cost = store.count(state, null);
-        } else {
-            cost = store.count(state, Property2Index.encode(value));
-        }
-        return cost;
+        Iterable<String> it = value == null ? null : Property2Index.encode(value);
+        return store.count(state, it, MAX_COST);
     }
 
     /**

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/ContentMirrorStoreStrategy.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/ContentMirrorStoreStrategy.java?rev=1431411&r1=1431410&r2=1431411&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/ContentMirrorStoreStrategy.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/ContentMirrorStoreStrategy.java
Thu Jan 10 15:18:24 2013
@@ -124,7 +124,7 @@ public class ContentMirrorStoreStrategy 
             }
             indexEntry.setProperty("match", true);
         }
-        long matchCount = countMatchingLeaves(child.getNodeState());
+        long matchCount = countMatchingLeaves(child.getNodeState(), 0, 2);
         if (matchCount == 0) {
             index.removeNode(key);
         } else if (unique && matchCount > 1) {
@@ -132,13 +132,18 @@ public class ContentMirrorStoreStrategy 
         }
     }
 
-    static int countMatchingLeaves(NodeState state) {
-        int count = 0;
+    static int countMatchingLeaves(NodeState state, int initialCount, int max) {
+        int count = initialCount;
         if (state.getProperty("match") != null) {
             count++;
         }
-        for (ChildNodeEntry entry : state.getChildNodeEntries()) {
-            count += countMatchingLeaves(entry.getNodeState());
+        if (count < max) {
+            for (ChildNodeEntry entry : state.getChildNodeEntries()) {
+                if (count >= max) {
+                    break;
+                }
+                count = countMatchingLeaves(entry.getNodeState(), count, max);
+            }
         }
         return count;
     }
@@ -169,15 +174,18 @@ public class ContentMirrorStoreStrategy 
     }
     
     @Override
-    public int count(NodeState index, Iterable<String> values) {
+    public int count(NodeState index, Iterable<String> values, int max) {
         int count = 0;
         if (values == null) {
-            count += countMatchingLeaves(index);
+            count = countMatchingLeaves(index, count, max);
         } else {
             for (String p : values) {
+                if (count > max) {
+                    break;
+                }
                 NodeState s = index.getChildNode(p);
                 if (s != null) {
-                    count += countMatchingLeaves(s);
+                    count = countMatchingLeaves(s, count, max);
                 }
             }
         }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/IndexStoreStrategy.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/IndexStoreStrategy.java?rev=1431411&r1=1431410&r2=1431411&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/IndexStoreStrategy.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/p2/strategy/IndexStoreStrategy.java
Thu Jan 10 15:18:24 2013
@@ -66,8 +66,9 @@ public interface IndexStoreStrategy {
      * 
      * @param index the index node (may not be null)
      * @param values values to look for (null to check for property existence)
+     * @param max the maximum value to return
      * @return the aggregated count of occurrences for each provided value
      */
-    int count(NodeState index, Iterable<String> values);
+    int count(NodeState index, Iterable<String> values, int max);
 
 }

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexTest.java?rev=1431411&r1=1431410&r2=1431411&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/p2/Property2IndexTest.java
Thu Jan 10 15:18:24 2013
@@ -75,6 +75,12 @@ public class Property2IndexTest {
         assertEquals(ImmutableSet.of(), find(lookup, "foo", "ghi"));
         assertEquals(MANY, find(lookup, "foo", "xyz").size());
         assertEquals(MANY + 2, find(lookup, "foo", null).size());
+
+        assertEquals(Math.min(100, MANY), 
+                (int) lookup.getCost("foo", PropertyValues.newString("xyz")));
+        assertEquals(Math.min(100, MANY), 
+                (int) lookup.getCost("foo", null));
+
         
     }
     



Mime
View raw message