Index query fixes after commit 5e340942 75/2875/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 2 May 2019 13:00:54 +0000 (16:00 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 3 May 2019 11:22:00 +0000 (14:22 +0300)
* Remove excessive use of explicit Types/Names constants in search
  queries by using public static fields from Dependencies
* Added missing quoting to terms in index searches
* Use DependencyResources function where the Map returned by
  Dependencies is not needed
* Fixed some searches to be case-insensitive again

gitlab #291

Change-Id: I9faa2f89f956e1ebe36b5216e42be4d1852ae9df
(cherry picked from commit e23dba36)

bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/IndexQueries.java
bundles/org.simantics.event/src/org/simantics/event/writer/EventSourceResolver.java
bundles/org.simantics.modeling/src/org/simantics/modeling/services/CaseInsensitiveComponentFunctionNamingStrategy.java

index 8b1a928..26a6e68 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.db.indexing;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
@@ -21,6 +20,7 @@ import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.indexing.exception.IndexCorruptedException;
+import org.simantics.db.layer0.genericrelation.Dependencies;
 import org.simantics.db.layer0.genericrelation.IndexQueries;
 import org.simantics.db.layer0.genericrelation.IndexedRelations;
 import org.simantics.db.layer0.util.Layer0Utils;
@@ -92,10 +92,9 @@ public class IndexUtils {
         Layer0 L0 = Layer0.getInstance(graph);
         HashSet<Resource> results = new HashSet<Resource>();
         
-               String search = "Name:" + name;
+        String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name);
 
-        for(Map<String, Object> entry : find(graph, model, search)) {
-               Resource resource = (Resource)entry.get("Resource");
+        for(Resource resource : findResources(graph, model, search)) {
             if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);
         }
         return results;
@@ -106,10 +105,9 @@ public class IndexUtils {
         HashSet<Resource> results = new HashSet<Resource>();
         Layer0 L0 = Layer0.getInstance(graph);
         String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
-               String search = "Types:" + IndexQueries.quoteTerm(typeName);
+        String search = IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName);
 
-        for(Map<String, Object> entry : find(graph, model, search)) {
-               Resource resource = (Resource)entry.get("Resource");
+        for(Resource resource : findResources(graph, model, search)) {
                if(graph.isInstanceOf(resource, type)) results.add(resource);
         }
         return results;
@@ -121,10 +119,10 @@ public class IndexUtils {
         
         HashSet<Resource> results = new HashSet<Resource>();
         String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
-               String search = "Types:" + IndexQueries.quoteTerm(typeName) + " AND Name:" + IndexQueries.quoteTerm(name);
+        
+        String search = IndexQueries.and(IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName), IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name));
 
-        for(Map<String, Object> entry : find(graph, model, search)) {
-               Resource resource = (Resource)entry.get("Resource");
+        for(Resource resource : findResources(graph, model, search)) {
                if(graph.isInstanceOf(resource, type)) results.add(resource);
         }
         return results;
index 44175b8..1702af0 100644 (file)
@@ -22,20 +22,14 @@ import org.simantics.scl.runtime.function.FunctionImpl4;
 import org.simantics.scl.runtime.function.UnsaturatedFunction2;
 
 /**
- * dependencies:
- *      (ReadGraph, Resource model, String query) -> List<Map<String, Object>>
- *      (ReadGraph, Resource model, String query, Integer maxResults) -> List<Map<String, Object>>
+ * dependencyResources:
+ *      (ReadGraph, Resource model, String query) -> List<Resource>
+ *      (ReadGraph, Resource model, String query, Integer maxResults) -> List<Resource>
  * 
  * @author Antti Villberg
  */
 public class DependencyResources extends FunctionImpl4<ReadGraph, Resource, String, Integer, Object> {
 
-    public static final String FIELD_MODEL    = "Model";
-    public static final String FIELD_PARENT   = "Parent";
-    public static final String FIELD_RESOURCE = "Resource";
-    public static final String FIELD_NAME     = "Name";
-    public static final String FIELD_TYPES    = "Types";
-
     protected Resource getIndexRelation(ReadGraph graph) {
         return Layer0X.getInstance(graph).DependenciesRelation;
     }
index 212da38..2be6bf5 100644 (file)
@@ -159,18 +159,57 @@ public class IndexQueries {
        }
 
        public static String escapeTerm(String field, String term, boolean escapeWildcards) {
-               StringBuilder sb = new StringBuilder();
-               return escapeTerm(field, term, escapeWildcards, sb).toString();
+               return escapeTerm(field, term, escapeWildcards, new StringBuilder()).toString();
        }
-       
+
+       public static StringBuilder quoteTerm(String field, String term, StringBuilder result) {
+               if (field != null)
+                       result.append(field).append(':');
+               result.append("\"");
+               result.append(term.replaceAll("(\"|\\\\)", "\\\\$0"));
+               result.append("\"");
+               return result;
+       }
+
        public static String quoteTerm(String term) {
-               StringBuilder sb = new StringBuilder();
-               sb.append("\"");
-               sb.append(term.replaceAll("(\"|\\\\)", "\\\\$0"));
-               sb.append("\"");
+               return quoteTerm(null, term, new StringBuilder(term.length()*2)).toString();
+       }
+
+       public static String quoteTerm(String field, String term) {
+               return quoteTerm(field, term,
+                               new StringBuilder(
+                                               term.length()*2
+                                               + (field != null ? field.length() + 1 : 0))
+                               ).toString();
+       }
+
+       private static String join(String withString, String... exps) {
+               if (exps.length == 0)
+                       return "";
+               StringBuilder sb = new StringBuilder(128);
+               for (int i = 0; i < exps.length - 1; ++i) {
+                       sb.append(exps[i]).append(withString);
+               }
+               sb.append(exps[exps.length - 1]);
                return sb.toString();
        }
 
+       public static String and(String exp1, String exp2) {
+               return exp1 + " AND " + exp2;
+       }
+
+       public static String and(String... exps) {
+               return join(" AND ", exps);
+       }
+
+       public static String or(String exp1, String exp2) {
+               return exp1 + " OR " + exp2;
+       }
+
+       public static String or(String... exps) {
+               return join(" OR ", exps);
+       }
+
 //     public static void main(String[] args) {
 //             System.out.println("esc: " + escape("AND01", true, true));
 //             System.out.println("esc: " + escape("AND 01", true, true));
index a382660..9e2b666 100644 (file)
@@ -15,7 +15,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
-import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.eclipse.core.runtime.IProgressMonitor;
@@ -35,6 +34,7 @@ import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.CancelTransactionException;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.genericrelation.Dependencies;
+import org.simantics.db.layer0.genericrelation.IndexQueries;
 import org.simantics.db.layer0.request.PossibleModel;
 import org.simantics.event.Activator;
 import org.simantics.event.ontology.EventResource;
@@ -152,7 +152,7 @@ public class EventSourceResolver extends DatabaseJob {
                         if (model == null)
                             throw new CancelTransactionException();
 
-                        indexFunction = graph.adapt(L0X.Dependencies, Function.class);
+                        indexFunction = graph.adapt(L0X.DependencyResources, Function.class);
 
                         if (!initialEventsResolved) {
                             MapList<String, Resource> initialEventsBySource = new MapList<String,Resource>();
@@ -201,9 +201,9 @@ public class EventSourceResolver extends DatabaseJob {
                         monitor.subTask("Resolve events with source " + sourceName);
                         if (DEBUG)
                             System.out.println(EventSourceResolver.this + ": resolving source name " + sourceName);
-                        List<Map<String, Object>> results = (List<Map<String, Object>>) indexFunction.apply(graph, model, "Name:" + sourceName);
-                        for (Map<String, Object> result : results) {
-                            Resource r = (Resource) result.get(Dependencies.FIELD_RESOURCE);
+                        List<Resource> results = (List<Resource>) indexFunction.apply(graph, model,
+                                IndexQueries.quoteTerm(Dependencies.FIELD_NAME, sourceName));
+                        for (Resource r : results) {
                             if (eventSourceFilter != null && !eventSourceFilter.accept(graph, r))
                                 continue;
                             Resource rModel = graph.sync(new PossibleModel(r));
index ad48d1f..d5a2b92 100644 (file)
@@ -27,6 +27,7 @@ import org.simantics.db.common.request.PossibleIndexRoot;
 import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.genericrelation.Dependencies;
 import org.simantics.db.layer0.genericrelation.IndexQueries;
 import org.simantics.db.service.GraphChangeListenerSupport;
 import org.simantics.layer0.Layer0;
@@ -45,7 +46,7 @@ import gnu.trove.set.hash.THashSet;
  * 
  * <p>
  * The type of the function is expected to be:
- * <code>ReadGraph => Resource -> String -> Integer -> List&lt;Map&lt;String,Object&gt;&gt;</code>
+ * <code>ReadGraph => Resource -> String -> Integer -> List&lt;Resource&gt;</code>
  * 
  * @author Tuukka Lehtonen
  * 
@@ -135,7 +136,7 @@ public class CaseInsensitiveComponentFunctionNamingStrategy extends ComponentNam
                        
                     synchronized (this) {
                        
-                        String search = "Name:" + lowercaseName + "*";
+                        String search = IndexQueries.escapeTerm(Dependencies.FIELD_NAME_SEARCH, lowercaseName, true) + "*"; //$NON-NLS-1$
                         @SuppressWarnings("unchecked")
                         List<Resource> components = (List<Resource>) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);
 
@@ -227,7 +228,7 @@ public class CaseInsensitiveComponentFunctionNamingStrategy extends ComponentNam
 
         synchronized (this) {
                
-            String search = "Name:" + proposition + "*";
+            String search = Dependencies.FIELD_NAME_SEARCH + ":" + IndexQueries.escape(proposition.toLowerCase(), true) + "*"; //$NON-NLS-1$ //$NON-NLS-2$
             
             Set<String> reserved = graph.syncRequest(new ComponentsRequest(new Tuple4(indexRoot, index, search, getComparator())), TransientCacheAsyncListener.instance());
 
@@ -267,7 +268,7 @@ public class CaseInsensitiveComponentFunctionNamingStrategy extends ComponentNam
                 if (propositionPreFilter != null)
                     proposition = propositionPreFilter.apply(proposition);
 
-                String search = "Name:" + IndexQueries.quoteTerm( proposition );
+                String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME_SEARCH, proposition.toLowerCase());
                 @SuppressWarnings("unchecked")
                 List<Resource> components = (List<Resource>) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);