chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r954775 - in /incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src: main/java/org/apache/chemistry/opencmis/inmemory/query/ test/java/org/apache/chemistry/opencmis/inmemory/query/
Date Tue, 15 Jun 2010 09:14:41 GMT
Author: jens
Date: Tue Jun 15 09:14:41 2010
New Revision: 954775

URL: http://svn.apache.org/viewvc?rev=954775&view=rev
Log:
CMIS-216
implement  order by for one sort spec in in-memory

Modified:
    incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/query/InMemoryQueryProcessor.java
    incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/query/EvalQueryTest.java

Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/query/InMemoryQueryProcessor.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/query/InMemoryQueryProcessor.java?rev=954775&r1=954774&r2=954775&view=diff
==============================================================================
--- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/query/InMemoryQueryProcessor.java
(original)
+++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/query/InMemoryQueryProcessor.java
Tue Jun 15 09:14:41 2010
@@ -21,6 +21,8 @@ package org.apache.chemistry.opencmis.in
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Map;
@@ -35,6 +37,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
 import org.apache.chemistry.opencmis.commons.impl.dataobjects.ObjectListImpl;
 import org.apache.chemistry.opencmis.inmemory.TypeManager;
+import org.apache.chemistry.opencmis.inmemory.query.QueryObject.SortSpec;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
 import org.apache.chemistry.opencmis.inmemory.types.PropertyCreationHelper;
 import org.apache.chemistry.opencmis.server.support.query.CalendarHelper;
@@ -90,7 +93,9 @@ public class InMemoryQueryProcessor impl
     public ObjectList buildResultList(TypeManager tm, String user, 
             Boolean includeAllowableActions, IncludeRelationships includeRelationships, String
renditionFilter,
             BigInteger maxItems, BigInteger skipCount) {
-        // TODO sort according to order by criteria
+        
+        sortMatches();
+
         ObjectListImpl res = new ObjectListImpl();
         List<ObjectData> objDataList = new ArrayList<ObjectData>();
         Map<String, String> props = queryObj.getRequestedProperties();
@@ -120,6 +125,45 @@ public class InMemoryQueryProcessor impl
         }
         return false;
     }
+    
+    private void sortMatches() {
+        final List<SortSpec> orderBy = queryObj.getOrderBys();
+        if (orderBy.size() > 1)
+            LOG.warn("ORDER BY has more than one sort criterium, all but the first are ignored.");
+        class ResultComparator implements Comparator<StoredObject> {
+
+            public int compare(StoredObject so1, StoredObject so2) {
+                SortSpec s = orderBy.get(0);
+                CmisSelector sel = s.getSelector();
+                int result;
+                
+                if (sel instanceof ColumnReference) {
+                    String propId = ((ColumnReference)sel).getPropertyId();
+                    Object propVal1 = so1.getProperties().get(propId).getFirstValue();
+                    Object propVal2 = so2.getProperties().get(propId).getFirstValue();
+                    if (propVal1 == null && propVal2 == null)
+                        result = 0;
+                    else if (propVal1 == null)
+                        result = -1;
+                    else if (propVal2 == null)
+                        result = 1;
+                    else 
+                        result = ((Comparable)propVal1).compareTo(propVal2);            
       
+                } else {
+                    String funcName = ((FunctionReference)sel).getName();
+                    // evaluate function here, currently ignore
+                    result = 0;
+                }
+                if (!s.isAscending())
+                    result = -result;
+                return result;
+            }
+        }
+
+        if (orderBy.size() > 0)
+            Collections.sort(matches, new ResultComparator());
+        
+    }
 
     private void match(StoredObject so) {
         // log.debug("Tree is: " + (whereTree == null ? "(empty)" : whereTree.toStringTree()));

Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/query/EvalQueryTest.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/query/EvalQueryTest.java?rev=954775&r1=954774&r2=954775&view=diff
==============================================================================
--- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/query/EvalQueryTest.java
(original)
+++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/query/EvalQueryTest.java
Tue Jun 15 09:14:41 2010
@@ -365,6 +365,111 @@ public class EvalQueryTest extends Abstr
         assertTrue(resultContains("epsilon", res));        
     }
 
+    @Test
+    public void testOrderByString() {
+        String statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + UnitTestTypeSystemCreator.PROP_ID_STRING;
+        ObjectList res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("alpha", 0, res));
+        assertTrue(resultContainsAtPos("beta", 1, res));
+        assertTrue(resultContainsAtPos("delta", 2, res));
+        assertTrue(resultContainsAtPos("epsilon", 3, res));
+        assertTrue(resultContainsAtPos("gamma", 4, res));
+
+        statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + UnitTestTypeSystemCreator.PROP_ID_STRING
+ " DESC";
+        res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("alpha", 4, res));
+        assertTrue(resultContainsAtPos("beta", 3, res));
+        assertTrue(resultContainsAtPos("delta", 2, res));
+        assertTrue(resultContainsAtPos("epsilon", 1, res));
+        assertTrue(resultContainsAtPos("gamma", 0, res));
+}
+
+    @Test
+    public void testOrderByInteger() {
+        String statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + PROP_ID_INT;
+        ObjectList res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("alpha", 0, res));
+        assertTrue(resultContainsAtPos("beta", 1, res));
+        assertTrue(resultContainsAtPos("gamma", 2, res));
+        assertTrue(resultContainsAtPos("delta", 3, res));
+        assertTrue(resultContainsAtPos("epsilon", 4, res));
+
+        statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + PROP_ID_INT + " DESC";
+        res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("alpha", 4, res));
+        assertTrue(resultContainsAtPos("beta", 3, res));
+        assertTrue(resultContainsAtPos("gamma", 2, res));
+        assertTrue(resultContainsAtPos("delta", 1, res));
+        assertTrue(resultContainsAtPos("epsilon", 0, res));
+}
+
+    @Test
+    public void testOrderByDecimal() {
+        String statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + PROP_ID_DECIMAL;
+        ObjectList res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("alpha", 0, res));
+        assertTrue(resultContainsAtPos("beta", 1, res));
+        assertTrue(resultContainsAtPos("delta", 2, res));
+        assertTrue(resultContainsAtPos("gamma", 3, res));
+        assertTrue(resultContainsAtPos("epsilon", 4, res));
+
+        statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + PROP_ID_DECIMAL + "
DESC";
+        res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("alpha", 4, res));
+        assertTrue(resultContainsAtPos("beta", 3, res));
+        assertTrue(resultContainsAtPos("delta", 2, res));
+        assertTrue(resultContainsAtPos("gamma", 1, res));
+        assertTrue(resultContainsAtPos("epsilon", 0, res));
+    }
+
+    @Test
+    public void testOrderByDate() {
+        String statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + PROP_ID_DATETIME;
+        ObjectList res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("beta", 0, res));
+        assertTrue(resultContainsAtPos("alpha", 1, res));
+        assertTrue(resultContainsAtPos("gamma", 2, res));
+        assertTrue(resultContainsAtPos("delta", 3, res));
+        assertTrue(resultContainsAtPos("epsilon", 4, res));
+
+        statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + PROP_ID_DATETIME + "
DESC";
+        res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("beta", 4, res));
+        assertTrue(resultContainsAtPos("alpha", 3, res));
+        assertTrue(resultContainsAtPos("gamma", 2, res));
+        assertTrue(resultContainsAtPos("delta", 1, res));
+        assertTrue(resultContainsAtPos("epsilon", 0, res));
+    }
+
+    @Test
+    public void testOrderByBool() {
+        String statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + PROP_ID_BOOLEAN;
+        ObjectList res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("beta", 0, res) || resultContainsAtPos("beta", 1,
res));
+        assertTrue(resultContainsAtPos("epsilon", 0, res) || resultContainsAtPos("epsilon",
1, res));
+        assertTrue(resultContainsAtPos("alpha", 2, res) || resultContainsAtPos("alpha", 3,
res) || resultContainsAtPos("alpha", 4, res));
+        assertTrue(resultContainsAtPos("gamma", 2, res) || resultContainsAtPos("gamma", 3,
res) || resultContainsAtPos("gamma", 4, res));
+        assertTrue(resultContainsAtPos("delta", 2, res) || resultContainsAtPos("delta", 3,
res) || resultContainsAtPos("delta", 4, res));
+
+        statement = "SELECT * FROM " + COMPLEX_TYPE + " ORDER BY " + PROP_ID_BOOLEAN + "
DESC";
+        res = doQuery(statement);
+        assertEquals(5, res.getObjects().size());
+        assertTrue(resultContainsAtPos("beta", 3, res) || resultContainsAtPos("beta", 4,
res));
+        assertTrue(resultContainsAtPos("epsilon", 3, res) || resultContainsAtPos("epsilon",
4, res));
+        assertTrue(resultContainsAtPos("alpha", 2, res) || resultContainsAtPos("alpha", 1,
res) || resultContainsAtPos("alpha", 0, res));
+        assertTrue(resultContainsAtPos("gamma", 2, res) || resultContainsAtPos("gamma", 1,
res) || resultContainsAtPos("gamma", 0, res));
+        assertTrue(resultContainsAtPos("delta", 2, res) || resultContainsAtPos("delta", 1,
res) || resultContainsAtPos("delta", 0, res));
+}
+
     private ObjectList doQuery(String queryString) {
         log.debug("\nExecuting query: " + queryString);
         ObjectList res = fDiscSvc.query(fRepositoryId, queryString, false, false,
@@ -383,4 +488,9 @@ public class EvalQueryTest extends Abstr
         }
         return false;
     }
+
+    private boolean resultContainsAtPos(String name, int index, ObjectList results) {
+        String nameProp = (String) results.getObjects().get(index).getProperties().getProperties().get(PropertyIds.NAME).getFirstValue();
+        return name.equals(nameProp);
+    }
 }



Mime
View raw message