]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGRepresentation.java
Speeding up platform startup time
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TGRepresentation.java
1 package org.simantics.db.layer0.util;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.Map;
6
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.RequestProcessor;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.request.UniqueRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
13 import org.simantics.graph.db.TransferableGraphs;
14 import org.simantics.graph.representation.TransferableGraph1;
15 import org.simantics.utils.datastructures.hints.IHintContext.Key;
16
17 public class TGRepresentation implements Representation {
18
19         protected boolean ignoreVirtualResources = false;
20         protected Resource[] resources;
21         private TransferableGraph1 value = null;
22         private TransferableGraphConfiguration2 configuration;
23         
24         public TGRepresentation(Resource ... resources) {
25                 this.resources = resources;
26         }
27
28         public TGRepresentation(boolean ignoreVirtualResources, Resource ... resources) {
29                 this.ignoreVirtualResources = ignoreVirtualResources;
30                 this.resources = resources;
31         }
32
33         public TGRepresentation(TransferableGraphConfiguration2 configuration) {
34                 this.configuration = configuration;
35         }
36
37         public TGRepresentation(TransferableGraph1 value) {
38                 this.value = value;
39         }
40
41         @Override
42         public Key getKey() {
43                 return SimanticsKeys.KEY_TRANSFERABLE_GRAPH;
44         }
45         
46         public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
47                 
48                 if(configuration == null) {
49                         configuration = new TransferableGraphConfiguration2(graph, Arrays.asList(resources), ignoreVirtualResources, false);
50                         configuration.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
51                 }
52                 
53                 try (ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(configuration))) {
54                         return TransferableGraphs.create(graph, source);
55                 } catch (IOException e) {
56                         throw new DatabaseException(e);
57                 }
58         }
59         
60         @SuppressWarnings("unchecked")
61         @Override
62         public <T> T getValue(RequestProcessor processor, Map<String,Object> hints) throws DatabaseException {
63                 
64                 if(value == null) {
65
66                         value = processor.syncRequest(new UniqueRead<TransferableGraph1>() {
67
68                                 @Override
69                                 public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {
70                                         return compute(graph, hints);
71                                 }
72
73                         });
74
75                 }
76                 
77                 return (T)value;
78                 
79         }
80         
81 }