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