Fixed ClusterTable cluster hash/importance map book-keeping problem
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionMonitorSupportImpl.java
1 package fi.vtt.simantics.procore.internal;
2
3 import org.simantics.db.MonitorContext;
4 import org.simantics.db.MonitorHandler;
5 import org.simantics.db.SessionVariables;
6 import org.simantics.db.service.SessionMonitorSupport;
7
8 public class SessionMonitorSupportImpl implements SessionMonitorSupport {
9         
10         final private SessionImplSocket session;
11         
12         SessionMonitorSupportImpl(SessionImplSocket session) {
13                 this.session = session;
14         }
15     
16     @Override
17     public synchronized MonitorContext registerMonitor(MonitorHandler h) {
18         assert h != null;
19
20         MonitorContext ctx = new MonitorContext() {
21             @Override
22             public int getInteger(String key) {
23                 if (SessionVariables.QUEUED_READS.equals(key)) {
24                     return session.state.getReadCount();
25                 } else if (SessionVariables.QUEUED_WRITES.equals(key)) {
26                     return session.state.getWriteCount();
27                 }
28                 return -1;
29             }
30
31             @Override
32             public String getString(String key) {
33                 return null;
34             }
35         };
36         session.monitorContexts.map(ctx, h);
37
38         MonitorHandler[] newHandlers = new MonitorHandler[session.monitorHandlers.length + 1];
39         System.arraycopy(session.monitorHandlers, 0, newHandlers, 0, session.monitorHandlers.length);
40         newHandlers[session.monitorHandlers.length] = h;
41         session.monitorHandlers = newHandlers;
42
43         return ctx;
44     }
45
46     @Override
47     public synchronized void unregisterMonitor(MonitorContext ctx) {
48         MonitorHandler h = session.monitorContexts.getRight(ctx);
49         if (h == null)
50             return;
51         int i = 0;
52         for (i = 0; i < session.monitorHandlers.length; i++) {
53             if (h.equals(session.monitorHandlers))
54                 break;
55         }
56         if (i < session.monitorHandlers.length) {
57             MonitorHandler[] newHandlers = new MonitorHandler[session.monitorHandlers.length - 1];
58             System.arraycopy(session.monitorHandlers, 0, newHandlers, 0, i);
59             System.arraycopy(session.monitorHandlers, i + 1, newHandlers, i, session.monitorHandlers.length - i - 1);
60         }
61     }
62     
63 }