Author: reschke
Date: Fri Nov 27 14:58:25 2015
New Revision: 1716883

URL: http://svn.apache.org/viewvc?rev=1716883&view=rev
Log:
OAK-3691: RDBDocumentStore: refactor document update logic

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

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1716883&r1=1716882&r2=1716883&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 Fri Nov 27 14:58:25 2015
@@ -886,10 +886,7 @@ public class RDBDocumentStore implements
                 for (UpdateOp update : chunks) {
                     UpdateUtils.assertUnconditional(update);
                     T doc = collection.newDocument(this);
-                    update.increment(MODCOUNT, 1);
-                    if (hasChangesToCollisions(update)) {
-                        update.increment(COLLISIONSMODCOUNT, 1);
-                    }
+                    addUpdateCounters(update);
                     UpdateUtils.applyChanges(doc, update);
                     if (!update.getId().equals(doc.getId())) {
                         throw new DocumentStoreException("ID mismatch - 
UpdateOp: " + update.getId() + ", ID property: "
@@ -928,10 +925,7 @@ public class RDBDocumentStore implements
             if (checkConditions && !checkConditions(doc, 
update.getConditions())) {
                 return null;
             }
-            update.increment(MODCOUNT, 1);
-            if (hasChangesToCollisions(update)) {
-                update.increment(COLLISIONSMODCOUNT, 1);
-            }
+            addUpdateCounters(update);
             UpdateUtils.applyChanges(doc, update);
             try {
                 insertDocuments(collection, Collections.singletonList(doc));
@@ -967,11 +961,11 @@ public class RDBDocumentStore implements
     @CheckForNull
     private <T extends Document> T internalUpdate(Collection<T> collection, 
UpdateOp update, T oldDoc, boolean checkConditions,
             int maxRetries) {
-        T doc = applyChanges(collection, oldDoc, update, checkConditions);
-        if (doc == null) {
-            // conditions not met
+        if (checkConditions && !UpdateUtils.checkConditions(oldDoc, 
update.getConditions())) {
             return null;
         } else {
+            addUpdateCounters(update);
+            T doc = createNewDocument(collection, oldDoc, update);
             Lock l = getAndLock(update.getId());
             try {
                 boolean success = false;
@@ -998,10 +992,13 @@ public class RDBDocumentStore implements
                             return null;
                         }
 
-                        doc = applyChanges(collection, oldDoc, update, 
checkConditions);
-                        if (doc == null) {
+                        if (checkConditions && 
!UpdateUtils.checkConditions(oldDoc, update.getConditions())) {
                             return null;
                         }
+                        else {
+                            addUpdateCounters(update);
+                            doc = createNewDocument(collection, oldDoc, 
update);
+                        }
                     } else {
                         updateCache(collection, oldDoc, doc);
                     }
@@ -1019,20 +1016,20 @@ public class RDBDocumentStore implements
         }
     }
 
-    @CheckForNull
-    private <T extends Document> T applyChanges(Collection<T> collection, T 
oldDoc, UpdateOp update, boolean checkConditions) {
+    @Nonnull
+    private <T extends Document> T createNewDocument(Collection<T> collection, 
T oldDoc, UpdateOp update) {
         T doc = collection.newDocument(this);
         oldDoc.deepCopy(doc);
-        if (checkConditions && !checkConditions(doc, update.getConditions())) {
-            return null;
-        }
+        UpdateUtils.applyChanges(doc, update);
+        doc.seal();
+        return doc;
+    }
+
+    private static void addUpdateCounters(UpdateOp update) {
         if (hasChangesToCollisions(update)) {
             update.increment(COLLISIONSMODCOUNT, 1);
         }
         update.increment(MODCOUNT, 1);
-        UpdateUtils.applyChanges(doc, update);
-        doc.seal();
-        return doc;
     }
 
     @CheckForNull
@@ -1098,10 +1095,9 @@ public class RDBDocumentStore implements
                                 // invalidated
                                 nodesCache.invalidate(new StringValue(id));
                             } else {
-                                T newDoc = applyChanges(collection, oldDoc, 
update, true);
-                                if (newDoc != null) {
-                                    updateCache(collection, oldDoc, newDoc);
-                                }
+                                addUpdateCounters(update);
+                                T newDoc = createNewDocument(collection, 
oldDoc, update);
+                                updateCache(collection, oldDoc, newDoc);
                             }
                         } finally {
                             lock.unlock();
@@ -1728,19 +1724,21 @@ public class RDBDocumentStore implements
         return castAsT(fresh);
     }
 
-    private boolean hasChangesToCollisions(UpdateOp update) {
-        if (! USECMODCOUNT) return false;
-
-        for (Entry<Key, Operation> e : 
checkNotNull(update).getChanges().entrySet()) {
-            Key k = e.getKey();
-            Operation op = e.getValue();
-            if (op.type == Operation.Type.SET_MAP_ENTRY) {
-                if (NodeDocument.COLLISIONS.equals(k.getName())) {
-                    return true;
+    private static boolean hasChangesToCollisions(UpdateOp update) {
+        if (!USECMODCOUNT) {
+            return false;
+        } else {
+            for (Entry<Key, Operation> e : 
checkNotNull(update).getChanges().entrySet()) {
+                Key k = e.getKey();
+                Operation op = e.getValue();
+                if (op.type == Operation.Type.SET_MAP_ENTRY) {
+                    if (NodeDocument.COLLISIONS.equals(k.getName())) {
+                        return true;
+                    }
                 }
             }
+            return false;
         }
-        return false;
     }
 
     protected Cache<CacheValue, NodeDocument> getNodeDocumentCache() {


Reply via email to