jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alexparvule...@apache.org
Subject svn commit: r1186266 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/query/lucene/ main/java/org/apache/jackrabbit/core/query/lucene/join/ main/java/org/apache/jackrabbit/core/query/lucene/sort/ test/java/org/apache/jac...
Date Wed, 19 Oct 2011 15:23:48 GMT
Author: alexparvulescu
Date: Wed Oct 19 15:23:48 2011
New Revision: 1186266

URL: http://svn.apache.org/viewvc?rev=1186266&view=rev
Log:
JCR-2959 SQL2 QueryEngine should use Lucene for sort
 - removed redundant 'reversed' info, as lucene is already reversing the comparator
  - cosmetics
  - added option to enable the feature from the SQL2OrderByTest test

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SharedFieldComparatorSource.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/AbstractFieldComparator.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparator.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparatorSource.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/ValueComparableWrapper.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2OrderByTest.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SharedFieldComparatorSource.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SharedFieldComparatorSource.java?rev=1186266&r1=1186265&r2=1186266&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SharedFieldComparatorSource.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SharedFieldComparatorSource.java
Wed Oct 19 15:23:48 2011
@@ -188,10 +188,8 @@ public class SharedFieldComparatorSource
         @Override
         protected Comparable sortValue(int doc) {
             try {
-                int idx = readerIndex(doc);
-                IndexReader reader = readers.get(idx);
-                Document document = reader.document(doc - starts[idx], FieldSelectors.UUID);
-                String uuid = document.get(FieldNames.UUID);
+                final String uuid = getUUIDForIndex(doc);
+                
                 Path path = hmgr.getPath(new NodeId(uuid));
                 PathBuilder builder = new PathBuilder(path);
                 builder.addAll(propertyName.getElements());

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java?rev=1186266&r1=1186265&r2=1186266&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/join/QueryEngine.java
Wed Oct 19 15:23:48 2011
@@ -515,7 +515,7 @@ public class QueryEngine {
                 sortFields.add(new SortField(null, SortField.SCORE, !isAsc));
             } else {
                 // TODO use native sort if available
-                sortFields.add(new SortField(p, dofcs, isAsc));
+                sortFields.add(new SortField(p, dofcs, !isAsc));
             }
         }
         return sortFields.toArray(new SortField[sortFields.size()]);

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/AbstractFieldComparator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/AbstractFieldComparator.java?rev=1186266&r1=1186265&r2=1186266&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/AbstractFieldComparator.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/AbstractFieldComparator.java
Wed Oct 19 15:23:48 2011
@@ -21,7 +21,9 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.jackrabbit.core.query.lucene.FieldComparatorBase;
+import org.apache.jackrabbit.core.query.lucene.FieldNames;
 import org.apache.jackrabbit.core.query.lucene.MultiIndexReader;
+import org.apache.lucene.document.Document;
 import org.apache.lucene.index.IndexReader;
 
 /**
@@ -138,4 +140,11 @@ public abstract class AbstractFieldCompa
             readers.add(reader);
         }
     }
+
+    protected String getUUIDForIndex(int doc) throws IOException {
+        int idx = readerIndex(doc);
+        IndexReader reader = readers.get(idx);
+        Document document = reader.document(doc - starts[idx]);
+        return document.get(FieldNames.UUID);
+    }
 }
\ No newline at end of file

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparator.java?rev=1186266&r1=1186265&r2=1186266&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparator.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparator.java
Wed Oct 19 15:23:48 2011
@@ -22,9 +22,6 @@ import javax.jcr.Value;
 import javax.jcr.query.qom.Ordering;
 
 import org.apache.jackrabbit.commons.query.qom.OperandEvaluator;
-import org.apache.jackrabbit.core.query.lucene.FieldNames;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.index.IndexReader;
 
 public final class DynamicOperandFieldComparator extends
         AbstractFieldComparator {
@@ -32,28 +29,23 @@ public final class DynamicOperandFieldCo
     private final Session session;
     private final OperandEvaluator evaluator;
     private final Ordering ordering;
-    private final boolean reversed;
 
     public DynamicOperandFieldComparator(final Session session,
             final OperandEvaluator evaluator, final Ordering ordering,
-            int numHits, final boolean reversed) {
+            int numHits) {
         super(numHits);
         this.session = session;
         this.evaluator = evaluator;
         this.ordering = ordering;
-        this.reversed = reversed;
     }
 
     @Override
-    protected Comparable sortValue(int doc) {
+    protected Comparable<ValueComparableWrapper> sortValue(int doc) {
         try {
-            int idx = readerIndex(doc);
-            IndexReader reader = readers.get(idx);
-            Document document = reader.document(doc - starts[idx]);
-            final String uuid = document.get(FieldNames.UUID);
+            final String uuid = getUUIDForIndex(doc);
             final Node n = session.getNodeByIdentifier(uuid);
-            Value[] v = evaluator.getValues(ordering.getOperand(), n);
-            return new ValueComparableWrapper(v, reversed);
+            final Value[] v = evaluator.getValues(ordering.getOperand(), n);
+            return new ValueComparableWrapper(v);
         } catch (Exception e) {
             e.printStackTrace();
         }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparatorSource.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparatorSource.java?rev=1186266&r1=1186265&r2=1186266&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparatorSource.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/DynamicOperandFieldComparatorSource.java
Wed Oct 19 15:23:48 2011
@@ -47,7 +47,6 @@ public final class DynamicOperandFieldCo
     public FieldComparator newComparator(String fieldname, int numHits,
             int sortPos, boolean reversed) throws IOException {
         final Ordering o = orderByProperties.get(fieldname);
-        return new DynamicOperandFieldComparator(session, evaluator, o,
-                numHits, reversed);
+        return new DynamicOperandFieldComparator(session, evaluator, o, numHits);
     }
 }
\ No newline at end of file

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/ValueComparableWrapper.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/ValueComparableWrapper.java?rev=1186266&r1=1186265&r2=1186266&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/ValueComparableWrapper.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/sort/ValueComparableWrapper.java
Wed Oct 19 15:23:48 2011
@@ -21,29 +21,16 @@ import javax.jcr.Value;
 import org.apache.jackrabbit.core.query.lucene.join.ValueComparator;
 
 class ValueComparableWrapper implements Comparable<ValueComparableWrapper> {
-    private final ValueComparator comparator = new ValueComparator();
+    private final static ValueComparator comparator = new ValueComparator();
 
     private final Value[] v;
-    private final boolean reversed;
 
-    public ValueComparableWrapper(final Value[] v, final boolean reversed) {
+    public ValueComparableWrapper(final Value[] v) {
         this.v = v;
-        this.reversed = reversed;
-    }
-
-    public Value[] getValue() {
-        return v;
     }
 
     public int compareTo(ValueComparableWrapper o) {
-        final int d = compare(v, o.getValue());
-        if (d != 0) {
-            if (reversed) {
-                return -d;
-            }
-            return d;
-        }
-        return 0;
+        return compare(v, o.v);
     }
 
     private int compare(Value[] a, Value[] b) {
@@ -55,4 +42,4 @@ class ValueComparableWrapper implements 
         }
         return a.length - b.length;
     }
-}
\ No newline at end of file
+}

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2OrderByTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2OrderByTest.java?rev=1186266&r1=1186265&r2=1186266&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2OrderByTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/SQL2OrderByTest.java
Wed Oct 19 15:23:48 2011
@@ -29,6 +29,12 @@ import org.apache.jackrabbit.commons.Jcr
 public class SQL2OrderByTest extends AbstractQueryTest {
 
     // TODO order by aggregate test?
+    // TODO enable the test once the native sort is properly handled.
+
+    static {
+        // To see JCR-2959 in action, enable the following
+        // System.setProperty(QueryEngine.NATIVE_SORT_SYSTEM_PROPERTY, "true");
+    }
 
     @Override
     protected void setUp() throws Exception {
@@ -73,77 +79,131 @@ public class SQL2OrderByTest extends Abs
         Node n2 = testRootNode.addNode("node2");
         Node n3 = testRootNode.addNode("node3");
 
-        n1.setProperty("text", "aaa");
         n1.setProperty("value", 3);
-        n2.setProperty("text", "bbb");
-        n2.setProperty("value", 2);
-        n3.setProperty("text", "ccc");
-        n3.setProperty("value", 1);
+        n2.setProperty("value", 1);
+        n3.setProperty("value", 2);
 
         testRootNode.getSession().save();
 
         QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] WHERE ISCHILDNODE(["
-                + testRoot + "]) ORDER BY [value] desc");
-        checkSeq(qr, new Node[] { n1, n2, n3 });
+                + testRoot + "]) ORDER BY [value]");
+        checkSeq(qr, new Node[] { n2, n3, n1 });
     }
 
-    public void testOrderByVal2() throws RepositoryException {
+    public void testOrderByValDesc() throws RepositoryException {
 
         Node n1 = testRootNode.addNode("node1");
         Node n2 = testRootNode.addNode("node2");
         Node n3 = testRootNode.addNode("node3");
 
-        n1.setProperty("text", "aaa");
         n1.setProperty("value", 3);
-        n2.setProperty("text", "bbb");
-        n2.setProperty("value", 2);
-        n3.setProperty("text", "ccc");
-        n3.setProperty("value", 1);
+        n2.setProperty("value", 1);
+        n3.setProperty("value", 2);
 
         testRootNode.getSession().save();
 
         QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] WHERE ISCHILDNODE(["
-                + testRoot + "]) ORDER BY [value]");
-        checkSeq(qr, new Node[] { n3, n2, n1 });
+                + testRoot + "]) ORDER BY [value] desc");
+        checkSeq(qr, new Node[] { n1, n3, n2 });
+    }
+
+    public void testOrderByValMult() throws RepositoryException {
+
+        Node n1 = testRootNode.addNode("node1");
+        Node n2 = testRootNode.addNode("node2");
+        Node n3 = testRootNode.addNode("node3");
+
+        n1.setProperty("value", 2);
+        n1.setProperty("text", "b");
+
+        n2.setProperty("value", 1);
+        n2.setProperty("text", "x");
+
+        n3.setProperty("value", 2);
+        n3.setProperty("text", "a");
+
+        testRootNode.getSession().save();
+
+        QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] WHERE ISCHILDNODE(["
+                + testRoot + "]) ORDER BY [value], [text]");
+        checkSeq(qr, new Node[] { n2, n3, n1 });
+    }
+
+    public void testOrderByValMultDesc() throws RepositoryException {
+
+        Node n1 = testRootNode.addNode("node1");
+        Node n2 = testRootNode.addNode("node2");
+        Node n3 = testRootNode.addNode("node3");
+
+        n1.setProperty("value", 2);
+        n1.setProperty("text", "b");
+
+        n2.setProperty("value", 1);
+        n2.setProperty("text", "x");
+
+        n3.setProperty("value", 2);
+        n3.setProperty("text", "a");
+
+        testRootNode.getSession().save();
+
+        QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] WHERE ISCHILDNODE(["
+                + testRoot + "]) ORDER BY [value] desc, [text] desc");
+        checkSeq(qr, new Node[] { n1, n3, n2 });
+    }
+
+    public void testOrderByValMultMix() throws RepositoryException {
+
+        Node n1 = testRootNode.addNode("node1");
+        Node n2 = testRootNode.addNode("node2");
+        Node n3 = testRootNode.addNode("node3");
+
+        n1.setProperty("value", 2);
+        n1.setProperty("text", "b");
+
+        n2.setProperty("value", 1);
+        n2.setProperty("text", "x");
+
+        n3.setProperty("value", 2);
+        n3.setProperty("text", "a");
+
+        testRootNode.getSession().save();
+
+        QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] WHERE ISCHILDNODE(["
+                + testRoot + "]) ORDER BY [value], [text] desc");
+        checkSeq(qr, new Node[] { n2, n1, n3 });
     }
 
-    public void testOrderByFunction() throws RepositoryException {
+    public void testOrderByFnc() throws RepositoryException {
 
         Node n1 = testRootNode.addNode("node1", "nt:unstructured");
         Node n2 = testRootNode.addNode("node2", "nt:unstructured");
         Node n3 = testRootNode.addNode("node3", "nt:unstructured");
 
-        n1.setProperty("t", "aa");
-        n1.setProperty("value", 3);
-        n2.setProperty("t", "bbb");
-        n2.setProperty("value", 2);
-        n3.setProperty("t", "ccc");
-        n3.setProperty("value", 1);
+        n1.setProperty("value", "aaa");
+        n2.setProperty("value", "a");
+        n3.setProperty("value", "aa");
 
         testRootNode.getSession().save();
 
-        QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] as sissy WHERE ISCHILDNODE(["
-                + testRoot + "]) ORDER BY LENGTH([t]), t");
-        checkSeq(qr, new Node[] { n1, n2, n3 });
+        QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] WHERE ISCHILDNODE(["
+                + testRoot + "]) ORDER BY LENGTH([value])");
+        checkSeq(qr, new Node[] { n2, n3, n1 });
     }
 
-    public void testOrderByFunction2() throws RepositoryException {
+    public void testOrderByFncDesc() throws RepositoryException {
 
         Node n1 = testRootNode.addNode("node1", "nt:unstructured");
         Node n2 = testRootNode.addNode("node2", "nt:unstructured");
         Node n3 = testRootNode.addNode("node3", "nt:unstructured");
 
-        n1.setProperty("t", "aa");
-        n1.setProperty("value", 3);
-        n2.setProperty("t", "bbb");
-        n2.setProperty("value", 2);
-        n3.setProperty("t", "ccc");
-        n3.setProperty("value", 1);
+        n1.setProperty("value", "aaa");
+        n2.setProperty("value", "a");
+        n3.setProperty("value", "aa");
 
         testRootNode.getSession().save();
 
-        QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] as sissy WHERE ISCHILDNODE(["
-                + testRoot + "]) ORDER BY LENGTH([t]), t desc");
+        QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] WHERE ISCHILDNODE(["
+                + testRoot + "]) ORDER BY LENGTH([value]) desc");
         checkSeq(qr, new Node[] { n1, n3, n2 });
     }
 



Mime
View raw message