]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history.rest/src/org/simantics/history/rest/HistoryCache.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.history.rest / src / org / simantics / history / rest / HistoryCache.java
1 package org.simantics.history.rest;
2
3 import org.simantics.Simantics;
4 import org.simantics.charts.Charts;
5 import org.simantics.db.AsyncReadGraph;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.request.ResourceRead;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.db.procedure.AsyncListener;
12 import org.simantics.db.request.Read;
13 import org.simantics.db.service.SerialisationSupport;
14 import org.simantics.history.HistorySamplerItem;
15 import org.simantics.history.HistorySamplerItem2;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.utils.datastructures.Pair;
18
19 public class HistoryCache {
20         
21         long runId;
22         Variable run;
23         
24         gnu.trove.map.TLongObjectMap<HistorySamplerItem> historySamplers = new gnu.trove.map.hash.TLongObjectHashMap<>();
25         gnu.trove.map.TLongObjectMap<HistorySamplerItem2> historySamplers2 = new gnu.trove.map.hash.TLongObjectHashMap<>();
26         
27         boolean disposed = false;
28         
29         public HistoryCache(long runId) throws DatabaseException{
30                 this.runId = runId;
31                 Pair<Resource, Variable> pair = Simantics.getSession().syncRequest(new Read<Pair<Resource, Variable>>() {
32                         @Override
33                         public Pair<Resource, Variable> perform(ReadGraph graph) throws DatabaseException {
34                                 Resource resource = getResource(graph, runId);
35                                 return new Pair<>(resource, graph.adapt(resource, Variable.class));
36                         }
37                 });
38                 run = pair.second;
39                 // Attach listener for automatic disposal
40                 Simantics.getSession().async(new ResourceRead<Resource>(pair.first) {
41                         @Override
42                         public Resource perform(ReadGraph graph) throws DatabaseException {
43                                 Layer0 L0 = Layer0.getInstance(graph);
44                                 return graph.getPossibleObject(resource, L0.PartOf);
45                         }
46                 }, new AsyncListener<Resource>() {
47                         @Override
48                         public void execute(AsyncReadGraph graph, Resource result) {
49                                 if (result == null)
50                                         dispose();
51                                 
52                         }
53                         
54                         @Override
55                         public void exception(AsyncReadGraph graph, Throwable throwable) {
56                                 dispose();
57                         }
58                         @Override
59                         public boolean isDisposed() {
60                                 return disposed;
61                         }
62                 });
63         }
64         
65         public HistorySamplerItem getSamplerItem(final long itemId) throws DatabaseException {
66                 HistorySamplerItem item = historySamplers.get(itemId);
67                 if (item == null) {
68                         item = Simantics.getSession().syncRequest(new Read<HistorySamplerItem>() {
69                                 @Override
70                                 public HistorySamplerItem perform(ReadGraph graph) throws DatabaseException {
71                                         Resource item = getResource(graph, itemId);
72                                         return Charts.createHistorySamplerItem(graph,run, item);
73                                 }
74                         });
75                         historySamplers.put(itemId, item);
76                 }
77                 return item;
78         }
79         
80         public HistorySamplerItem2 getSamplerItem2(final long itemId) throws DatabaseException {
81                 HistorySamplerItem2 item = historySamplers2.get(itemId);
82                 if (item == null) {
83                         item = Simantics.getSession().syncRequest(new Read<HistorySamplerItem2>() {
84                                 @Override
85                                 public HistorySamplerItem2 perform(ReadGraph graph) throws DatabaseException {
86                                         Resource item = getResource(graph, itemId);
87                                         return Charts.createHistorySamplerItem2(graph,run, item);
88                                 }
89                         });
90                         historySamplers2.put(itemId, item);
91                 }
92                 return item;
93         }
94         
95         public void dispose() {
96                 disposed = true;
97                 for (HistorySamplerItem i : historySamplers.valueCollection())
98                         i.close();
99                 historySamplers.clear();
100                 for (HistorySamplerItem2 i : historySamplers2.valueCollection())
101                         i.close();
102                 historySamplers2.clear();
103         }
104         
105         public boolean isDisposed() {
106                 return disposed;
107         }
108         
109         
110         private Resource getResource(ReadGraph graph, long id) throws DatabaseException { 
111                 SerialisationSupport ss = graph.getService(SerialisationSupport.class);
112             return ss.getResource(id);
113         }
114         
115 }