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 mr...@apache.org on 2015/02/02 11:06:39 UTC

svn commit: r1656432 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document: MissingLastRevSeeker.java util/Utils.java

Author: mreutegg
Date: Mon Feb  2 10:06:38 2015
New Revision: 1656432

URL: http://svn.apache.org/r1656432
Log:
OAK-2462: reduce code duplication between Utils.getSelectedDocuments and MissingLastRevCandidateIterator

Applied patch provided by Julian Reschke

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MissingLastRevSeeker.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MissingLastRevSeeker.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MissingLastRevSeeker.java?rev=1656432&r1=1656431&r2=1656432&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MissingLastRevSeeker.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MissingLastRevSeeker.java Mon Feb  2 10:06:38 2015
@@ -19,17 +19,15 @@
 
 package org.apache.jackrabbit.oak.plugins.document;
 
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
+import org.apache.jackrabbit.oak.plugins.document.ClusterNodeInfo.RecoverLockState;
+import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 
 import com.google.common.base.Predicate;
-import com.google.common.collect.AbstractIterator;
 import com.google.common.collect.Iterables;
 
-import org.apache.jackrabbit.oak.plugins.document.util.Utils;
-
-import static org.apache.jackrabbit.oak.plugins.document.ClusterNodeInfo.RecoverLockState;
+import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.MODIFIED_IN_SECS;
+import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.getModifiedInSecs;
+import static org.apache.jackrabbit.oak.plugins.document.util.Utils.getSelectedDocuments;
 
 /**
  * Utils to retrieve _lastRev missing update candidates.
@@ -41,7 +39,7 @@ public class MissingLastRevSeeker {
     public MissingLastRevSeeker(DocumentStore store) {
         this.store = store;
     }
-    
+
     /**
      * Gets the clusters which potentially need _lastRev recovery.
      *
@@ -50,7 +48,7 @@ public class MissingLastRevSeeker {
     public Iterable<ClusterNodeInfoDocument> getAllClusters() {
         return ClusterNodeInfoDocument.all(store);
     }
-    
+
     /**
      * Gets the cluster node info for the given cluster node id.
      *
@@ -71,83 +69,17 @@ public class MissingLastRevSeeker {
      */
     public Iterable<NodeDocument> getCandidates(final long startTime) {
         // Fetch all documents where lastmod >= startTime
-        Iterable<NodeDocument> nodes = new MissingLastRevCandidateIterable(store, NodeDocument.getModifiedInSecs(startTime));
+        Iterable<NodeDocument> nodes = getSelectedDocuments(store, 
+                MODIFIED_IN_SECS, getModifiedInSecs(startTime));
         return Iterables.filter(nodes, new Predicate<NodeDocument>() {
             @Override
             public boolean apply(NodeDocument input) {
-                Long modified = (Long) input.get(NodeDocument.MODIFIED_IN_SECS);
-                return (modified != null && (modified >= NodeDocument.getModifiedInSecs(startTime)));
+                Long modified = (Long) input.get(MODIFIED_IN_SECS);
+                return (modified != null && (modified >= getModifiedInSecs(startTime)));
             }
         });
     }
 
-    /**
-     * Query the document store for nodes newer than a specific timestamp; implement
-     * this as an Iterator that gets relatively small chunks from the store.
-     */
-    private class MissingLastRevCandidateIterable implements Iterable<NodeDocument> {
-
-        private final DocumentStore store;
-        private final long startTime;
-
-        public MissingLastRevCandidateIterable(DocumentStore store, long startTime) {
-            this.store = store;
-            this.startTime = startTime;
-        }
-
-        @Override
-        public Iterator<NodeDocument> iterator() {
-            return new MissingLastRevCandidateIterator(store, startTime);
-        }
-
-        private class MissingLastRevCandidateIterator extends AbstractIterator<NodeDocument> {
-
-            private static final int CHUNKSIZE = 256;
-            private final DocumentStore store;
-            private final long startTime;
-            private String startIndex = NodeDocument.MIN_ID_VALUE;
-
-            private List<NodeDocument> results = new LinkedList<NodeDocument>();
-
-            public MissingLastRevCandidateIterator(DocumentStore store, long startTime) {
-                this.store = store;
-                this.startTime = startTime;
-            }
-
-            private boolean refill() {
-                if (startIndex == null) {
-                    return false;
-                } else {
-                    List<NodeDocument> found = store.query(Collection.NODES, startIndex, NodeDocument.MAX_ID_VALUE,
-                            NodeDocument.MODIFIED_IN_SECS, startTime, CHUNKSIZE);
-                    if (found.isEmpty()) {
-                        startIndex = null;
-                        return false;
-                    } else {
-                        results.addAll(found);
-                        return true;
-                    }
-                }
-            }
-
-            @Override
-            protected NodeDocument computeNext() {
-                if (!results.isEmpty()) {
-                    NodeDocument result = results.remove(0);
-                    startIndex = result.getId();
-                    return result;
-                } else {
-                    // need to refill
-                    if (refill()) {
-                        return computeNext();
-                    } else {
-                        return endOfData();
-                    }
-                }
-            }
-        }
-    }
-
     public boolean acquireRecoveryLock(int clusterId){
         //This approach has a race condition where two different cluster nodes
         //can acquire the lock simultaneously.

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java?rev=1656432&r1=1656431&r2=1656432&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java Mon Feb  2 10:06:38 2015
@@ -517,20 +517,25 @@ public class Utils {
     /**
      * Returns an {@link Iterable} over all {@link NodeDocument}s in the given
      * store matching a condition on an <em>indexed property</em>. The returned
-     * {@linkplain Iterable} does not guarantee a consistent view on the store.
+     * {@link Iterable} does not guarantee a consistent view on the store.
      * it may return documents that have been added to the store after this
      * method had been called.
      *
      * @param store
      *            a {@link DocumentStore}.
+     * @param indexedProperty the name of the indexed property.
+     * @param startValue the lower bound value for the indexed property
+     *                   (inclusive).
      * @return an {@link Iterable} over all documents in the store matching the
      *         condition
      */
-    public static Iterable<NodeDocument> getSelectedDocuments(final DocumentStore store, String indexedProperty, long startValue) {
+    public static Iterable<NodeDocument> getSelectedDocuments(
+            DocumentStore store, String indexedProperty, long startValue) {
         return internalGetSelectedDocuments(store, indexedProperty, startValue);
     }
 
-    private static Iterable<NodeDocument> internalGetSelectedDocuments(final DocumentStore store, final String indexedProperty,
+    private static Iterable<NodeDocument> internalGetSelectedDocuments(
+            final DocumentStore store, final String indexedProperty,
             final long startValue) {
         return new Iterable<NodeDocument>() {
             @Override