From: Tuukka Lehtonen Date: Mon, 7 Aug 2017 15:05:35 +0000 (+0300) Subject: Optimized EntityInstances and ModelingUtils.search*Shallow queries X-Git-Tag: v1.31.0~264^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=4b5b16eec880434af849ddd6522332884bfb5538 Optimized EntityInstances and ModelingUtils.search*Shallow queries Shallow queries were previously doing tons of useless queries into dependent ontologies which would have been filtered out anyway. Also EntityInstances.QueryIndex.perform now optimizes two corner cases: * only one search result which is usual for GUID searches * removal of Types:*Entity filter term which is useless because all instances are entities. refs #7415 Change-Id: I89b9495ea51ca8fba4bd40db113c91f4a12932d0 --- diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java index 488545c5f..2347fa99e 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java @@ -70,58 +70,64 @@ public class EntityInstances implements Instances { public List perform(ReadGraph graph) throws DatabaseException { Resource type = parameter2; - - Layer0 L0 = Layer0.getInstance(graph); - Layer0X L0X = Layer0X.getInstance(graph); - String typeName = graph.getRelatedValue(type, L0.HasName); - if (typeName.isEmpty()) + String filter = constructLuceneQuery(graph, type, parameter3); + if (filter == null) return Collections.emptyList(); @SuppressWarnings({ "unchecked", "rawtypes" }) - Function dependencyResources = graph.syncRequest(new Adapter(L0X.DependencyResources, Function.class), TransientCacheListener.instance()); - - StringBuilder filtersb = new StringBuilder(); - filtersb.append("Types:*").append( IndexQueries.escape( typeName, true ) ); - if (parameter3.length() > 0) - filtersb.append(" AND ").append( parameter3 ); - String filter = filtersb.toString(); + Function dependencyResources = graph.syncRequest(new Adapter(Layer0X.getInstance(graph).DependencyResources, Function.class), TransientCacheListener.instance()); if (TRACE_QUERIES) { System.out.println("EntityInstances.QueryIndex: finding " + filter + " from index " + graph.getPossibleURI(parameter)); //new Exception("EntityInstances: finding " + filter + " from index " + graph.getPossibleURI(parameter)).printStackTrace(); } - + @SuppressWarnings("unchecked") - List results = (List)dependencyResources.apply(graph, parameter, filter); + List results = (List)dependencyResources.apply(graph, parameter, filter); if (results == null || results.isEmpty()) return Collections.emptyList(); if (TRACE_QUERIES) System.out.println(" EntityInstances.QueryIndex: got " + results.size() + " results"); -// // TreeSet to keep the results in deterministic order. -// Set resultSet = new TreeSet(); -// for (Map entry : results) { -// Resource res = (Resource)entry.get("Resource"); -// if (res != null && !resultSet.contains(res)) -// resultSet.add(res); -// } + // Optimize single result case. + if (results.size() == 1) { + Resource singleResult = results.get(0); + List result = graph.isInstanceOf(singleResult, type) ? results : Collections.emptyList(); + if (TRACE_QUERIES) + System.out.println(" EntityInstances.QueryIndex: got " + results.size() + " unique type-matching results"); + return result; + } CollectionSupport coll = graph.getService(CollectionSupport.class); - List result = coll.createList(); - - for (Resource res : Layer0Utils.sortByCluster(graph, results)) { + List result = coll.createList(results.size()); + for (Resource res : Layer0Utils.sortByCluster(graph, results)) if (graph.isInstanceOf(res, type)) result.add(res); - } if (TRACE_QUERIES) System.out.println(" EntityInstances.QueryIndex: got " + results.size() + " unique type-matching results"); - + return result; + } + private String constructLuceneQuery(ReadGraph graph, Resource type, String filter) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + StringBuilder sb = new StringBuilder(); + if (!L0.Entity.equals(type)) { + String typeName = graph.getPossibleRelatedValue(type, L0.HasName, Bindings.STRING); + if (typeName == null || typeName.isEmpty()) + return null; + sb.append("Types:*").append( IndexQueries.escape( typeName, true ) ); + } + if (filter.length() > 0) { + if (sb.length() > 0) + sb.append(" AND "); + sb.append(filter); + } + return sb.toString(); } - + @Override public String toString() { return "QueryIndex " + parameter + " " + parameter2 + " " + parameter3; diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/CollectionSupportImpl.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/CollectionSupportImpl.java index 5f9a8993e..37c89ff5f 100644 --- a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/CollectionSupportImpl.java +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/CollectionSupportImpl.java @@ -633,6 +633,11 @@ public class CollectionSupportImpl implements CollectionSupport { this.backend = new TIntArrayList(); } + ResourceList(SessionImplSocket session, int capacity) { + this.session = session; + this.backend = new TIntArrayList(capacity); + } + ResourceList(SessionImplSocket session, Collection rs) { this.session = session; this.backend = new TIntArrayList(rs.size()); @@ -853,6 +858,11 @@ public class CollectionSupportImpl implements CollectionSupport { return new ResourceList(session); } + @Override + public List createList(int capacity) { + return new ResourceList(session, capacity); + } + static final class StatementList implements Collection { final private SessionImplSocket session; diff --git a/bundles/org.simantics.db/src/org/simantics/db/service/CollectionSupport.java b/bundles/org.simantics.db/src/org/simantics/db/service/CollectionSupport.java index aa522203d..d5cebc864 100644 --- a/bundles/org.simantics.db/src/org/simantics/db/service/CollectionSupport.java +++ b/bundles/org.simantics.db/src/org/simantics/db/service/CollectionSupport.java @@ -38,6 +38,7 @@ public interface CollectionSupport { Set createSet(); Set createSet(int capacity); List createList(); + List createList(int capacity); List asSortedList(Collection set); void sort(List list); Collection createStatementList(); diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/ModelingUtils.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/ModelingUtils.java index 511eeb3fa..2afdac992 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/ModelingUtils.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/ModelingUtils.java @@ -91,6 +91,7 @@ import org.simantics.db.layer0.adapter.CopyHandler; import org.simantics.db.layer0.adapter.GenericRelationIndex; import org.simantics.db.layer0.adapter.Instances; import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; +import org.simantics.db.layer0.adapter.impl.EntityInstances.QueryIndex; import org.simantics.db.layer0.adapter.impl.ImportAdvisorFactory; import org.simantics.db.layer0.genericrelation.IndexedRelations; import org.simantics.db.layer0.migration.MigrationUtils; @@ -704,7 +705,7 @@ public class ModelingUtils { } public static List searchByTypeShallow(ReadGraph graph, Resource model, Resource type) throws DatabaseException { - return filterByIndexRoot(graph, model, searchByType(graph, model, type)); + return graph.syncRequest(new QueryIndex(model, type, ""), TransientCacheListener.>instance()); } public static List searchByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException { @@ -721,7 +722,7 @@ public class ModelingUtils { } public static List searchByQueryShallow(ReadGraph graph, Resource model, String query) throws DatabaseException { - return filterByIndexRoot(graph, model, searchByQuery(graph, model, query)); + return graph.syncRequest(new QueryIndex(model, Layer0.getInstance(graph).Entity, query), TransientCacheListener.>instance()); } public static List searchByQuery(ReadGraph graph, Resource model, String query) throws DatabaseException { @@ -774,7 +775,7 @@ public class ModelingUtils { } public static List searchByTypeAndNameShallow(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException { - return filterByIndexRoot(graph, model, searchByTypeAndName(graph, model, type, name)); + return graph.syncRequest(new QueryIndex(model, type, name), TransientCacheListener.>instance()); } /**