public List<Resource> 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.<Function>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.<Function>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<Resource> results = (List<Resource>)dependencyResources.apply(graph, parameter, filter);
+ List<Resource> results = (List<Resource>)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<Resource> resultSet = new TreeSet<Resource>();
-// for (Map<String, Object> 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<Resource> 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<Resource> result = coll.createList();
-
- for (Resource res : Layer0Utils.sortByCluster(graph, results)) {
+ List<Resource> 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;
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;
}
public static List<Resource> 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.<List<Resource>>instance());
}
public static List<Resource> searchByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
}
public static List<Resource> 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.<List<Resource>>instance());
}
public static List<Resource> searchByQuery(ReadGraph graph, Resource model, String query) throws DatabaseException {
}
public static List<Resource> 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.<List<Resource>>instance());
}
/**