]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ConsistsOfProcess.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ConsistsOfProcess.java
index a2e64868f7c21d43c1456c4662810c20f6ca4ed6..55fe1ac0111f008f90c3d0b816720b01ce34ce03 100644 (file)
@@ -6,7 +6,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.ResourceMap;
@@ -14,81 +14,78 @@ import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
-import org.simantics.db.procedure.AsyncContextMultiProcedure;
-import org.simantics.db.procedure.Procedure;
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
+import org.simantics.db.procedure.SyncContextMultiProcedure;
 import org.simantics.db.service.DirectQuerySupport;
 import org.simantics.layer0.Layer0;
 import org.simantics.utils.datastructures.Pair;
 
 class ConsistsOfProcess {
 
-       final List<InternalEntry> result;
+       final List<ConsistsOfProcessEntry> result;
        final Set<Resource> childrenWithNoName;
-       final AsyncContextMultiProcedure<InternalEntry, Resource> structure;
-       final AsyncContextMultiProcedure<InternalEntry, Resource> names;
+       final SyncContextMultiProcedure<ConsistsOfProcessEntry, Resource> structure;
+       final SyncContextMultiProcedure<ConsistsOfProcessEntry, Resource> names;
 
-       public static Pair<List<InternalEntry>,Set<Resource>> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<Resource> resources, Set<Resource> exclusions, boolean ignoreVirtual) throws DatabaseException {
-       ConsistsOfProcess process = new ConsistsOfProcess(graph, status, resources, exclusions, ignoreVirtual);
+       public static Pair<List<ConsistsOfProcessEntry>,Set<Resource>> walk(ReadGraph graph, Collection<SeedSpec> specs, boolean ignoreVirtual) throws DatabaseException {
+               return walk(graph, null, specs, ignoreVirtual);
+       }
+
+       public static Pair<List<ConsistsOfProcessEntry>,Set<Resource>> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<SeedSpec> specs, boolean ignoreVirtual) throws DatabaseException {
+               
+       ConsistsOfProcess process = new ConsistsOfProcess(graph, status, specs, ignoreVirtual);
        return Pair.make(process.result, process.childrenWithNoName);
+       
     }
     
-    static class InternalEntry {
-       public InternalEntry parent;
+    static class ConsistsOfProcessEntry {
+       public ConsistsOfProcessEntry parent;
        public Resource resource;
-       public String name;
        public boolean valid = true;
-       InternalEntry(InternalEntry parent, Resource resource, String name) {
+       public String name = null;
+       ConsistsOfProcessEntry(ConsistsOfProcessEntry parent, Resource resource) {
                this.parent = parent;
                this.resource = resource;
-               this.name = name;
        }
     }
        
-    private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<Resource> resources, final Set<Resource> exclusions, final boolean ignoreVirtual) throws DatabaseException {
+    private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<SeedSpec> seeds, final boolean ignoreVirtual) throws DatabaseException {
 
                final Layer0 L0 = Layer0.getInstance(graph);
                final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
                
-               result = new ArrayList<InternalEntry>();
+               result = new ArrayList<>();
                childrenWithNoName = new HashSet<>();
-               names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
+               names = dqs.compileForEachObject(graph, L0.HasName, new SyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
 
                        @Override
-                       public void execute(AsyncReadGraph graph, InternalEntry entry, Resource nameResource) {
+                       public void execute(ReadGraph graph, ConsistsOfProcessEntry entry, Resource nameResource) throws DatabaseException {
                
                                if(status != null)
                                        status.put(nameResource, ExtentStatus.EXCLUDED);
                                
-                               graph.forPossibleValue(nameResource, new Procedure<String>() {
-
-                                       @Override
-                                       public void execute(String name) {
-                                           if(!entry.valid) return;
-                                           
-                                           if(name == null) {
-                                               entry.valid = false;
-                                           } else if (entry.name != null) {
-                                               entry.valid = false;
-                                           } else {
-                                               entry.name = name;
-                                           }
-                                       }
+                           if(!entry.valid) return;
+
+                               String name = graph.getValue(nameResource, Bindings.STRING);
+                               if(name == null) {
+                                       entry.valid = false;
+                               } else if (entry.name != null) {
+                                       entry.valid = false;
+                               } else {
+                                       entry.name = name;
+                               }
 
-                                       @Override
-                                       public void exception(Throwable t) {
-                                               Logger.defaultLogError(t);
-                                       }
-                                       
-                               });
+                               
                        }
 
                        @Override
-                       public void exception(AsyncReadGraph graph, Throwable throwable) {
+                       public void exception(ReadGraph graph, Throwable throwable) {
                                Logger.defaultLogError(throwable);
                        }
 
                        @Override
-                       public void finished(AsyncReadGraph graph, InternalEntry entry) {
+                       public void finished(ReadGraph graph, ConsistsOfProcessEntry entry) {
                            if(entry.valid) {
                                if(entry.name != null) {
                                    result.add(entry);
@@ -103,15 +100,16 @@ class ConsistsOfProcess {
                        }
                });
                
-               structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
+               structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new SyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
 
                        @Override
-                       public void execute(AsyncReadGraph graph, InternalEntry parent, Resource child) {
-                               
-                               if(exclusions.contains(child)) return;
+                       public void execute(ReadGraph graph, ConsistsOfProcessEntry parent, Resource child) {
+
+                               if(status != null)
+                                       if(ExtentStatus.EXCLUDED.equals(status.get(child))) return;
                                
                                if(!ignoreVirtual || child.isPersistent()) {
-                                       InternalEntry entry = new InternalEntry(parent, child, null);
+                                       ConsistsOfProcessEntry entry = new ConsistsOfProcessEntry(parent, child);
                                        dqs.forEachObjectCompiled(graph, child, entry, structure);
                                        dqs.forEachObjectCompiled(graph, child, entry, names);
                                }
@@ -119,11 +117,11 @@ class ConsistsOfProcess {
                        }
 
                        @Override
-                       public void finished(AsyncReadGraph graph, InternalEntry parent) {
+                       public void finished(ReadGraph graph, ConsistsOfProcessEntry parent) {
                        }
 
                        @Override
-                       public void exception(AsyncReadGraph graph, Throwable throwable) {
+                       public void exception(ReadGraph graph, Throwable throwable) {
                                Logger.defaultLogError(throwable);
                        }
 
@@ -133,9 +131,21 @@ class ConsistsOfProcess {
 
                        @Override
                        public void run(ReadGraph graph) throws DatabaseException {
-                               for(Resource r  : resources) {
-                                       InternalEntry root = new InternalEntry(null, r, null);
-                                       dqs.forEachObjectCompiled(graph, r, root, structure);
+                               for(SeedSpec seed  : seeds) {
+                                       
+                                       if(status != null) {
+                                               ExtentStatus es = status.get(seed.resource);
+                                               if(ExtentStatus.EXCLUDED.equals(es)) continue;
+                                               if(ExtentStatus.EXTERNAL.equals(es)) continue;
+                                       }
+                                       
+                                       ConsistsOfProcessEntry entry = new ConsistsOfProcessEntry(null, seed.resource);
+                                       
+                                       dqs.forEachObjectCompiled(graph, seed.resource, entry, structure);
+                                       if(SeedSpecType.INTERNAL.equals(seed.specType)) {
+                                               // Process names only for internal seeds
+                                               dqs.forEachObjectCompiled(graph, seed.resource, entry, names);
+                                       }
                                }
                        }