]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/QueryMemoryWatcher.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / QueryMemoryWatcher.java
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/QueryMemoryWatcher.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/QueryMemoryWatcher.java
new file mode 100644 (file)
index 0000000..178177a
--- /dev/null
@@ -0,0 +1,46 @@
+package org.simantics.db.common;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.service.QueryControl;\r
+\r
+public class QueryMemoryWatcher {\r
+\r
+    final ReadGraph graph;\r
+    final QueryControl support;\r
+    final int stopTime;\r
+    final long baseline;\r
+    final long highLimit;\r
+    final long lowLimit;\r
+    boolean collecting = false;\r
+    \r
+    public QueryMemoryWatcher(ReadGraph graph, int allowedQueries) {\r
+        this(graph, allowedQueries, 0.5, 10);\r
+    }\r
+\r
+    public QueryMemoryWatcher(ReadGraph graph, int allowedQueries, double ratio, int stopTime) {\r
+        this.support = graph.getService(QueryControl.class);\r
+        baseline = support.count();\r
+        highLimit = baseline + allowedQueries;\r
+        lowLimit = baseline + (int)(allowedQueries*ratio);\r
+        this.graph = graph;\r
+        this.stopTime = 10;\r
+        //System.err.println("QueryMemoryWatcher started with " + baseline + " queries.");\r
+    }\r
+    \r
+    public void maintain() {\r
+        long current = support.count();\r
+        if(collecting) {\r
+            if(current > lowLimit) support.gc(graph, stopTime);\r
+            else {\r
+//                System.err.println("QueryMemoryWatcher stops collecting");\r
+                collecting = false;\r
+            }\r
+        } else {\r
+            if(current > highLimit) {\r
+//                System.err.println("QueryMemoryWatcher starts collecting");\r
+                collecting = true;\r
+            }\r
+        }\r
+    }\r
+    \r
+}\r