From d7277e1d4d17e67c01662734147f0228115ba118 Mon Sep 17 00:00:00 2001
From: Vikas Saurabh <vsaurabh@adobe.com>
Date: Wed, 10 Dec 2014 20:44:17 +0530
Subject: [PATCH 1/4] OAK-2341: A bit of refactor to make root NodeState
 available in count method of IndexStoreStrategy

---
 .../plugins/index/property/OrderedPropertyIndexLookup.java |  2 +-
 .../oak/plugins/index/property/PropertyIndex.java          |  2 +-
 .../oak/plugins/index/property/PropertyIndexEditor.java    |  9 +++++++--
 .../oak/plugins/index/property/PropertyIndexLookup.java    |  2 +-
 .../oak/plugins/index/property/PropertyIndexPlan.java      |  9 ++++++---
 .../property/strategy/ContentMirrorStoreStrategy.java      | 14 +++++++-------
 .../index/property/strategy/IndexStoreStrategy.java        |  8 +++++---
 .../index/property/strategy/UniqueEntryStoreStrategy.java  |  6 +++---
 .../oak/plugins/index/reference/ReferenceEditor.java       |  6 +++---
 .../property/strategy/ContentMirrorStoreStrategyTest.java  |  2 +-
 10 files changed, 35 insertions(+), 25 deletions(-)
 mode change 100644 => 100755 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/OrderedPropertyIndexLookup.java
 mode change 100644 => 100755 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java
 mode change 100644 => 100755 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexEditor.java
 mode change 100644 => 100755 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java
 mode change 100644 => 100755 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexPlan.java
 mode change 100644 => 100755 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/IndexStoreStrategy.java
 mode change 100644 => 100755 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/UniqueEntryStoreStrategy.java
 mode change 100644 => 100755 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/reference/ReferenceEditor.java
 mode change 100644 => 100755 oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java

diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/OrderedPropertyIndexLookup.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/OrderedPropertyIndexLookup.java
old mode 100644
new mode 100755
index dc35da4..7bfac4c
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/OrderedPropertyIndexLookup.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/OrderedPropertyIndexLookup.java
@@ -177,7 +177,7 @@ public class OrderedPropertyIndexLookup {
         if (indexMeta != null) {
             // we relay then on the standard property index for the cost
             cost = COST_OVERHEAD
-                   + getStrategy(indexMeta).count(indexMeta, PropertyIndex.encode(value), MAX_COST);
+                   + getStrategy(indexMeta).count(root, indexMeta, PropertyIndex.encode(value), MAX_COST);
         }
         return cost;
     }
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java
old mode 100644
new mode 100755
index 8b495af..293289b
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndex.java
@@ -132,7 +132,7 @@ class PropertyIndex implements QueryIndex {
             if (PROPERTY.equals(definition.getString(TYPE_PROPERTY_NAME))
                     && definition.hasChildNode(INDEX_CONTENT_NODE_NAME)) {
                 PropertyIndexPlan plan = new PropertyIndexPlan(
-                        entry.getName(), definition, filter);
+                        entry.getName(), root, definition, filter);
                 if (plan.getCost() != Double.POSITIVE_INFINITY) {
                     LOG.debug("property cost for {} is {}",
                             plan.getName(), plan.getCost());
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexEditor.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexEditor.java
old mode 100644
new mode 100755
index e1ec00e..d14c373
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexEditor.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexEditor.java
@@ -78,7 +78,10 @@ class PropertyIndexEditor implements IndexEditor {
     /** Index definition node builder */
     private final NodeBuilder definition;
 
-    private final Set<String> propertyNames;
+    /** Root node state */
+    private final NodeState root;
+
+     private final Set<String> propertyNames;
 
     /** Type predicate, or {@code null} if there are no type restrictions */
     private final Predicate<NodeState> typePredicate;
@@ -111,6 +114,7 @@ class PropertyIndexEditor implements IndexEditor {
         this.name = null;
         this.path = "/";
         this.definition = definition;
+        this.root = root;
 
         //initPropertyNames(definition);
 
@@ -146,6 +150,7 @@ class PropertyIndexEditor implements IndexEditor {
         this.name = name;
         this.path = null;
         this.definition = parent.definition;
+        this.root = parent.root;
         this.propertyNames = parent.getPropertyNames();
         this.typePredicate = parent.typePredicate;
         this.keysToCheckForUniqueness = parent.keysToCheckForUniqueness;
@@ -276,7 +281,7 @@ class PropertyIndexEditor implements IndexEditor {
                 NodeState indexMeta = definition.getNodeState();
                 IndexStoreStrategy s = getStrategy(true);
                 for (String key : keysToCheckForUniqueness) {
-                    if (s.count(indexMeta, singleton(key), 2) > 1) {
+                    if (s.count(root, indexMeta, singleton(key), 2) > 1) {
                         String msg = String.format("Uniqueness constraint violated at path [%s] for one of the " +
                                         "property in %s having value %s", getPath(), propertyNames, key);
                         throw new CommitFailedException(
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java
old mode 100644
new mode 100755
index cfd96e2..4cb91a8
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexLookup.java
@@ -129,7 +129,7 @@ public class PropertyIndexLookup {
             return Double.POSITIVE_INFINITY;
         }
         return COST_OVERHEAD +
-                getStrategy(indexMeta).count(filter, indexMeta, encode(value), MAX_COST);
+                getStrategy(indexMeta).count(filter, root, indexMeta, encode(value), MAX_COST);
     }
 
     /**
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexPlan.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexPlan.java
old mode 100644
new mode 100755
index 8ace776..e5605ec
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexPlan.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/PropertyIndexPlan.java
@@ -72,6 +72,8 @@ public class PropertyIndexPlan {
     private static final IndexStoreStrategy UNIQUE =
             new UniqueEntryStoreStrategy();
 
+    private final NodeState root;
+
     private final NodeState definition;
 
     private final String name;
@@ -92,8 +94,9 @@ public class PropertyIndexPlan {
 
     private final int depth;
 
-    PropertyIndexPlan(String name, NodeState definition, Filter filter) {
+    PropertyIndexPlan(String name, NodeState root, NodeState definition, Filter filter) {
         this.name = name;
+        this.root = root;
         this.definition = definition;
         this.properties = newHashSet(definition.getNames(PROPERTY_NAMES));
 
@@ -135,7 +138,7 @@ public class PropertyIndexPlan {
 
                 if (restriction != null) {
                     Set<String> values = getValues(restriction);
-                    double cost = strategy.count(filter, definition, values, MAX_COST);
+                    double cost = strategy.count(filter, root, definition, values, MAX_COST);
                     if (cost < bestCost) {
                         bestDepth = depth;
                         bestValues = values;
@@ -152,7 +155,7 @@ public class PropertyIndexPlan {
                 if (constraint instanceof OrImpl) {
                     Set<String> values = findMultiProperty((OrImpl) constraint);
                     if (values != null) {
-                        double cost = strategy.count(filter, definition, values, MAX_COST);
+                        double cost = strategy.count(filter, root, definition, values, MAX_COST);
                         if (cost < bestCost) {
                             bestDepth = 1;
                             bestValues = values;
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java
index 83b6217..106e55d 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategy.java
@@ -164,21 +164,21 @@ public class ContentMirrorStoreStrategy implements IndexStoreStrategy {
     }
 
     @Override
-    public long count(NodeState indexMeta, Set<String> values, int max) {
-        return count(indexMeta, INDEX_CONTENT_NODE_NAME, values, max);
+    public long count(NodeState root, NodeState indexMeta, Set<String> values, int max) {
+        return count(root, indexMeta, INDEX_CONTENT_NODE_NAME, values, max);
     }
 
     @Override
-    public long count(final Filter filter, NodeState indexMeta, Set<String> values, int max) {
-        return count(filter, indexMeta, INDEX_CONTENT_NODE_NAME, values, max);
+    public long count(final Filter filter, NodeState root, NodeState indexMeta, Set<String> values, int max) {
+        return count(filter, root, indexMeta, INDEX_CONTENT_NODE_NAME, values, max);
     }
 
-    public long count(NodeState indexMeta, final String indexStorageNodeName,
+    public long count(NodeState root, NodeState indexMeta, final String indexStorageNodeName,
             Set<String> values, int max) {
-        return count(null, indexMeta, indexStorageNodeName, values, max);
+        return count(null, root, indexMeta, indexStorageNodeName, values, max);
     }
 
-    public long count(Filter filter, NodeState indexMeta, final String indexStorageNodeName,
+    public long count(Filter filter, NodeState root, NodeState indexMeta, final String indexStorageNodeName,
             Set<String> values, int max) {
         NodeState index = indexMeta.getChildNode(indexStorageNodeName);
         long count = 0;
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/IndexStoreStrategy.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/IndexStoreStrategy.java
old mode 100644
new mode 100755
index 4bbcd96..87ea4ca
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/IndexStoreStrategy.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/IndexStoreStrategy.java
@@ -54,24 +54,26 @@ public interface IndexStoreStrategy {
     /**
      * Count the occurrence of a given set of values. Used in calculating the
      * cost of an index.
-     * 
+     *
+     * @param root the root node (may not be null)
      * @param indexMeta the index metadata 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
      */
-    long count(NodeState indexMeta, Set<String> values, int max);
+    long count(NodeState root, NodeState indexMeta, Set<String> values, int max);
 
     /**
      * Count the occurrence of a given set of values. Used in calculating the
      * cost of an index.
      *
      * @param filter the filter which can be used to estimate better cost
+     * @param root the root node (may not be null)
      * @param indexMeta the index metadata 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
      */
-    long count(Filter filter, NodeState indexMeta, Set<String> values, int max);
+    long count(Filter filter, NodeState root, NodeState indexMeta, Set<String> values, int max);
 
 }
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/UniqueEntryStoreStrategy.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/UniqueEntryStoreStrategy.java
old mode 100644
new mode 100755
index 3486e1f..74ac0ba
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/UniqueEntryStoreStrategy.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/UniqueEntryStoreStrategy.java
@@ -145,7 +145,7 @@ public class UniqueEntryStoreStrategy implements IndexStoreStrategy {
     }
 
     @Override
-    public long count(NodeState indexMeta, Set<String> values, int max) {
+    public long count(NodeState root, NodeState indexMeta, Set<String> values, int max) {
         NodeState index = indexMeta.getChildNode(INDEX_CONTENT_NODE_NAME);
         long count = 0;
         if (values == null) {
@@ -179,8 +179,8 @@ public class UniqueEntryStoreStrategy implements IndexStoreStrategy {
     }
 
     @Override
-    public long count(final Filter filter, NodeState indexMeta, Set<String> values, int max) {
-        return count(indexMeta, values, max);
+    public long count(final Filter filter, NodeState root, NodeState indexMeta, Set<String> values, int max) {
+        return count(root, indexMeta, values, max);
     }
     
 }
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/reference/ReferenceEditor.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/reference/ReferenceEditor.java
old mode 100644
new mode 100755
index 289b7e5..cfabe84
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/reference/ReferenceEditor.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/reference/ReferenceEditor.java
@@ -298,7 +298,7 @@ class ReferenceEditor extends DefaultEditor implements IndexEditor {
     public Editor childNodeDeleted(String name, NodeState before)
             throws CommitFailedException {
         String uuid = before.getString(JCR_UUID);
-        if (uuid != null && check(definition.getNodeState(), REF_NAME, uuid)) {
+        if (uuid != null && check(root, definition.getNodeState(), REF_NAME, uuid)) {
             rmIds.add(uuid);
         }
         return new ReferenceEditor(this, name, uuid);
@@ -346,9 +346,9 @@ class ReferenceEditor extends DefaultEditor implements IndexEditor {
         }
     }
 
-    private static boolean check(NodeState definition, String name, String key) {
+    private static boolean check(NodeState root, NodeState definition, String name, String key) {
         return definition.hasChildNode(name)
-                && STORE.count(definition, name, of(key), 1) > 0;
+                && STORE.count(root, definition, name, of(key), 1) > 0;
     }
 
 }
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java
old mode 100644
new mode 100755
index fba22b3..d0c759e
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/strategy/ContentMirrorStoreStrategyTest.java
@@ -137,7 +137,7 @@ public class ContentMirrorStoreStrategyTest {
         store.update(index, "b", EMPTY, KEY);
         Assert.assertTrue(
                 "ContentMirrorStoreStrategy should guarantee uniqueness on insert",
-                store.count(indexMeta.getNodeState(), Collections.singleton("key"), 2) > 1);
+                store.count(root, indexMeta.getNodeState(), Collections.singleton("key"), 2) > 1);
     }
 
 }
-- 
2.1.1

