You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ju...@apache.org on 2013/11/01 17:12:12 UTC

svn commit: r1537971 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate: ItemDelegate.java NodeDelegate.java PropertyDelegate.java

Author: jukka
Date: Fri Nov  1 16:12:12 2013
New Revision: 1537971

URL: http://svn.apache.org/r1537971
Log:
OAK-1139: Avoid the duplicate property lookup during getProperty("name").getString()

Preload the PropertyState and update it whenever the session is updated

Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/ItemDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/PropertyDelegate.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/ItemDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/ItemDelegate.java?rev=1537971&r1=1537970&r2=1537971&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/ItemDelegate.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/ItemDelegate.java Fri Nov  1 16:12:12 2013
@@ -47,6 +47,32 @@ public abstract class ItemDelegate {
     }
 
     /**
+     * Checks whether the session has changed since this delegate instance
+     * was last accessed, thus triggering an {@link #update() update} of the
+     * internal state of this delegate.
+     *
+     * @return {@code true} if the session was recently updated,
+     *         {@code false} if not
+     */
+    protected boolean checkUpdate() {
+        long sessionCount = sessionDelegate.getUpdateCount();
+        if (updateCount != sessionCount) {
+            updateCount = sessionCount;
+            update();
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Called by {@link #checkUpdate()} to update the internal state of this
+     * delegate.
+     */
+    protected void update() {
+    }
+
+    /**
      * Performs a sanity check on this item and the associated session.
      *
      * @throws RepositoryException if this item has been rendered invalid
@@ -55,13 +81,9 @@ public abstract class ItemDelegate {
      */
     public synchronized void checkAlive() throws RepositoryException {
         sessionDelegate.checkAlive();
-        long sessionCount = sessionDelegate.getUpdateCount();
-        if (updateCount != sessionCount) {
-            if (!exists()) {
-                throw new InvalidItemStateException(
-                        "This item does not exist anymore : " + getPath());
-            }
-            updateCount = sessionCount;
+        if (checkUpdate() && !exists()) {
+            throw new InvalidItemStateException(
+                    "This item does not exist anymore");
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java?rev=1537971&r1=1537970&r2=1537971&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/NodeDelegate.java Fri Nov  1 16:12:12 2013
@@ -247,14 +247,18 @@ public class NodeDelegate extends ItemDe
      *         no such property exists
      */
     @CheckForNull
-    public PropertyDelegate getPropertyOrNull(String relPath) throws RepositoryException {
+    public PropertyDelegate getPropertyOrNull(String relPath)
+            throws RepositoryException {
         Tree parent = getTree(PathUtils.getParentPath(relPath));
         String name = PathUtils.getName(relPath);
-        if (parent != null && parent.hasProperty(name)) {
-            return new PropertyDelegate(sessionDelegate, parent, name);
-        } else {
-            return null;
+        if (parent != null) {
+            PropertyDelegate property =
+                    new PropertyDelegate(sessionDelegate, parent, name);
+            if (property.exists()) {
+                return property;
+            }
         }
+        return null;
     }
 
     /**

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/PropertyDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/PropertyDelegate.java?rev=1537971&r1=1537970&r2=1537971&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/PropertyDelegate.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/delegate/PropertyDelegate.java Fri Nov  1 16:12:12 2013
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.oak.jcr.delegate;
 
 import static com.google.common.base.Objects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
@@ -40,39 +41,51 @@ public class PropertyDelegate extends It
     /** The underlying {@link org.apache.jackrabbit.oak.api.Tree} of this property's parent */
     private final Tree parent;
 
+    @Nonnull
     private final String name;
 
+    @CheckForNull
+    private PropertyState state;
+
     PropertyDelegate(SessionDelegate sessionDelegate, Tree parent, String name) {
         super(sessionDelegate);
-        this.parent = parent;
-        this.name = name;
+        this.parent = checkNotNull(parent);
+        this.name = checkNotNull(name);
+        this.state = parent.getProperty(name);
     }
 
+    /**
+     * The session has been updated since the last time this property delegate
+     * was accessed, so we need to re-retrieve the property state to get any
+     * potential updates. It might also be that this property was removed,
+     * in which case the {@link #state} reference will be {@code null}.
+     */
     @Override
-    @Nonnull
+    protected void update() {
+        state = parent.getProperty(name);
+    }
+
+    @Override @Nonnull
     public String getName() {
         return name;
     }
 
-    @Override
-    @Nonnull
+    @Override @Nonnull
     public String getPath() {
         return PathUtils.concat(parent.getPath(), name);
     }
 
-    @Override
-    @CheckForNull
+    @Override @CheckForNull
     public NodeDelegate getParent() {
         return parent.exists() ? new NodeDelegate(sessionDelegate, parent) : null;
     }
 
     @Override
     public boolean exists() {
-        return parent.exists() && parent.hasProperty(name);
+        return state != null;
     }
 
-    @Override
-    @CheckForNull
+    @Override @CheckForNull
     public Status getStatus() {
         return parent.getPropertyStatus(name);
     }
@@ -84,11 +97,12 @@ public class PropertyDelegate extends It
 
     @Nonnull
     public PropertyState getPropertyState() throws InvalidItemStateException {
-        PropertyState p = parent.getProperty(name);
-        if (p == null) {
-            throw new InvalidItemStateException();
+        if (state != null) {
+            return state;
+        } else {
+            throw new InvalidItemStateException(
+                    "The " + name + " property does not exist");
         }
-        return p;
     }
 
     @Nonnull
@@ -130,8 +144,7 @@ public class PropertyDelegate extends It
      */
     @Override
     public boolean remove() {
-        boolean exists = parent.hasProperty(name);
-        if (exists) {
+        if (parent.hasProperty(name)) {
             parent.removeProperty(name);
             return true;
         } else {