]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Manually "cherry-picked" changes from ae2e31aa to release/1.35.2.1 33/4633/1 release/1.35.2.1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 23 Dec 2020 12:57:10 +0000 (14:57 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 23 Dec 2020 12:57:10 +0000 (14:57 +0200)
The hope is this will at least help with OOM problems in long-living
simupedia applications also with this older platform version.

gitlab #594

Change-Id: Ic03dff331498fbf508ba8d2ccb6af3fbdfaf7913

bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntryBase.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCollectorImpl.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHashSet.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodesRequest.java

index d65622dadd0a18ecafb1c15b3c7c22b15b345cb0..9f4e5e3e410cd9910bd1f748f305d7a568f80ac2 100644 (file)
@@ -42,6 +42,7 @@ public abstract class CacheEntry {
     abstract <T> T getResult();
 
     abstract CacheEntry pruneFirstParents();
+    abstract void pruneParentSet();
     abstract void removeParent(CacheEntry entry);
     abstract void addParent(CacheEntry entry);
     abstract boolean hasParents();
index d30d59320b656b1178d9dab10730fa2d2ca61d0d..297f4738f33d8f74aa1ec6b9344c7bc2bfd2dc1f 100644 (file)
@@ -258,6 +258,31 @@ abstract public class CacheEntryBase extends CacheEntry {
         
     }
     
+
+    @Override
+    void pruneParentSet() {
+        // First parent is discarded => look for more parents
+        if(p2OrParents instanceof QueryIdentityHashSet) {
+
+            QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
+            set.removeDiscardedReally();
+            if(set.isEmpty()) p2OrParents = null;
+
+        } else if(p2OrParents instanceof CacheEntry) {
+
+            CacheEntry entry = (CacheEntry)p2OrParents;
+            if(entry.isDiscarded()) {
+                // Second entry is also discarded => all empty
+                p2OrParents = null;
+            }
+
+        } else {
+
+            // Nothing left
+
+        }
+    }
+
     @Override
     final public void removeParent(CacheEntry entry) {
        
index 965f1e39f41fb1dc4d99000d533c03dde6c21da3..1e6cc021423409266074e16ec01dab50ff550ca9 100644 (file)
@@ -144,6 +144,7 @@ class QueryCollectorImpl implements QueryProcessor.QueryCollector {
                                                        
                                                } else {
                                                        
+                                                       entry.pruneParentSet();
                                                        support.setLevel(entry, parent.getLevel() + 1);
                                                        
                                                }
index 4842cf5a6ca44f6cbb543fad1cac8c4c5944e115..396f1ba07790de3f597895a5a8536ff02fed6e90 100644 (file)
@@ -226,7 +226,11 @@ abstract public class QueryIdentityHash extends THash {
                public Object getOriginalRequest() {
                        throw new UnsupportedOperationException();
                }
-        
+
+        @Override
+        void pruneParentSet() {
+        }
+
     };
 
     /**
index 3a7eb61820203ffc5e079d0a57b12f7fe8c86842..ac2602fcb647b4ad54e8de9edf1a8c4412313d95 100644 (file)
@@ -147,7 +147,25 @@ final public class QueryIdentityHashSet extends QueryIdentityHash implements Ite
         }
         
     }
-    
+
+    final public void removeDiscardedReally() {
+
+        tempDisableAutoCompaction();
+        try {
+
+            for(int i=0;i<_set.length;i++) {
+                CacheEntry entry = _set[i];
+                if(entry != null && REMOVED != entry) {
+                    if(entry.isDiscarded()) removeAt(i);
+                }
+            }
+
+        } finally {
+            reenableAutoCompaction(false);
+        }
+
+    }
+
     /**
      * Creates an iterator over the values of the set.  The iterator
      * supports element deletion.
index 9a53060bef2c0b246a21e465da1f0e53756d2f17..be5570f7f5a2d80bfe8b5be65ab1e1951dba8405 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.document.server.request;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashSet;
@@ -9,64 +10,64 @@ import java.util.Set;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.VariableRead;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.document.server.JSONObject;
 
 public class DocumentRequest extends VariableRead<List<JSONObject>> {
-       
-       public static boolean PROFILE = false;
-       // Thresholds in microseconds
-       public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
-       public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
+
+    public static boolean PROFILE = false;
+    // Thresholds in microseconds
+    public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
+    public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
 
     public DocumentRequest(Variable var) {
         super(var);
-       }
+    }
+
+    static class CollectNodesRequest extends UnaryRead<Collection<Variable>, Collection<JSONObject>> {
+
+        public CollectNodesRequest(Collection<Variable> nodes) {
+            super(nodes);
+        }
+
+        @Override
+        public Collection<JSONObject> perform(ReadGraph graph) throws DatabaseException {
+            HashSet<JSONObject> rs = new HashSet<>(); // result
+            for(Variable node : parameter) {
+                rs.add(graph.syncRequest(new NodeRequest(node)));
+            }
+            return rs;
+        }
+
+    }
+
+    private static final Comparator<JSONObject> JSON_COMPARATOR = (o1, o2) -> o1.id.compareTo(o2.id);
+
+    @Override
+    public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
+
+        long s = System.nanoTime();
 
-       @Override
-       public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
-               
-               long s = System.nanoTime();
-               
         Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
-        HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
         if(nodes.isEmpty()) {
             return Collections.emptyList();
         }
-        
-        
-        /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
-        
-        for (Variable node : nodes) {
-               nodeMap.put(node.getURI(graph), node);
-        }
-        System.out.println("*************************************************************************");
-        for (Variable node : nodeMap.values()) {
-               System.out.println("               " + node.getURI(graph));
-        }*/
-        
-        for(Variable node : nodes) {
-            rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
-        }
 
-               ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
-               Collections.sort(result, new Comparator<JSONObject>() {
+        Collection<JSONObject> rs = graph.syncRequest(new CollectNodesRequest(nodes));
+
+        ArrayList<JSONObject> result = new ArrayList<>(rs);
+        Collections.sort(result, JSON_COMPARATOR);
 
-                       @Override
-                       public int compare(JSONObject o1, JSONObject o2) {
-                               return o1.id.compareTo(o2.id);
-                       }
-                       
-               });
-        
         if(PROFILE) {
-               long dura = System.nanoTime()-s;
-               System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+            long dura = System.nanoTime()-s;
+            System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
         }
 
-               return result;
+        return result;
+
+    }
 
-       }
 }
\ No newline at end of file
index f3f1177fc4d1f613950bc7b4f7d56e2ec010105f..17666df6fbcd87e5c9b49a57d0f8256b7041c307 100644 (file)
@@ -1,12 +1,12 @@
 package org.simantics.document.server.request;
 
-import gnu.trove.set.hash.THashSet;
-
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
 import org.simantics.db.ReadGraph;
+import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.VariableChildren;
 import org.simantics.db.layer0.request.VariableRead;
@@ -17,31 +17,46 @@ public class NodesRequest extends VariableRead<Set<Variable>> {
 
     public NodesRequest(Variable var) {
         super(var);
-       }
-
-       @Override
-       public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
-
-               long s = System.nanoTime();
-               
-               StructuralResource2.getInstance(graph);
-               if(variable == null)
-                       return Collections.emptySet();
-               
-               Set<Variable> nodes = new THashSet<Variable>();
-               Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
-               for(Variable child : children) {
-                       Set<Variable> childNodes = graph.syncRequest(new NodesRequest2(child));
-                       nodes.addAll(childNodes);
-               }
-               
+    }
+
+    static class CollectNodesRequest2 extends UnaryRead<Collection<Variable>, Set<Variable>> {
+
+        public CollectNodesRequest2(Collection<Variable> nodes) {
+            super(nodes);
+        }
+
+        @Override
+        public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
+            HashSet<Variable> rs = new HashSet<>(); // result
+            for(Variable child : parameter) {
+                Set<Variable> childNodes = graph.syncRequest(new NodesRequest2(child));
+                rs.addAll(childNodes);
+            }
+            return rs;
+        }
+
+    }
+
+    @Override
+    public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
+
+        long s = System.nanoTime();
+
+        StructuralResource2.getInstance(graph);
+        if(variable == null)
+            return Collections.emptySet();
+
+        Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
+
+        Set<Variable> nodes = graph.syncRequest(new CollectNodesRequest2(children));
+
         if(DocumentRequest.PROFILE) {
-               long dura = System.nanoTime()-s;
-               System.err.println("NodesRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+            long dura = System.nanoTime()-s;
+            System.err.println("NodesRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
         }
-               
-               return nodes;
 
-       }
+        return nodes;
+
+    }
 
 }
\ No newline at end of file