jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1360617 - in /jackrabbit/oak/trunk/oak-jcr/src: main/java/org/apache/jackrabbit/oak/jcr/ test/java/org/apache/jackrabbit/oak/jcr/ test/java/org/apache/jackrabbit/oak/jcr/query/
Date Thu, 12 Jul 2012 10:23:28 GMT
Author: jukka
Date: Thu Jul 12 10:23:27 2012
New Revision: 1360617

URL: http://svn.apache.org/viewvc?rev=1360617&view=rev
Log:
OAK-167: Support orderable nodes

Add initial support for iterating over explicitly ordered nodes.
Simplify AbstractRepositoryTest

Added:
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java
Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AbstractRepositoryTest.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CRUDTest.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CompatibilityIssuesTest.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java?rev=1360617&r1=1360616&r2=1360617&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
Thu Jul 12 10:23:27 2012
@@ -16,13 +16,21 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.jcr.InvalidItemStateException;
 
+import org.apache.commons.collections.Predicate;
+import org.apache.commons.collections.iterators.FilterIterator;
+import org.apache.commons.collections.iterators.IteratorChain;
 import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
@@ -162,12 +170,71 @@ public class NodeDelegate extends ItemDe
     }
 
     /**
-     * Get child nodes
+     * Returns an iterator for traversing all the children of this node.
+     * If the node is orderable (there is an "<code>oak:childOrder</code>"
+     * property) then the iterator will return child nodes in the specified
+     * order. Otherwise the ordering of the iterator is undefined.
+     *
      * @return  child nodes of the node
      */
+    @SuppressWarnings("unchecked")
     @Nonnull
     public Iterator<NodeDelegate> getChildren() throws InvalidItemStateException {
-        return nodeDelegateIterator(getTree().getChildren().iterator());
+        Tree tree = getTree();
+        long count = tree.getChildrenCount();
+        if (count == 0) {
+            // Optimise the most common case
+            return Collections.<NodeDelegate>emptySet().iterator();
+        } else if (count == 1) {
+            // Optimise another typical case
+            Tree child = tree.getChildren().iterator().next();
+            NodeDelegate delegate = new NodeDelegate(sessionDelegate, child);
+            return Collections.singleton(delegate).iterator();
+        } else {
+            // TODO: Use a proper namespace for this property?
+            PropertyState order = tree.getProperty("childOrder");
+            if (order == null || !order.isArray()) {
+                // No specified ordering
+                return nodeDelegateIterator(tree.getChildren().iterator());
+            } else {
+                // Collect child nodes in the specified order
+                final Map<String, NodeDelegate> ordered =
+                        new LinkedHashMap<String, NodeDelegate>();
+
+                for (CoreValue value : order.getValues()) {
+                    String name = value.getString();
+                    Tree child = tree.getChild(name);
+                    if (child != null) {
+                        ordered.put(name, new NodeDelegate(sessionDelegate, child));
+                    }
+                }
+
+                if (ordered.size() == count) {
+                    // We have all the child nodes
+                    return ordered.values().iterator();
+                } else {
+                    // The specified ordering didn't cover all the children,
+                    // so return a combined iterator that first iterates
+                    // through the ordered subset and then all the remaining
+                    // children in an undefined order
+                    Predicate predicate = new Predicate() {
+                        @Override
+                        public boolean evaluate(Object object) {
+                            if (object instanceof Tree) {
+                                Tree tree = (Tree) object;
+                                return !ordered.containsKey(tree.getName());
+                            } else {
+                                return false;
+                            }
+                        }
+                    };
+                    return new IteratorChain(
+                            ordered.values().iterator(),
+                            nodeDelegateIterator(new FilterIterator(
+                                    tree.getChildren().iterator(), predicate)));
+                }
+            }
+        }
     }
 
     /**

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AbstractRepositoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AbstractRepositoryTest.java?rev=1360617&r1=1360616&r2=1360617&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AbstractRepositoryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AbstractRepositoryTest.java
Thu Jul 12 10:23:27 2012
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.oak.jcr;
 
 import org.apache.jackrabbit.commons.JcrUtils;
+import org.junit.After;
 
 import javax.jcr.GuestCredentials;
 import javax.jcr.Node;
@@ -34,35 +35,24 @@ import javax.jcr.Session;
  * this instance and clean up the repository when done.
  */
 public abstract class AbstractRepositoryTest {
-    private Repository repository;
-    private Session session;
+    private Repository repository = null;
+    private Session session = null;
 
+    @After
     public void logout() throws RepositoryException {
-        Session cleanupSession = createAnonymousSession();
-        try {
-            Node root = cleanupSession.getRootNode();
-            NodeIterator ns = root.getNodes();
-            while (ns.hasNext()) {
-                ns.nextNode().remove();
-            }
-            cleanupSession.save();
-        }
-        finally {
-            cleanupSession.logout();
-        }
-
         // release session field
         if (session != null) {
             session.logout();
             session = null;
         }
+        // release repository field
+        repository = null;
     }
 
     protected Repository getRepository() throws RepositoryException {
         if (repository == null) {
-            repository = JcrUtils.getRepository("jcr-oak://inmemory/CRUDTest");
+            repository = new RepositoryImpl();
         }
-
         return repository;
     }
 

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CRUDTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CRUDTest.java?rev=1360617&r1=1360616&r2=1360617&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CRUDTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CRUDTest.java
Thu Jul 12 10:23:27 2012
@@ -16,12 +16,9 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
-import org.junit.After;
 import org.junit.Test;
 
-import javax.jcr.GuestCredentials;
 import javax.jcr.Node;
-import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
@@ -30,14 +27,8 @@ import static junit.framework.Assert.ass
 
 public class CRUDTest extends AbstractRepositoryTest {
 
-    @After
-    public void tearDown() throws RepositoryException {
-        logout();
-    }
-
     @Test
     public void testCRUD() throws RepositoryException {
-        Repository repository = getRepository();
         Session session = createAnonymousSession();
         try {
             // Create

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CompatibilityIssuesTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CompatibilityIssuesTest.java?rev=1360617&r1=1360616&r2=1360617&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CompatibilityIssuesTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/CompatibilityIssuesTest.java
Thu Jul 12 10:23:27 2012
@@ -31,11 +31,6 @@ import static org.junit.Assert.fail;
  */
 public class CompatibilityIssuesTest extends AbstractRepositoryTest {
 
-    @After
-    public void tearDown() throws RepositoryException {
-        logout();
-    }
-
     /**
      * Trans-session isolation differs from Jackrabbit 2. Snapshot isolation can
      * result in write skew as this test demonstrates: the check method enforces

Added: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java?rev=1360617&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java
(added)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/OrderableNodesTest.java
Thu Jul 12 10:23:27 2012
@@ -0,0 +1,64 @@
+/*
+ * 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.oak.jcr;
+
+import org.junit.Test;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
+
+public class OrderableNodesTest extends AbstractRepositoryTest {
+
+    @Test
+    public void testSimpleOrdering() throws RepositoryException {
+        Session session = getSession();
+        Node root = session.getRootNode().addNode("test");
+
+        root.addNode("a");
+        root.addNode("b");
+        root.addNode("c");
+
+        NodeIterator iterator;
+
+        root.setProperty("childOrder", "abc".split(""));
+        iterator = root.getNodes();
+        assertEquals("a", iterator.nextNode().getName());
+        assertEquals("b", iterator.nextNode().getName());
+        assertEquals("c", iterator.nextNode().getName());
+        assertFalse(iterator.hasNext());
+
+        root.setProperty("childOrder", "cab".split(""));
+        iterator = root.getNodes();
+        assertEquals("c", iterator.nextNode().getName());
+        assertEquals("a", iterator.nextNode().getName());
+        assertEquals("b", iterator.nextNode().getName());
+        assertFalse(iterator.hasNext());
+
+        root.setProperty("childOrder", "bc".split(""));
+        iterator = root.getNodes();
+        assertEquals("b", iterator.nextNode().getName());
+        assertEquals("c", iterator.nextNode().getName());
+        assertEquals("a", iterator.nextNode().getName());
+        assertFalse(iterator.hasNext());
+    }
+
+}

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java?rev=1360617&r1=1360616&r2=1360617&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
Thu Jul 12 10:23:27 2012
@@ -62,7 +62,6 @@ import javax.jcr.observation.EventListen
 import javax.jcr.observation.ObservationManager;
 
 import org.apache.jackrabbit.JcrConstants;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -103,11 +102,6 @@ public class RepositoryTest extends Abst
         }
     }
 
-    @After
-    public void tearDown() throws RepositoryException {
-        logout();
-    }
-
     @Test
     public void createRepository() throws RepositoryException {
         Repository repository = getRepository();

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java?rev=1360617&r1=1360616&r2=1360617&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryTest.java
Thu Jul 12 10:23:27 2012
@@ -32,7 +32,6 @@ import javax.jcr.query.QueryResult;
 import javax.jcr.query.Row;
 import javax.jcr.query.RowIterator;
 import org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest;
-import org.junit.After;
 import org.junit.Test;
 
 /**
@@ -40,11 +39,6 @@ import org.junit.Test;
  */
 public class QueryTest extends AbstractRepositoryTest {
 
-    @After
-    public void tearDown() throws RepositoryException {
-        logout();
-    }
-
     @SuppressWarnings("deprecation")
     @Test
     public void simple() throws RepositoryException {



Mime
View raw message