jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r982695 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons: flat/BTreeManager.java flat/Rank.java flat/TreeTraverser.java iterator/LazyIteratorChain.java
Date Thu, 05 Aug 2010 17:00:54 GMT
Author: mduerig
Date: Thu Aug  5 17:00:53 2010
New Revision: 982695

URL: http://svn.apache.org/viewvc?rev=982695&view=rev
Log:
JCR-2688: Provide utility for handling large number of child nodes/properties
applied Jukka's patch with some modifications

Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/BTreeManager.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/Rank.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/TreeTraverser.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/BTreeManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/BTreeManager.java?rev=982695&r1=982694&r2=982695&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/BTreeManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/BTreeManager.java
Thu Aug  5 17:00:53 2010
@@ -16,12 +16,7 @@
  */
 package org.apache.jackrabbit.commons.flat;
 
-import static org.apache.jackrabbit.commons.iterator.Iterators.filterIterator;
-import static org.apache.jackrabbit.commons.iterator.Iterators.nodes;
-import static org.apache.jackrabbit.commons.iterator.Iterators.properties;
-
 import org.apache.jackrabbit.JcrConstants;
-import org.apache.jackrabbit.commons.iterator.Predicate;
 
 import javax.jcr.Item;
 import javax.jcr.Node;
@@ -32,6 +27,7 @@ import javax.jcr.RepositoryException;
 
 import java.util.Comparator;
 import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 /**
  * <p>
@@ -256,31 +252,56 @@ public class BTreeManager implements Tre
     /**
      * Returns a {@link SizedIterator} of the child nodes of <code>node</code>.
      */
+    @SuppressWarnings("unchecked")
     protected SizedIterator<Node> getNodes(Node node) throws RepositoryException {
         NodeIterator nodes = node.getNodes();
-        return getSizedIterator(nodes(nodes), nodes.getSize());
+        return getSizedIterator(nodes, nodes.getSize());
     }
 
     /**
-     * Returns a {@link SizedIterator} of the properties of <code>node</code>
which
-     * excludes the <code>jcr.primaryType</code> property.
+     * Returns a {@link SizedIterator} of the properties of <code>node</code>
+     * which excludes the <code>jcr.primaryType</code> property.
      */
-    protected SizedIterator<Property> getProperties(final Node node) throws RepositoryException
{
+    protected SizedIterator<Property> getProperties(Node node) throws RepositoryException
{
         final PropertyIterator properties = node.getProperties();
 
-        Iterator<Property> filtered = filterIterator(properties(properties), new Predicate<Property>()
{
-            public boolean evaluate(Property property) {
-                try {
-                    return !JcrConstants.JCR_PRIMARYTYPE.equals(property.getName());
+        Iterator<Property> filteredIterator = new Iterator<Property>() {
+            Property next = null;
+
+            public boolean hasNext() {
+                while (next == null && properties.hasNext()) {
+                    Property p = properties.nextProperty();
+                    try {
+                        if (!JcrConstants.JCR_PRIMARYTYPE.equals(p.getName())) {
+                            next = p;
+                        }
+                    }
+                    catch (RepositoryException ignore) {
+                        next = p;
+                    }
                 }
-                catch (RepositoryException ignore) {
-                    return true;
+
+                return next != null;
+            }
+
+            public Property next() {
+                if (hasNext()) {
+                    Property property = next;
+                    next = null;
+                    return property;
                 }
+                else {
+                    throw new NoSuchElementException();
+                }
+            }
+
+            public void remove() {
+                throw new UnsupportedOperationException();
             }
-        });
+        };
 
         long size = properties.getSize();
-        return getSizedIterator(filtered, size > 0 ? size - 1 : size);
+        return getSizedIterator(filteredIterator, size > 0 ? size - 1 : size);
     }
 
     /**
@@ -311,7 +332,7 @@ public class BTreeManager implements Tre
     /**
      * Wraps <code>iterator</code> into a {@link SizedIterator} given a
      * <code>size</code>. The value of the <code>size</code> parameter
must
-     * correctly reflect the number of items in <code>iterator</code>.Ê
+     * correctly reflect the number of items in <code>iterator</code>.
      */
     protected final <T> SizedIterator<T> getSizedIterator(final Iterator<T>
iterator, final long size) {
         return new SizedIterator<T>() {

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/Rank.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/Rank.java?rev=982695&r1=982694&r2=982695&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/Rank.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/Rank.java
Thu Aug  5 17:00:53 2010
@@ -16,11 +16,10 @@
  */
 package org.apache.jackrabbit.commons.flat;
 
-import static org.apache.jackrabbit.commons.iterator.Iterators.arrayIterator;
-import static org.apache.jackrabbit.commons.iterator.Iterators.empty;
-
 import java.lang.reflect.Array;
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -192,10 +191,9 @@ public class Rank<T> {
         if (n > 0) {
             take(n, first, values.length - 1);
             first += n;
-            return arrayIterator(values, first - n, first);
-        }
-        else {
-            return empty();
+            return Arrays.asList(values).subList(first - n, first).iterator();
+        } else {
+            return Collections.<T>emptySet().iterator();
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/TreeTraverser.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/TreeTraverser.java?rev=982695&r1=982694&r2=982695&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/TreeTraverser.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/flat/TreeTraverser.java
Thu Aug  5 17:00:53 2010
@@ -16,10 +16,6 @@
  */
 package org.apache.jackrabbit.commons.flat;
 
-import static org.apache.jackrabbit.commons.iterator.Iterators.empty;
-import static org.apache.jackrabbit.commons.iterator.Iterators.iteratorChain;
-import static org.apache.jackrabbit.commons.iterator.Iterators.properties;
-import static org.apache.jackrabbit.commons.iterator.Iterators.singleton;
 import static org.apache.jackrabbit.commons.iterator.LazyIteratorChain.chain;
 
 import javax.jcr.Item;
@@ -28,6 +24,7 @@ import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 
+import java.util.Collections;
 import java.util.Iterator;
 
 /**
@@ -247,16 +244,16 @@ public final class TreeTraverser impleme
      * Returns an iterator of the nodes of the sub-tree rooted at
      * <code>node</code>.
      */
+    @SuppressWarnings("unchecked")
     private Iterator<Node> iterator(Node node) {
         try {
             if (inclusionPolicy.include(node)) {
-                return iteratorChain(singleton(node), chain(childIterators(node)));
+                return chain(singleton(node), chain(childIterators(node)));
             }
             else {
                 return chain(childIterators(node));
             }
-        }
-        catch (RepositoryException e) {
+        } catch (RepositoryException e) {
             errorHandler.call(node, e);
             return empty();
         }
@@ -265,25 +262,21 @@ public final class TreeTraverser impleme
     /**
      * Returns an iterator of iterators of the child nodes of <code>node</code>.
      */
-    private Iterator<Iterator<Node>> childIterators(final Node node) {
+    private Iterator<Iterator<Node>> childIterators(Node node) {
         try {
+            final NodeIterator childNodes = node.getNodes();
             return new Iterator<Iterator<Node>>() {
-                private final NodeIterator childNodes = node.getNodes();
-
                 public boolean hasNext() {
                     return childNodes.hasNext();
                 }
-
                 public Iterator<Node> next() {
                     return iterator(childNodes.nextNode());
                 }
-
                 public void remove() {
                     throw new UnsupportedOperationException();
                 }
             };
-        }
-        catch (RepositoryException e) {
+        } catch (RepositoryException e) {
             errorHandler.call(node, e);
             return empty();
         }
@@ -303,12 +296,12 @@ public final class TreeTraverser impleme
                 return nodes.hasNext();
             }
 
+            @SuppressWarnings("unchecked")
             public Iterator<Property> next() {
                 Node n = nodes.next();
                 try {
-                    return properties(n.getProperties());
-                }
-                catch (RepositoryException e) {
+                    return n.getProperties();
+                } catch (RepositoryException e) {
                     errorHandler.call(n, e);
                     return empty();
                 }
@@ -320,4 +313,15 @@ public final class TreeTraverser impleme
         };
     }
 
+    // -----------------------------------------------------< utility >---
+
+    private static <T> Iterator<T> empty() {
+        return Collections.<T>emptySet().iterator();
+    }
+
+    private <T> Iterator<T> singleton(T value) {
+        return Collections.singleton(value).iterator();
+    }
+
+
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java?rev=982695&r1=982694&r2=982695&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java
Thu Aug  5 17:00:53 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.commons.iterator;
 
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
@@ -42,11 +43,27 @@ public class LazyIteratorChain<T> implem
         return new LazyIteratorChain<T>(iterators);
     }
 
+    /**
+     * Returns the concatenation of all iterators in <code>iterators</code>.
+     *
+     * @param <T>
+     * @param iterators
+     * @return
+     */
+    public static <T> Iterator<T> chain(Iterator<T>... iterators) {
+        return new LazyIteratorChain<T>(iterators);
+    }
+
     public LazyIteratorChain(Iterator<Iterator<T>> iterators) {
         super();
         this.iterators = iterators;
     }
 
+    public LazyIteratorChain(Iterator<T>... iterators) {
+        super();
+        this.iterators = Arrays.asList(iterators).iterator();
+    }
+
     public boolean hasNext() {
         while ((currentIterator == null || !currentIterator.hasNext()) && iterators.hasNext())
{
             currentIterator = iterators.next();



Mime
View raw message