jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r599349 - in /jackrabbit/trunk/jackrabbit-jcr2spi/src: main/java/org/apache/jackrabbit/jcr2spi/ main/java/org/apache/jackrabbit/jcr2spi/hierarchy/ main/java/org/apache/jackrabbit/jcr2spi/version/ test/java/org/apache/jackrabbit/jcr2spi/
Date Thu, 29 Nov 2007 08:59:35 GMT
Author: angela
Date: Thu Nov 29 00:59:23 2007
New Revision: 599349

URL: http://svn.apache.org/viewvc?rev=599349&view=rev
Log:
JCR-1166: JCR2SPI does not provide actual size on RangeIterator.getSize()

Added:
    jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/LazyItemIteratorTest.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/LazyItemIterator.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/NodeEntryImpl.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/version/VersionHistoryImpl.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/LazyItemIterator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/LazyItemIterator.java?rev=599349&r1=599348&r2=599349&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/LazyItemIterator.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/LazyItemIterator.java
Thu Nov 29 00:59:23 2007
@@ -60,6 +60,13 @@
     /** Iterator over HierarchyEntry elements */
     private final Iterator iter;
 
+    /**
+     * The number of items.
+     * Note, that the size may change over the time due to the lazy behaviour
+     * of this iterator that may only upon iteration found out, that a
+     * hierarchy entry has been invalidated or removed in the mean time.
+     */
+    private long size;
     /** the position of the next item */
     private int pos;
 
@@ -75,8 +82,13 @@
     public LazyItemIterator(ItemManager itemMgr, Iterator hierarchyEntryIterator) {
         this.itemMgr = itemMgr;
         this.iter = hierarchyEntryIterator;
-        // prefetch first item
+        if (hierarchyEntryIterator instanceof RangeIterator) {
+            size = ((RangeIterator) hierarchyEntryIterator).getSize();
+        } else {
+            size = UNDEFINED_SIZE;
+        }
         pos = 0;
+        // prefetch first item
         next = prefetchNext();
     }
 
@@ -96,10 +108,10 @@
             ItemId id = (ItemId) itemIds.next();
             entries.add(hierarchyMgr.getHierarchyEntry(id));
         }
-        this.iter = entries.iterator();
-
-        // prefetch first item
+        iter = entries.iterator();
+        size = entries.size();
         pos = 0;
+        // prefetch first item
         next = prefetchNext();
     }
 
@@ -117,10 +129,12 @@
                 nextItem = itemMgr.getItem(entry);
             } catch (ItemNotFoundException e) {
                 log.debug("Ignoring nonexistent item " + entry);
-                // try the next
+                // reduce the size ... and try the next one
+                size--;
             } catch (RepositoryException e) {
                 log.error("failed to fetch item " + entry + ", skipping...", e);
-                // try the next
+                // reduce the size... and try the next one
+                size--;
             }
         }
         return nextItem;
@@ -163,16 +177,21 @@
     }
 
     /**
-     * Always returns -1.
+     * Returns the number of <code>Item</code>s in this iterator or -1 if the
+     * size is unkown.
+     * </p>
+     * Note: The number returned by this method may differ from the number
+     * of <code>Item</code>s actually returned by calls to hasNext() / getNextNode().
+     * This is caused by the lazy instantiation behaviour of this iterator,
+     * that may detect only upon iteration that an Item has been invalidated
+     * or removed in the mean time. As soon as an invalid <code>Item</code> is
+     * detected, the size of this iterator is adjusted.
      *
-     * @return always returns -1.
+     * @return the number of <code>Item</code>s in this iterator.
      * @see RangeIterator#getSize()
      */
     public long getSize() {
-        // Always returns -1, since the entry-iterator may contains items that
-        // are not accessible due to access constraints. -1 seems preferable
-        // to returning a size that is not correct.
-        return LazyItemIterator.UNDEFINED_SIZE;
+        return size;
     }
 
     /**

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/NodeEntryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/NodeEntryImpl.java?rev=599349&r1=599348&r2=599349&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/NodeEntryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/hierarchy/NodeEntryImpl.java
Thu Nov 29 00:59:23 2007
@@ -35,6 +35,7 @@
 import org.apache.jackrabbit.jcr2spi.util.StateUtility;
 import org.apache.jackrabbit.name.NameConstants;
 import org.apache.jackrabbit.name.PathBuilder;
+import org.apache.jackrabbit.commons.iterator.RangeIteratorAdapter;
 import org.apache.commons.collections.iterators.IteratorChain;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -563,7 +564,7 @@
                 entries.add(entry);
             }
         }
-        return Collections.unmodifiableCollection(entries).iterator();
+        return new RangeIteratorAdapter(Collections.unmodifiableCollection(entries));
     }
 
     /**
@@ -683,7 +684,7 @@
             // no need to filter out properties, there are no removed properties
             props = properties.getPropertyEntries();
         }
-        return Collections.unmodifiableCollection(props).iterator();
+        return new RangeIteratorAdapter(Collections.unmodifiableCollection(props));
     }
 
     /**

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/version/VersionHistoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/version/VersionHistoryImpl.java?rev=599349&r1=599348&r2=599349&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/version/VersionHistoryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/version/VersionHistoryImpl.java
Thu Nov 29 00:59:23 2007
@@ -30,6 +30,7 @@
 import org.apache.jackrabbit.conversion.NameException;
 import org.apache.jackrabbit.spi.Path;
 import org.apache.jackrabbit.name.NameConstants;
+import org.apache.jackrabbit.commons.iterator.RangeIteratorAdapter;
 
 import javax.jcr.version.VersionHistory;
 import javax.jcr.version.Version;
@@ -119,7 +120,7 @@
                 versionEntries.add(entry);
             }
         }
-        return new LazyItemIterator(itemMgr, versionEntries.iterator());
+        return new LazyItemIterator(itemMgr, new RangeIteratorAdapter(versionEntries));
     }
 
     /**

Added: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/LazyItemIteratorTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/LazyItemIteratorTest.java?rev=599349&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/LazyItemIteratorTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/LazyItemIteratorTest.java
Thu Nov 29 00:59:23 2007
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.jcr2spi;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.jackrabbit.test.NotExecutableException;
+
+import javax.jcr.Node;
+import javax.jcr.RangeIterator;
+import javax.jcr.RepositoryException;
+
+/**
+ * <code>LazyItemIteratorTest</code> contains implementation specific test
+ * cases, that check if the <code>LazyItemIterator</code> returns a better
+ * estimate for the number of <code>Item</code>s to be available in the
+ * iteration than -1.
+ */
+public class LazyItemIteratorTest extends AbstractJCRTest {
+
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    public void testSizeGreaterMinusOne() throws RepositoryException {
+        RangeIterator it = testRootNode.getProperties();
+        // a node always has at least a single property
+        assertTrue(it.getSize() > 0);
+
+        if (testRootNode.hasNodes()) {
+            it = testRootNode.getNodes();
+            // a node always has at least a single property
+            assertTrue(it.getSize() > 0);
+        }
+    }
+
+    public void testSizeOfEmptyIteratorIsZero() throws RepositoryException {
+        int i = 0;
+        String nameHint = "noExisting";
+        String name = nameHint;
+        while (testRootNode.hasProperty(name)) {
+            name = name + i;
+            i++;
+        }
+        // retrieve PropertyIterator for a name that does not exist as Property
+        RangeIterator it = testRootNode.getProperties(name);
+        assertTrue(it.getSize() == 0);
+
+        name = nameHint;
+        while (testRootNode.hasNode(name)) {
+            name = name + i;
+            i++;
+        }
+        // retrieve NodeIterator for a name that does not exist as Node
+        it = testRootNode.getNodes(name);
+        assertTrue(it.getSize() == 0);
+    }
+
+    public void testSizeShrinksIfInvalidItemFound() throws NotExecutableException, RepositoryException
{
+        RangeIterator it;
+        try {
+            testRootNode.addNode(nodeName1, testNodeType);
+            testRootNode.addNode(nodeName2, testNodeType);
+            Node child = testRootNode.addNode(nodeName3, testNodeType);
+            testRootNode.save();
+
+            it = testRootNode.getNodes();
+            // remove 1 child -> force the iterator to contain an entry that
+            // cannot be resolved into a node.
+            child.remove();
+
+        } catch (RepositoryException e) {
+            throw new NotExecutableException();
+        }
+
+        // now the original size is off by one and will be adjusted automatically
+        long size = it.getSize();
+        long zise = 0;
+        while (it.hasNext()) {
+            it.next();
+            zise++;
+        }
+        // original size is bigger by 1 than the calculated size during the
+        // iteration.
+        assertTrue(size == zise+1);
+        // retrieve size again and check if it has been been adjusted.
+        assertTrue(it.getSize() == zise);
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/LazyItemIteratorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/LazyItemIteratorTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java?rev=599349&r1=599348&r2=599349&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java
Thu Nov 29 00:59:23 2007
@@ -82,6 +82,7 @@
         // various
         suite.addTestSuite(ReplaceNode.class);
         suite.addTestSuite(HierarchyNodeTest.class);
+        suite.addTestSuite(LazyItemIteratorTest.class);
 
         return suite;
     }



Mime
View raw message