You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2021/10/04 12:57:18 UTC

[unomi] 02/02: Modify property condition builders to use collections instead of lists (works with sets now)

This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch draft-custom-items
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 4f2f288a95cef2614cb75b2e2ab51d60d3ce5998
Author: Serge Huber <sh...@jahia.com>
AuthorDate: Mon Oct 4 14:57:08 2021 +0200

    Modify property condition builders to use collections instead of lists (works with sets now)
---
 itests/pom.xml                                     |  2 +-
 .../conditions/ConditionContextHelper.java         | 10 ++--
 .../PropertyConditionESQueryBuilder.java           | 24 ++++-----
 .../conditions/PropertyConditionEvaluator.java     | 61 ++++++++++++++++------
 4 files changed, 62 insertions(+), 35 deletions(-)

diff --git a/itests/pom.xml b/itests/pom.xml
index e3bec40..50c1ccf 100644
--- a/itests/pom.xml
+++ b/itests/pom.xml
@@ -178,7 +178,7 @@
                             <autoCreateIndex>true</autoCreateIndex>
                             <timeout>120</timeout>
                             <environmentVariables>
-                                <ES_JAVA_OPTS>-Xms2g -Xmx2g</ES_JAVA_OPTS>
+                                <ES_JAVA_OPTS>-Xms4g -Xmx4g</ES_JAVA_OPTS>
                             </environmentVariables>
                             <instanceSettings>
                                 <properties>
diff --git a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/conditions/ConditionContextHelper.java b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/conditions/ConditionContextHelper.java
index 13bc667..211ed32 100644
--- a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/conditions/ConditionContextHelper.java
+++ b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/conditions/ConditionContextHelper.java
@@ -18,6 +18,7 @@
 package org.apache.unomi.persistence.elasticsearch.conditions;
 
 import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.core.util.IOUtils;
@@ -31,10 +32,7 @@ import org.slf4j.LoggerFactory;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 public class ConditionContextHelper {
     private static final Logger logger = LoggerFactory.getLogger(ConditionContextHelper.class);
@@ -144,9 +142,9 @@ public class ConditionContextHelper {
         return null;
     }
 
-    public static <T> List<T> foldToASCII(List<T> s) {
+    public static <T> Collection<T> foldToASCII(Collection<T> s) {
         if (s != null) {
-            return Lists.transform(s, new Function<T, T>() {
+            return Collections2.transform(s, new Function<T, T>() {
                 @Override
                 public T apply(T o) {
                     if (o instanceof String) {
diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionESQueryBuilder.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionESQueryBuilder.java
index 1613081..4f2612b 100644
--- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionESQueryBuilder.java
+++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionESQueryBuilder.java
@@ -31,10 +31,7 @@ import org.joda.time.DateTime;
 import org.joda.time.format.DateTimeFormatter;
 import org.joda.time.format.ISODateTimeFormat;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 public class PropertyConditionESQueryBuilder implements ConditionESQueryBuilder {
 
@@ -59,15 +56,15 @@ public class PropertyConditionESQueryBuilder implements ConditionESQueryBuilder
         Object expectedValueDate = convertDateToISO(condition.getParameter("propertyValueDate"));
         Object expectedValueDateExpr = condition.getParameter("propertyValueDateExpr");
 
-        List<?> expectedValues = ConditionContextHelper.foldToASCII((List<?>) condition.getParameter("propertyValues"));
-        List<?> expectedValuesInteger = (List<?>) condition.getParameter("propertyValuesInteger");
-        List<?> expectedValuesDouble = (List<?>) condition.getParameter("propertyValuesDouble");
-        List<?> expectedValuesDate = convertDatesToISO((List<?>) condition.getParameter("propertyValuesDate"));
-        List<?> expectedValuesDateExpr = (List<?>) condition.getParameter("propertyValuesDateExpr");
+        Collection<?> expectedValues = ConditionContextHelper.foldToASCII((Collection<?>) condition.getParameter("propertyValues"));
+        Collection<?> expectedValuesInteger = (Collection<?>) condition.getParameter("propertyValuesInteger");
+        Collection<?> expectedValuesDouble = (Collection<?>) condition.getParameter("propertyValuesDouble");
+        Collection<?> expectedValuesDate = convertDatesToISO((Collection<?>) condition.getParameter("propertyValuesDate"));
+        Collection<?> expectedValuesDateExpr = (Collection<?>) condition.getParameter("propertyValuesDateExpr");
 
         Object value = ObjectUtils.firstNonNull(expectedValue, expectedValueInteger, expectedValueDouble, expectedValueDate, expectedValueDateExpr);
         @SuppressWarnings("unchecked")
-        List<?> values = ObjectUtils.firstNonNull(expectedValues, expectedValuesInteger, expectedValuesDouble, expectedValuesDate, expectedValuesDateExpr);
+        Collection<?> values = ObjectUtils.firstNonNull(expectedValues, expectedValuesInteger, expectedValuesDouble, expectedValuesDate, expectedValuesDateExpr);
 
         switch (comparisonOperator) {
             case "equals":
@@ -90,7 +87,8 @@ public class PropertyConditionESQueryBuilder implements ConditionESQueryBuilder
                 return QueryBuilders.rangeQuery(name).lte(value);
             case "between":
                 checkRequiredValuesSize(values, name, comparisonOperator, 2);
-                return QueryBuilders.rangeQuery(name).gte(values.get(0)).lte(values.get(1));
+                Iterator<?> iterator = values.iterator();
+                return QueryBuilders.rangeQuery(name).gte(iterator.next()).lte(iterator.next());
             case "exists":
                 return QueryBuilders.existsQuery(name);
             case "missing":
@@ -175,7 +173,7 @@ public class PropertyConditionESQueryBuilder implements ConditionESQueryBuilder
         return null;
     }
 
-    private void checkRequiredValuesSize(List<?> values, String name, String operator, int expectedSize) {
+    private void checkRequiredValuesSize(Collection<?> values, String name, String operator, int expectedSize) {
         if (values == null || values.size() != expectedSize) {
             throw new IllegalArgumentException("Impossible to build ES filter, missing " + expectedSize + " values for a condition using comparisonOperator: " + operator + ", and propertyName: " + name);
         }
@@ -205,7 +203,7 @@ public class PropertyConditionESQueryBuilder implements ConditionESQueryBuilder
         }
     }
 
-    private List<?> convertDatesToISO(List<?> datesValues) {
+    private Collection<?> convertDatesToISO(Collection<?> datesValues) {
         List<Object> results = new ArrayList<>();
         if (datesValues == null) {
             return null;
diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluator.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluator.java
index f637a7b..6a0aadc 100644
--- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluator.java
+++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluator.java
@@ -88,16 +88,16 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
         }
     }
 
-    private boolean compareMultivalue(Object actualValue, List<?> expectedValues, List<?> expectedValuesDate, List<?> expectedValuesNumber, List<?> expectedValuesDateExpr, String op) {
+    private boolean compareMultivalue(Object actualValue, Collection<?> expectedValues, Collection<?> expectedValuesDate, Collection<?> expectedValuesNumber, Collection<?> expectedValuesDateExpr, String op) {
         @SuppressWarnings("unchecked")
-        List<?> expected = ObjectUtils.firstNonNull(expectedValues, expectedValuesDate, expectedValuesNumber);
+        Collection<?> expected = ObjectUtils.firstNonNull(expectedValues, expectedValuesDate, expectedValuesNumber);
         if (actualValue == null) {
             return expected == null;
         } else if (expected == null) {
             return false;
         }
 
-        List<Object> actual = ConditionContextHelper.foldToASCII(getValueSet(actualValue));
+        Collection<Object> actual = ConditionContextHelper.foldToASCII(getValueSet(actualValue));
 
         boolean result = true;
 
@@ -241,16 +241,16 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
             List<?> expectedValuesDate = (List<?>) condition.getParameter("propertyValuesDate");
             List<?> expectedValuesDateExpr = (List<?>) condition.getParameter("propertyValuesDateExpr");
             return compare(actualValue, null,
-                    (expectedValuesDate != null && expectedValuesDate.size() >= 1) ? getDate(expectedValuesDate.get(0)) : null,
-                    (expectedValuesInteger != null && expectedValuesInteger.size() >= 1) ? (Integer) expectedValuesInteger.get(0) : null,
-                    (expectedValuesDateExpr != null && expectedValuesDateExpr.size() >= 1) ? (String) expectedValuesDateExpr.get(0) : null,
-                    (expectedValuesDouble != null && expectedValuesDouble.size() >= 1) ? (String) expectedValuesDouble.get(0) : null) >= 0
+                    getDate(getFirst(expectedValuesDate)),
+                    getFirst(expectedValuesInteger),
+                    getFirst(expectedValuesDateExpr),
+                    getFirst(expectedValuesDouble)) >= 0
                     &&
                     compare(actualValue, null,
-                            (expectedValuesDate != null && expectedValuesDate.size() >= 2) ? getDate(expectedValuesDate.get(1)) : null,
-                            (expectedValuesInteger != null && expectedValuesInteger.size() >= 2) ? (Integer) expectedValuesInteger.get(1) : null,
-                            (expectedValuesDateExpr != null && expectedValuesDateExpr.size() >= 2) ? (String) expectedValuesDateExpr.get(1) : null,
-                            (expectedValuesDouble != null && expectedValuesDouble.size() >= 2) ? (String) expectedValuesDouble.get(1) : null) <= 0;
+                            getDate(getSecond(expectedValuesDate)),
+                            getSecond(expectedValuesInteger),
+                            getSecond(expectedValuesDateExpr),
+                            getSecond(expectedValuesDouble)) <= 0;
         } else if (op.equals("contains")) {
             return actualValue.toString().contains(expectedValue);
         } else if (op.equals("notContains")) {
@@ -262,10 +262,10 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
         } else if (op.equals("matchesRegex")) {
             return expectedValue != null && Pattern.compile(expectedValue).matcher(actualValue.toString()).matches();
         } else if (op.equals("in") || op.equals("inContains") || op.equals("notIn") || op.equals("hasSomeOf") || op.equals("hasNoneOf") || op.equals("all")) {
-            List<?> expectedValues = ConditionContextHelper.foldToASCII((List<?>) condition.getParameter("propertyValues"));
-            List<?> expectedValuesInteger = (List<?>) condition.getParameter("propertyValuesInteger");
-            List<?> expectedValuesDate = (List<?>) condition.getParameter("propertyValuesDate");
-            List<?> expectedValuesDateExpr = (List<?>) condition.getParameter("propertyValuesDateExpr");
+            Collection<?> expectedValues = ConditionContextHelper.foldToASCII((Collection<?>) condition.getParameter("propertyValues"));
+            Collection<?> expectedValuesInteger = (Collection<?>) condition.getParameter("propertyValuesInteger");
+            Collection<?> expectedValuesDate = (Collection<?>) condition.getParameter("propertyValuesDate");
+            Collection<?> expectedValuesDateExpr = (Collection<?>) condition.getParameter("propertyValuesDateExpr");
 
             return compareMultivalue(actualValue, expectedValues, expectedValuesDate, expectedValuesInteger, expectedValuesDateExpr, op);
         } else if (op.equals("isDay") && expectedValueDate != null) {
@@ -464,4 +464,35 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
         }
     }
 
+    private Object getFirst(Collection<?> collection) {
+        if (collection == null) {
+            return null;
+        }
+        if (collection.isEmpty()) {
+            return null;
+        }
+        Iterator<?> iterator = collection.iterator();
+        if (iterator.hasNext()) {
+            return iterator.next();
+        }
+        return null;
+    }
+
+    private Object getSecond(Collection<?> collection) {
+        if (collection == null) {
+            return null;
+        }
+        if (collection.isEmpty()) {
+            return null;
+        }
+        Iterator<?> iterator = collection.iterator();
+        if (iterator.hasNext()) {
+            iterator.next();
+            if (iterator.hasNext()) {
+                return iterator.next();
+            }
+        }
+        return null;
+    }
+
 }