]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / EntityInstances.java
index 2347fa99e5c0e7331b3fbd12d04f3705371af52e..838728b17c98169b8f937c09ae811ccdd3bff82e 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2019 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -17,7 +17,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
-import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.primitiverequest.Adapter;
@@ -26,6 +25,7 @@ import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.request.TernaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.adapter.Instances;
+import org.simantics.db.layer0.genericrelation.Dependencies;
 import org.simantics.db.layer0.genericrelation.IndexQueries;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.request.ReadExt;
@@ -114,17 +114,18 @@ public class EntityInstances implements Instances {
         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 ) );
+            boolean emptyFilter = filter.isEmpty();
+            if (emptyFilter || !type.equals(L0.Entity)) {
+                IndexQueries.appendResourceIdTerm(sb, Dependencies.FIELD_TYPE_RESOURCE, type);
             }
-            if (filter.length() > 0) {
+            if (!emptyFilter) {
                 if (sb.length() > 0)
                     sb.append(" AND ");
                 sb.append(filter);
             }
+            if (sb.length() == 0) {
+               sb.append("*:*");
+            }
             return sb.toString();
         }
 
@@ -140,7 +141,8 @@ public class EntityInstances implements Instances {
 
                @Override
                public int getType() {
-                       return RequestFlags.IMMEDIATE_UPDATE;
+                       // This query should not be immediate update since it takes a long time!
+                       return RequestFlags.INVALIDATE;
                }
 
     }
@@ -180,15 +182,17 @@ public class EntityInstances implements Instances {
        CollectionSupport coll = graph.getService(CollectionSupport.class);
        
        THashSet<Resource> visited = new THashSet<>();
-       List<Resource> rec = findRec(graph, index, filter, visited);
+       List<Resource> rec_ = findRec(graph, index, filter, visited);
+       // We must not modify rec_!
+       List<Resource> rec = rec_;
        for(Resource global : Layer0Utils.listGlobalOntologies(graph)) {
                if(!visited.add(global)) continue;
                List<Resource> rs = graph.syncRequest(new QueryIndex(global, type, filter), TransientCacheListener.<List<Resource>>instance());
-               if(rec.isEmpty() && !rs.isEmpty()) {
-                       // TODO: rec could be an immutable empty list
-                       rec = new ArrayList<Resource>();
+               if(!rs.isEmpty()) {
+                   if(rec == rec_)
+                       rec = new ArrayList<>(rec);
+                rec.addAll(rs);
                }
-               rec.addAll(rs);
        }
        Collection<Resource> result = coll.asSortedList(rec);
        return result; 
@@ -197,13 +201,7 @@ public class EntityInstances implements Instances {
     @Override
     public Collection<Resource> findByName(ReadGraph graph, Resource model,
             String name) throws DatabaseException {
-        Layer0 L0 = Layer0.getInstance(graph);
-       CollectionSupport coll = graph.getService(CollectionSupport.class);
-        List<Resource> results = coll.createList();
-        for(Resource match : find(graph, model, name)) {
-            if(name.equals(graph.getPossibleRelatedValue(match, L0.HasName, Bindings.STRING))) results.add(match);
-        }
-        return results;
+        return find(graph, model, IndexQueries.quoteTerm(name));
     }
 
 }