]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/GraphBundle.java
Platform startup performance improvements
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / GraphBundle.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.project.management;
13
14 import java.util.function.Supplier;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.simantics.databoard.binding.error.BindingException;
19 import org.simantics.databoard.binding.error.RuntimeBindingException;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.RequestProcessor;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.request.ResourceRead;
24 import org.simantics.db.common.utils.Transaction;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.graph.representation.TransferableGraph1;
27 import org.simantics.layer0.DatabaseManagementResource;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * GraphBundle represents a bundle graph that may exist in memory 
33  * in a OSGi Bundle Context, a P2 Bundle Pool, or in Simantics Database.
34  * 
35  * The string representation of the version is in the following format: 
36  *   <id>/<major.minor.micro(.qualifier)>
37  * 
38  * Here is what is said about osgi version numbers:
39  * 
40  *  Major - Differences in the major part indicate significant differences 
41  *          such that backward compability is not guaranteed.
42  *          
43  *  Minor - Changes in the minor part indicate that the newer version of the
44  *          entity is backward compatible with the older version, but it 
45  *          includes additional functionality and/or API.
46  *     
47  *  Service - The service part indicates the presence of bug fixes and minor
48  *          implementation (i.e., hidden) changes over previous versions.
49  *  
50  *  Qualifier - The qualifier is not interpreted by the system. Qualifiers are
51  *           compared using standard string comparison. Qualifier is determined
52  *           at build time by builder. It may be millisecond time or version 
53  *           control revision number. The value is monotonically increasing. 
54  *           
55  *   
56  * The class is hash-equals-comparable.
57  *
58  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
59  */
60 public class GraphBundle implements Comparable<GraphBundle> {
61
62         private static final Logger LOGGER = LoggerFactory.getLogger(GraphBundle.class);
63
64         /** Versioned Id pattern */     
65         static String ID_PATTERN_STRING =                "[a-zA-Z_0-9\\-]+(?:\\.[a-zA-Z_0-9\\-]+)*";
66         static String VERSION_PATTERN_STRING =           "(\\d+).(\\d+).(\\d+).([a-zA-Z_0-9\\-]+)";
67         static Pattern ID_PATTERN = Pattern.compile(ID_PATTERN_STRING);
68         static Pattern VERSION_PATTERN = Pattern.compile(VERSION_PATTERN_STRING);
69         static Pattern VERSIONED_ID_PATTERN = Pattern.compile("(" + ID_PATTERN_STRING + ")/" + VERSION_PATTERN_STRING + "");
70                 
71         /** User-friendly name */
72         String name;
73         
74         /** If {@link #graph} is null then this may be defined to fetch the data on-demand */
75         Supplier<TransferableGraph1> graphSource;
76         
77         /** Actual graph */
78         TransferableGraph1 graph;
79         
80         /** GraphBundle resource in database */ 
81         Resource resource;
82         
83         /** Graph hash code */
84         int hashcode;
85         
86         /** Id */
87         String id;
88         
89         // Version
90         int major, minor, service;
91         
92         // Optional qualifier
93         String qualifier;
94         
95         /** Database install Info, optional */
96         long[] resourceArray;
97
98         /** Should this ontology be installed immutable **/
99         boolean immutable = true;
100
101         GraphBundle() {}
102
103         public GraphBundle(String name, TransferableGraph1 data, String versionedId) 
104         throws RuntimeBindingException {
105                 try {
106                         // Assert version id is correct
107                         Matcher m = VERSIONED_ID_PATTERN.matcher(versionedId); 
108                         if (!m.matches()) {
109                                 throw new IllegalArgumentException("Illegal VersionId \""+versionedId+"\", <id>/<major.minor.micro.qualifier> is expected.");
110                         }
111
112                         this.name = name;
113                         this.graph = data;
114                         this.hashcode = hash(data);
115                         this.id = m.group(1);
116                         this.major = Integer.valueOf( m.group(2) );
117                         this.minor = Integer.valueOf( m.group(3) );
118                         if (m.group(4) != null) {
119                                 this.service = Integer.valueOf( m.group(4) );
120                         }
121                         this.qualifier = m.group(5);
122                 } catch (BindingException e) {
123                         // Unexpected
124                         throw new RuntimeBindingException(e);
125                 }
126         }
127
128         public GraphBundle(String name, TransferableGraph1 data, String id, String version) 
129         throws RuntimeBindingException {
130                 Matcher m = ID_PATTERN.matcher(id);
131                 if (!m.matches()) 
132                         throw new IllegalArgumentException("Illegal Id, got \""+id+"\"");
133                 m = VERSION_PATTERN.matcher(version);
134                 if (!m.matches()) 
135                         throw new IllegalArgumentException("Illegal Version, got \""+id+"\", <id>/<major.minor.micro.qualifier> is expected.");
136                 try {
137                         this.name = name;
138                         this.graph = data;
139                         this.hashcode = hash(data);
140                         this.id = id;
141                         this.major = Integer.valueOf( m.group(1) );
142                         this.minor = Integer.valueOf( m.group(2) );
143                         this.service = Integer.valueOf( m.group(3) );
144                         if (m.group(4) != null) {
145                                 this.qualifier = m.group(4);
146                         }
147                 } catch (BindingException e) {
148                         // Unexpected
149                         throw new RuntimeBindingException(e);
150                 }
151         }
152
153         public GraphBundle(String name, Supplier<TransferableGraph1> source, int hashCode, String id, String version) {
154                 Matcher m = ID_PATTERN.matcher(id);
155                 if (!m.matches()) 
156                         throw new IllegalArgumentException("Illegal Id, got \""+id+"\"");
157                 m = VERSION_PATTERN.matcher(version);
158                 if (!m.matches()) 
159                         throw new IllegalArgumentException("Illegal Version, got \""+id+"\", <id>/<major.minor.micro.qualifier> is expected.");
160                 this.name = name;
161                 this.graphSource = source;
162                 this.hashcode = hashCode;
163                 this.id = id;
164                 this.major = Integer.valueOf( m.group(1) );
165                 this.minor = Integer.valueOf( m.group(2) );
166                 this.service = Integer.valueOf( m.group(3) );
167                 if (m.group(4) != null) {
168                         this.qualifier = m.group(4);
169                 }
170         }
171
172         private int hash(TransferableGraph1 data) throws BindingException {
173                 return data == null ? 0 : TransferableGraph1.BINDING.hashValue( data );
174         }
175
176         public String getName() {
177                 return name;
178         }
179
180         @Override
181         public int compareTo(GraphBundle o) {
182             int cur = id.compareTo(o.id);
183             if(cur != 0)
184                 return cur;
185             
186             cur = major - o.major;
187             if(cur != 0)
188             return cur;
189             
190             cur = minor - o.minor;
191         if(cur != 0)
192             return cur;
193         
194         cur = service - o.service;        
195         return cur;
196         }
197         
198         /**
199          * This method excepts {@link Transaction#readGraph()} to return a non-null
200          * value, i.e. a database transaction must be in progress that has been
201          * started with
202          * {@link Transaction#startTransaction(RequestProcessor, boolean)}.
203          * 
204          * @return
205          * @see #getGraph(RequestProcessor)
206          */
207         public TransferableGraph1 getGraph() {
208                 if (graph == null) {
209                         if (graphSource != null) {
210                                 graph = graphSource.get();
211                         }
212                         if (graph == null) {
213                                 ReadGraph g = Transaction.readGraph();
214                                 if (g == null)
215                                         throw new IllegalStateException("No read transaction available");
216                                 try {
217                                         graph = readTg(g);
218                                 } catch (DatabaseException e) {
219                                         LOGGER.error("Failed to read transferable graph from " + resource, e);
220                                 }
221                         }
222                 }
223                 return graph;
224         }
225
226         public TransferableGraph1 getGraph(RequestProcessor processor) {
227                 if (graph == null) {
228                         try {
229                                 graph = processor.syncRequest(new ResourceRead<TransferableGraph1>(resource) {
230                                         @Override
231                                         public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {
232                                                 return readTg(graph);
233                                         }
234                                 });
235                         } catch (DatabaseException e) {
236                                 LOGGER.error("Failed to read transferable graph from " + resource, e);
237                         }
238                 }
239                 return graph;
240         }
241
242         private TransferableGraph1 readTg(ReadGraph graph) throws DatabaseException {
243                 DatabaseManagementResource DatabaseManagement = DatabaseManagementResource.getInstance(graph);
244                 return graph.getRelatedValue(resource, DatabaseManagement.HasFile, TransferableGraph1.BINDING); 
245         }
246
247         public int getHashcode() {
248                 return hashcode;
249         }
250         
251         @Override
252         public int hashCode() {
253                 return 31*id.hashCode() + 7*major + 3*minor + 11*service + (qualifier!=null?13*qualifier.hashCode():0) + hashcode;
254         }
255         
256         @Override
257         public boolean equals(Object obj) {
258                 if (obj instanceof GraphBundle == false) return false;
259                 GraphBundle other = (GraphBundle) obj;
260                 if (other.hashcode != hashcode) return false;
261                 if (!other.id.equals(id)) return false;
262                 if (other.major != major) return false;
263                 if (other.minor != minor) return false;
264                 if (other.service != service) return false;
265                 if (!objectEquals(other.qualifier, qualifier )) return false;
266                 return true;
267         }       
268         
269     static boolean objectEquals(Object o1, Object o2) {
270         if (o1 == o2) return true;
271         if (o1 == null && o2 == null) return true;
272         if (o1 == null || o2 == null) return false;
273         return o1.equals(o2);
274     }   
275     
276     public boolean getImmutable() {
277         return immutable;
278     }
279         
280         public String getId() {
281                 return id;
282         }
283         
284         public int getMajor() {
285                 return major;
286         }
287         
288         public int getMinor() {
289                 return minor;
290         }
291         
292         public int getService() {
293                 return service;
294         }
295         
296         public String getQualifier() {
297                 return qualifier;
298         }
299
300         public String getVersionedId() {
301                 return id+"/"+major+"."+minor+"."+service+"."+qualifier;
302         }
303         
304         @Override
305         public String toString() {
306                 return name+", "+id+"/"+getVersionedId()+", hash="+hashcode;
307         }
308
309         public long[] getResourceArray() {
310                 return resourceArray;
311         }
312
313         public void setResourceArray(long[] resourceArray) {
314                 this.resourceArray = resourceArray;
315         }
316         
317         public static void main(String[] args) {
318                 Matcher m = VERSIONED_ID_PATTERN.matcher("org.simantics.layer0/1.1.1.qualifier");
319                 if (m.matches()) {
320                         System.out.println( m.groupCount() );
321                 }
322                 
323                 m = VERSIONED_ID_PATTERN.matcher("org.simantics.layer0/1.1.1");
324                 if (m.matches()) {
325                         System.out.println( m.groupCount() );
326                 }
327                 
328                 m = VERSIONED_ID_PATTERN.matcher("org.simantics.layer0/1.1.1.200810101010");
329                 if (m.matches()) {
330                         System.out.println( m.groupCount() );
331                 }
332                 
333                 m = VERSIONED_ID_PATTERN.matcher("org.simantics.layer0/1.1");
334                 if (m.matches()) {
335                         System.out.println( m.groupCount() );
336                 }
337                 
338         }
339         
340 }
341