Repository: incubator-unomi
Updated Branches:
  refs/heads/feature-optimized-aggregate-query d7e5c2d2f -> b6c9c7839


UNOMI-185 refactor and clean code after successful test with optimized query


Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/b6c9c783
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/b6c9c783
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/b6c9c783

Branch: refs/heads/feature-optimized-aggregate-query
Commit: b6c9c7839a350c2bc08be2127035ce2f8745d114
Parents: d7e5c2d
Author: dgaillard <[email protected]>
Authored: Wed Jun 27 12:01:56 2018 +0200
Committer: dgaillard <[email protected]>
Committed: Wed Jun 27 12:01:56 2018 +0200

----------------------------------------------------------------------
 .../apache/unomi/api/services/QueryService.java |  2 +-
 .../ElasticSearchPersistenceServiceImpl.java    | 12 +++----
 .../persistence/spi/PersistenceService.java     |  2 +-
 .../apache/unomi/rest/QueryServiceEndPoint.java | 33 +++++++-------------
 .../services/services/QueryServiceImpl.java     |  8 ++---
 5 files changed, 23 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b6c9c783/api/src/main/java/org/apache/unomi/api/services/QueryService.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/unomi/api/services/QueryService.java 
b/api/src/main/java/org/apache/unomi/api/services/QueryService.java
index 3f3cb7b..6ff5cc3 100644
--- a/api/src/main/java/org/apache/unomi/api/services/QueryService.java
+++ b/api/src/main/java/org/apache/unomi/api/services/QueryService.java
@@ -65,7 +65,7 @@ public interface QueryService {
      * @return a Map associating a specific value of the property to the 
cardinality of items with that value
      * @see Item Item for a discussion of {@code ITEM_TYPE}
      */
-    Map<String, Long> getAggregateOptimized(String itemType, String property, 
AggregateQuery query);
+    Map<String, Long> getAggregateWithOptimizedQuery(String itemType, String 
property, AggregateQuery query);
 
     /**
      * Retrieves the number of items of the specified type as defined by the 
Item subclass public field {@code ITEM_TYPE} and matching the specified {@link 
Condition}.

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b6c9c783/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
 
b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
index c7ec8d0..0f527db 100644
--- 
a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
+++ 
b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
@@ -1482,17 +1482,17 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
     }
 
     @Override
-    public Map<String, Long> aggregateQuery(final Condition filter, final 
BaseAggregate aggregate, final String itemType) {
+    public Map<String, Long> aggregateQuery(Condition filter, BaseAggregate 
aggregate, String itemType) {
         return aggregateQuery(filter, aggregate, itemType, false);
     }
 
     @Override
-    public Map<String, Long> aggregateQueryOptimized(Condition filter, 
BaseAggregate aggregate, String itemType) {
+    public Map<String, Long> aggregateWithOptimizedQuery(Condition filter, 
BaseAggregate aggregate, String itemType) {
         return aggregateQuery(filter, aggregate, itemType, true);
     }
 
-    private Map<String, Long> aggregateQuery(final Condition filter, final 
BaseAggregate aggregate, final String itemType, final boolean
-            optimized) {
+    private Map<String, Long> aggregateQuery(final Condition filter, final 
BaseAggregate aggregate, final String itemType,
+            final boolean optimizedQuery) {
         return new InClassLoaderExecute<Map<String, Long>>(metricsService, 
this.getClass().getName() + ".aggregateQuery") {
 
             @Override
@@ -1572,7 +1572,7 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
 
                 // If the request is optimized then we don't need a global 
aggregation which is very slow and we can put the query with a
                 // filter on range items in the query block so we don't 
retrieve all the document before filtering the whole
-                if (optimized) {
+                if (optimizedQuery) {
                     for (AggregationBuilder aggregationBuilder : 
lastAggregation) {
                         builder.addAggregation(aggregationBuilder);
                     }
@@ -1600,7 +1600,7 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 SearchResponse response = builder.execute().actionGet();
                 Aggregations aggregations = response.getAggregations();
                 if (aggregations != null) {
-                    if (optimized) {
+                    if (optimizedQuery) {
                         if (response.getHits() != null) {
                             results.put("_filtered", 
response.getHits().getTotalHits());
                         }

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b6c9c783/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
----------------------------------------------------------------------
diff --git 
a/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
 
b/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
index c98eb17..ec22654 100644
--- 
a/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
+++ 
b/persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
@@ -432,7 +432,7 @@ public interface PersistenceService {
      * @param itemType  the String representation of the item type we want to 
retrieve the count of, as defined by its class' {@code ITEM_TYPE} field
      * @return a Map associating aggregation dimension name as key and 
cardinality for that dimension as value
      */
-    Map<String, Long> aggregateQueryOptimized(Condition filter, BaseAggregate 
aggregate, String itemType);
+    Map<String, Long> aggregateWithOptimizedQuery(Condition filter, 
BaseAggregate aggregate, String itemType);
 
     /**
      * Updates the persistence's engine indices if needed.

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b6c9c783/rest/src/main/java/org/apache/unomi/rest/QueryServiceEndPoint.java
----------------------------------------------------------------------
diff --git a/rest/src/main/java/org/apache/unomi/rest/QueryServiceEndPoint.java 
b/rest/src/main/java/org/apache/unomi/rest/QueryServiceEndPoint.java
index 3d68e05..25e2456 100644
--- a/rest/src/main/java/org/apache/unomi/rest/QueryServiceEndPoint.java
+++ b/rest/src/main/java/org/apache/unomi/rest/QueryServiceEndPoint.java
@@ -82,36 +82,25 @@ public class QueryServiceEndPoint {
      *
      * Retrieves the number of items with the specified type as defined by the 
Item subclass public field {@code ITEM_TYPE} and aggregated by possible values 
of the specified
      * property or, if the specified query is not {@code null}, perform that 
aggregate query.
-     * Also return the global count of document matching the {@code ITEM_TYPE}
+     * Also return the global count of document matching the {@code ITEM_TYPE} 
if you don't use {@code optimizedQuery} or set it to false,
+     * otherwise if {@code optimizedQuery} is set to true then it won't return 
the global count but the query will be executed much faster.
      *
      * @param type           the String representation of the item type we 
want to retrieve the count of, as defined by its class' {@code ITEM_TYPE} field
      * @param property       the property we're aggregating on, i.e. for each 
possible value of this property, we are counting how many items of the 
specified type have that value
-     * @param aggregateQuery the {@link AggregateQuery} specifying the 
aggregation that should be perfomed
+     * @param aggregateQuery the {@link AggregateQuery} specifying the 
aggregation that should be performed
+     * @param optimizedQuery the {@code optimizedQuery} specifying if we 
should optimized the aggregate query or not
      * @return a Map associating a specific value of the property to the 
cardinality of items with that value
      * @see Item Item for a discussion of {@code ITEM_TYPE}
      */
     @POST
     @Path("/{type}/{property}")
-    public Map<String, Long> getAggregate(@PathParam("type") String type, 
@PathParam("property") String property, AggregateQuery aggregateQuery) {
-        return queryService.getAggregate(type, property, aggregateQuery);
-    }
-
-    /**
-     * Retrieves the number of items with the specified type as defined by the 
Item subclass public field {@code ITEM_TYPE} and aggregated by possible values 
of the specified
-     * property or, if the specified query is not {@code null}, perform that 
aggregate query.
-     * This aggregate won't return the global count and should therefore be 
much faster than {@link #getAggregate(String, String, AggregateQuery)}
-     *
-     * @param type           the String representation of the item type we 
want to retrieve the count of, as defined by its class' {@code ITEM_TYPE} field
-     * @param property       the property we're aggregating on, i.e. for each 
possible value of this property, we are counting how many items of the 
specified type have that value
-     * @param aggregateQuery the {@link AggregateQuery} specifying the 
aggregation that should be perfomed
-     * @return a Map associating a specific value of the property to the 
cardinality of items with that value
-     * @see Item Item for a discussion of {@code ITEM_TYPE}
-     */
-    @POST
-    @Path("/{type}/{property}/optimized")
-    public Map<String, Long> getAggregateOptimized(@PathParam("type") String 
type, @PathParam("property") String property, AggregateQuery
-            aggregateQuery) {
-        return queryService.getAggregateOptimized(type, property, 
aggregateQuery);
+    public Map<String, Long> getAggregate(@PathParam("type") String type, 
@PathParam("property") String property,
+            @QueryParam("optimizedQuery") boolean optimizedQuery, 
AggregateQuery aggregateQuery) {
+        if (optimizedQuery) {
+            return queryService.getAggregateWithOptimizedQuery(type, property, 
aggregateQuery);
+        } else {
+            return queryService.getAggregate(type, property, aggregateQuery);
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b6c9c783/services/src/main/java/org/apache/unomi/services/services/QueryServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/services/src/main/java/org/apache/unomi/services/services/QueryServiceImpl.java
 
b/services/src/main/java/org/apache/unomi/services/services/QueryServiceImpl.java
index 5da7076..bdbc9d1 100644
--- 
a/services/src/main/java/org/apache/unomi/services/services/QueryServiceImpl.java
+++ 
b/services/src/main/java/org/apache/unomi/services/services/QueryServiceImpl.java
@@ -62,7 +62,7 @@ public class QueryServiceImpl implements QueryService {
     }
 
     @Override
-    public Map<String, Long> getAggregateOptimized(String itemType, String 
property, AggregateQuery query) {
+    public Map<String, Long> getAggregateWithOptimizedQuery(String itemType, 
String property, AggregateQuery query) {
         return getAggregate(itemType, property, query, true);
     }
 
@@ -82,7 +82,7 @@ public class QueryServiceImpl implements QueryService {
         return persistenceService.queryCount(condition, itemType);
     }
 
-    private Map<String, Long> getAggregate(String itemType, String property, 
AggregateQuery query, boolean optimized) {
+    private Map<String, Long> getAggregate(String itemType, String property, 
AggregateQuery query, boolean optimizedQuery) {
         if (query != null) {
             // resolve condition
             if (query.getCondition() != null) {
@@ -115,8 +115,8 @@ public class QueryServiceImpl implements QueryService {
             }
 
             // fall back on terms aggregate
-            if (optimized) {
-                return 
persistenceService.aggregateQueryOptimized(query.getCondition(), baseAggregate, 
itemType);
+            if (optimizedQuery) {
+                return 
persistenceService.aggregateWithOptimizedQuery(query.getCondition(), 
baseAggregate, itemType);
             } else {
                 return persistenceService.aggregateQuery(query.getCondition(), 
baseAggregate, itemType);
             }

Reply via email to