--- /dev/null
+package org.simantics.history.rest;
+
+import org.simantics.Simantics;
+import org.simantics.charts.Charts;
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.procedure.AsyncListener;
+import org.simantics.db.request.Read;
+import org.simantics.db.service.SerialisationSupport;
+import org.simantics.history.HistorySamplerItem;
+import org.simantics.history.HistorySamplerItem2;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.datastructures.Pair;
+
+public class HistoryCache {
+
+ long runId;
+ Variable run;
+
+ gnu.trove.map.TLongObjectMap<HistorySamplerItem> historySamplers = new gnu.trove.map.hash.TLongObjectHashMap<>();
+ gnu.trove.map.TLongObjectMap<HistorySamplerItem2> historySamplers2 = new gnu.trove.map.hash.TLongObjectHashMap<>();
+
+ boolean disposed = false;
+
+ public HistoryCache(long runId) throws DatabaseException{
+ this.runId = runId;
+ Pair<Resource, Variable> pair = Simantics.getSession().syncRequest(new Read<Pair<Resource, Variable>>() {
+ @Override
+ public Pair<Resource, Variable> perform(ReadGraph graph) throws DatabaseException {
+ Resource resource = getResource(graph, runId);
+ return new Pair<>(resource, graph.adapt(resource, Variable.class));
+ }
+ });
+ run = pair.second;
+ // Attach listener for automatic disposal
+ Simantics.getSession().async(new ResourceRead<Resource>(pair.first) {
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ return graph.getPossibleObject(resource, L0.PartOf);
+ }
+ }, new AsyncListener<Resource>() {
+ @Override
+ public void execute(AsyncReadGraph graph, Resource result) {
+ if (result == null)
+ dispose();
+
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ dispose();
+ }
+ @Override
+ public boolean isDisposed() {
+ return disposed;
+ }
+ });
+ }
+
+ public HistorySamplerItem getSamplerItem(final long itemId) throws DatabaseException {
+ HistorySamplerItem item = historySamplers.get(itemId);
+ if (item == null) {
+ item = Simantics.getSession().syncRequest(new Read<HistorySamplerItem>() {
+ @Override
+ public HistorySamplerItem perform(ReadGraph graph) throws DatabaseException {
+ Resource item = getResource(graph, itemId);
+ return Charts.createHistorySamplerItem(graph,run, item);
+ }
+ });
+ historySamplers.put(itemId, item);
+ }
+ return item;
+ }
+
+ public HistorySamplerItem2 getSamplerItem2(final long itemId) throws DatabaseException {
+ HistorySamplerItem2 item = historySamplers2.get(itemId);
+ if (item == null) {
+ item = Simantics.getSession().syncRequest(new Read<HistorySamplerItem2>() {
+ @Override
+ public HistorySamplerItem2 perform(ReadGraph graph) throws DatabaseException {
+ Resource item = getResource(graph, itemId);
+ return Charts.createHistorySamplerItem2(graph,run, item);
+ }
+ });
+ historySamplers2.put(itemId, item);
+ }
+ return item;
+ }
+
+ public void dispose() {
+ disposed = true;
+ for (HistorySamplerItem i : historySamplers.valueCollection())
+ i.close();
+ historySamplers.clear();
+ for (HistorySamplerItem2 i : historySamplers2.valueCollection())
+ i.close();
+ historySamplers2.clear();
+ }
+
+ public boolean isDisposed() {
+ return disposed;
+ }
+
+
+ private Resource getResource(ReadGraph graph, long id) throws DatabaseException {
+ SerialisationSupport ss = graph.getService(SerialisationSupport.class);
+ return ss.getResource(id);
+ }
+
+}