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 re...@apache.org on 2014/04/04 16:20:21 UTC

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

Author: reschke
Date: Fri Apr  4 14:20:20 2014
New Revision: 1584709

URL: http://svn.apache.org/r1584709
Log:
OAK-1266 - use cached documents for updates

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=1584709&r1=1584708&r2=1584709&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 Apr  4 14:20:20 2014
@@ -85,54 +85,7 @@ public class RDBDocumentStore implements
 
     @Override
     public <T extends Document> T find(final Collection<T> collection, final String id, int maxCacheAge) {
-        if (collection != Collection.NODES) {
-            return readDocument(collection, id);
-        } else {
-            CacheValue cacheKey = new StringValue(id);
-            NodeDocument doc;
-            if (maxCacheAge > 0) {
-                // first try without lock
-                doc = nodesCache.getIfPresent(cacheKey);
-                if (doc != null) {
-                    if (maxCacheAge == Integer.MAX_VALUE || System.currentTimeMillis() - doc.getCreated() < maxCacheAge) {
-                        return castAsT(unwrap(doc));
-                    }
-                }
-            }
-            try {
-                Lock lock = getAndLock(id);
-                try {
-                    if (maxCacheAge == 0) {
-                        invalidateCache(collection, id);
-                    }
-                    while (true) {
-                        doc = nodesCache.get(cacheKey, new Callable<NodeDocument>() {
-                            @Override
-                            public NodeDocument call() throws Exception {
-                                NodeDocument doc = (NodeDocument) readDocument(collection, id);
-                                if (doc != null) {
-                                    doc.seal();
-                                }
-                                return wrap(doc);
-                            }
-                        });
-                        if (maxCacheAge == 0 || maxCacheAge == Integer.MAX_VALUE) {
-                            break;
-                        }
-                        if (System.currentTimeMillis() - doc.getCreated() < maxCacheAge) {
-                            break;
-                        }
-                        // too old: invalidate, try again
-                        invalidateCache(collection, id);
-                    }
-                } finally {
-                    lock.unlock();
-                }
-                return castAsT(unwrap(doc));
-            } catch (ExecutionException e) {
-                throw new IllegalStateException("Failed to load document with " + id, e);
-            }
-        }
+        return readDocumentCached(collection, id, maxCacheAge);
     }
 
     @Override
@@ -297,6 +250,57 @@ public class RDBDocumentStore implements
         }
     }
 
+    private <T extends Document> T readDocumentCached(final Collection<T> collection, final String id, int maxCacheAge) {
+        if (collection != Collection.NODES) {
+            return readDocumentUncached(collection, id);
+        } else {
+            CacheValue cacheKey = new StringValue(id);
+            NodeDocument doc;
+            if (maxCacheAge > 0) {
+                // first try without lock
+                doc = nodesCache.getIfPresent(cacheKey);
+                if (doc != null) {
+                    if (maxCacheAge == Integer.MAX_VALUE || System.currentTimeMillis() - doc.getCreated() < maxCacheAge) {
+                        return castAsT(unwrap(doc));
+                    }
+                }
+            }
+            try {
+                Lock lock = getAndLock(id);
+                try {
+                    if (maxCacheAge == 0) {
+                        invalidateCache(collection, id);
+                    }
+                    while (true) {
+                        doc = nodesCache.get(cacheKey, new Callable<NodeDocument>() {
+                            @Override
+                            public NodeDocument call() throws Exception {
+                                NodeDocument doc = (NodeDocument) readDocumentUncached(collection, id);
+                                if (doc != null) {
+                                    doc.seal();
+                                }
+                                return wrap(doc);
+                            }
+                        });
+                        if (maxCacheAge == 0 || maxCacheAge == Integer.MAX_VALUE) {
+                            break;
+                        }
+                        if (System.currentTimeMillis() - doc.getCreated() < maxCacheAge) {
+                            break;
+                        }
+                        // too old: invalidate, try again
+                        invalidateCache(collection, id);
+                    }
+                } finally {
+                    lock.unlock();
+                }
+                return castAsT(unwrap(doc));
+            } catch (ExecutionException e) {
+                throw new IllegalStateException("Failed to load document with " + id, e);
+            }
+        }
+    }
+
     @CheckForNull
     private <T extends Document> boolean internalCreate(Collection<T> collection, List<UpdateOp> updates) {
         try {
@@ -316,7 +320,7 @@ public class RDBDocumentStore implements
     @CheckForNull
     private <T extends Document> T internalCreateOrUpdate(Collection<T> collection, UpdateOp update, boolean allowCreate,
             boolean checkConditions) {
-        T oldDoc = readDocument(collection, update.getId());
+        T oldDoc = readDocumentCached(collection, update.getId(), Integer.MAX_VALUE);
 
         if (oldDoc == null) {
             if (!allowCreate) {
@@ -337,7 +341,7 @@ public class RDBDocumentStore implements
             }
             catch (MicroKernelException ex) {
                 // may have failed due to a race condition; try update instead
-                oldDoc = readDocument(collection, update.getId());
+                oldDoc = readDocumentCached(collection, update.getId(), Integer.MAX_VALUE);
                 if (oldDoc == null) {
                     // something else went wrong
                     throw(ex);
@@ -363,7 +367,7 @@ public class RDBDocumentStore implements
                 if (!success) {
                     // retry with a fresh document
                     retries -= 1;
-                    oldDoc = readDocument(collection, update.getId());
+                    oldDoc = readDocumentCached(collection, update.getId(), Integer.MAX_VALUE);
                     doc = applyChanges(collection, oldDoc, update, checkConditions);
                     if (doc == null) {
                         return null;
@@ -501,7 +505,7 @@ public class RDBDocumentStore implements
     }
 
     @CheckForNull
-    private <T extends Document> T readDocument(Collection<T> collection, String id) {
+    private <T extends Document> T readDocumentUncached(Collection<T> collection, String id) {
         Connection connection = null;
         String tableName = getTable(collection);
         try {