]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/QueryMemoryWatcher.java
Merge commit 'bf75fd9'
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / QueryMemoryWatcher.java
1 package org.simantics.db.common;\r
2 \r
3 import org.simantics.db.ReadGraph;\r
4 import org.simantics.db.service.QueryControl;\r
5 \r
6 public class QueryMemoryWatcher {\r
7 \r
8     final ReadGraph graph;\r
9     final QueryControl support;\r
10     final int stopTime;\r
11     final long baseline;\r
12     final long highLimit;\r
13     final long lowLimit;\r
14     boolean collecting = false;\r
15     \r
16     public QueryMemoryWatcher(ReadGraph graph, int allowedQueries) {\r
17         this(graph, allowedQueries, 0.5, 10);\r
18     }\r
19 \r
20     public QueryMemoryWatcher(ReadGraph graph, int allowedQueries, double ratio, int stopTime) {\r
21         this.support = graph.getService(QueryControl.class);\r
22         baseline = support.count();\r
23         highLimit = baseline + allowedQueries;\r
24         lowLimit = baseline + (int)(allowedQueries*ratio);\r
25         this.graph = graph;\r
26         this.stopTime = 10;\r
27         //System.err.println("QueryMemoryWatcher started with " + baseline + " queries.");\r
28     }\r
29     \r
30     public void maintain() {\r
31         long current = support.count();\r
32         if(collecting) {\r
33             if(current > lowLimit) support.gc(graph, stopTime);\r
34             else {\r
35 //                System.err.println("QueryMemoryWatcher stops collecting");\r
36                 collecting = false;\r
37             }\r
38         } else {\r
39             if(current > highLimit) {\r
40 //                System.err.println("QueryMemoryWatcher starts collecting");\r
41                 collecting = true;\r
42             }\r
43         }\r
44     }\r
45     \r
46 }\r