1 package org.simantics.history.rest;
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;
19 public class HistoryCache {
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<>();
27 boolean disposed = false;
29 public HistoryCache(long runId) throws DatabaseException{
31 Pair<Resource, Variable> pair = Simantics.getSession().syncRequest(new Read<Pair<Resource, Variable>>() {
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));
39 // Attach listener for automatic disposal
40 Simantics.getSession().async(new ResourceRead<Resource>(pair.first) {
42 public Resource perform(ReadGraph graph) throws DatabaseException {
43 Layer0 L0 = Layer0.getInstance(graph);
44 return graph.getPossibleObject(resource, L0.PartOf);
46 }, new AsyncListener<Resource>() {
48 public void execute(AsyncReadGraph graph, Resource result) {
55 public void exception(AsyncReadGraph graph, Throwable throwable) {
59 public boolean isDisposed() {
65 public HistorySamplerItem getSamplerItem(final long itemId) throws DatabaseException {
66 HistorySamplerItem item = historySamplers.get(itemId);
68 item = Simantics.getSession().syncRequest(new Read<HistorySamplerItem>() {
70 public HistorySamplerItem perform(ReadGraph graph) throws DatabaseException {
71 Resource item = getResource(graph, itemId);
72 return Charts.createHistorySamplerItem(graph,run, item);
75 historySamplers.put(itemId, item);
80 public HistorySamplerItem2 getSamplerItem2(final long itemId) throws DatabaseException {
81 HistorySamplerItem2 item = historySamplers2.get(itemId);
83 item = Simantics.getSession().syncRequest(new Read<HistorySamplerItem2>() {
85 public HistorySamplerItem2 perform(ReadGraph graph) throws DatabaseException {
86 Resource item = getResource(graph, itemId);
87 return Charts.createHistorySamplerItem2(graph,run, item);
90 historySamplers2.put(itemId, item);
95 public void dispose() {
97 for (HistorySamplerItem i : historySamplers.valueCollection())
99 historySamplers.clear();
100 for (HistorySamplerItem2 i : historySamplers2.valueCollection())
102 historySamplers2.clear();
105 public boolean isDisposed() {
110 private Resource getResource(ReadGraph graph, long id) throws DatabaseException {
111 SerialisationSupport ss = graph.getService(SerialisationSupport.class);
112 return ss.getResource(id);