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