]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
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 8b1a928b353497bbc733c1b89e7831e986659dd7..26a6e68ae9f9378dd4b7a429c7ac2ccdbe317684 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.db.indexing;
 
 package org.simantics.db.indexing;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 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.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;
 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>();
         
         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;
             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);
         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;
                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);
         
         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;
                if(graph.isInstanceOf(resource, type)) results.add(resource);
         }
         return results;
index 44175b820dd8c8b10eccdbce25a61c3b35cad038..1702af0f38e6182832673997ab7b55588ae5f335 100644 (file)
@@ -22,20 +22,14 @@ import org.simantics.scl.runtime.function.FunctionImpl4;
 import org.simantics.scl.runtime.function.UnsaturatedFunction2;
 
 /**
 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> {
 
  * 
  * @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;
     }
     protected Resource getIndexRelation(ReadGraph graph) {
         return Layer0X.getInstance(graph).DependenciesRelation;
     }
index 212da3830a1decfaac2ef541ba0f0d5155070252..2be6bf5d0e1f270655ed0828acbc07975e075b52 100644 (file)
@@ -159,18 +159,57 @@ public class IndexQueries {
        }
 
        public static String escapeTerm(String field, String term, boolean escapeWildcards) {
        }
 
        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) {
        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();
        }
 
                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));
 //     public static void main(String[] args) {
 //             System.out.println("esc: " + escape("AND01", true, true));
 //             System.out.println("esc: " + escape("AND 01", true, true));
index a382660ad4f367e978ce5c4a25e2f7bea36e3276..9e2b6667744e409f2a9e5c8f3df9e95598d37d4c 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.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;
 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.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;
 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();
 
                         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>();
 
                         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);
                         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));
                             if (eventSourceFilter != null && !eventSourceFilter.accept(graph, r))
                                 continue;
                             Resource rModel = graph.sync(new PossibleModel(r));
index ad48d1f2f809ab89e48a545da83ae88f26e094ca..d5a2b92a42306f466bd4d94a2f00dd4de536e0e8 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.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;
 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:
  * 
  * <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
  * 
  * 
  * @author Tuukka Lehtonen
  * 
@@ -135,7 +136,7 @@ public class CaseInsensitiveComponentFunctionNamingStrategy extends ComponentNam
                        
                     synchronized (this) {
                        
                        
                     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);
 
                         @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) {
                
 
         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());
 
             
             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);
 
                 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);
 
                 @SuppressWarnings("unchecked")
                 List<Resource> components = (List<Resource>) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);