]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationStateImpl.java
Index tokenized lowercase versions of name and types for UI searches
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / migration / MigrationStateImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.db.layer0.migration;
13
14 import java.io.Closeable;
15 import java.io.File;
16 import java.io.IOException;
17 import java.text.DateFormat;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.simantics.databoard.Bindings;
26 import org.simantics.databoard.binding.mutable.Variant;
27 import org.simantics.databoard.container.DataContainer;
28 import org.simantics.databoard.container.DataContainers;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.Session;
32 import org.simantics.db.WriteGraph;
33 import org.simantics.db.WriteOnlyGraph;
34 import org.simantics.db.common.request.WriteResultRequest;
35 import org.simantics.db.common.utils.Logger;
36 import org.simantics.db.common.utils.NameUtils;
37 import org.simantics.db.exception.AssumptionException;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
40 import org.simantics.db.layer0.internal.SimanticsInternal;
41 import org.simantics.db.layer0.util.Layer0Utils;
42 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
43 import org.simantics.db.layer0.util.TGProgressMonitor;
44 import org.simantics.db.layer0.util.TGTransferableGraphSource;
45 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
46 import org.simantics.db.request.Read;
47 import org.simantics.db.service.ManagementSupport;
48 import org.simantics.db.service.SerialisationSupport;
49 import org.simantics.graph.db.IImportAdvisor;
50 import org.simantics.graph.db.IImportAdvisor2;
51 import org.simantics.graph.db.ImportResult;
52 import org.simantics.graph.db.StreamingTransferableGraphFileReader;
53 import org.simantics.graph.db.TGStatusMonitor;
54 import org.simantics.graph.db.TransferableGraphImporter;
55 import org.simantics.graph.db.TransferableGraphSource;
56 import org.simantics.graph.db.TransferableGraphs;
57 import org.simantics.graph.db.WrapperAdvisor;
58 import org.simantics.graph.representation.TransferableGraph1;
59 import org.simantics.graph.representation.TransferableGraphFileReader;
60 import org.simantics.layer0.Layer0;
61 import org.simantics.utils.logging.TimeLogger;
62
63 public class MigrationStateImpl implements MigrationState {
64
65         final private HashMap<String, Object> properties = new HashMap<String, Object>();
66         
67     @SuppressWarnings("unchecked")
68     @Override
69     public <T> T probeProperty(String key) throws DatabaseException {
70         return (T)properties.get(key);
71     }
72
73         @SuppressWarnings("unchecked")
74     @Override
75         public <T> T getProperty(String key) throws DatabaseException {
76             
77             T property = (T)properties.get(key);
78             if(property != null) return property;
79             
80             if(MigrationStateKeys.BASE_URI.equals(key)) {
81                 throw new IllegalStateException("Base URI needs to be supplied for migration.");
82         } else if (MigrationStateKeys.SESSION.equals(key)) {
83             throw new IllegalStateException("Session needs to be supplied for migration.");
84         } else if (MigrationStateKeys.MODEL_FILE.equals(key)) {
85             throw new IllegalStateException("Model file needs to be supplied for migration.");
86             } else if (MigrationStateKeys.CURRENT_TG.equals(key)) {
87                 
88                 final Resource resource = probeProperty(MigrationStateKeys.CURRENT_RESOURCE);
89                 final Collection<Resource> roots = probeProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
90                 if(roots != null) {
91                     Session session = getProperty(MigrationStateKeys.SESSION);
92                     TransferableGraph1 tg = session.syncRequest(new Read<TransferableGraph1>() {
93                         @Override
94                         public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {
95                             TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, roots, true, false);
96                             TransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
97                             return TransferableGraphs.create(graph, source);
98                         }
99                     });
100                     if (resource != null)
101                         MigrationUtils.clearTempResource(session, resource);
102                     setProperty(MigrationStateKeys.CURRENT_RESOURCE, null);
103                     setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, null);
104                 setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, null);
105                     setProperty(MigrationStateKeys.CURRENT_TG, tg);
106                     return (T)tg;
107                 }
108                 
109             try {
110                 File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
111                 TimeLogger.log(MigrationStateImpl.class, "reading TG into memory from " + modelFile);
112                 TransferableGraph1 tg = TransferableGraphFileReader.read(modelFile);
113                 TimeLogger.log(MigrationStateImpl.class, "read TG into memory from " + modelFile);
114                 setProperty(MigrationStateKeys.CURRENT_TG, tg);
115                 return (T)tg;
116             } catch (DatabaseException e) {
117                 throw e;
118             } catch (Throwable t) {
119                 throw new DatabaseException(t);
120             }
121                 
122         } else if (MigrationStateKeys.CURRENT_TGS.equals(key)) {
123
124             File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
125             return (T) initializeTransferableGraphSource(modelFile);
126
127         } else if (MigrationStateKeys.CURRENT_DATA_CONTAINER.equals(key)) {
128             
129             try {
130                 
131                 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
132                 DataContainer dc = tgs.getHeader();
133                 setProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER, dc);
134                 return (T)dc;
135                 
136             } catch (DatabaseException e) {
137                 throw e;
138             } catch (Throwable t) {
139                 throw new DatabaseException(t);
140             }
141             
142         } else if (MigrationStateKeys.TG_EXTENSIONS.equals(key)) {
143             
144             try {
145                 
146                 TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
147                 Map<String,Variant> extensions = tgs.getExtensions();
148                 setProperty(MigrationStateKeys.TG_EXTENSIONS, extensions);
149                 return (T)extensions;
150                 
151             } catch (DatabaseException e) {
152                 throw e;
153             } catch (Throwable t) {
154                 throw new DatabaseException(t);
155             }
156
157         } else if (MigrationStateKeys.CURRENT_RESOURCE.equals(key) || MigrationStateKeys.CURRENT_ROOT_RESOURCES.equals(key)) {
158
159             final Session session = getProperty(MigrationStateKeys.SESSION);
160             final IProgressMonitor monitor = probeProperty(MigrationStateKeys.PROGRESS_MONITOR);
161             final boolean updateDependencies = MigrationUtils.getProperty(this, MigrationStateKeys.UPDATE_DEPENDENCIES, Boolean.TRUE);
162
163             File temporaryTg = exportCurrentTgAsTemporaryFile(session, monitor);
164             if (temporaryTg != null)
165                 setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg));
166
167             TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
168             if (tgs != null) {
169                 importTransferableGraphSource(monitor, session, updateDependencies, tgs);
170                 // Delete temporary file if necessary
171                 if (temporaryTg != null)
172                     temporaryTg.delete();
173                 return getProperty(key);
174             }
175
176         } else if (MigrationStateKeys.UPDATE_DEPENDENCIES.equals(key)) {
177
178             return null;
179
180         }
181
182         return null;
183
184         }
185
186         @Override
187         public <T> void setProperty(String key, T value) {
188                 properties.put(key, value);
189         }
190
191         @Override
192         public void dispose() {
193                 // Close all possible open file handles
194                 try {
195                         StreamingTransferableGraphFileReader tgs = probeProperty(MigrationStateKeys.CURRENT_TGS_READER);
196                         uncheckedClose(tgs);
197                 } catch (DatabaseException e) {
198                         Logger.defaultLogError(e);
199                 }
200         }
201
202         private static void uncheckedClose(Closeable closeable) {
203                 try {
204                         if (closeable != null)
205                                 closeable.close();
206                 } catch (IOException e) {
207                         //ignore
208                 }
209         }
210
211     private Resource createTemporaryRoot(WriteGraph graph) throws DatabaseException {
212         Layer0 L0 = Layer0.getInstance(graph);
213         Resource project = SimanticsInternal.getProject();
214         Resource root = graph.getPossibleObject(project, L0.PartOf);
215         Resource temp = Layer0Utils.getPossibleChild(graph, root, "Temp");
216         if (temp == null) 
217             throw new AssumptionException("Temporary folder 'Temp' not found under " + graph.getPossibleURI(root));
218
219         Resource indexRoot = graph.newResource();
220         String indexRootName = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date());
221         indexRootName = NameUtils.findFreshName(graph, indexRootName, temp, L0.ConsistsOf, "%s-%d");
222         graph.claim(indexRoot, L0.InstanceOf, L0.IndexRoot);
223         graph.addLiteral(indexRoot, L0.HasName, L0.String, indexRootName, Bindings.STRING);
224         graph.claim(temp, L0.ConsistsOf, indexRoot);
225         return indexRoot;
226     }
227
228         private File exportCurrentTgAsTemporaryFile(Session session, IProgressMonitor monitor) throws DatabaseException {
229                 TransferableGraph1 tg = probeProperty(MigrationStateKeys.CURRENT_TG);
230                 if (tg == null)
231                         return null;
232
233                 try {
234                         // Write TG back to disk and initialize CURRENT_TGS for the migrated TG.
235                         File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
236                         File tempFile = File.createTempFile("temporary-tgs", ".tg", SimanticsInternal.getTemporaryDirectory());
237                         TimeLogger.log(MigrationStateImpl.class, "export temporary TG " + tempFile);
238
239                         DataContainer dc = DataContainers.readHeader(modelFile);
240                         TransferableGraphs.writeTransferableGraph(session, dc.format, dc.version, dc.metadata,
241                                         new TGTransferableGraphSource(tg),
242                                         tempFile,
243                                         new TGExportMonitor(monitor, "Exporting temporary transferable graph"));
244
245                         // Allow potentially large TG structure to be GC'ed.
246                         setProperty(MigrationStateKeys.CURRENT_TG, null);
247
248                         TimeLogger.log(MigrationStateImpl.class, "export temporary TG done " + tempFile);
249                         return tempFile;
250                 } catch (Exception e) {
251                         throw new DatabaseException(e);
252                 }
253         }
254
255         private TransferableGraphSource initializeTransferableGraphSource(File dataContainer) throws DatabaseException {
256                 try {
257                         StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer);
258                         TransferableGraphSource tgs = reader.readTG();
259                         setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader);
260                         setProperty(MigrationStateKeys.CURRENT_TGS, tgs);
261                         return tgs;
262                 } catch (DatabaseException e) {
263                         throw e;
264                 } catch (IOException e) {
265                         throw new DatabaseException("An I/O exception occurred during reading '" + dataContainer.getAbsolutePath() + "'", e);
266                 } catch (Throwable t) {
267                         throw new DatabaseException(t);
268                 }
269         }
270
271         private void importTransferableGraphSource(IProgressMonitor monitor, Session session, boolean updateDependencies, TransferableGraphSource tgs) throws DatabaseException {
272                 TimeLogger.log(MigrationStateImpl.class, "import TGS " + tgs);
273                 final Resource indexRoot = session.syncRequest(new WriteResultRequest<Resource>() {
274                         @Override
275                         public Resource perform(WriteGraph graph) throws DatabaseException {
276                                 if(!updateDependencies)
277                                         Layer0Utils.setDependenciesIndexingDisabled(graph, true);
278                                 return createTemporaryRoot(graph);
279                         }
280                 });
281
282                 IImportAdvisor baseAdvisor = MigrationUtils.getProperty(this, MigrationStateKeys.IMPORT_ADVISOR, new DefaultPasteImportAdvisor(indexRoot));
283                 IImportAdvisor2 advisor = new WrapperAdvisor(baseAdvisor) {
284                         @Override
285                         public Resource getTarget() {
286                                 return indexRoot;
287                         }
288                         @Override
289                         public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
290                                 super.beforeWrite(graph, process);
291                                 if(!updateDependencies)
292                                         Layer0Utils.setDependenciesIndexingDisabled(graph, true);
293                         }
294                         @Override
295                         public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
296                                 super.afterWrite(graph, process);
297                                 Boolean storeResources = probeProperty(MigrationStateKeys.GET_RESOURCE_IDS);
298                                 if(storeResources != null && storeResources) {
299                                         long[] ids = process.getResourceIds(session.getService(SerialisationSupport.class));
300                                         setProperty(MigrationStateKeys.RESOURCE_IDS, ids);
301                                 }
302                         }
303                 };
304
305                 // Make sure that the supplied advisor is redirected to temp
306                 advisor.redirect(indexRoot);
307
308                 String task = "Importing model into database";
309                 monitor.subTask(task);
310                 ImportResult ir = TransferableGraphs.importGraph1(session, tgs, advisor, new TGImportMonitor(monitor, task));
311                 monitor.subTask("");
312
313                 setProperty(MigrationStateKeys.IMPORT_RESULT, ir);
314                 setProperty(MigrationStateKeys.CURRENT_RESOURCE, indexRoot);
315                 setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, new ArrayList<>(advisor.getRoots()));
316                 setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, session.getService(ManagementSupport.class).getHeadRevisionId());
317                 TimeLogger.log(MigrationStateImpl.class, "imported TGS " + tgs);
318         }
319
320         
321         static class TGImportMonitor implements TGStatusMonitor {
322                 private final IProgressMonitor monitor;
323                 private final String message;
324                 public TGImportMonitor(IProgressMonitor monitor, String message) {
325                         this.monitor = monitor;
326                         this.message = message;
327                 }
328                 @Override
329                 public void status(int percentage) {
330                         monitor.subTask(message + " (" + percentage + "%)");
331                 }
332                 @Override
333                 public boolean isCanceled() {
334                         return monitor.isCanceled();
335                 }
336         }
337
338         static class TGExportMonitor extends TGProgressMonitor {
339                 private final String message;
340                 public TGExportMonitor(IProgressMonitor monitor, String message) {
341                         super(monitor);
342                         this.message = message;
343                 }
344                 @Override
345                 protected void workDone(int percentage) {
346                         monitor.subTask(message + " (" + percentage + "%)");
347                 }
348         }
349
350 }