--- /dev/null
+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