From: Tuukka Lehtonen Date: Thu, 2 May 2019 13:00:54 +0000 (+0300) Subject: Index query fixes after commit 5e340942 X-Git-Tag: v1.43.0~136^2~158 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=0935b78fb5a162719de5dab404b7161de2e7e717 Index query fixes after commit 5e340942 * 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) --- diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java index 8b1a928b3..26a6e68ae 100644 --- a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexUtils.java @@ -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 results = new HashSet(); - String search = "Name:" + name; + String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name); - for(Map 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 results = new HashSet(); 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 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 results = new HashSet(); 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 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; diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java index 44175b820..1702af0f3 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java @@ -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> - * (ReadGraph, Resource model, String query, Integer maxResults) -> List> + * dependencyResources: + * (ReadGraph, Resource model, String query) -> List + * (ReadGraph, Resource model, String query, Integer maxResults) -> List * * @author Antti Villberg */ public class DependencyResources extends FunctionImpl4 { - 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; } diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/IndexQueries.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/IndexQueries.java index 212da3830..2be6bf5d0 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/IndexQueries.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/IndexQueries.java @@ -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)); diff --git a/bundles/org.simantics.event/src/org/simantics/event/writer/EventSourceResolver.java b/bundles/org.simantics.event/src/org/simantics/event/writer/EventSourceResolver.java index a382660ad..9e2b66677 100644 --- a/bundles/org.simantics.event/src/org/simantics/event/writer/EventSourceResolver.java +++ b/bundles/org.simantics.event/src/org/simantics/event/writer/EventSourceResolver.java @@ -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 initialEventsBySource = new MapList(); @@ -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> results = (List>) indexFunction.apply(graph, model, "Name:" + sourceName); - for (Map result : results) { - Resource r = (Resource) result.get(Dependencies.FIELD_RESOURCE); + List results = (List) 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)); diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/services/CaseInsensitiveComponentFunctionNamingStrategy.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/services/CaseInsensitiveComponentFunctionNamingStrategy.java index ad48d1f2f..d5a2b92a4 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/services/CaseInsensitiveComponentFunctionNamingStrategy.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/services/CaseInsensitiveComponentFunctionNamingStrategy.java @@ -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; * *

* The type of the function is expected to be: - * ReadGraph => Resource -> String -> Integer -> List<Map<String,Object>> + * ReadGraph => Resource -> String -> Integer -> List<Resource> * * @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 components = (List) 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 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 components = (List) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);