← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21566: expose in-memory ordering for web-api requests, allows ordering on any kind of simple property, q...

 

------------------------------------------------------------
revno: 21566
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2016-01-04 13:04:35 +0700
message:
  expose in-memory ordering for web-api requests, allows ordering on any kind of simple property, query engine will optimize depending on if property is persisted or not
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Order.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Query.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderOptions.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2016-01-04 06:04:35 +0000
@@ -28,12 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
 import org.hibernate.Criteria;
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Projections;
@@ -44,13 +38,19 @@
 import org.hisp.dhis.schema.Schema;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
 /**
  * Implementation of QueryEngine that uses Hibernate Criteria and
  * supports idObjects only.
  *
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public class CriteriaQueryEngine<T extends IdentifiableObject> 
+public class CriteriaQueryEngine<T extends IdentifiableObject>
     implements QueryEngine<T>
 {
     @Autowired
@@ -122,15 +122,15 @@
 
     private Criteria buildCriteria( Criteria criteria, Query query )
     {
-        List<org.hisp.dhis.query.Criterion> criterions = getCriterions( query );
+        Query criteriaQuery = getCriteriaQuery( query );
 
-        for ( org.hisp.dhis.query.Criterion criterion : criterions )
+        for ( org.hisp.dhis.query.Criterion criterion : criteriaQuery.getCriterions() )
         {
             addCriterion( criteria, criterion, query.getSchema() );
         }
 
         // no more criterions available, so we can do our own paging
-        if ( query.getCriterions().isEmpty() )
+        if ( query.isEmpty() )
         {
             if ( query.getFirstResult() != null )
             {
@@ -141,11 +141,11 @@
             {
                 criteria.setMaxResults( query.getMaxResults() );
             }
+        }
 
-            for ( Order order : query.getOrders() )
-            {
-                criteria.addOrder( getHibernateOrder( order ) );
-            }
+        for ( Order order : criteriaQuery.getOrders() )
+        {
+            criteria.addOrder( getHibernateOrder( order ) );
         }
 
         return criteria;
@@ -156,12 +156,11 @@
      * the criterions will be passed on to the next query engine.
      *
      * @param query Query
-     * @return List of usable criterions for this engine
+     * @return Query instance
      */
-    private List<org.hisp.dhis.query.Criterion> getCriterions( Query query )
+    private Query getCriteriaQuery( Query query )
     {
-        List<org.hisp.dhis.query.Criterion> criterions = new ArrayList<>();
-
+        Query criteriaQuery = Query.from( query.getSchema() );
         Iterator<org.hisp.dhis.query.Criterion> criterionIterator = query.getCriterions().iterator();
 
         while ( criterionIterator.hasNext() )
@@ -174,13 +173,13 @@
 
                 if ( !restriction.getPath().contains( "\\." ) )
                 {
-                    if ( query.getSchema().haveProperty( restriction.getPath() ) )
+                    if ( criteriaQuery.getSchema().haveProperty( restriction.getPath() ) )
                     {
                         Property property = query.getSchema().getProperty( restriction.getPath() );
 
                         if ( property.isSimple() && property.isPersisted() )
                         {
-                            criterions.add( criterion );
+                            criteriaQuery.getCriterions().add( criterion );
                             criterionIterator.remove();
                         }
                     }
@@ -188,7 +187,13 @@
             }
         }
 
-        return criterions;
+        if ( query.ordersPersisted() )
+        {
+            criteriaQuery.addOrders( query.getOrders() );
+            query.clearOrders();
+        }
+
+        return criteriaQuery;
     }
 
     private void addJunction( org.hibernate.criterion.Junction junction, org.hisp.dhis.query.Criterion criterion, Schema schema )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2016-01-04 06:04:35 +0000
@@ -40,7 +40,7 @@
  *
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public class DefaultQueryService 
+public class DefaultQueryService
     implements QueryService
 {
     private static final Log log = LogFactory.getLog( DefaultQueryService.class );
@@ -104,13 +104,13 @@
         {
             objects = criteriaQueryEngine.query( query );
 
-            if ( query.getCriterions().isEmpty() )
+            if ( query.isEmpty() )
             {
                 return objects;
             }
         }
 
-        log.debug( "Doing in-memory filtering for " + query.getCriterions().size() + " criterions." );
+        log.debug( "Doing in-memory for " + query.getCriterions().size() + " criterions and " + query.getOrders().size() + " orders." );
 
         query.setObjects( objects );
         return inMemoryQueryEngine.query( query );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Order.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Order.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Order.java	2016-01-04 06:04:35 +0000
@@ -73,6 +73,16 @@
         return property;
     }
 
+    public boolean isPersisted()
+    {
+        return property.isPersisted() && property.isSimple();
+    }
+
+    public boolean isNonPersisted()
+    {
+        return !property.isPersisted() && property.isSimple();
+    }
+
     public int compare( Object lside, Object rside )
     {
         Object o1 = ReflectionUtils.invokeMethod( lside, property.getGetterMethod() );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Query.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Query.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Query.java	2016-01-04 06:04:35 +0000
@@ -64,11 +64,34 @@
         return schema;
     }
 
+    public boolean isEmpty()
+    {
+        return criterions.isEmpty() && orders.isEmpty();
+    }
+
     public List<Order> getOrders()
     {
         return orders;
     }
 
+    public boolean ordersPersisted()
+    {
+        for ( Order order : orders )
+        {
+            if ( order.isNonPersisted() )
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    public void clearOrders()
+    {
+        orders.clear();
+    }
+
     public Integer getFirstResult()
     {
         return firstResult;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderOptions.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderOptions.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderOptions.java	2016-01-04 06:04:35 +0000
@@ -95,7 +95,7 @@
 
     private boolean validProperty( Property property )
     {
-        return property.isPersisted() && property.isSimple();
+        return property.isSimple();
     }
 
     private boolean validDirection( String direction )