jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r164248 - in /incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene: DocOrderNodeIteratorImpl.java NodeIteratorImpl.java
Date Fri, 22 Apr 2005 15:34:38 GMT
Author: mreutegg
Date: Fri Apr 22 08:34:37 2005
New Revision: 164248

URL: http://svn.apache.org/viewcvs?rev=164248&view=rev
Log:
Create document order NodeIterator only when needed.

Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/DocOrderNodeIteratorImpl.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/NodeIteratorImpl.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/DocOrderNodeIteratorImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/DocOrderNodeIteratorImpl.java?rev=164248&r1=164247&r2=164248&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/DocOrderNodeIteratorImpl.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/DocOrderNodeIteratorImpl.java
Fri Apr 22 08:34:37 2005
@@ -37,7 +37,7 @@
     private static final Logger log = Logger.getLogger(DocOrderNodeIteratorImpl.class);
 
     /** A node iterator with ordered nodes */
-    private final NodeIteratorImpl orderedNodes;
+    private NodeIteratorImpl orderedNodes;
 
     /**
      * Creates a <code>DocOrderNodeIteratorImpl</code> that orders the nodes
@@ -48,83 +48,13 @@
      */
     DocOrderNodeIteratorImpl(final ItemManager itemMgr, String[] uuids, Float[] scores) {
         super(itemMgr, uuids, scores);
-        long time = System.currentTimeMillis();
-        ScoreNode[] nodes = new ScoreNode[uuids.length];
-        for (int i = 0; i < uuids.length; i++) {
-            nodes[i] = new ScoreNode(uuids[i], scores[i]);
-        }
-
-        Arrays.sort(nodes, new Comparator() {
-            public int compare(Object o1, Object o2) {
-                ScoreNode n1 = (ScoreNode) o1;
-                ScoreNode n2 = (ScoreNode) o2;
-                try {
-                    NodeImpl node1 = (NodeImpl) itemMgr.getItem(new NodeId(n1.uuid));
-                    NodeImpl node2 = (NodeImpl) itemMgr.getItem(new NodeId(n2.uuid));
-                    Path.PathElement[] path1 = node1.getPrimaryPath().getElements();
-                    Path.PathElement[] path2 = node2.getPrimaryPath().getElements();
-
-                    // find nearest common ancestor
-                    int commonDepth = 0; // root
-                    while (path1.length > commonDepth && path2.length > commonDepth)
{
-                        if (path1[commonDepth].equals(path2[commonDepth])) {
-                            commonDepth++;
-                        } else {
-                            break;
-                        }
-                    }
-                    // path elements at last depth were equal
-                    commonDepth--;
-
-                    // check if either path is an ancestor of the other
-                    if (path1.length - 1 == commonDepth) {
-                        // path1 itself is ancestor of path2
-                        return -1;
-                    }
-                    if (path2.length - 1 == commonDepth) {
-                        // path2 itself is ancestor of path1
-                        return 1;
-                    }
-                    // get common ancestor node
-                    NodeImpl commonNode = (NodeImpl) node1.getAncestor(commonDepth);
-                    // move node1/node2 to the commonDepth + 1
-                    // node1 and node2 then will be child nodes of commonNode
-                    node1 = (NodeImpl) node1.getAncestor(commonDepth + 1);
-                    node2 = (NodeImpl) node2.getAncestor(commonDepth + 1);
-                    for (NodeIterator it = commonNode.getNodes(); it.hasNext();) {
-                        Node child = it.nextNode();
-                        if (child.isSame(node1)) {
-                            return -1;
-                        } else if (child.isSame(node2)) {
-                            return 1;
-                        }
-                    }
-                    log.error("Internal error: unable to determine document order of nodes:");
-                    log.error("\tNode1: " + node1.getPath());
-                    log.error("\tNode2: " + node2.getPath());
-                    return 0;
-                } catch (RepositoryException e) {
-                    log.error("Exception while sorting nodes in document order: " + e.toString(),
e);
-                    // todo ???
-                    return 0;
-                }
-            }
-        });
-
-        for (int i = 0; i < nodes.length; i++) {
-            uuids[i] = nodes[i].uuid;
-            scores[i] = nodes[i].score;
-        }
-        if (log.isDebugEnabled()) {
-            log.debug("" + uuids.length + " node(s) ordered in " + (System.currentTimeMillis()
- time) + " ms");
-        }
-        orderedNodes = new NodeIteratorImpl(itemMgr, uuids, scores);
     }
 
     /**
      * {@inheritDoc}
      */
     public Node nextNode() {
+        initOrderedIterator();
         return orderedNodes.nextNode();
     }
 
@@ -132,6 +62,7 @@
      * {@inheritDoc}
      */
     public void skip(long skipNum) {
+        initOrderedIterator();
         orderedNodes.skip(skipNum);
     }
 
@@ -139,13 +70,14 @@
      * {@inheritDoc}
      */
     public long getSize() {
-        return orderedNodes.getSize();
+        return uuids.length;
     }
 
     /**
      * {@inheritDoc}
      */
     public long getPosition() {
+        initOrderedIterator();
         return orderedNodes.getPosition();
     }
 
@@ -160,6 +92,7 @@
      * {@inheritDoc}
      */
     public boolean hasNext() {
+        initOrderedIterator();
         return orderedNodes.hasNext();
     }
 
@@ -167,6 +100,7 @@
      * {@inheritDoc}
      */
     public Object next() {
+        initOrderedIterator();
         return orderedNodes.next();
     }
 
@@ -174,6 +108,7 @@
      * {@inheritDoc}
      */
     float getScore() {
+        initOrderedIterator();
         return orderedNodes.getScore();
     }
 
@@ -181,10 +116,90 @@
      * {@inheritDoc}
      */
     NodeImpl nextNodeImpl() {
+        initOrderedIterator();
         return orderedNodes.nextNodeImpl();
     }
 
     //------------------------< internal >--------------------------------------
+
+    /**
+     * Initializes the NodeIterator in document order, if needed.
+     */
+    private void initOrderedIterator() {
+        if (orderedNodes == null) {
+            long time = System.currentTimeMillis();
+            ScoreNode[] nodes = new ScoreNode[uuids.length];
+            for (int i = 0; i < uuids.length; i++) {
+                nodes[i] = new ScoreNode(uuids[i], scores[i]);
+            }
+
+            Arrays.sort(nodes, new Comparator() {
+                public int compare(Object o1, Object o2) {
+                    ScoreNode n1 = (ScoreNode) o1;
+                    ScoreNode n2 = (ScoreNode) o2;
+                    try {
+                        NodeImpl node1 = (NodeImpl) itemMgr.getItem(new NodeId(n1.uuid));
+                        NodeImpl node2 = (NodeImpl) itemMgr.getItem(new NodeId(n2.uuid));
+                        Path.PathElement[] path1 = node1.getPrimaryPath().getElements();
+                        Path.PathElement[] path2 = node2.getPrimaryPath().getElements();
+
+                        // find nearest common ancestor
+                        int commonDepth = 0; // root
+                        while (path1.length > commonDepth && path2.length >
commonDepth) {
+                            if (path1[commonDepth].equals(path2[commonDepth])) {
+                                commonDepth++;
+                            } else {
+                                break;
+                            }
+                        }
+                        // path elements at last depth were equal
+                        commonDepth--;
+
+                        // check if either path is an ancestor of the other
+                        if (path1.length - 1 == commonDepth) {
+                            // path1 itself is ancestor of path2
+                            return -1;
+                        }
+                        if (path2.length - 1 == commonDepth) {
+                            // path2 itself is ancestor of path1
+                            return 1;
+                        }
+                        // get common ancestor node
+                        NodeImpl commonNode = (NodeImpl) node1.getAncestor(commonDepth);
+                        // move node1/node2 to the commonDepth + 1
+                        // node1 and node2 then will be child nodes of commonNode
+                        node1 = (NodeImpl) node1.getAncestor(commonDepth + 1);
+                        node2 = (NodeImpl) node2.getAncestor(commonDepth + 1);
+                        for (NodeIterator it = commonNode.getNodes(); it.hasNext();) {
+                            Node child = it.nextNode();
+                            if (child.isSame(node1)) {
+                                return -1;
+                            } else if (child.isSame(node2)) {
+                                return 1;
+                            }
+                        }
+                        log.error("Internal error: unable to determine document order of
nodes:");
+                        log.error("\tNode1: " + node1.getPath());
+                        log.error("\tNode2: " + node2.getPath());
+                        return 0;
+                    } catch (RepositoryException e) {
+                        log.error("Exception while sorting nodes in document order: " + e.toString(),
e);
+                        // todo ???
+                        return 0;
+                    }
+                }
+            });
+
+            for (int i = 0; i < nodes.length; i++) {
+                uuids[i] = nodes[i].uuid;
+                scores[i] = nodes[i].score;
+            }
+            if (log.isDebugEnabled()) {
+                log.debug("" + uuids.length + " node(s) ordered in " + (System.currentTimeMillis()
- time) + " ms");
+            }
+            orderedNodes = new NodeIteratorImpl(itemMgr, uuids, scores);
+        }
+    }
 
     private static final class ScoreNode {
 

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/NodeIteratorImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/NodeIteratorImpl.java?rev=164248&r1=164247&r2=164248&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/NodeIteratorImpl.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/query/lucene/NodeIteratorImpl.java
Fri Apr 22 08:34:37 2005
@@ -36,13 +36,13 @@
     private static final Logger log = Logger.getLogger(NodeIteratorImpl.class);
 
     /** The UUIDs of the nodes in the result set */
-    private final String[] uuids;
+    protected final String[] uuids;
 
     /** The score values for the nodes in the result set */
-    private final Float[] scores;
+    protected final Float[] scores;
 
     /** ItemManager to turn UUIDs into Node instances */
-    private final ItemManager itemMgr;
+    protected final ItemManager itemMgr;
 
     /** Current position in the UUID array */
     private int pos = 0;



Mime
View raw message