]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationStateImpl.java
Importing TG files creates copies of files without deleting them.
[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, false);
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                 TransferableGraphSource tgs = (TransferableGraphSource)properties.get(MigrationStateKeys.CURRENT_TGS);
166                 if (tgs != null)
167                     try {
168                         tgs.close();
169                     } catch (IOException e) {
170                         Logger.defaultLogError(e);
171                     }
172                 setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg, true));
173             }
174
175             TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
176             if (tgs != null) {
177                 importTransferableGraphSource(monitor, session, updateDependencies, tgs);
178                 // Delete temporary file if necessary
179                 if (temporaryTg != null)
180                     temporaryTg.delete();
181                 return getProperty(key);
182             }
183
184         } else if (MigrationStateKeys.UPDATE_DEPENDENCIES.equals(key)) {
185
186             return null;
187
188         }
189
190         return null;
191
192         }
193
194         @Override
195         public <T> void setProperty(String key, T value) {
196                 properties.put(key, value);
197         }
198
199         @Override
200         public void dispose() {
201                 // Close all possible open file handles
202                 try {
203                         StreamingTransferableGraphFileReader tgs = probeProperty(MigrationStateKeys.CURRENT_TGS_READER);
204                         uncheckedClose(tgs);
205                 } catch (DatabaseException e) {
206                         Logger.defaultLogError(e);
207                 }
208         }
209
210         private static void uncheckedClose(Closeable closeable) {
211                 try {
212                         if (closeable != null)
213                                 closeable.close();
214                 } catch (IOException e) {
215                         //ignore
216                 }
217         }
218
219     private Resource createTemporaryRoot(WriteGraph graph) throws DatabaseException {
220         Layer0 L0 = Layer0.getInstance(graph);
221         Resource project = SimanticsInternal.getProject();
222         Resource root = graph.getPossibleObject(project, L0.PartOf);
223         Resource temp = Layer0Utils.getPossibleChild(graph, root, "Temp");
224         if (temp == null) 
225             throw new AssumptionException("Temporary folder 'Temp' not found under " + graph.getPossibleURI(root));
226
227         Resource indexRoot = graph.newResource();
228         String indexRootName = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date());
229         indexRootName = NameUtils.findFreshName(graph, indexRootName, temp, L0.ConsistsOf, "%s-%d");
230         graph.claim(indexRoot, L0.InstanceOf, L0.IndexRoot);
231         graph.addLiteral(indexRoot, L0.HasName, L0.String, indexRootName, Bindings.STRING);
232         graph.claim(temp, L0.ConsistsOf, indexRoot);
233         return indexRoot;
234     }
235
236         private File exportCurrentTgAsTemporaryFile(Session session, IProgressMonitor monitor) throws DatabaseException {
237                 TransferableGraph1 tg = probeProperty(MigrationStateKeys.CURRENT_TG);
238                 if (tg == null)
239                         return null;
240
241                 try {
242                         // Write TG back to disk and initialize CURRENT_TGS for the migrated TG.
243                         File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);
244                         File tempFile = File.createTempFile("temporary-tgs", ".tg", SimanticsInternal.getTemporaryDirectory());
245                         TimeLogger.log(MigrationStateImpl.class, "export temporary TG " + tempFile);
246
247                         DataContainer dc = DataContainers.readHeader(modelFile);
248                         TransferableGraphs.writeTransferableGraph(session, dc.format, dc.version, dc.metadata,
249                                         new TGTransferableGraphSource(tg),
250                                         tempFile,
251                                         new TGExportMonitor(monitor, "Exporting temporary transferable graph"));
252
253                         // Allow potentially large TG structure to be GC'ed.
254                         setProperty(MigrationStateKeys.CURRENT_TG, null);
255
256                         TimeLogger.log(MigrationStateImpl.class, "export temporary TG done " + tempFile);
257                         return tempFile;
258                 } catch (Exception e) {
259                         throw new DatabaseException(e);
260                 }
261         }
262
263         private TransferableGraphSource initializeTransferableGraphSource(File dataContainer) throws DatabaseException {
264                 return initializeTransferableGraphSource(dataContainer, false);
265         }
266         private TransferableGraphSource initializeTransferableGraphSource(File dataContainer, boolean deleteOnClose) throws DatabaseException {
267                 try {
268                         StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer, deleteOnClose);
269                         TransferableGraphSource tgs = reader.readTG();
270                         setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader);
271                         setProperty(MigrationStateKeys.CURRENT_TGS, tgs);
272                         return tgs;
273                 } catch (DatabaseException e) {
274                         throw e;
275                 } catch (IOException e) {
276                         throw new DatabaseException("An I/O exception occurred during reading '" + dataContainer.getAbsolutePath() + "'", e);
277                 } catch (Throwable t) {
278                         throw new DatabaseException(t);
279                 }
280         }
281
282         private void importTransferableGraphSource(IProgressMonitor monitor, Session session, boolean updateDependencies, TransferableGraphSource tgs) throws DatabaseException {
283                 TimeLogger.log(MigrationStateImpl.class, "import TGS " + tgs);
284                 final Resource indexRoot = session.syncRequest(new WriteResultRequest<Resource>() {
285                         @Override
286                         public Resource perform(WriteGraph graph) throws DatabaseException {
287                                 if(!updateDependencies)
288                                         Layer0Utils.setDependenciesIndexingDisabled(graph, true);
289                                 return createTemporaryRoot(graph);
290                         }
291                 });
292
293                 IImportAdvisor baseAdvisor = MigrationUtils.getProperty(this, MigrationStateKeys.IMPORT_ADVISOR, new DefaultPasteImportAdvisor(indexRoot));
294                 IImportAdvisor2 advisor = new WrapperAdvisor(baseAdvisor) {
295                         @Override
296                         public Resource getTarget() {
297                                 return indexRoot;
298                         }
299                         @Override
300                         public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
301                                 super.beforeWrite(graph, process);
302                                 if(!updateDependencies)
303                                         Layer0Utils.setDependenciesIndexingDisabled(graph, true);
304                         }
305                         @Override
306                         public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
307                                 super.afterWrite(graph, process);
308                                 Boolean storeResources = probeProperty(MigrationStateKeys.GET_RESOURCE_IDS);
309                                 if(storeResources != null && storeResources) {
310                                         long[] ids = process.getResourceIds(session.getService(SerialisationSupport.class));
311                                         setProperty(MigrationStateKeys.RESOURCE_IDS, ids);
312                                 }
313                         }
314                 };
315
316                 // Make sure that the supplied advisor is redirected to temp
317                 advisor.redirect(indexRoot);
318
319                 String task = "Importing model into database";
320                 monitor.subTask(task);
321                 ImportResult ir; 
322                 if (properties.containsKey(MigrationStateKeys.FAIL_ON_MISSING)) {
323                         ir = TransferableGraphs.importGraph1(session, null, tgs, advisor, new TGImportMonitor(monitor, task),(Boolean)properties.get(MigrationStateKeys.FAIL_ON_MISSING));
324                 } else {
325                         ir = TransferableGraphs.importGraph1(session, tgs, advisor, new TGImportMonitor(monitor, task));        
326                 }
327                 
328                 monitor.subTask("");
329
330                 setProperty(MigrationStateKeys.IMPORT_RESULT, ir);
331                 setProperty(MigrationStateKeys.CURRENT_RESOURCE, indexRoot);
332                 setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, new ArrayList<>(advisor.getRoots()));
333                 setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, session.getService(ManagementSupport.class).getHeadRevisionId());
334                 TimeLogger.log(MigrationStateImpl.class, "imported TGS " + tgs);
335         }
336
337         
338         static class TGImportMonitor implements TGStatusMonitor {
339                 private final IProgressMonitor monitor;
340                 private final String message;
341                 public TGImportMonitor(IProgressMonitor monitor, String message) {
342                         this.monitor = monitor;
343                         this.message = message;
344                 }
345                 @Override
346                 public void status(int percentage) {
347                         monitor.subTask(message + " (" + percentage + "%)");
348                 }
349                 @Override
350                 public boolean isCanceled() {
351                         return monitor.isCanceled();
352                 }
353         }
354
355         static class TGExportMonitor extends TGProgressMonitor {
356                 private final String message;
357                 public TGExportMonitor(IProgressMonitor monitor, String message) {
358                         super(monitor);
359                         this.message = message;
360                 }
361                 @Override
362                 protected void workDone(int percentage) {
363                         monitor.subTask(message + " (" + percentage + "%)");
364                 }
365         }
366
367 }