]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Merge "Optimize ReadGraph.hasStatements(s,p) to not use getObjects(s,p)" into release...
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 17 Jun 2020 20:13:57 +0000 (20:13 +0000)
committerGerrit Code Review <gerrit2@simantics>
Wed, 17 Jun 2020 20:13:57 +0000 (20:13 +0000)
bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/browsecontexts/BrowseContext.java
bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/children/ChildContribution.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/HierarchyMultiMap.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/SessionGarbageCollection.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java
bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/AdapterRegistry2.java
bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/reflection/GraphObject2.java
bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/reflection/ThisResource2.java

index 42ac584209e721dafcffc5d6b9a24e650445fde2..f94ffb3e4bfa7b27a05e8cc88ef2351303250813 100644 (file)
@@ -57,10 +57,12 @@ import org.simantics.browsing.ui.model.visuals.VisualsContribution;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ResourceNotFoundException;
 import org.simantics.db.layer0.variable.Variable;
-import org.simantics.db.request.Read;
 import org.simantics.scl.reflection.OntologyVersions;
 import org.simantics.viewpoint.ontology.ViewpointResource;
 import org.slf4j.Logger;
@@ -76,21 +78,21 @@ public class BrowseContext {
     private static final Logger LOGGER = LoggerFactory.getLogger(BrowseContext.class);
        public static final boolean DEBUG = false;
        
-    NodeTypeMultiMap<ChildContribution> childContributions = new NodeTypeMultiMap<ChildContribution>();
-    NodeTypeMultiMap<ChildContribution> parentContributions = new NodeTypeMultiMap<ChildContribution>();
-    OrderedNodeTypeMultiMap<LabelContribution> labelContributions = new OrderedNodeTypeMultiMap<LabelContribution>();
-    OrderedNodeTypeMultiMap<ImageContribution> imageContributions = new OrderedNodeTypeMultiMap<ImageContribution>();
-    OrderedNodeTypeMultiMap<CheckedStateContribution> checkedStateContributions = new OrderedNodeTypeMultiMap<CheckedStateContribution>();
-    OrderedNodeTypeMultiMap<LabelDecorationContribution> labelDecorationContributions = new OrderedNodeTypeMultiMap<LabelDecorationContribution>();
-    OrderedNodeTypeMultiMap<ImageDecorationContribution> imageDecorationContributions = new OrderedNodeTypeMultiMap<ImageDecorationContribution>();
-    OrderedNodeTypeMultiMap<ModifierContribution> modifierContributions = new OrderedNodeTypeMultiMap<ModifierContribution>();
-    OrderedNodeTypeMultiMap<SorterContribution> sorterContributions = new OrderedNodeTypeMultiMap<SorterContribution>();
-    OrderedNodeTypeMultiMap<FlatNodeContribution> flatNodeContributions = new OrderedNodeTypeMultiMap<FlatNodeContribution>();
+    NodeTypeMultiMap<ChildContribution> childContributions = new NodeTypeMultiMap<>();
+    NodeTypeMultiMap<ChildContribution> parentContributions = new NodeTypeMultiMap<>();
+    OrderedNodeTypeMultiMap<LabelContribution> labelContributions = new OrderedNodeTypeMultiMap<>();
+    OrderedNodeTypeMultiMap<ImageContribution> imageContributions = new OrderedNodeTypeMultiMap<>();
+    OrderedNodeTypeMultiMap<CheckedStateContribution> checkedStateContributions = new OrderedNodeTypeMultiMap<>();
+    OrderedNodeTypeMultiMap<LabelDecorationContribution> labelDecorationContributions = new OrderedNodeTypeMultiMap<>();
+    OrderedNodeTypeMultiMap<ImageDecorationContribution> imageDecorationContributions = new OrderedNodeTypeMultiMap<>();
+    OrderedNodeTypeMultiMap<ModifierContribution> modifierContributions = new OrderedNodeTypeMultiMap<>();
+    OrderedNodeTypeMultiMap<SorterContribution> sorterContributions = new OrderedNodeTypeMultiMap<>();
+    OrderedNodeTypeMultiMap<FlatNodeContribution> flatNodeContributions = new OrderedNodeTypeMultiMap<>();
     OrderedNodeTypeMultiMap<TooltipContribution> tooltipContributions = new OrderedNodeTypeMultiMap<>();
 
     private final String[] uris; 
 
-    private BrowseContext(String[] uris) {
+    private BrowseContext(String... uris) {
         if (uris == null)
             throw new NullPointerException("null URIs");
         this.uris = uris;
@@ -109,6 +111,38 @@ public class BrowseContext {
         return defaultContext;
     }
 
+    private static BrowseContext loadCachedVisuals(ReadGraph g, Resource visualsContributionResource) throws DatabaseException, InvalidContribution {
+        try {
+            return g.syncRequest(new ResourceRead<BrowseContext>(visualsContributionResource) {
+                @Override
+                public BrowseContext perform(ReadGraph graph) throws DatabaseException {
+                    try {
+                        BrowseContext bc = new BrowseContext();
+                        VisualsContribution.load(g, visualsContributionResource,
+                                bc.labelContributions,
+                                bc.imageContributions,
+                                bc.checkedStateContributions,
+                                bc.labelDecorationContributions,
+                                bc.imageDecorationContributions,
+                                bc.modifierContributions,
+                                bc.sorterContributions,
+                                bc.flatNodeContributions,
+                                bc.tooltipContributions
+                                );
+                        return bc;
+                    } catch (InvalidContribution e) {
+                        throw new DatabaseException(e);
+                    }
+                }
+            }, TransientCacheAsyncListener.instance());
+        } catch (DatabaseException e) {
+            Throwable c = e.getCause();
+            if (c instanceof InvalidContribution)
+                throw (InvalidContribution) c;
+            throw e;
+        }
+    }
+
     /**
      * Creates a new BrowseContext for the given Collection of {@link Resource}s.
      * 
@@ -125,24 +159,23 @@ public class BrowseContext {
             
             for(Resource childContributionResource : 
                 g.getObjects(browseContextResource, vr.BrowseContext_HasChildContribution)) {
-                ChildContribution contribution = ChildContribution.create(g, childContributionResource);
+                ChildContribution contribution = ChildContribution.createCached(g, childContributionResource);
                 browseContext.childContributions.put(contribution.getParentNodeType(), contribution);
                 browseContext.parentContributions.put(contribution.getChildNodeType(), contribution);
             }
             
             for(Resource visualsContributionResource : 
                 g.getObjects(browseContextResource, vr.BrowseContext_HasVisualsContribution)) {
-                VisualsContribution.load(g, visualsContributionResource,
-                        browseContext.labelContributions,
-                        browseContext.imageContributions,
-                        browseContext.checkedStateContributions,
-                        browseContext.labelDecorationContributions,
-                        browseContext.imageDecorationContributions,
-                        browseContext.modifierContributions,
-                        browseContext.sorterContributions,
-                        browseContext.flatNodeContributions,
-                        browseContext.tooltipContributions
-                        );
+                BrowseContext visuals = loadCachedVisuals(g, visualsContributionResource);
+                visuals.labelContributions.appendTo(browseContext.labelContributions);
+                visuals.imageContributions.appendTo(browseContext.imageContributions);
+                visuals.checkedStateContributions.appendTo(browseContext.checkedStateContributions);
+                visuals.labelDecorationContributions.appendTo(browseContext.labelDecorationContributions);
+                visuals.imageDecorationContributions.appendTo(browseContext.imageDecorationContributions);
+                visuals.modifierContributions.appendTo(browseContext.modifierContributions);
+                visuals.sorterContributions.appendTo(browseContext.sorterContributions);
+                visuals.flatNodeContributions.appendTo(browseContext.flatNodeContributions);
+                visuals.tooltipContributions.appendTo(browseContext.tooltipContributions);
             }
         }
         //browseContext.visualize();
@@ -150,11 +183,11 @@ public class BrowseContext {
     }
 
     public static Set<String> getBrowseContextClosure(RequestProcessor processor, final Set<String> browseContexts) throws DatabaseException {
-        return processor.syncRequest(new Read<Set<String>>() {
+        return processor.syncRequest(new UnaryRead<Set<String>, Set<String>>(browseContexts) {
             @Override
             public Set<String> perform(ReadGraph graph) throws DatabaseException {
-                Collection<Resource> browseContextResources = new ArrayList<Resource>(browseContexts.size());
-                for (String browseContext : browseContexts) {
+                Collection<Resource> browseContextResources = new ArrayList<>(parameter.size());
+                for (String browseContext : parameter) {
                     try {
                         browseContextResources.add(graph.getResource(browseContext));
                     } catch (ResourceNotFoundException e) {
@@ -162,19 +195,29 @@ public class BrowseContext {
                     }
                 }
                 Collection<Resource> allBrowseContextResources = BrowseContext.findSubcontexts(graph, browseContextResources);
-                Set<String> result = new HashSet<String>();
+                Set<String> result = new HashSet<>();
                 for (Resource r : allBrowseContextResources)
                     result.add(graph.getURI(r));
-                        return result;
+                return result;
             }
-        });
+        }, TransientCacheAsyncListener.instance());
     }
 
-    public static Collection<Resource> findSubcontexts(ReadGraph g,
+    public static Collection<Resource> findSubcontexts(ReadGraph g, Collection<Resource> browseContexts)
+            throws DatabaseException {
+        return g.syncRequest(new UnaryRead<Collection<Resource>, Collection<Resource>>(browseContexts) {
+            @Override
+            public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
+                return findSubcontexts0(graph, parameter);
+            }
+        }, TransientCacheAsyncListener.instance());
+    }
+
+    private static Collection<Resource> findSubcontexts0(ReadGraph g,
             Collection<Resource> browseContexts) throws DatabaseException {
         ViewpointResource vr = ViewpointResource.getInstance(g);
-        HashSet<Resource> result = new HashSet<Resource>(browseContexts);
-        ArrayList<Resource> stack = new ArrayList<Resource>(browseContexts);
+        HashSet<Resource> result = new HashSet<>(browseContexts);
+        ArrayList<Resource> stack = new ArrayList<>(browseContexts);
         while(!stack.isEmpty()) {
             Resource cur = stack.remove(stack.size()-1);
             for(Resource sc : g.getObjects(cur, vr.BrowseContext_Includes))
@@ -183,7 +226,7 @@ public class BrowseContext {
         }
         return result;
     }
-    
+
     /**
      * Finds the possible children of the given {@link NodeContext} parameter.
      * 
index ca9f59da0cd89e7f5bd34474b28d07288527bcab..09a15fefcb6bd5a4235fd2a4433130057c54b170 100644 (file)
@@ -22,6 +22,8 @@ import org.simantics.browsing.ui.model.nodetypes.NodeType;
 import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.exception.PendingVariableException;
@@ -52,6 +54,26 @@ public class ChildContribution {
         this.priority = priority;
     }
 
+    public static ChildContribution createCached(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
+        try {
+            return g.syncRequest(new ResourceRead<ChildContribution>(childContributionResource) {
+                @Override
+                public ChildContribution perform(ReadGraph graph) throws DatabaseException {
+                    try {
+                        return create(g, resource);
+                    } catch (InvalidContribution e) {
+                        throw new DatabaseException(e);
+                    }
+                }
+            }, TransientCacheAsyncListener.instance());
+        } catch (DatabaseException e) {
+            Throwable c = e.getCause();
+            if (c instanceof InvalidContribution)
+                throw (InvalidContribution) c;
+            throw e;
+        }
+    }
+
     public static ChildContribution create(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
         ViewpointResource vr = ViewpointResource.getInstance(g);
         
index a4ac87b50d1472d02626647145e6c412905aa7b6..b12d5ad2b9b4ab9d2f7d74733a2ebfe1fae21b71 100644 (file)
@@ -37,7 +37,28 @@ public abstract class HierarchyMultiMap<A,B> {
         bs.add(b);
         cache = null;
     }
-    
+
+    /**
+     * Appends the contents of the specified map to this map.
+     * 
+     * @param from the map to append contents from
+     */
+    public void append(HierarchyMultiMap<A,B> from) {
+        from.appendTo(this);
+    }
+
+    /**
+     * Appends the contents of this map to the specified map.
+     * 
+     * @param to the map to append to
+     */
+    public void appendTo(HierarchyMultiMap<A,B> to) {
+        map.forEachEntry((a, bl) -> {
+            bl.forEach(b -> to.put(a, b));
+            return true;
+        });
+    }
+
     /**
      * Gets the values stored into the map for the key {@code a} or
      * its superelements.
index c168e22860b25dba84ff1b715376c4a878effcfb..c48ec36309e601cf885f9b8125f16f7e79c579f8 100644 (file)
@@ -76,7 +76,18 @@ public class SessionGarbageCollection {
         QueryControl qc = graph.getService(QueryControl.class);
         ClusterControl cc = graph.getService(ClusterControl.class);
         _monitor.beginTask("Collect clusters", IProgressMonitor.UNKNOWN);
-        cc.gc(graph, clusterTarget);
+        //cc.gc(graph, clusterTarget);
+        if(clusterTarget == -1) {
+            int used = cc.used();
+            //System.err.println("session gc, cluster use = " + used);
+            int baseline = 32*(1<<20);
+            if(used > baseline) {
+                int dynamicTarget = (int)(0.95 * (used-baseline)) + baseline;
+                cc.gc(graph, dynamicTarget);
+            }
+        } else {
+            cc.gc(graph, clusterTarget);
+        }
         _monitor.beginTask("Collect queries", IProgressMonitor.UNKNOWN);
         qc.gc(graph, allowedTimeInMs);
     }
index 7df13315f8f68b093fdbf6cb91b0359b5bb909e1..a52870a1560301e6e372210d827818785fc217c1 100644 (file)
@@ -524,10 +524,11 @@ public final class ClusterTable implements IClusterTable {
                 ArrayList<CollectorCluster> toRelease = new ArrayList<CollectorCluster>();
 
                 for (CollectorCluster cluster : support.getResidentClusters()) {
-                    target -= support.getClusterSize(cluster);
-                    if (target > 0) {
-                        toRelease.add(cluster);
-                    } else {
+                    toRelease.add(cluster);
+                    long clusterSize = support.getClusterSize(cluster);
+                    //System.err.println("release cluster with " + (clusterSize/1024) + " kiB - " + cluster);
+                    target -= clusterSize;
+                    if (target <= 0) {
                         break;
                     }
                 }
index 0e52b8a417da4ebecf82c27369cf7581171c58e4..478d3ebee93b22dcf2cacd18326dc407ff5595ca 100644 (file)
@@ -237,9 +237,9 @@ public class AdapterRegistry2 {
                 NamedNodeMap attr = n.getAttributes();
                 IDynamicAdapter2 da = null;
                 if(n.getNodeName().equals("this"))
-                    da = new ThisResource2();
+                    da = ThisResource2.INSTANCE;
                 else if(n.getNodeName().equals("graph"))
-                    da = new GraphObject2();
+                    da = GraphObject2.INSTANCE;
                 else if(n.getNodeName().equals("bundle")) {
                     String bundleId = null;
                     Node fc = n.getFirstChild();
index e8f171b8ca02a170268d4270e442ed3655ffad9d..8a2db578f6488debda2a66fa3d1719c6772cca10 100644 (file)
@@ -15,6 +15,8 @@ import org.simantics.db.ReadGraph;
 
 public class GraphObject2 implements IDynamicAdapter2 {
 
+       public static final IDynamicAdapter2 INSTANCE = new GraphObject2();
+
        @Override
        public Class<?> getType() {
                return ReadGraph.class;
index 24001ee0ae90840a5333a3c2665a098b92f42f23..bacf3adee83f92185c46572121f751e0a8e67de7 100644 (file)
@@ -16,6 +16,8 @@ import org.simantics.db.ReadGraph;
 
 public class ThisResource2 implements IDynamicAdapter2 {
 
+       public static IDynamicAdapter2 INSTANCE = new ThisResource2();
+
        @Override
        public Class<?> getType() {
                return Resource.class;