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 to...@apache.org on 2016/05/04 09:50:08 UTC

svn commit: r1742240 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java

Author: tomekr
Date: Wed May  4 09:50:08 2016
New Revision: 1742240

URL: http://svn.apache.org/viewvc?rev=1742240&view=rev
Log:
OAK-4112: Replace the query exclusive lock with a cache tracker
Don't call getIfPresent() twice in the putNonConflictingDocs.

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java?rev=1742240&r1=1742239&r2=1742240&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java Wed May  4 09:50:08 2016
@@ -218,23 +218,11 @@ public class NodeDocumentCache implement
         Lock lock = locks.acquire(id);
         try {
             NodeDocument cachedDoc = getIfPresent(id);
-            if (cachedDoc == null || cachedDoc == NodeDocument.NULL) {
+            if (isNewer(cachedDoc, doc)) {
                 newerDoc = doc;
                 putInternal(doc);
             } else {
-                Long cachedModCount = cachedDoc.getModCount();
-                Long modCount = doc.getModCount();
-
-                if (cachedModCount == null || modCount == null) {
-                    throw new IllegalStateException("Missing " + Document.MOD_COUNT);
-                }
-
-                if (modCount > cachedModCount) {
-                    newerDoc = doc;
-                    putInternal(doc);
-                } else {
-                    newerDoc = cachedDoc;
-                }
+                newerDoc = cachedDoc;
             }
         } finally {
             lock.unlock();
@@ -439,13 +427,14 @@ public class NodeDocumentCache implement
             String id = d.getId();
             Lock lock = locks.acquire(id);
             try {
+                NodeDocument cachedDoc = getIfPresent(id);
                 // if an old document is present in the cache, we can simply update it
-                if (getIfPresent(id) != null) {
-                    putIfNewer(d);
+                if (cachedDoc != null && isNewer(cachedDoc, d)) {
+                    putInternal(d);
                 // if the document hasn't been invalidated or added during the tracker lifetime,
                 // we can put it as well
-                } else if (!tracker.mightBeenAffected(id)) {
-                    putIfNewer(d);
+                } else if (cachedDoc == null && !tracker.mightBeenAffected(id)) {
+                    putInternal(d);
                 }
             } finally {
                 lock.unlock();
@@ -470,4 +459,28 @@ public class NodeDocumentCache implement
             tracker.putDocument(doc.getId());
         }
     }
+
+    /**
+     * Check if the doc is more recent than the cachedDoc. If the cachedDoc
+     * is {@code null} or {@code NodeDocument.NULL}, the doc will be considered
+     * as more recent as well.
+     *
+     * @param cachedDoc the document already present in cache
+     * @param doc the tested document
+     * @return {@code true} iff the cacheDoc is null or older than the doc
+     */
+    private boolean isNewer(@Nullable NodeDocument cachedDoc, @Nonnull NodeDocument doc) {
+        if (cachedDoc == null || cachedDoc == NodeDocument.NULL) {
+            return true;
+        }
+
+        Long cachedModCount = cachedDoc.getModCount();
+        Long modCount = doc.getModCount();
+
+        if (cachedModCount == null || modCount == null) {
+            throw new IllegalStateException("Missing " + Document.MOD_COUNT);
+        }
+
+        return modCount > cachedModCount;
+    }
 }