]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Even better optimization of document requests
authorAntti Villberg <antti.villberg@semantum.fi>
Fri, 25 Jan 2019 10:26:23 +0000 (12:26 +0200)
committerAntti Villberg <antti.villberg@semantum.fi>
Fri, 25 Jan 2019 10:26:23 +0000 (12:26 +0200)
Change-Id: I3b76b4ec8e767e974de83f4e52deba7c940b7174

bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java

index 4915fc002ecdbc91b0a54806f796daa8f730b85e..186edd25247fa9e67aa506404600cf18fb785ed6 100644 (file)
@@ -8,6 +8,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
 
 import org.simantics.Simantics;
@@ -15,6 +16,7 @@ import org.simantics.databoard.Bindings;
 import org.simantics.databoard.Datatypes;
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.type.Datatype;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.DirectStatements;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RequestProcessor;
@@ -26,12 +28,13 @@ import org.simantics.db.common.primitiverequest.Adapter;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.BinaryRead;
-import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.ResourceAsyncRead;
 import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.request.WriteResultRequest;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ServiceException;
 import org.simantics.db.layer0.function.All;
 import org.simantics.db.layer0.request.ProjectModels;
 import org.simantics.db.layer0.request.PropertyInfo;
@@ -51,6 +54,7 @@ import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.VariableMap;
 import org.simantics.db.layer0.variable.VariableMapImpl;
 import org.simantics.db.layer0.variable.Variables;
+import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.service.DirectQuerySupport;
 import org.simantics.document.base.ontology.DocumentationResource;
 import org.simantics.document.server.bean.Command;
@@ -214,44 +218,66 @@ public class Functions {
 
     };
 
-    static class DocumentPropertyKeys extends ResourceRead<List<String>> {
+    static class DocumentPropertyKeys extends ResourceAsyncRead<List<String>> {
 
         protected DocumentPropertyKeys(Resource resource) {
             super(resource);
         }
 
         @Override
-        public List<String> perform(ReadGraph graph) throws DatabaseException {
+        public void perform(AsyncReadGraph graph, final AsyncProcedure<List<String>> procedure) {
 
-            List<String> result = new ArrayList<>();
+            final List<String> result = new ArrayList<>();
 
             DocumentationResource DOC = DocumentationResource.getInstance(graph);
 
-            if(graph.hasStatement(resource, DOC.Properties_commands))
-                result.add("commands");
-            if(graph.hasStatement(resource, DOC.Properties_dataDefinitions))
-                result.add("dataDefinitions");
+            try {
+                if(graph.hasStatement(resource, DOC.Properties_commands))
+                    result.add("commands");
+                if(graph.hasStatement(resource, DOC.Properties_dataDefinitions))
+                    result.add("dataDefinitions");
+            } catch(ServiceException e) {
+                LOGGER.info(e.getMessage(), e);
+            }
+            
+            graph.forEachDirectPredicate(resource, new AsyncProcedure<Set<Resource>>() {
 
-            DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
-            DirectStatements ds = dqs.getDirectPersistentStatements(graph, resource);
+                @Override
+                public void execute(AsyncReadGraph graph, Set<Resource> predicates) {
+                    
+                    for(Resource predicate : predicates) {
 
-            for(Statement stm : ds) {
+                        try {
 
-                Resource predicate = stm.getPredicate();
-                PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+                            PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+
+                            if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
+                                result.add(info.name);
+                            } else {
+                                Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation);
+                                if(definition != null) {
+                                    PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition));
+                                    result.add(info2.name);
+                                }
+                            }
+
+                        } catch (DatabaseException e) {
+                            LOGGER.info(e.getMessage(), e);
+                        }
 
-                if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
-                    result.add(info.name);
-                } else {
-                    Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation);
-                    if(definition != null) {
-                        PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition));
-                        result.add(info2.name);
                     }
+
+                    procedure.execute(graph, result);
+                    
                 }
-            }
 
-            return result;
+                @Override
+                public void exception(AsyncReadGraph graph, Throwable throwable) {
+                    LOGGER.info(throwable.getMessage(), throwable);
+                    procedure.exception(graph, throwable);
+                }
+                
+            });
 
         }