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