Author: reschke
Date: Fri Jan 19 14:50:18 2018
New Revision: 1821663

URL: http://svn.apache.org/viewvc?rev=1821663&view=rev
Log:
OAK-7181: RDBDocumentStore: use try-with-resources for ChangesTracker

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

Modified: 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1821663&r1=1821662&r2=1821663&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 Fri Jan 19 14:50:18 2018
@@ -486,6 +486,24 @@ public class RDBDocumentStore implements
         return result;
     }
 
+    @CheckForNull
+    private <T extends Document> CacheChangesTracker 
obtainTracker(Collection<T> collection, Set<String> keys) {
+        if (collection == Collection.NODES) {
+            return this.nodesCache.registerTracker(keys);
+        } else {
+            return null;
+        }
+    }
+
+    @CheckForNull
+    private <T extends Document> CacheChangesTracker 
obtainTracker(Collection<T> collection, String fromKey, String toKey) {
+        if (collection == Collection.NODES) {
+            return this.nodesCache.registerTracker(fromKey, toKey);
+        } else {
+            return null;
+        }
+    }
+
     private <T extends Document> Map<UpdateOp, T> bulkUpdate(Collection<T> 
collection, List<UpdateOp> updates, Map<String, T> oldDocs, boolean upsert) {
         Set<String> missingDocs = new HashSet<String>();
         for (UpdateOp op : updates) {
@@ -495,12 +513,7 @@ public class RDBDocumentStore implements
         }
         oldDocs.putAll(readDocumentsUncached(collection, missingDocs));
 
-        CacheChangesTracker tracker = null;
-        if (collection == Collection.NODES) {
-            tracker = nodesCache.registerTracker(Sets.union(oldDocs.keySet(), 
missingDocs));
-        }
-
-        try {
+        try (CacheChangesTracker tracker = obtainTracker(collection, 
Sets.union(oldDocs.keySet(), missingDocs) )) {
             List<T> docsToUpdate = new ArrayList<T>(updates.size());
             Set<String> keysToUpdate = new HashSet<String>();
             for (UpdateOp update : updates) {
@@ -547,10 +560,6 @@ public class RDBDocumentStore implements
             } finally {
                 this.ch.closeConnection(connection);
             }
-        } finally {
-            if (tracker != null) {
-                tracker.close();
-            }
         }
     }
 
@@ -1601,11 +1610,8 @@ public class RDBDocumentStore implements
 
         final Stopwatch watch = startWatch();
         int resultSize = 0;
-        CacheChangesTracker tracker = null;
-        try {
-            if (collection == Collection.NODES) {
-                tracker = nodesCache.registerTracker(fromKey, toKey);
-            }
+
+        try (CacheChangesTracker tracker = obtainTracker(collection, fromKey, 
toKey)) {
             long now = System.currentTimeMillis();
             connection = this.ch.getROConnection();
             String from = collection == Collection.NODES && 
NodeDocument.MIN_ID_VALUE.equals(fromKey) ? null : fromKey;
@@ -1664,9 +1670,6 @@ public class RDBDocumentStore implements
             LOG.error("SQL exception on query", ex);
             throw new DocumentStoreException(ex);
         } finally {
-            if (tracker != null) {
-                tracker.close();
-            }
             this.ch.closeConnection(connection);
             stats.doneQuery(watch.elapsed(TimeUnit.NANOSECONDS), collection, 
fromKey, toKey,
                     !conditions.isEmpty(), resultSize, -1, false);


Reply via email to